package cokr.xit.ens.biz.traffic.cheonan.signtalk.domain.repository;

import cokr.xit.ens.biz.traffic.cheonan.signtalk.domain.TcaElctrnNticSndng;
import cokr.xit.ens.biz.traffic.cheonan.signtalk.domain.TcaElctrnNticSndngDetail;
import com.querydsl.core.types.Projections;
import com.querydsl.core.types.dsl.Expressions;
import com.querydsl.jpa.impl.JPAQueryFactory;
import lombok.RequiredArgsConstructor;

import java.util.List;
import java.util.Optional;

import static cokr.xit.ens.biz.traffic.cheonan.signtalk.domain.QTcaElctrnNticSndng.tcaElctrnNticSndng;
import static cokr.xit.ens.biz.traffic.cheonan.signtalk.domain.QTcaElctrnNticSndngDetail.tcaElctrnNticSndngDetail;
import static cokr.xit.ens.biz.traffic.cheonan.signtalk.domain.QTcaNhtTmplatManage.tcaNhtTmplatManage;
import static cokr.xit.ens.modules.common.ctgy.intgrnnoti.domain.QIntgrnSendDetail.intgrnSendDetail;

@RequiredArgsConstructor
public class TcaElctrnNticSndngDetailRepositoryImpl implements TcaElctrnNticSndngDetailRepositoryCustom {

    private final JPAQueryFactory query;

    @Override
    public List<TcaElctrnNticSndngDetail> findAllDecByElctrnNticSndng(TcaElctrnNticSndng elctrnNticSndng) {

        return query.select(Projections.fields(TcaElctrnNticSndngDetail.class
                        , tcaElctrnNticSndngDetail.elctrnNticSndngDetailId
                        , tcaElctrnNticSndngDetail.elctrnNticSndng
                        , tcaElctrnNticSndngDetail.signguCode
                        , tcaElctrnNticSndngDetail.mainCode
                        , Expressions.stringTemplate("ECL_DECRYPT({0})", tcaElctrnNticSndngDetail.ihidnum).as("ihidnum")
                        , tcaElctrnNticSndngDetail.tmpltMsgData
                        , tcaElctrnNticSndngDetail.mobilePageCn
                        , tcaElctrnNticSndngDetail.create
                        , tcaElctrnNticSndngDetail.update
                        , tcaElctrnNticSndngDetail.externalDocumentUuid
                        , tcaElctrnNticSndngDetail.ffnlgCode
                        , tcaElctrnNticSndngDetail.useInsttIdntfcId
                ))
                .from(tcaElctrnNticSndngDetail)
                .where(tcaElctrnNticSndngDetail.elctrnNticSndng.eq(elctrnNticSndng))
                .fetch();
    }


    @Override
    public List<TcaElctrnNticSndngDetail> findAllFetchByElctrnNticSndngId(String elctrnNticSndngId) {
        return query.selectFrom(tcaElctrnNticSndngDetail)
                .innerJoin(tcaElctrnNticSndngDetail.elctrnNticSndng, tcaElctrnNticSndng)
                .fetchJoin()
                .innerJoin(tcaElctrnNticSndng.tcaNhtTmplatManage, tcaNhtTmplatManage)
                .fetchJoin()
                .where(tcaElctrnNticSndngDetail.elctrnNticSndng.elctrnNticSndngId.eq(elctrnNticSndngId))
                .fetch();

    }


    @Override
    public Optional<Long> findLastSendMastIdByElctrnNticSndngId(String elctrnNticSndngId) {
        return Optional.ofNullable(query.select(intgrnSendDetail.intgrnSendMast.intSendMastId)
                .from(intgrnSendDetail)
//                .where(intgrnSendDetail.linkedUuid.in(findExternalDocumentUuidsByLnkInputId(elctrnNticSndngId)))
                .innerJoin(tcaElctrnNticSndngDetail).on(intgrnSendDetail.linkedUuid.eq(tcaElctrnNticSndngDetail.externalDocumentUuid))
                .where(tcaElctrnNticSndngDetail.elctrnNticSndng.elctrnNticSndngId.eq(elctrnNticSndngId))
                .groupBy(intgrnSendDetail.intgrnSendMast.intSendMastId)
                .orderBy(intgrnSendDetail.intgrnSendMast.intSendMastId.desc())
                .fetchFirst());
    }

    private List<String> findExternalDocumentUuidsByLnkInputId(String elctrnNticSndngId) {
        return query.select(tcaElctrnNticSndngDetail.externalDocumentUuid)
                .from(tcaElctrnNticSndngDetail)
                .where(tcaElctrnNticSndngDetail.elctrnNticSndng.elctrnNticSndngId.eq(elctrnNticSndngId))
                .fetch();
    }


    @Override
    public String findIhidnumDecByElctrnNticSndngDetailId(String tcaElctrnNticSndngDetailId) {
        return query.select(Expressions.stringTemplate("ECL_DECRYPT({0})", tcaElctrnNticSndngDetail.ihidnum).as("ihidnum"))
                .from(tcaElctrnNticSndngDetail)
                .where(tcaElctrnNticSndngDetail.elctrnNticSndngDetailId.eq(tcaElctrnNticSndngDetailId))
                .fetchOne();
    }
}
