package com.ga.api.service.contract;

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

import java.time.LocalDateTime;

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

    private final AgentContractMapper mapper;

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

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

    @Transactional
    public Long create(AgentContractSaveReq req) {
        AgentContractVO vo = new AgentContractVO();
        vo.setAgentId(req.getAgentId());
        vo.setCarrierId(req.getCarrierId());
        vo.setContractNo(req.getContractNo());
        vo.setSignedDate(req.getSignedDate());
        vo.setEffectiveFrom(req.getEffectiveFrom());
        vo.setEffectiveTo(req.getEffectiveTo());
        vo.setOnboardingBonusAmount(req.getOnboardingBonusAmount());
        vo.setStatus(AgentContractStatus.ACTIVE.name());
        mapper.insert(vo);
        return vo.getContractId();
    }

    @Transactional
    public void update(Long contractId, AgentContractSaveReq req) {
        AgentContractResp existing = mapper.selectById(contractId);
        if (existing == null) throw new BizException(ErrorCode.NOT_FOUND);
        AgentContractVO vo = new AgentContractVO();
        vo.setContractId(contractId);
        vo.setAgentId(req.getAgentId());
        vo.setCarrierId(req.getCarrierId());
        vo.setContractNo(req.getContractNo());
        vo.setSignedDate(req.getSignedDate());
        vo.setEffectiveFrom(req.getEffectiveFrom());
        vo.setEffectiveTo(req.getEffectiveTo());
        vo.setOnboardingBonusAmount(req.getOnboardingBonusAmount());
        mapper.update(vo);
    }

    @Transactional
    public void terminate(Long contractId, String reason) {
        AgentContractResp existing = mapper.selectById(contractId);
        if (existing == null) throw new BizException(ErrorCode.NOT_FOUND);
        if (AgentContractStatus.TERMINATED.name().equals(existing.getStatus())) {
            throw new BizException(ErrorCode.INVALID_STATUS, "이미 해촉된 위촉계약입니다");
        }
        mapper.updateStatus(contractId, AgentContractStatus.TERMINATED.name(), reason);
    }

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