import { requestClient } from '#/api/request';

const prefix = 'order/';

export async function simulatePayApi(data: {
  order_id: number;
  order_type: 'product' | 'register';
}) {
  return requestClient.post<any>(`${prefix}simulate-pay`, data);
}

/**
 * 产品订单重新结算：作废残缺分账后按规则重建并入账（防重复加账）
 */
export async function retrySettleApi(data: { order_id: number }) {
  return requestClient.post<any>(`${prefix}retry-settle`, data);
}

/**
 * 超管手动确认收货 / 重试解冻（自动确认收货队列失败后的人工补偿）
 */
export async function retryConfirmReceiveApi(data: { order_id: number }) {
  return requestClient.post<any>(`${prefix}retry-confirm-receive`, data);
}

export async function getOrderTraceApi(params: {
  order_id: number;
  scene: 'product' | 'register';
}) {
  return requestClient.get<any>(`${prefix}trace`, { params });
}

export async function getOrderLedgerDetailApi(params: {
  order_id: number;
  order_type: number;
  /** 平台端可按受益方类型筛选：全部/门店/平台/供应商/配送仓库 */
  scope?: 'all' | 'platform' | 'store' | 'supplier' | 'warehouse';
}) {
  return requestClient.get<any>(`${prefix}ledger-detail`, { params });
}

export async function getOrderReconciliationDetailApi(params: {
  product_order_id: number;
}) {
  return requestClient.get<any>(`${prefix}reconciliation-detail`, { params });
}

export async function getChinaErpSyncLogsApi(params: {
  order_id: number;
  page?: number;
  pageSize?: number;
}) {
  return requestClient.get<{
    list: Array<{
      content: string;
      content_preview: string;
      created_at: string;
      id: number;
      is_success: number;
      order_id: number;
    }>;
    page: number;
    pageSize: number;
    total: number;
  }>(`${prefix}china-erp-sync-logs`, { params });
}

export async function syncChinaOrderToErpApi(data: { order_id: number }) {
  return requestClient.post<any>(`${prefix}sync-china-erp`, data);
}

export async function accrueSalespersonCommissionApi(data: { order_id: number }) {
  return requestClient.post<any>('salesperson/accrue-order', data);
}

/** 手动退回推广员分成（仅已退款/已取消订单） */
export async function reverseSalespersonCommissionApi(data: {
  order_id: number;
  store_id: number;
  salesperson_id: number;
}) {
  return requestClient.post<any>('salesperson/reverse-commission', data);
}
