package cokr.xit.ens.biz.traffic.cheonan.signtalk.service.support;

import cokr.xit.ens.biz.traffic.cheonan.signtalk.domain.repository.TcaElctrnNticSndngDetailRepository;
import cokr.xit.ens.core.aop.EnsResponseVO;
import cokr.xit.ens.core.exception.EnsException;
import cokr.xit.ens.core.exception.code.EnsErrCd;
import cokr.xit.ens.modules.common.biztmplt.EnsPhaseProcSupport;
import cokr.xit.ens.modules.common.ctgy.intgrnnoti.service.IntgrnNotiService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Slf4j
@Component
@RequiredArgsConstructor
public class TrafficCaSigntalkMaker implements EnsPhaseProcSupport<EnsResponseVO, String> {

    private final TcaElctrnNticSndngDetailRepository elctrnNticSndngDetailRepository;

    private final IntgrnNotiService intgrnNotiService;


    @Override
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public EnsResponseVO statReady(List<String> elctrnNticSndngIds) {
        log.info("no process");
        return null;
    }


    @Override
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public EnsResponseVO statBegin(String elctrnNticSndngId) {
        log.info("no process");
        return null;
    }


    /**
     * 인증톡 전자고지 제작
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public EnsResponseVO execute(String elctrnNticSndngId) {
        EnsResponseVO responseVO = null;
        try {
            Long intSendMastId = elctrnNticSndngDetailRepository.findLastSendMastIdByElctrnNticSndngId(elctrnNticSndngId)
                    .orElseThrow(() -> new EnsException(EnsErrCd.MAKE404, "일치하는 ENS 접수자료가 없습니다."));

            responseVO = intgrnNotiService.remake(intSendMastId);
        } catch (EnsException e) {
            responseVO = EnsResponseVO.errBuilder()
                    .errCode(e.getErrCd())
                    .errMsg(e.getMessage())
                    .build();
        }
        return responseVO;
    }

}
