import request from '@/utils/request';
import type { ApiResult, PageResult } from '@/api';
import type {
  ShopWithdrawRecord,
  ShopWithdrawRecordParam,
  WithdrawAuditRequest,
  WithdrawConfig
} from './model';

/**
 * 分页查询提现记录
 */
export async function getWithdrawRecordPage(
  params: ShopWithdrawRecordParam
) {
  const res = await request.get<ApiResult<PageResult<ShopWithdrawRecord>>>(
    '/shop/shop-withdraw-record/page',
    { params }
  );
  if (res.data.code === 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 获取提现记录列表（向后兼容）
 */
export function getWithdrawRecordList(params: ShopWithdrawRecordParam) {
  return request.get('/shop/shop-withdraw-record/page', { params });
}

/**
 * 审核提现申请
 */
export async function auditWithdrawRecord(
  id: number,
  data: WithdrawAuditRequest
) {
  const res = await request.post<ApiResult<unknown>>(
    `/shop/shop-withdraw-record/audit/${id}`,
    data
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 确认打款
 */
export async function confirmTransfer(id: number, data?: WithdrawAuditRequest) {
  const res = await request.post<ApiResult<unknown>>(
    `/shop/shop-withdraw-record/transfer/${id}`,
    data
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 获取提现配置
 */
export async function getWithdrawConfig() {
  const res = await request.get<ApiResult<WithdrawConfig[]>>(
    '/shop/shop-withdraw-config/list'
  );
  if (res.data.code === 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 更新提现配置
 */
export async function updateWithdrawConfig(data: WithdrawConfig) {
  const res = await request.post<ApiResult<unknown>>(
    '/shop/shop-withdraw-config/update',
    data
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}
