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

export interface ContractEndorsementRow extends Record<string, unknown> {
  endorsementId: number;
  contractNo: string;
  agentId: number;
  agentName: string;
  endorsementType: string;   // PREMIUM / INSURED / BENEFICIARY / ETC
  endorsementTypeName: string;
  changeDescription: string;
  endorsementDate: string;
  recalculateRequired: boolean;
  recalculatedAt?: string;
  status: string;
  processedAt?: string;
  createdAt: string;
}

export interface ContractEndorsementSaveReq {
  contractNo: string;
  agentId: number;
  endorsementType: string;
  changeDescription: string;
  endorsementDate: string;
  recalculateRequired: boolean;
}

export interface EndorsementSearchParam {
  contractNo?: string;
  agentId?: number;
  agentName?: string;
  endorsementType?: string;
  status?: string;
  startDate?: string;
  endDate?: string;
  pageNum?: number;
  pageSize?: number;
}

export const endorsementApi = {
  list: (p: EndorsementSearchParam) =>
    unwrap<PageResponse<ContractEndorsementRow>>(
      api.get('/api/contract-endorsements', { params: p }),
    ),
  create: (body: ContractEndorsementSaveReq) =>
    unwrap<ContractEndorsementRow>(api.post('/api/contract-endorsements', body)),
  update: (id: number, body: Partial<ContractEndorsementSaveReq>) =>
    unwrap<ContractEndorsementRow>(api.put(`/api/contract-endorsements/${id}`, body)),
  process: (id: number) =>
    unwrap<void>(api.post(`/api/contract-endorsements/${id}/process`)),
  delete: (id: number) =>
    unwrap<void>(api.delete(`/api/contract-endorsements/${id}`)),
};
