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

import com.caliverse.admin.domain.entity.HISTORYTYPE;
import com.caliverse.admin.domain.entity.Mail;
import com.caliverse.admin.domain.entity.metadata.MetaSystemMailData;
import com.caliverse.admin.dynamodb.domain.atrrib.MailAttrib;
import com.caliverse.admin.dynamodb.domain.doc.MailDoc;
import com.caliverse.admin.dynamodb.entity.MailItem;
import com.caliverse.admin.dynamodb.repository.BaseDynamoDBRepository;
import com.caliverse.admin.dynamodb.repository.MailRepository;
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.DynamoDBConstants;
import com.caliverse.admin.global.common.exception.RestApiException;
import com.caliverse.admin.global.common.utils.CommonUtils;
import com.caliverse.admin.global.common.utils.DateUtils;
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 java.util.List;

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

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

    @Override
    public void insertRecvSystemMail(String userGuid, String nickname, List<MailItem> mailItems, MetaSystemMailData systemMailData, List<String> arguments) {
        try {
            LocalDateTime nowDate = LocalDateTime.now();
            LocalDateTime maxDate = DateUtils.getMaxTime();

            String expireTime = DateUtils.stringToISODateTimeMillisNano(maxDate);

            String pk = DynamoDBConstants.PK_KEY_RECV_MAIL + userGuid;
            String sk = String.format("%s-%s", DateUtils.getMailDateFormat(nowDate), CommonUtils.getCreateGuId().replace("-",""));

            MailAttrib attrib = new MailAttrib();
            attrib.setAttribType(DynamoDBConstants.ATTRIB_MAIL);
            attrib.setMailGuid(sk);
            attrib.setRead(false);
            attrib.setGetItem(false);
            attrib.setSystemMail(true);
            attrib.setSenderNickName(systemMailData.getSender());
            attrib.setSenderGuid("");
            attrib.setReceiverNickName(nickname);
            attrib.setReceiverGuid(userGuid);
            attrib.setTitle(systemMailData.getMailTitle());
            attrib.setText(systemMailData.getMailDesc());
            attrib.setExpireTime(expireTime);
            attrib.setCreateTime(DateUtils.stringToISODateTimeMillisNano(nowDate));
            attrib.setTextByMetaData(true);
            attrib.setItemList(mailItems);
            attrib.setPackageOrderId("");
            attrib.setContentsArguments(arguments);

            MailDoc doc = new MailDoc();
            doc.setPK(pk);
            doc.setSK(sk);
            doc.setDocType(DynamoDBConstants.DOC_Mail);
            doc.setAttribValue(objectMapper.writeValueAsString(attrib));
            doc.setCreatedDateTime(convertUTCDate(nowDate));
            doc.setUpdatedDateTime(convertUTCDate(nowDate));
            doc.setDeletedDateTime(DynamoDBConstants.MIN_DATE);
            doc.setRestoredDateTime(DynamoDBConstants.MIN_DATE);
            doc.setTtl(DateUtils.dateToISOUnixTime(maxDate));

            save(doc);

            log.info("MailDoc Recv Insert Success: {}", objectMapper.writeValueAsString(doc));

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

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

    @Override
    public void insertSentMail(Mail mail) {

    }
}
