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

// ── Response VO ────────────────────────────────────────────────────────────────
export interface DisputeResp {
  disputeId: number;
  agentId: number;
  agentName: string;
  orgName?: string;
  settleMonth: string;
  settleId?: number;
  status: string;
  statusName?: string;
  disputedAmount?: number;
  claimAmount?: number;
  reason: string;
  evidenceUrl?: string;
  reviewerName?: string;
  reviewedAt?: string;
  resolvedByName?: string;
  resolvedAt?: string;
  resolution?: string;
  adjustmentAmount?: number;
  correctionId?: number;
  createdAt: string;
}

// ── Save Request ───────────────────────────────────────────────────────────────
export interface DisputeSaveReq {
  agentId: number;
  settleMonth: string;
  settleId?: number;
  disputedAmount?: number;
  claimAmount?: number;
  reason: string;
  evidenceUrl?: string;
}

// ── Search Param ───────────────────────────────────────────────────────────────
export interface DisputeSearchParam {
  agentId?: number;
  settleMonth?: string;
  status?: string;
  pageNum?: number;
  pageSize?: number;
}

// ── API ────────────────────────────────────────────────────────────────────────
export const disputeApi = {
  list: (p: DisputeSearchParam) =>
    unwrap<PageResponse<DisputeResp>>(api.get('/api/disputes', { params: p })),

  get: (disputeId: number) =>
    unwrap<DisputeResp>(api.get(`/api/disputes/${disputeId}`)),

  create: (body: DisputeSaveReq) =>
    unwrap<DisputeResp>(api.post('/api/disputes', body)),

  review: (disputeId: number) =>
    unwrap<void>(api.post(`/api/disputes/${disputeId}/review`)),

  approve: (disputeId: number, resolution: string, adjustmentAmount?: number) =>
    unwrap<void>(api.post(`/api/disputes/${disputeId}/approve`, { resolution, adjustmentAmount })),

  reject: (disputeId: number, resolution: string) =>
    unwrap<void>(api.post(`/api/disputes/${disputeId}/reject`, { resolution })),
};
