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

export interface ComplaintRow extends Record<string, unknown> {
  complaintId: number;
  contractNo: string;
  agentId: number;
  agentName: string;
  complaintType: string;
  content: string;
  receivedDate: string;
  status: string;
  closedAt?: string;
  chargebackTriggered: boolean;
  chargebackAmount?: number;
  processNote?: string;
  createdAt: string;
}

export interface ComplaintSaveReq {
  contractNo: string;
  agentId: number;
  complaintType: string;
  content: string;
  receivedDate: string;
  chargebackTriggered: boolean;
}

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

export const complaintApi = {
  list: (p: ComplaintSearchParam) =>
    unwrap<PageResponse<ComplaintRow>>(
      api.get('/api/complaints', { params: p }),
    ),
  create: (body: ComplaintSaveReq) =>
    unwrap<ComplaintRow>(api.post('/api/complaints', body)),
  update: (id: number, body: Partial<ComplaintSaveReq>) =>
    unwrap<ComplaintRow>(api.put(`/api/complaints/${id}`, body)),
  close: (id: number, processNote: string) =>
    unwrap<void>(api.post(`/api/complaints/${id}/close`, { processNote })),
  delete: (id: number) =>
    unwrap<void>(api.delete(`/api/complaints/${id}`)),
};
