package com.ga.api.service.chargeback;

import com.ga.common.exception.BizException;
import com.ga.common.exception.ErrorCode;
import com.ga.common.model.PageResponse;
import com.ga.core.mapper.chargeback.ChargebackGradeMapper;
import com.ga.core.vo.chargeback.ChargebackGradeResp;
import com.ga.core.vo.chargeback.ChargebackGradeSaveReq;
import com.ga.core.vo.chargeback.ChargebackGradeSearchParam;
import com.ga.core.vo.chargeback.ChargebackGradeVO;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

@Service
@RequiredArgsConstructor
public class ChargebackGradeService {

    private static final DateTimeFormatter DATE_FMT = DateTimeFormatter.ofPattern("yyyy-MM-dd");

    private final ChargebackGradeMapper gradeMapper;

    @Transactional(readOnly = true)
    public PageResponse<ChargebackGradeResp> list(ChargebackGradeSearchParam param) {
        param.startPage();
        return PageResponse.of(gradeMapper.selectList(param));
    }

    @Transactional(readOnly = true)
    public ChargebackGradeResp detail(Long gradeId) {
        ChargebackGradeResp resp = gradeMapper.selectDetailById(gradeId);
        if (resp == null) throw new BizException(ErrorCode.NOT_FOUND);
        return resp;
    }

    @Transactional
    public void create(ChargebackGradeSaveReq req) {
        String today = LocalDate.now().format(DATE_FMT);
        ChargebackGradeVO existing = gradeMapper.selectByMonths(
                req.getProductCategory(), req.getMonthsFrom(), today);
        if (existing != null) {
            throw new BizException(ErrorCode.DUPLICATE_DATA);
        }
        gradeMapper.insert(toVO(req));
    }

    @Transactional
    public void update(Long gradeId, ChargebackGradeSaveReq req) {
        ChargebackGradeVO existing = gradeMapper.selectById(gradeId);
        if (existing == null) throw new BizException(ErrorCode.NOT_FOUND);
        ChargebackGradeVO vo = toVO(req);
        vo.setGradeId(gradeId);
        vo.setIsActive(existing.getIsActive());
        gradeMapper.update(vo);
    }

    @Transactional
    public void deactivate(Long gradeId) {
        ChargebackGradeVO existing = gradeMapper.selectById(gradeId);
        if (existing == null) throw new BizException(ErrorCode.NOT_FOUND);
        if (Boolean.FALSE.equals(existing.getIsActive())) {
            throw new BizException(ErrorCode.INVALID_PARAMETER);
        }
        gradeMapper.updateStatus(gradeId, false);
    }

    @Transactional(readOnly = true)
    public ChargebackGradeVO selectByMonths(String productCategory, Integer monthsElapsed, String baseDate) {
        return gradeMapper.selectByMonths(productCategory, monthsElapsed, baseDate);
    }

    private ChargebackGradeVO toVO(ChargebackGradeSaveReq req) {
        ChargebackGradeVO vo = new ChargebackGradeVO();
        vo.setProductCategory(req.getProductCategory());
        vo.setMonthsFrom(req.getMonthsFrom());
        vo.setMonthsTo(req.getMonthsTo());
        vo.setChargebackPercent(req.getChargebackPercent());
        vo.setEffectiveFrom(req.getEffectiveFrom());
        vo.setEffectiveTo(req.getEffectiveTo());
        vo.setIsActive(true);
        return vo;
    }
}
