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.OverrideLedgerMapper;
import com.ga.core.mapper.commission.OverrideSummaryMapper;
import com.ga.core.vo.commission.*;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

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

    private final OverrideLedgerMapper ledgerMapper;
    private final OverrideSummaryMapper summaryMapper;

    public PageResponse<OverrideLedgerResp> list(OverrideLedgerSearchParam param) {
        param.startPage();
        return PageResponse.of(ledgerMapper.selectList(param));
    }

    public OverrideLedgerResp detail(Long ledgerId) {
        OverrideLedgerResp resp = ledgerMapper.selectById(ledgerId);
        if (resp == null) throw new BizException(ErrorCode.NOT_FOUND);
        return resp;
    }

    public List<OverrideSummaryResp> summaryList(OverrideSummarySearchParam param) {
        return summaryMapper.selectList(param);
    }

    public OverrideSummaryResp summaryByAgentAndMonth(Long toAgentId, String settleMonth) {
        OverrideSummaryResp resp = summaryMapper.selectByAgentAndMonth(toAgentId, settleMonth);
        if (resp == null) throw new BizException(ErrorCode.NOT_FOUND);
        return resp;
    }
}
