package com.ga.api.service.tax;

import com.ga.common.exception.BizException;
import com.ga.common.exception.ErrorCode;
import com.ga.common.model.PageResponse;
import com.ga.core.mapper.tax.WithholdingTaxReportMapper;
import com.ga.core.vo.tax.WithholdingTaxReportResp;
import com.ga.core.vo.tax.WithholdingTaxReportSaveReq;
import com.ga.core.vo.tax.WithholdingTaxReportSearchParam;
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 WithholdingTaxReportService {

    private final WithholdingTaxReportMapper mapper;

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

    public WithholdingTaxReportResp detail(Long reportId) {
        WithholdingTaxReportResp resp = mapper.selectDetailById(reportId);
        if (resp == null) throw new BizException(ErrorCode.NOT_FOUND);
        return resp;
    }

    @Transactional
    public int regenerate(WithholdingTaxReportSaveReq req) {
        return mapper.upsertQuarterlyAggregate(req.getSettleQuarter());
    }

    @Transactional
    public void complete(List<Long> ids) {
        if (ids == null || ids.isEmpty()) {
            throw new BizException(ErrorCode.INVALID_PARAMETER);
        }
        mapper.updateFileStatusToFiled(ids);
    }
}
