/**
 * [MOCK-007] 产品模块DataSource
 * AI注意: 这是Mock实现，不是真实业务逻辑
 * 仅在USE_MOCK=true时启用
 */

import { Product } from '../types/product';

// 产品数据接口
export interface ProductDataSource {
  list(params?: {
    page?: number;
    pageSize?: number;
    category?: string;
    status?: string;
  }): Promise<{ data: Product[]; total: number }>;
  
  get(id: string): Promise<Product | null>;
  
  create(product: Omit<Product, 'id' | 'createdAt' | 'updatedAt'>): Promise<Product>;
  
  update(id: string, product: Partial<Product>): Promise<Product | null>;
  
  delete(id: string): Promise<boolean>;
  
  search(keyword: string): Promise<Product[]>;
  
  getByCategory(category: string): Promise<Product[]>;
  
  updateStatus(id: string, status: string): Promise<Product | null>;
}

// 产品DataSource实现
export class ProductDataSourceImpl implements ProductDataSource {
  async list(params?: {
    page?: number;
    pageSize?: number;
    category?: string;
    status?: string;
  }): Promise<{ data: Product[]; total: number }> {
    // 这里应该调用实际的API
    // 暂时返回Mock数据
    const mockProducts: Product[] = [
      {
        id: '1',
        name: '智能手表',
        description: '多功能智能手表，支持心率监测、运动追踪等功能',
        price: 199.99,
        category: '电子产品',
        status: 'active',
        stock: 100,
        images: ['https://example.com/watch1.jpg', 'https://example.com/watch2.jpg'],
        createdAt: new Date('2026-03-01'),
        updatedAt: new Date('2026-03-10'),
      },
      {
        id: '2',
        name: '无线耳机',
        description: '高音质无线耳机，降噪功能',
        price: 129.99,
        category: '电子产品',
        status: 'active',
        stock: 200,
        images: ['https://example.com/earphone1.jpg', 'https://example.com/earphone2.jpg'],
        createdAt: new Date('2026-03-02'),
        updatedAt: new Date('2026-03-11'),
      },
      {
        id: '3',
        name: '运动鞋',
        description: '轻便透气运动鞋',
        price: 89.99,
        category: '服装',
        status: 'active',
        stock: 150,
        images: ['https://example.com/shoe1.jpg', 'https://example.com/shoe2.jpg'],
        createdAt: new Date('2026-03-03'),
        updatedAt: new Date('2026-03-12'),
      },
    ];

    let filteredProducts = mockProducts;
    
    if (params?.category) {
      filteredProducts = filteredProducts.filter(p => p.category === params.category);
    }
    
    if (params?.status) {
      filteredProducts = filteredProducts.filter(p => p.status === params.status);
    }

    const page = params?.page || 1;
    const pageSize = params?.pageSize || 10;
    const start = (page - 1) * pageSize;
    const end = start + pageSize;
    const paginatedProducts = filteredProducts.slice(start, end);

    return {
      data: paginatedProducts,
      total: filteredProducts.length,
    };
  }

  async get(id: string): Promise<Product | null> {
    // 这里应该调用实际的API
    // 暂时返回Mock数据
    const mockProducts: Product[] = [
      {
        id: '1',
        name: '智能手表',
        description: '多功能智能手表，支持心率监测、运动追踪等功能',
        price: 199.99,
        category: '电子产品',
        status: 'active',
        stock: 100,
        images: ['https://example.com/watch1.jpg', 'https://example.com/watch2.jpg'],
        createdAt: new Date('2026-03-01'),
        updatedAt: new Date('2026-03-10'),
      },
      {
        id: '2',
        name: '无线耳机',
        description: '高音质无线耳机，降噪功能',
        price: 129.99,
        category: '电子产品',
        status: 'active',
        stock: 200,
        images: ['https://example.com/earphone1.jpg', 'https://example.com/earphone2.jpg'],
        createdAt: new Date('2026-03-02'),
        updatedAt: new Date('2026-03-11'),
      },
    ];

    const product = mockProducts.find(p => p.id === id);
    return product || null;
  }

  async create(product: Omit<Product, 'id' | 'createdAt' | 'updatedAt'>): Promise<Product> {
    // 这里应该调用实际的API
    // 暂时返回Mock数据
    const newProduct: Product = {
      ...product,
      id: `product_${Date.now()}`,
      createdAt: new Date(),
      updatedAt: new Date(),
    };

    return newProduct;
  }

  async update(id: string, product: Partial<Product>): Promise<Product | null> {
    // 这里应该调用实际的API
    // 暂时返回Mock数据
    const existingProduct = await this.get(id);
    if (!existingProduct) {
      return null;
    }

    const updatedProduct: Product = {
      ...existingProduct,
      ...product,
      updatedAt: new Date(),
    };

    return updatedProduct;
  }

  async delete(id: string): Promise<boolean> {
    // 这里应该调用实际的API
    // 暂时返回Mock数据
    return true;
  }

  async search(keyword: string): Promise<Product[]> {
    // 这里应该调用实际的API
    // 暂时返回Mock数据
    const mockProducts: Product[] = [
      {
        id: '1',
        name: '智能手表',
        description: '多功能智能手表，支持心率监测、运动追踪等功能',
        price: 199.99,
        category: '电子产品',
        status: 'active',
        stock: 100,
        images: ['https://example.com/watch1.jpg', 'https://example.com/watch2.jpg'],
        createdAt: new Date('2026-03-01'),
        updatedAt: new Date('2026-03-10'),
      },
      {
        id: '2',
        name: '无线耳机',
        description: '高音质无线耳机，降噪功能',
        price: 129.99,
        category: '电子产品',
        status: 'active',
        stock: 200,
        images: ['https://example.com/earphone1.jpg', 'https://example.com/earphone2.jpg'],
        createdAt: new Date('2026-03-02'),
        updatedAt: new Date('2026-03-11'),
      },
    ];

    return mockProducts.filter(p => 
      p.name.toLowerCase().includes(keyword.toLowerCase()) ||
      p.description.toLowerCase().includes(keyword.toLowerCase())
    );
  }

  async getByCategory(category: string): Promise<Product[]> {
    // 这里应该调用实际的API
    // 暂时返回Mock数据
    const mockProducts: Product[] = [
      {
        id: '1',
        name: '智能手表',
        description: '多功能智能手表，支持心率监测、运动追踪等功能',
        price: 199.99,
        category: '电子产品',
        status: 'active',
        stock: 100,
        images: ['https://example.com/watch1.jpg', 'https://example.com/watch2.jpg'],
        createdAt: new Date('2026-03-01'),
        updatedAt: new Date('2026-03-10'),
      },
      {
        id: '2',
        name: '无线耳机',
        description: '高音质无线耳机，降噪功能',
        price: 129.99,
        category: '电子产品',
        status: 'active',
        stock: 200,
        images: ['https://example.com/earphone1.jpg', 'https://example.com/earphone2.jpg'],
        createdAt: new Date('2026-03-02'),
        updatedAt: new Date('2026-03-11'),
      },
      {
        id: '3',
        name: '运动鞋',
        description: '轻便透气运动鞋',
        price: 89.99,
        category: '服装',
        status: 'active',
        stock: 150,
        images: ['https://example.com/shoe1.jpg', 'https://example.com/shoe2.jpg'],
        createdAt: new Date('2026-03-03'),
        updatedAt: new Date('2026-03-12'),
      },
    ];

    return mockProducts.filter(p => p.category === category);
  }

  async updateStatus(id: string, status: string): Promise<Product | null> {
    // 这里应该调用实际的API
    // 暂时返回Mock数据
    const existingProduct = await this.get(id);
    if (!existingProduct) {
      return null;
    }

    const updatedProduct: Product = {
      ...existingProduct,
      status,
      updatedAt: new Date(),
    };

    return updatedProduct;
  }
}

// 导出DataSource实例
export const productDataSource = new ProductDataSourceImpl();
