import request from '@/utils/request';
import type { ApiResult, PageResult } from '@/api';
import type {
  ShopCommissionRecord,
  ShopCommissionRecordParam,
  ShopCommissionStats
} from './model';

/**
 * 分页查询佣金记录
 */
export async function getCommissionRecordPage(
  params: ShopCommissionRecordParam
) {
  const res = await request.get<ApiResult<PageResult<ShopCommissionRecord>>>(
    '/shop/shop-commission-record/page',
    { params }
  );
  if (res.data.code === 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 获取佣金记录列表（向后兼容，返回 { data: { list, total } } 格式）
 */
export function getCommissionRecordList(params: ShopCommissionRecordParam) {
  return request.get('/shop/shop-commission-record/page', { params });
}

/**
 * 手动触发佣金结算
 */
export async function settleCommission() {
  const res = await request.post<ApiResult<unknown>>(
    '/shop/shop-commission-record/settle'
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 获取佣金统计
 */
export async function getCommissionStats(params?: ShopCommissionRecordParam) {
  const res = await request.get<ApiResult<ShopCommissionStats>>(
    '/shop/shop-commission-record/stats',
    { params }
  );
  if (res.data.code === 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}
