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

// ── Response VO ────────────────────────────────────────────────────────────────
export interface TaxInvoiceResp {
  invoiceId: number;
  invoiceNo: string;
  invoiceType: string;
  invoiceTypeName: string;
  issueDate: string;
  supplyAmount: number;
  taxAmount: number;
  totalAmount: number;
  counterpartName: string;
  counterpartBizNo: string;
  counterpartRep?: string;
  counterpartAddr?: string;
  agentId?: number;
  agentName?: string;
  settleMonth?: string;
  description?: string;
  cancelDate?: string;
  cancelReason?: string;
  issuedByName?: string;
  createdAt: string;
}

// ── Save Request ───────────────────────────────────────────────────────────────
export interface TaxInvoiceSaveReq {
  invoiceNo: string;
  invoiceType: 'SUPPLY' | 'PURCHASE';
  issueDate: string;
  supplyAmount: number;
  taxAmount: number;
  counterpartName: string;
  counterpartBizNo: string;
  counterpartRep?: string;
  counterpartAddr?: string;
  agentId?: number;
  settleMonth?: string;
  settleId?: number;
  description?: string;
}

// ── Search Param ───────────────────────────────────────────────────────────────
export interface TaxInvoiceSearchParam {
  agentId?: number;
  settleMonth?: string;
  invoiceType?: string;
  pageNum?: number;
  pageSize?: number;
}

// ── API ────────────────────────────────────────────────────────────────────────
export const taxInvoiceApi = {
  list: (p: TaxInvoiceSearchParam) =>
    unwrap<PageResponse<TaxInvoiceResp>>(api.get('/api/tax-invoices', { params: p })),

  get: (invoiceId: number) =>
    unwrap<TaxInvoiceResp>(api.get(`/api/tax-invoices/${invoiceId}`)),

  create: (body: TaxInvoiceSaveReq) =>
    unwrap<TaxInvoiceResp>(api.post('/api/tax-invoices', body)),

  cancel: (invoiceId: number, cancelReason: string) =>
    unwrap<void>(api.post(`/api/tax-invoices/${invoiceId}/cancel`, { cancelReason })),
};
