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

export interface ContractSuccessionRow extends Record<string, unknown> {
  successionId: number;
  contractId: number;
  policyNo: string;
  fromAgentId: number;
  fromAgentName: string;
  toAgentId: number;
  toAgentName: string;
  successionDate: string;
  reason: string;          // TERMINATION / TRANSFER / REASSIGN
  reasonName: string;
  memo?: string;
  status: string;          // ACTIVE / CANCELED
  statusName: string;
  createdAt: string;
}

export interface ContractSuccessionSaveReq {
  contractId: number;
  fromAgentId: number;
  toAgentId: number;
  successionDate: string;
  reason?: string;
  memo?: string;
}

export interface SuccessionSearchParam {
  contractId?: number;
  fromAgentId?: number;
  toAgentId?: number;
  reason?: string;
  status?: string;
  pageNum?: number;
  pageSize?: number;
}

export const successionApi = {
  list: (p: SuccessionSearchParam) =>
    unwrap<PageResponse<ContractSuccessionRow>>(
      api.get('/api/contract-successions', { params: p }),
    ),
  create: (body: ContractSuccessionSaveReq) =>
    unwrap<number>(api.post('/api/contract-successions', body)),
  cancel: (id: number) =>
    unwrap<void>(api.post(`/api/contract-successions/${id}/cancel`)),
  delete: (id: number) =>
    unwrap<void>(api.delete(`/api/contract-successions/${id}`)),
};
