/**
 * 订单相关 Mock 处理器
 */
import { http, HttpResponse } from 'msw';
import { generateOrderList, orderStats, orderTypeData } from '../data/order.js';
import { orderDetailResponse } from '../data/order-detail.js';
import { createSuccessResponse, createErrorResponse } from '../utils.js';

// 订单列表接口
const orderListHandler = http.get('*/order/list', ({ request }) => {
  const url = new URL(request.url);
  const page = parseInt(url.searchParams.get('page')) || 1;
  const listRows = parseInt(url.searchParams.get('size')) || 10;
  const status = url.searchParams.get('status') || 'all';

  // 生成订单数据
  const orders = generateOrderList(page, listRows);

  // 根据状态过滤数据
  let filteredOrders = orders;
  if (status !== 'all') {
    filteredOrders = orders.filter((order) => order.status === status);
  }

  const response = {
    orders: {
      current_page: page,
      per_page: listRows,
      total: 100,
      last_page: Math.ceil(100 / listRows),
      data: filteredOrders,
    },
    ...orderStats,
  };

  return HttpResponse.json(createSuccessResponse(response));
});

// 订单类型接口
const orderTypeHandler = http.get(
  'https://shop.trust-will.com/shop/admin/order/order/getType',
  () => {
    return HttpResponse.json(createSuccessResponse(orderTypeData));
  },
);

// 订单详情接口
const orderDetailHandler = http.get(
  'https://shop.trust-will.com/shop/admin/order/order/:id',
  ({ params }) => {
    const { id } = params;

    // 返回订单详情数据
    return HttpResponse.json(orderDetailResponse);
  },
);

// 订单发货接口
const orderDispatchHandler = http.post(
  'https://shop.trust-will.com/shop/admin/order/order/dispatch',
  async ({ request }) => {
    const data = await request.json();

    // 模拟发货处理
    return HttpResponse.json(createSuccessResponse({ message: '发货成功' }));
  },
);

// 订单退款接口
const orderRefundHandler = http.put(
  'https://shop.trust-will.com/shop/admin/order/order/fullRefund/:id',
  async ({ params, request }) => {
    const { id } = params;
    const data = await request.json();

    // 模拟退款处理
    return HttpResponse.json(createSuccessResponse({ message: '退款成功' }));
  },
);

// 订单改价接口
const orderChangeFeeHandler = http.put(
  'https://shop.trust-will.com/shop/admin/order/order/changeFee/:id',
  async ({ params, request }) => {
    const { id } = params;
    const data = await request.json();

    // 模拟改价处理
    return HttpResponse.json(createSuccessResponse({ message: '改价成功' }));
  },
);

// 订单日志接口
const orderActionHandler = http.get(
  'https://shop.trust-will.com/shop/admin/order/order/action/:id',
  ({ params }) => {
    const { id } = params;

    const logs = [
      {
        id: 1,
        action: '创建订单',
        operator: '系统',
        create_time: '2025/06/06 12:30:30',
        remark: '用户下单',
      },
      {
        id: 2,
        action: '支付成功',
        operator: '系统',
        create_time: '2025/06/06 12:31:30',
        remark: '微信支付',
      },
    ];

    return HttpResponse.json(createSuccessResponse(logs));
  },
);

// 联系用户接口
const contactUserHandler = http.post(
  'https://shop.trust-will.com/shop/admin/order/order/contact/:id',
  async ({ params }) => {
    const { id } = params;

    // 模拟联系用户
    return HttpResponse.json(createSuccessResponse({ message: '已发送联系信息' }));
  },
);

// 导出订单接口
const exportOrderHandler = http.get(
  'https://shop.trust-will.com/shop/admin/order/order/export/:id',
  async ({ params }) => {
    const { id } = params;

    // 模拟导出订单
    return HttpResponse.json(
      createSuccessResponse({
        message: '导出成功',
        download_url: 'https://mall-oss.trust-will.com/storage/default/order_export.xlsx',
      }),
    );
  },
);

// 关闭订单接口
const closeOrderHandler = http.put(
  'https://shop.trust-will.com/shop/admin/order/order/close/:id',
  async ({ params }) => {
    const { id } = params;

    // 模拟关闭订单
    return HttpResponse.json(createSuccessResponse({ message: '订单已关闭' }));
  },
);

// 导出订单处理器
export const orderHandlers = [
  orderListHandler,
  orderTypeHandler,
  orderDetailHandler,
  orderDispatchHandler,
  orderRefundHandler,
  orderChangeFeeHandler,
  orderActionHandler,
  contactUserHandler,
  exportOrderHandler,
  closeOrderHandler,
];
