package cokr.xit.ens.biz.traffic.cheonan.init;

import cokr.xit.ens.biz.traffic.cheonan.signtalk.domain.repository.TcaNhtTmplatManageRepository;
import cokr.xit.ens.biz.traffic.cheonan.signtalk.domain.repository.TcaRlaybsnmManageRepository;
import cokr.xit.ens.core.init.InitJpaRunner;
import cokr.xit.ens.modules.common.code.PostSeCd;
import cokr.xit.ens.modules.common.ctgy.sys.mng.model.OrgMngDTO;
import cokr.xit.ens.modules.ktsigntalk.common.code.MTypeCd;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * 주정차웹-천안
 */
//TODO: 데이터초기화 목적으로 사용. ex) 최초 배포 시
//@Component
@Transactional
@Profile({"prod-traffic-cheonan", "local-test"})
public class TrafficCaJpaRunner extends InitJpaRunner {
    ObjectMapper mapper = new ObjectMapper();

//    @Value("${contract.custom.dng.nice.dici.rest.client-id}")
//    private String DNG_NICE_CD_CLIENT_ID;
//    @Value("${contract.custom.dng.nice.dici.rest.client-secret}")
//    private String DNG_NICE_CD_CLIENT_SECRET;
//    @Value("${contract.custom.dng.kakao.pay.mydoc.access-token}")
//    private String DNG_KKO_MD_ACCESS_TOKEN;
//    @Value("${contract.custom.dng.kakao.pay.mydoc.contract-uuid}")
//    private String DNG_KKO_MD_CONTRACT_UUID;
//
//    @Value("${contract.custom.sbg.nice.dici.rest.client-id}")
//    private String SBG_NICE_CD_CLIENT_ID;
//    @Value("${contract.custom.sbg.nice.dici.rest.client-secret}")
//    private String SBG_NICE_CD_CLIENT_SECRET;
//    @Value("${contract.custom.sbg.kakao.pay.mydoc.access-token}")
//    private String SBG_KKO_MD_ACCESS_TOKEN;
//    @Value("${contract.custom.sbg.kakao.pay.mydoc.contract-uuid}")
//    private String SBG_KKO_MD_CONTRACT_UUID;

    @Autowired
    private TcaRlaybsnmManageRepository tcaRlaybsnmManageRepository;
    @Autowired
    private TcaNhtTmplatManageRepository tcaNhtTmplatManageRepository;

    @Override
    protected List<OrgMngDTO> createOrgMngDTO() {
//        List<OrgMngDTO> list = new ArrayList<>();
//        list.add(createOrgMngDTO("TCA01", "", "천안-동남구", "N", null, null, DNG_NICE_CD_CLIENT_ID, DNG_NICE_CD_CLIENT_SECRET, DNG_KKO_MD_ACCESS_TOKEN, DNG_KKO_MD_CONTRACT_UUID));
//        list.add(createOrgMngDTO("TCA02", "", "천안-서북구", "N", null, null, SBG_NICE_CD_CLIENT_ID, SBG_NICE_CD_CLIENT_SECRET, SBG_KKO_MD_ACCESS_TOKEN, SBG_KKO_MD_CONTRACT_UUID));
//        return list;
        return tcaRlaybsnmManageRepository.findAll().stream()
                .map(entity -> OrgMngDTO.builder()
                        .orgCd(entity.getSignguCode())
                        .orgNm(entity.getSignguNm())

                        .kkoMdAccessToken(entity.getKakaoAccessToken())
                        .kkoMdContractUuid(entity.getKakaoContractUuid())

                        .kkoBpCsignYn("N")
//                        .kkoBpBillerCode(KKO_BP_BILLER_CODE)
//                        .kkoBpAuthorization(KKO_BP_AUTHORIZATION)
//                        .kkoBpCsignPrepayApi(kkoBpPrepayApi)
//                        .kkoBpCsignPayResultApi(kkoBpPayResultApi)

//                        .kkoAtBsid(KKO_AT_BSID)
//                        .kkoAtPasswd(KKO_AT_PASSWD)
//                        .kkoAtSenderKey(KKO_AT_SENDER_KEY)

                        .niceCdSiteCode(null)
                        .niceCdSitePw(entity.getNiceSiteCode())
                        .niceCdClientId(entity.getNiceClientId())
                        .niceCdClientSercet(entity.getNiceClientSecret())

//                        .nvStXNaverClientId(NV_ST_X_NAVER_CLIENT_ID)
//                        .nvStXNaverClientSecret(NV_ST_X_NAVER_CLIENT_SECRET)
//                        .nvStOrgId(nvOrgId)

//                        .ktStAccessToken(null)
//                        .ktStServiceCd(null)
//                        .ktStServiceKey(null)
                        .ktStClientId(entity.getKtClientId())
                        .ktStClientSecret(entity.getKtClientSecret())
                        .build())
                .collect(Collectors.toList());
    }

    @Override
    protected List<Map<String, Object>> createTmpltMngDTO() {
//        List<Map<String, Object>> list = new ArrayList<>();
//        list.add(mapper.convertValue(createTmpltMngIntgrn(PostSeCd.kkoMydoc, PostSeCd.ktGibis, null, "TCA01", "TCA0001", "주정차단속-천안-동남구", "[천안-동남구] 소유차량(#{CAR_NO})의 주정차과태료", "Y"
//                        , null
//                        , createTmpltMngKkoMydoc(null, null, null, null, null, "044-211-3377", "문의처", "Y", "Y", "Y")
//                        , null
//                        , null
//                        , createTmpltMngKtGibis(null, null, null, null, null, "MC001", MTypeCd.mms, 1, "044-211-3377", "044-211-3377", "Y", "Y", "Y")
//                )
//                , Map.class));
//        list.add(mapper.convertValue(createTmpltMngIntgrn(PostSeCd.kkoMydoc, PostSeCd.ktGibis, null, "TCA02", "TCA0001", "주정차단속-천안-서북구", "[천안-서북구] 소유차량(#{CAR_NO})의 주정차과태료", "Y"
//                        , null
//                        , createTmpltMngKkoMydoc(null, null, null, null, null, "044-211-3377", "문의처", "Y", "Y", "Y")
//                        , null
//                        , null
//                        , createTmpltMngKtGibis(null, null, null, null, null, "MC001", MTypeCd.mms, 1, "044-211-3377", "044-211-3377", "Y", "Y", "Y")
//                )
//                , Map.class));
//        return list;


        return tcaNhtTmplatManageRepository.findAll().stream()
                .map(entity -> createTmpltMngIntgrn(PostSeCd.kkoMydoc, PostSeCd.ktGibis, null, entity.getSignguCode(), entity.getNhtTmplatId(), entity.getNhtSj(), entity.getNhtCn(), "Y"
                        , null
                        , createTmpltMngKkoMydoc(null, null, null, null, null, entity.getCstmrCnterTlphonNo(), entity.getCstmrCnterNm() == null ? "문의처" : entity.getCstmrCnterNm(), "Y", "Y", "Y")
                        , null
                        , null
                        , createTmpltMngKtGibis(null, null, null, null, null, "?????", MTypeCd.mms, "2", 1, entity.getCstmrCnterTlphonNo(), entity.getCstmrCnterTlphonNo(), "Y", "Y", "Y")
                ))
                .map(tmpltMngIntgrnDTO -> (Map<String, Object>) mapper.convertValue(tmpltMngIntgrnDTO, Map.class))
                .collect(Collectors.toList());
    }


}