/**
 * 商品相关的 Mock 处理器
 */
import { http, HttpResponse } from 'msw';
import { delay } from '../utils.js';

// 商品列表数据
const goodsList = {
  total: 2854,
  per_page: 10,
  current_page: 1,
  last_page: 286,
  data: [
    {
      id: 'BZ6542',
      image:
        'https://mall-oss.trust-will.com/storage/default/20250701/7bf12c56300454532244dec2aff808eb.jpg?x-oss-process=image/resize,w_200,h_200',
      title: 'BOLON Classic Aviator Polarized Sunglasses, Exclusive Eyewear Brand...',
      subtitle: '商家科技有限公司',
      price: ['¥1,000', '¥1,999'],
      sales: 2000,
      views: 392,
      stock: 2655,
      status: 'up',
      status_text: '已上架',
      sku_id: 'BS55666665',
      supplier: '商家科技有限公司',
      create_time: '2024-01-15 10:30:00',
      is_sku: false,
      activities: [],
      promos: [],
    },
    {
      id: 'BZ6543',
      image:
        'https://mall-oss.trust-will.com/storage/default/20250701/7bf12c56300454532244dec2aff808eb.jpg?x-oss-process=image/resize,w_200,h_200',
      title: 'BOLON Classic Aviator Polarized Sunglasses, Exclusive Eyewear Brand...',
      original_price: '¥1,999',
      current_price: '¥1,000',
      sales: 2000,
      views: 392,
      stock: 2655,
      status: 'up',
      status_text: '已上架',
      sku_id: 'BS55666665',
      supplier: '商家科技有限公司',
      create_time: '2024-01-15 10:30:00',
    },
    {
      id: 'BZ6544',
      image:
        'https://mall-oss.trust-will.com/storage/default/20250701/7bf12c56300454532244dec2aff808eb.jpg?x-oss-process=image/resize,w_200,h_200',
      title: 'BOLON Classic Aviator Polarized Sunglasses, Exclusive Eyewear Brand...',
      original_price: '¥1,999',
      current_price: '¥1,000',
      sales: 2000,
      views: 392,
      stock: 2655,
      status: 'up',
      status_text: '已上架',
      sku_id: 'BS55666665',
      supplier: '商家科技有限公司',
      create_time: '2024-01-15 10:30:00',
    },
    {
      id: 'BZ6545',
      image:
        'https://mall-oss.trust-will.com/storage/default/20250701/7bf12c56300454532244dec2aff808eb.jpg?x-oss-process=image/resize,w_200,h_200',
      title: 'BOLON Classic Aviator Polarized Sunglasses, Exclusive Eyewear Brand...',
      original_price: '¥1,999',
      current_price: '¥1,000',
      sales: 2000,
      views: 392,
      stock: 2655,
      status: 'down',
      status_text: '已下架',
      sku_id: 'BS55666665',
      supplier: '商家科技有限公司',
      create_time: '2024-01-15 10:30:00',
    },
    {
      id: 'BZ6546',
      image:
        'https://mall-oss.trust-will.com/storage/default/20250701/7bf12c56300454532244dec2aff808eb.jpg?x-oss-process=image/resize,w_200,h_200',
      title: 'BOLON Classic Aviator Polarized Sunglasses, Exclusive Eyewear Brand...',
      original_price: '¥1,999',
      current_price: '¥1,000',
      sales: 2000,
      views: 392,
      stock: 2655,
      status: 'down',
      status_text: '已下架',
      sku_id: 'BS55666665',
      supplier: '商家科技有限公司',
      create_time: '2024-01-15 10:30:00',
    },
    {
      id: 'BZ6547',
      image:
        'https://mall-oss.trust-will.com/storage/default/20250701/7bf12c56300454532244dec2aff808eb.jpg?x-oss-process=image/resize,w_200,h_200',
      title: 'BOLON Classic Aviator Polarized Sunglasses, Exclusive Eyewear Brand...',
      original_price: '¥1,999',
      current_price: '¥1,000',
      sales: 2000,
      views: 392,
      stock: 2655,
      status: 'down',
      status_text: '已下架',
      sku_id: 'BS55666665',
      supplier: '商家科技有限公司',
      create_time: '2024-01-15 10:30:00',
    },
    {
      id: 'BZ6548',
      image:
        'https://mall-oss.trust-will.com/storage/default/20250701/7bf12c56300454532244dec2aff808eb.jpg?x-oss-process=image/resize,w_200,h_200',
      title: 'BOLON Classic Aviator Polarized Sunglasses, Exclusive Eyewear Brand...',
      original_price: '¥1,999',
      current_price: '¥1,000',
      sales: 2000,
      views: 392,
      stock: 2655,
      status: 'deleted',
      status_text: '已删除',
      sku_id: 'BS55666665',
      supplier: '商家科技有限公司',
      create_time: '2024-01-15 10:30:00',
    },
    {
      id: 'BZ6549',
      image:
        'https://mall-oss.trust-will.com/storage/default/20250701/7bf12c56300454532244dec2aff808eb.jpg?x-oss-process=image/resize,w_200,h_200',
      title: 'BOLON Classic Aviator Polarized Sunglasses, Exclusive Eyewear Brand...',
      original_price: '¥1,999',
      current_price: '¥1,000',
      sales: 2000,
      views: 392,
      stock: 2655,
      status: 'deleted',
      status_text: '已删除',
      sku_id: 'BS55666665',
      supplier: '商家科技有限公司',
      create_time: '2024-01-15 10:30:00',
    },
    {
      id: 'BZ6550',
      image:
        'https://mall-oss.trust-will.com/storage/default/20250701/7bf12c56300454532244dec2aff808eb.jpg?x-oss-process=image/resize,w_200,h_200',
      title: 'BOLON Classic Aviator Polarized Sunglasses, Exclusive Eyewear Brand...',
      original_price: '¥1,999',
      current_price: '¥1,000',
      sales: 2000,
      views: 392,
      stock: 2655,
      status: 'up',
      status_text: '已上架',
      sku_id: 'BS55666665',
      supplier: '商家科技有限公司',
      create_time: '2024-01-15 10:30:00',
    },
  ],
};

// 商品类型数据
const goodsTypeData = {
  error: 0,
  msg: 'success',
  data: {
    status: [
      { name: '全部', type: 'all' },
      { name: '已上架', type: 'up' },
      { name: '已下架', type: 'down' },
      { name: '已删除', type: 'deleted' },
    ],
    categories: [
      { id: 1, name: '服装鞋帽' },
      { id: 2, name: '数码电器' },
      { id: 3, name: '家居用品' },
      { id: 4, name: '美妆护肤' },
      { id: 5, name: '运动户外' },
      { id: 6, name: '食品饮料' },
      { id: 7, name: '母婴用品' },
      { id: 8, name: '图书文具' },
    ],
    activity_type: [
      { name: '普通商品', type: 'normal' },
      { name: '秒杀商品', type: 'seckill' },
      { name: '团购商品', type: 'groupon' },
    ],
  },
};

export const goodsHandlers = [
  // 商品列表接口
  http.get('https://shop.trust-will.com/shop/admin/goods/goods', async ({ request }) => {
    await delay(300);

    const url = new URL(request.url);
    const status = url.searchParams.get('status') || 'all';
    const page = parseInt(url.searchParams.get('page')) || 1;
    const per_page = parseInt(url.searchParams.get('size')) || 10;

    let filteredData = goodsList.data;

    // 根据状态筛选
    if (status && status !== 'all') {
      filteredData = goodsList.data.filter((item) => item.status === status);
    }

    // 分页处理
    const start = (page - 1) * per_page;
    const end = start + per_page;
    const pageData = filteredData.slice(start, end);

    return HttpResponse.json({
      error: 0,
      msg: 'success',
      data: {
        total: filteredData.length,
        per_page: per_page,
        current_page: page,
        last_page: Math.ceil(filteredData.length / per_page),
        data: pageData,
      },
    });
  }),

  // 商品类型接口
  http.get('https://shop.trust-will.com/shop/admin/goods/goods/getType', async () => {
    await delay(200);
    return HttpResponse.json(goodsTypeData);
  }),

  // 商品详情接口
  http.get('https://shop.trust-will.com/shop/admin/goods/goods/:id', async ({ params }) => {
    await delay(200);

    const { id } = params;
    const goods = goodsList.data.find((item) => item.id === id);

    if (goods) {
      return HttpResponse.json({
        error: 0,
        msg: 'success',
        data: goods,
      });
    } else {
      return HttpResponse.json({
        error: 1,
        msg: '商品不存在',
        data: null,
      });
    }
  }),

  // 保存商品
  http.post('https://shop.trust-will.com/shop/admin/goods/goods/save', async ({ request }) => {
    await delay(500);
    const data = await request.json();
    console.log('保存商品数据:', data);

    return HttpResponse.json({
      error: 0,
      msg: '保存成功',
      data: {
        id: 'BZ' + Date.now(),
        status: 'success',
      },
    });
  }),

  // 获取商品编辑详情
  http.get('https://shop.trust-will.com/shop/admin/goods/goods/detail/:id', async ({ params }) => {
    await delay(300);
    const { id } = params;

    return HttpResponse.json({
      error: 0,
      msg: '获取成功',
      data: {
        id: id,
        category_id: [1, 11, 111],
        title: 'BOLON Classic Aviator Polarized Sunglasses',
        subtitle: '经典飞行员偏光太阳镜',
        brand: 'BOLON',
        description:
          '这是一款经典的飞行员风格偏光太阳镜，采用优质材料制作，具有出色的防紫外线效果。',
        freight_template: 'free',
        goods_no: 'BL001',
        price: '299.00',
        market_price: '399.00',
        stock: 100,
        stock_warning: 10,
        status: 'up',
        supplier: '博伦眼镜有限公司',
        images: [
          'https://mall-oss.trust-will.com/storage/default/20250701/7bf12c56300454532244dec2aff808eb.jpg',
        ],
        white_bg_image: [],
        detail_images: [],
        has_spec: false,
        spec_price: '299.00',
        spec_stock: '100',
        spec_weight: '0.15',
        specs: [],
      },
    });
  }),

  // 获取商品分类树
  http.get('https://shop.trust-will.com/shop/admin/goods/category/tree', async () => {
    await delay(200);
    return HttpResponse.json({
      error: 0,
      msg: '获取成功',
      data: [
        {
          id: 1,
          name: '电子产品',
          children: [
            {
              id: 11,
              name: '手机通讯',
              children: [
                { id: 111, name: '智能手机' },
                { id: 112, name: '老人机' },
                { id: 113, name: '对讲机' },
              ],
            },
            {
              id: 12,
              name: '电脑办公',
              children: [
                { id: 121, name: '笔记本电脑' },
                { id: 122, name: '台式机' },
                { id: 123, name: '平板电脑' },
              ],
            },
          ],
        },
        {
          id: 2,
          name: '服装鞋帽',
          children: [
            {
              id: 21,
              name: '男装',
              children: [
                { id: 211, name: 'T恤' },
                { id: 212, name: '衬衫' },
                { id: 213, name: '裤子' },
              ],
            },
            {
              id: 22,
              name: '女装',
              children: [
                { id: 221, name: '连衣裙' },
                { id: 222, name: '上衣' },
                { id: 223, name: '下装' },
              ],
            },
          ],
        },
      ],
    });
  }),

  // 上传图片
  http.post('https://shop.trust-will.com/shop/admin/upload/image', async ({ request }) => {
    await delay(800);

    return HttpResponse.json({
      error: 0,
      msg: '上传成功',
      data: {
        url: `https://mall-oss.trust-will.com/storage/default/20250701/${Date.now()}.jpg`,
        name: `image_${Date.now()}.jpg`,
        size: Math.floor(Math.random() * 1000000) + 100000,
      },
    });
  }),
];
