import api, { PageResponse, unwrap } from './request';

export interface ExceptionLedgerResp {
  ledgerId: number;
  agentId: number;
  agentName: string;
  orgName?: string;
  exceptionCode: string;
  exceptionName?: string;
  direction: 'PLUS' | 'MINUS';
  policyNo?: string;
  companyCode?: string;
  insuranceType?: string;
  amount: number;
  taxAmount?: number;
  settleMonth: string;
  description?: string;
  approveStatus: string;
  approveStatusName?: string;
  approverId?: number;
  approverName?: string;
  approveDate?: string;
  recurringYn?: string;
  recurringLeft?: number;
  status: string;
}

export interface ExceptionLedgerSaveReq {
  agentId: number;
  exceptionCode: string;
  settleMonth: string;
  amount: number;
  policyNo?: string;
  companyCode?: string;
  insuranceType?: string;
  description?: string;
  recurringYn?: string;
  recurringMonths?: number;
}

export const exceptionApi = {
  list: (p: { settleMonth?: string; agentId?: number; status?: string; pageNum?: number; pageSize?: number }) =>
    unwrap<PageResponse<ExceptionLedgerResp>>(api.get('/api/ledger/exception', { params: p })),
  detail: (id: number) =>
    unwrap<ExceptionLedgerResp>(api.get(`/api/ledger/exception/${id}`)),
  create: (req: ExceptionLedgerSaveReq) =>
    unwrap<number>(api.post('/api/ledger/exception', req)),
  approve: (id: number, status: 'APPROVED' | 'REJECTED') =>
    unwrap<void>(api.put(`/api/ledger/exception/${id}/approve`, { status })),
};
