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

export interface SettleResp {
  settleId: number;
  agentId: number;
  agentName: string;
  orgName?: string;
  gradeName?: string;
  settleMonth: string;
  recruitTotal?: number;
  maintainTotal?: number;
  exceptionPlus?: number;
  exceptionMinus?: number;
  overrideTotal?: number;
  chargebackTotal?: number;
  grossAmount?: number;
  taxAmount?: number;
  netAmount?: number;
  status: string;
  statusName?: string;
  holdReason?: string;
}

export interface SettleSearchParam {
  pageNum?: number;
  pageSize?: number;
  settleMonth?: string;
  agentId?: number;
  orgId?: number;
  status?: string;
}

export const settleApi = {
  list: (p: SettleSearchParam) =>
    unwrap<PageResponse<SettleResp>>(api.get('/api/settle', { params: p })),
  summary: (settleMonth: string) =>
    unwrap<Record<string, number>>(api.get('/api/settle/summary', { params: { settleMonth } })),
  orgSummary: (settleMonth: string) =>
    unwrap<Array<Record<string, unknown>>>(api.get('/api/settle/org-summary', { params: { settleMonth } })),
  confirm: (settleId: number) =>
    unwrap<void>(api.put(`/api/settle/${settleId}/confirm`)),
  hold: (settleId: number, reason: string) =>
    unwrap<void>(api.put(`/api/settle/${settleId}/hold`, { reason })),
  release: (settleId: number) =>
    unwrap<void>(api.put(`/api/settle/${settleId}/release`)),
};

// ── MOD-2: AdvancePayment ─────────────────────────
export interface AdvancePaymentRow extends Record<string, unknown> {
  advanceId: number;
  agentId: number;
  agentName?: string;
  advanceMonth: string;
  principalAmount: number;
  recoveredAmount: number;
  balance: number;
  status: string;
  memo?: string;
}

export interface AdvancePaymentSaveReq {
  agentId: number;
  advanceMonth: string;
  principalAmount: number;
  memo?: string;
}

export interface AdvanceRecoveryRow extends Record<string, unknown> {
  recoveryId: number;
  advanceId: number;
  settleMonth: string;
  recoveryAmount: number;
}

export interface AdvancePaymentSearchParam {
  pageNum?: number;
  pageSize?: number;
  agentId?: number;
  agentName?: string;
  status?: string;
  advanceMonth?: string;
}

// ── MOD-5: CarrierReconIn ─────────────────────────
export interface CarrierReconInRow extends Record<string, unknown> {
  reconId: number;
  companyId: number;
  companyName?: string;
  settleMonth: string;
  fileName?: string;
  recordCount: number;
  reportedTotal: number;
  systemTotal: number;
  varianceAmount: number;
  status: string;
}

export interface CarrierReconInSaveReq {
  companyId: number;
  settleMonth: string;
  fileName?: string;
  recordCount: number;
  reportedTotal: number;
}

export interface ReconciliationVarianceRow extends Record<string, unknown> {
  varianceId: number;
  reconId: number;
  policyNo?: string;
  expectedAmount?: number;
  reportedAmount?: number;
  varianceReason?: string;
}

export interface CarrierReconSearchParam {
  pageNum?: number;
  pageSize?: number;
  settleMonth?: string;
  companyId?: number;
  status?: string;
}

export const advancePaymentApi = {
  list: (p: AdvancePaymentSearchParam) =>
    unwrap<PageResponse<AdvancePaymentRow>>(api.get('/api/advance-payments', { params: p })),
  create: (body: AdvancePaymentSaveReq) =>
    unwrap<AdvancePaymentRow>(api.post('/api/advance-payments', body)),
  waive: (advanceId: number) =>
    unwrap<void>(api.put(`/api/advance-payments/${advanceId}/waive`)),
  recoveries: (advanceId: number) =>
    unwrap<AdvanceRecoveryRow[]>(api.get(`/api/advance-payments/${advanceId}/recoveries`)),
};

export const carrierReconInApi = {
  list: (p: CarrierReconSearchParam) =>
    unwrap<PageResponse<CarrierReconInRow>>(api.get('/api/carrier-recon-in', { params: p })),
  create: (body: CarrierReconInSaveReq) =>
    unwrap<CarrierReconInRow>(api.post('/api/carrier-recon-in', body)),
  reconcile: (reconId: number) =>
    unwrap<CarrierReconInRow>(api.post(`/api/carrier-recon-in/${reconId}/reconcile`)),
  approve: (reconId: number) =>
    unwrap<void>(api.put(`/api/carrier-recon-in/${reconId}/approve`)),
  variances: (reconId: number) =>
    unwrap<ReconciliationVarianceRow[]>(api.get(`/api/carrier-recon-in/${reconId}/variances`)),
};

export interface BatchJobLog {
  jobId: number;
  jobName: string;
  settleMonth?: string;
  status: string;
  currentStep?: string;
  progressPct?: number;
  errorMessage?: string;
  startedAt?: string;
  finishedAt?: string;
  durationSec?: number;
}

export const batchApi = {
  run: (settleMonth: string) =>
    unwrap<number>(api.post('/api/batch/settlement/run', { settleMonth })),
  status: () =>
    unwrap<BatchJobLog>(api.get('/api/batch/status')),
};
