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

export interface ContractResp {
  contractId: number;
  agentId: number;
  agentName: string;
  orgName?: string;
  productCode?: string;
  productName?: string;
  companyCode?: string;
  companyName?: string;
  insuranceType?: string;
  policyNo: string;
  contractorName?: string;
  insuredName?: string;
  premium?: number;
  payCycle?: string;
  payCycleName?: string;
  status: string;
  statusName?: string;
  contractDate: string;
}

export interface ContractSearchParam {
  pageNum?: number;
  pageSize?: number;
  searchKeyword?: string;
  agentId?: number;
  companyId?: number;
  insuranceType?: string;
  status?: string;
  startDate?: string;
  endDate?: string;
}

export interface ContractSaveReq {
  agentId: number;
  productId: number;
  policyNo: string;
  contractorName?: string;
  insuredName?: string;
  premium?: number;
  payCycle?: string;
  payPeriod?: number;
  coveragePeriod?: number;
  contractDate?: string;
  effectiveDate?: string;
}

export const contractApi = {
  list: (p: ContractSearchParam) =>
    unwrap<PageResponse<ContractResp>>(api.get('/api/contracts', { params: p })),
  detail: (id: number) => unwrap<ContractResp>(api.get(`/api/contracts/${id}`)),
  create: (req: ContractSaveReq) => unwrap<number>(api.post('/api/contracts', req)),
  update: (id: number, req: ContractSaveReq) => unwrap<void>(api.put(`/api/contracts/${id}`, req)),
};
