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

export interface AgentContractRow extends Record<string, unknown> {
  contractId: number;
  agentId: number;
  agentName: string;
  carrierId: number;
  carrierName: string;
  contractType: string;
  effectiveFrom: string;
  effectiveTo?: string;
  commissionRate: number;
  status: string;
  terminatedAt?: string;
  terminationReason?: string;
  createdAt: string;
}

export interface AgentContractSaveReq {
  agentId: number;
  carrierId: number;
  contractType: string;
  effectiveFrom: string;
  effectiveTo?: string;
  commissionRate: number;
}

export interface AgentContractSearchParam {
  agentId?: number;
  agentName?: string;
  carrierId?: number;
  status?: string;
  pageNum?: number;
  pageSize?: number;
}

export const agentContractApi = {
  list: (p: AgentContractSearchParam) =>
    unwrap<PageResponse<AgentContractRow>>(
      api.get('/api/agent-contracts', { params: p }),
    ),
  create: (body: AgentContractSaveReq) =>
    unwrap<AgentContractRow>(api.post('/api/agent-contracts', body)),
  update: (id: number, body: Partial<AgentContractSaveReq>) =>
    unwrap<AgentContractRow>(api.put(`/api/agent-contracts/${id}`, body)),
  terminate: (id: number, reason: string) =>
    unwrap<void>(api.post(`/api/agent-contracts/${id}/terminate`, { reason })),
  delete: (id: number) =>
    unwrap<void>(api.delete(`/api/agent-contracts/${id}`)),
};
