package cokr.xit.ens.modules.common.ctgy.intgrnnoti.service.event;

import cokr.xit.ens.core.aop.EnsResponseVO;
import cokr.xit.ens.core.exception.code.EnsErrCd;
import cokr.xit.ens.modules.common.ctgy.intgrnnoti.service.IntgrnNotiService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.annotation.Order;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.transaction.event.TransactionPhase;
import org.springframework.transaction.event.TransactionalEventListener;

@Slf4j
@Component
@RequiredArgsConstructor
public class IntgrnNotiSendEventListener {
    private final IntgrnNotiService intgrnNotiService;

    @Order(1)
//    @EventListener
    @TransactionalEventListener(phase = TransactionPhase.AFTER_COMMIT)
    public void sendRealtime(IntgrnNotiSendRealtimeEvent event) {
        event.getSendMastIds().stream()
                .forEach(intSendMastId -> {
                    StringBuffer sb = new StringBuffer();
                    sb.append("\n=======================================================")
                            .append("\n[ENS] 통합고지 실시간전송")
                            .append("\n요청ID: " + intSendMastId);

                    EnsResponseVO responseVO = intgrnNotiService.remake(intSendMastId);
                    sb.append("\n제작 결과: " + responseVO.toString());
                    if (!EnsErrCd.OK.equals(responseVO.getErrCode())) {
                        sb.append("\n=======================================================");
                        log.info(sb.toString());
                        return;
                    }

                    responseVO = intgrnNotiService.sendBulk(intSendMastId);
                    sb.append("\n전송요청 결과: " + responseVO.toString());
                    if (!EnsErrCd.OK.equals(responseVO.getErrCode())) {
                        sb.append("\n=======================================================");
                        log.info(sb.toString());
                        return;
                    }

                    responseVO = intgrnNotiService.statBulk(intSendMastId);
                    sb.append("\n상태갱신 결과: " + responseVO.toString());
                    sb.append("\n=======================================================");
                    log.info(sb.toString());

                    if (event.getCallback() != null)
                        event.getCallback().execute();
                });
    }


    @Async
    @Order(1)
    @TransactionalEventListener(phase = TransactionPhase.AFTER_COMMIT)
    public void sendReserve(IntgrnNotiSendReserveEvent event) {
        event.getSendMastIds().stream()
                .forEach(intSendMastId -> {
                    StringBuffer sb = new StringBuffer();

                    EnsResponseVO responseVO = intgrnNotiService.remake(intSendMastId);
                    sb.append("\n=======================================================")
                            .append("\n[ENS] 통합고지 예약전송")
                            .append("\n요청ID: " + intSendMastId)
                            .append("\n제작 결과: " + responseVO.toString());

                    responseVO = intgrnNotiService.sendBulk(intSendMastId);
                    sb.append("\n-------------------------------------------------------")
                            .append("\n요청ID: " + intSendMastId)
                            .append("\n전송 결과: " + responseVO.toString())
                            .append("\n=======================================================");
                    log.info(sb.toString());

                    if (event.getCallback() != null)
                        event.getCallback().execute();
                });
    }
}
