package com.ga.api.service.commission;

import com.ga.common.exception.BizException;
import com.ga.common.exception.ErrorCode;
import com.ga.common.model.PageResponse;
import com.ga.core.mapper.commission.HqSettleItemMapper;
import com.ga.core.vo.commission.HqSettleItemResp;
import com.ga.core.vo.commission.HqSettleItemSaveReq;
import com.ga.core.vo.commission.HqSettleItemSearchParam;
import com.ga.core.vo.commission.HqSettleItemVO;
import lombok.Data;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;

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

    private final HqSettleItemMapper itemMapper;

    @Data
    public static class HqSummaryResp {
        private final String settleMonth;
        private final BigDecimal income;
        private final BigDecimal expense;
        private final BigDecimal profit;
    }

    public PageResponse<HqSettleItemResp> list(HqSettleItemSearchParam param) {
        param.startPage();
        return PageResponse.of(itemMapper.selectList(param));
    }

    public HqSettleItemResp detail(Long itemId) {
        HqSettleItemResp resp = itemMapper.selectById(itemId);
        if (resp == null) throw new BizException(ErrorCode.NOT_FOUND);
        return resp;
    }

    /**
     * 본사손익 집계: income − expense.
     * 둘 다 null이면 0으로 처리.
     */
    public HqSummaryResp summary(String settleMonth) {
        BigDecimal income = itemMapper.sumByMonthAndType(settleMonth, "INCOME");
        BigDecimal expense = itemMapper.sumByMonthAndType(settleMonth, "EXPENSE");
        if (income == null) income = BigDecimal.ZERO;
        if (expense == null) expense = BigDecimal.ZERO;
        BigDecimal profit = income.subtract(expense);
        return new HqSummaryResp(settleMonth, income, expense, profit);
    }

    @Transactional
    public Long create(HqSettleItemSaveReq req) {
        HqSettleItemVO vo = new HqSettleItemVO();
        vo.setSettleMonth(req.getSettleMonth());
        vo.setItemType(req.getItemType());
        vo.setItemName(req.getItemName());
        vo.setAmount(req.getAmount());
        vo.setMemo(req.getMemo());
        vo.setStatus("REGISTERED");
        itemMapper.insert(vo);
        return vo.getItemId();
    }

    @Transactional
    public void update(Long itemId, HqSettleItemSaveReq req) {
        HqSettleItemResp resp = itemMapper.selectById(itemId);
        if (resp == null) throw new BizException(ErrorCode.NOT_FOUND);
        HqSettleItemVO vo = new HqSettleItemVO();
        vo.setItemId(itemId);
        vo.setSettleMonth(req.getSettleMonth());
        vo.setItemType(req.getItemType());
        vo.setItemName(req.getItemName());
        vo.setAmount(req.getAmount());
        vo.setMemo(req.getMemo());
        itemMapper.update(vo);
    }

    @Transactional
    public void delete(Long itemId) {
        HqSettleItemResp resp = itemMapper.selectById(itemId);
        if (resp == null) throw new BizException(ErrorCode.NOT_FOUND);
        itemMapper.delete(itemId);
    }
}
