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

export interface YearEndStatementResp extends Record<string, unknown> {
  statementId: number;
  agentId: number;
  agentName?: string;
  agentCode?: string;
  businessNo?: string;
  orgName?: string;
  statementYear: number;
  statementType?: string;
  statementTypeName?: string;
  totalIncome: number;
  totalWithheld: number;
  totalLocalTax: number;
  filePath?: string;
  fileFormat?: string;
  fileStatus?: string;
  fileStatusName?: string;
  generatedAt?: string;
  sentAt?: string;
  sentTo?: string;
  createdAt?: string;
  updatedAt?: string;
}

export interface YearEndStatementSearchParam {
  statementYear?: number;
  agentId?: number;
  orgId?: number;
  fileStatus?: string;
  fileFormat?: string;
  pageNum?: number;
  pageSize?: number;
}

export interface YearEndStatementRegenerateReq {
  statementYear: number;
  agentIds?: number[];
  statementType?: string;
}

export interface YearEndStatementSendReq {
  ids: number[];
  sentTo: string;
}

export const yearEndStatementApi = {
  list: (params?: YearEndStatementSearchParam) =>
    unwrap<{ list: YearEndStatementResp[]; total: number }>(
      api.get('/api/year-end-statements', { params })
    ),
  detail: (id: number) =>
    unwrap<YearEndStatementResp>(api.get(`/api/year-end-statements/${id}`)),
  regenerate: (req: YearEndStatementRegenerateReq) =>
    unwrap<number>(api.post('/api/year-end-statements/regenerate', req)),
  generate: (id: number, fileFormat?: string) =>
    unwrap<void>(api.post(`/api/year-end-statements/${id}/generate`, { fileFormat })),
  send: (req: YearEndStatementSendReq) =>
    unwrap<void>(api.put('/api/year-end-statements/send', req)),
};
