package com.ga.api.service.complaint;

import com.ga.common.exception.BizException;
import com.ga.common.exception.ErrorCode;
import com.ga.common.model.PageResponse;
import com.ga.core.mapper.complaint.ComplaintMapper;
import com.ga.core.vo.complaint.*;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@RequiredArgsConstructor
@Transactional(readOnly = true)
public class ComplaintService {

    private final ComplaintMapper mapper;

    public PageResponse<ComplaintResp> list(ComplaintSearchParam param) {
        param.startPage();
        return PageResponse.of(mapper.selectList(param));
    }

    public ComplaintResp detail(Long complaintId) {
        ComplaintResp resp = mapper.selectById(complaintId);
        if (resp == null) throw new BizException(ErrorCode.NOT_FOUND);
        return resp;
    }

    @Transactional
    public Long create(ComplaintSaveReq req) {
        ComplaintVO vo = new ComplaintVO();
        vo.setContractId(req.getContractId());
        vo.setAgentId(req.getAgentId());
        vo.setCustomerName(req.getCustomerName());
        vo.setComplaintDate(req.getComplaintDate());
        vo.setChannel(req.getChannel());
        vo.setComplaintType(req.getComplaintType());
        vo.setSeverity(req.getSeverity() != null ? req.getSeverity() : "MID");
        vo.setStatus(ComplaintStatus.OPEN.name());
        vo.setResolution(req.getResolution());
        // chargebackTriggered=true 면 P5에서 chargeback_ledger 자동 생성 예정 — 지금은 플래그만 저장
        vo.setChargebackTriggered(req.getChargebackTriggered());
        mapper.insert(vo);
        return vo.getComplaintId();
    }

    @Transactional
    public void update(Long complaintId, ComplaintSaveReq req) {
        ComplaintResp existing = mapper.selectById(complaintId);
        if (existing == null) throw new BizException(ErrorCode.NOT_FOUND);
        ComplaintVO vo = new ComplaintVO();
        vo.setComplaintId(complaintId);
        vo.setContractId(req.getContractId());
        vo.setAgentId(req.getAgentId());
        vo.setCustomerName(req.getCustomerName());
        vo.setComplaintDate(req.getComplaintDate());
        vo.setChannel(req.getChannel());
        vo.setComplaintType(req.getComplaintType());
        vo.setSeverity(req.getSeverity());
        vo.setResolution(req.getResolution());
        vo.setChargebackTriggered(req.getChargebackTriggered());
        mapper.update(vo);
    }

    /**
     * 처리완료 — status=CLOSED.
     * chargebackTriggered=true 인 건의 실제 환수 생성은 P5 배치에서.
     */
    @Transactional
    public void close(Long complaintId, String resolution) {
        ComplaintResp existing = mapper.selectById(complaintId);
        if (existing == null) throw new BizException(ErrorCode.NOT_FOUND);
        if (ComplaintStatus.CLOSED.name().equals(existing.getStatus())) {
            throw new BizException(ErrorCode.INVALID_STATUS, "이미 처리완료된 민원입니다");
        }
        mapper.updateStatus(complaintId, ComplaintStatus.CLOSED.name(), resolution);
    }

    @Transactional
    public void delete(Long complaintId) {
        ComplaintResp existing = mapper.selectById(complaintId);
        if (existing == null) throw new BizException(ErrorCode.NOT_FOUND);
        mapper.delete(complaintId);
    }
}
