package go.kr.project.api.internal.service;

import go.kr.project.api.config.properties.VmisProperties;
import go.kr.project.api.model.Envelope;
import go.kr.project.api.model.request.BasicRequest;
import go.kr.project.api.model.request.LedgerRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * Populates incoming request models with values from YAML configuration.
 * Unconditionally overwrites the listed fields per requirement:
 * - INFO_SYS_ID, INFO_SYS_IP, SIGUNGU_CODE
 * - CNTC_INFO_CODE (service specific)
 * - CHARGER_ID, CHARGER_IP, CHARGER_NM
 */
@Slf4j
@Component
public class VmisRequestEnricher {

    private final VmisProperties props;

    public VmisRequestEnricher(VmisProperties props) {
        this.props = props;
    }

    public void enrichBasic(Envelope<BasicRequest> envelope) {
        if (envelope == null || envelope.getData() == null) return;
        VmisProperties.SystemProps sys = props.getSystem();
        String cntc = props.getGov().getServices().getBasic().getCntcInfoCode();
        for (BasicRequest req : envelope.getData()) {
            if (req == null) continue;
            req.setInfoSysId(sys.getInfoSysId());
            req.setInfoSysIp(sys.getInfoSysIp());
            req.setSigunguCode(sys.getSigunguCode());
            req.setCntcInfoCode(cntc);
            req.setChargerId(sys.getChargerId());
            req.setChargerIp(sys.getChargerIp());
            req.setChargerNm(sys.getChargerNm());

            // 조회구분코드 자동 설정: VHRNO가 있으면 "3" (자동차번호), VIN이 있으면 "2" (차대번호)
            if (req.getInqireSeCode() == null) {
                if (req.getVhrno() != null && !req.getVhrno().trim().isEmpty()) {
                    req.setInqireSeCode("3");  // 자동차번호로 조회
                } else if (req.getVin() != null && !req.getVin().trim().isEmpty()) {
                    req.setInqireSeCode("2");  // 차대번호로 조회
                }
            }
        }
        log.debug("[ENRICH] basic: applied INFO_SYS_ID={}, INFO_SYS_IP={}, SIGUNGU_CODE={}, CNTC_INFO_CODE={}",
                sys.getInfoSysId(), sys.getInfoSysIp(), sys.getSigunguCode(), cntc);
    }

    public void enrichLedger(Envelope<LedgerRequest> envelope) {
        if (envelope == null || envelope.getData() == null) return;
        VmisProperties.SystemProps sys = props.getSystem();
        String cntc = props.getGov().getServices().getLedger().getCntcInfoCode();
        for (LedgerRequest req : envelope.getData()) {
            if (req == null) continue;
            req.setInfoSysId(sys.getInfoSysId());
            req.setInfoSysIp(sys.getInfoSysIp());
            req.setSigunguCode(sys.getSigunguCode());
            req.setCntcInfoCode(cntc);
            req.setChargerId(sys.getChargerId());
            req.setChargerIp(sys.getChargerIp());
            req.setChargerNm(sys.getChargerNm());

            // 고정값 설정 (값이 없는 경우에만 설정)
            if (req.getOnesInformationOpen() == null || req.getOnesInformationOpen().isEmpty()) {
                req.setOnesInformationOpen("1");  // 개인정보공개 (소유자공개)
            }
            if (req.getRouteSeCode() == null || req.getRouteSeCode().isEmpty()) {
                req.setRouteSeCode("3");  // 경로구분코드
            }
            if (req.getDetailExpression() == null || req.getDetailExpression().isEmpty()) {
                req.setDetailExpression("1");  // 내역표시 (전체내역)
            }
            if (req.getInqireSeCode() == null || req.getInqireSeCode().isEmpty()) {
                req.setInqireSeCode("1");  // 조회구분코드 (열람)
            }
        }
        log.debug("[ENRICH] ledger: applied INFO_SYS_ID={}, INFO_SYS_IP={}, SIGUNGU_CODE={}, CNTC_INFO_CODE={}",
                sys.getInfoSysId(), sys.getInfoSysIp(), sys.getSigunguCode(), cntc);
    }
}
