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

// ── Response VO ────────────────────────────────────────────────────────────
export interface SettleCorrectionResp {
  correctionId: number;
  originalSettleId: number;
  agentId: number;
  agentName: string;
  orgName?: string;
  settleMonth: string;          // YYYYMM
  diffAmount: number;           // afterAmount - beforeAmount
  beforeAmount: number;
  afterAmount: number;
  reasonCode: string;
  reasonCodeName: string;
  reasonDetail?: string;
  status: 'DRAFT' | 'APPROVED' | 'APPLIED' | 'CANCELLED';
  statusName: string;
  requestedByName?: string;
  requestedAt?: string;
  approvedByName?: string;
  approvedAt?: string;
  cancelReason?: string;
  createdAt: string;
}

// ── Request VO ─────────────────────────────────────────────────────────────
export interface SettleCorrectionSaveReq {
  originalSettleId: number;
  agentId: number;
  settleMonth: string;          // YYYYMM
  beforeAmount: number;
  afterAmount: number;
  reasonCode: string;
  reasonDetail?: string;
}

// ── Search Params ──────────────────────────────────────────────────────────
export interface SettleCorrectionSearchParam {
  agentId?: number;
  settleMonth?: string;
  status?: string;
  reasonCode?: string;
  pageNum?: number;
  pageSize?: number;
}

// ── API ────────────────────────────────────────────────────────────────────
export const settleCorrectionApi = {
  list: (p: SettleCorrectionSearchParam) =>
    unwrap<PageResponse<SettleCorrectionResp>>(api.get('/api/settle-corrections', { params: p })),

  get: (correctionId: number) =>
    unwrap<SettleCorrectionResp>(api.get(`/api/settle-corrections/${correctionId}`)),

  create: (req: SettleCorrectionSaveReq) =>
    unwrap<number>(api.post('/api/settle-corrections', req)),

  approve: (correctionId: number) =>
    unwrap<void>(api.put(`/api/settle-corrections/${correctionId}/approve`)),

  cancel: (correctionId: number, cancelReason: string) =>
    unwrap<void>(api.put(`/api/settle-corrections/${correctionId}/cancel`, { cancelReason })),
};
