package com.caliverse.admin.dynamodb.repository.Impl;

import com.caliverse.admin.domain.entity.Event;
import com.caliverse.admin.domain.entity.HISTORYTYPE;
import com.caliverse.admin.domain.entity.Message;
import com.caliverse.admin.dynamodb.domain.atrrib.SystemMetaMailAttrib;
import com.caliverse.admin.dynamodb.domain.doc.SystemMetaMailDoc;
import com.caliverse.admin.dynamodb.repository.BaseDynamoDBRepository;
import com.caliverse.admin.dynamodb.repository.SystemMetaMailRepository;
import com.caliverse.admin.dynamodb.service.DynamoDBOperations;
import com.caliverse.admin.global.common.code.CommonCode;
import com.caliverse.admin.global.common.code.ErrorCode;
import com.caliverse.admin.global.common.constants.CommonConstants;
import com.caliverse.admin.global.common.constants.DynamoDBConstants;
import com.caliverse.admin.global.common.exception.RestApiException;
import com.caliverse.admin.global.common.utils.CommonUtils;
import com.caliverse.admin.global.common.utils.DynamodbUtil;
import com.caliverse.admin.history.service.DynamodbHistoryLogService;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

import static com.caliverse.admin.global.common.utils.CommonUtils.convertUTCDate;
import static com.caliverse.admin.global.common.utils.DynamodbUtil.createMailItems;
import static com.caliverse.admin.global.common.utils.DynamodbUtil.createSystemMessages;

@Component
@Slf4j
public class SystemMetaMailRepositoryImpl extends BaseDynamoDBRepository<SystemMetaMailDoc> implements SystemMetaMailRepository {
    public SystemMetaMailRepositoryImpl(DynamoDBOperations operations, DynamodbHistoryLogService dynamodbHistoryLogService, ObjectMapper objectMapper) {
        super(operations, SystemMetaMailDoc.class, dynamodbHistoryLogService, objectMapper);
    }

    @Override
    public void insert(Event event) {
        try {
            LocalDateTime nowDate = LocalDateTime.now();

            SystemMetaMailAttrib attrib = new SystemMetaMailAttrib();
            attrib.setAttribType(DynamoDBConstants.ATTRIB_SYSTEMMAIL);
            attrib.setMailId(event.getId().intValue());
            attrib.setStartTime(convertUTCDate(event.getStartDt()));
            attrib.setEndTime(convertUTCDate(event.getEndDt()));
            attrib.setSenderNickName(createSystemMessages(event.getMailList(), DynamodbUtil::getSenderByLanguage));
            attrib.setTitle(createSystemMessages(event.getMailList(), Message::getTitle));
            attrib.setText(createSystemMessages(event.getMailList(), Message::getContent));
            attrib.setItemList(createMailItems(event.getItemList()));

            SystemMetaMailDoc doc = new SystemMetaMailDoc();
            doc.setPK(DynamoDBConstants.PK_KEY_SYSTEM_MAIL);
            doc.setSK(String.valueOf(event.getId()));
            doc.setDocType(DynamoDBConstants.DOC_SYSTEMMAIL);
            doc.setAttribValue(objectMapper.writeValueAsString(attrib));
            doc.setCreatedDateTime(convertUTCDate(nowDate));
            doc.setUpdatedDateTime(convertUTCDate(nowDate));
            doc.setDeletedDateTime(DynamoDBConstants.MIN_DATE);
            doc.setRestoredDateTime(DynamoDBConstants.MIN_DATE);

            save(doc);

            dynamodbHistoryLogService.insertHistoryLog(
                    HISTORYTYPE.EVENT_ADD,
                    HISTORYTYPE.EVENT_ADD.name(),
                    doc
            );

        }catch (Exception e){
            log.error("insert Error: {}", e.getMessage());
            throw new RestApiException(CommonCode.ERROR.getHttpStatus(), ErrorCode.DYNAMODB_CONNECTION_ERROR.getMessage());
        }
    }
}
