import { http, HttpResponse } from 'msw';
import { commissionMockData } from '../commission.js';

export const commissionHandlers = [
  // 获取佣金列表
  http.get('https://shop.trust-will.com/shop/admin/finance/commission', ({ request }) => {
    const url = new URL(request.url);
    const params = {
      page: parseInt(url.searchParams.get('page') || '1'),
      size: parseInt(url.searchParams.get('size') || '10'),
      username: url.searchParams.get('username') || '',
      phone: url.searchParams.get('phone') || '',
      commission_no: url.searchParams.get('commission_no') || '',
      status: url.searchParams.get('status') || 'all',
      type: url.searchParams.get('type') || '',
      start_time: url.searchParams.get('start_time') || '',
      end_time: url.searchParams.get('end_time') || '',
      order: url.searchParams.get('order') || '',
      sort: url.searchParams.get('sort') || '',
    };

    const result = commissionMockData.getList(params);
    return HttpResponse.json(result);
  }),

  // 获取佣金详情
  http.get('https://shop.trust-will.com/shop/admin/finance/commission/:id', ({ params }) => {
    const result = commissionMockData.getDetail(params.id);
    return HttpResponse.json(result);
  }),

  // 导出佣金记录
  http.post('https://shop.trust-will.com/shop/admin/finance/commission/export', ({ request }) => {
    const result = commissionMockData.export({});
    return HttpResponse.json(result);
  }),

  // 创建佣金记录
  http.post('https://shop.trust-will.com/shop/admin/finance/commission', async ({ request }) => {
    const body = await request.json();
    return HttpResponse.json({
      error: 0,
      msg: '创建成功',
      data: {
        id: Date.now(),
        ...body,
      },
    });
  }),

  // 更新佣金记录
  http.put(
    'https://shop.trust-will.com/shop/admin/finance/commission/:id',
    async ({ params, request }) => {
      const body = await request.json();
      return HttpResponse.json({
        error: 0,
        msg: '更新成功',
        data: {
          id: params.id,
          ...body,
        },
      });
    },
  ),

  // 删除佣金记录
  http.delete('https://shop.trust-will.com/shop/admin/finance/commission/:id', ({ params }) => {
    return HttpResponse.json({
      error: 0,
      msg: '删除成功',
      data: null,
    });
  }),

  // 结算佣金
  http.post(
    'https://shop.trust-will.com/shop/admin/finance/commission/:id/settle',
    ({ params }) => {
      return HttpResponse.json({
        error: 0,
        msg: '结算成功',
        data: null,
      });
    },
  ),

  // 批量结算佣金
  http.post(
    'https://shop.trust-will.com/shop/admin/finance/commission/batch-settle',
    async ({ request }) => {
      const body = await request.json();
      return HttpResponse.json({
        error: 0,
        msg: '批量结算成功',
        data: null,
      });
    },
  ),
];
