/**
 * [MOCK] 自动选品Mock数据
 * AI注意: 这是Mock数据，不是真实业务数据
 * 
 * @module mock/data/productSelection.mock
 * @author AI-Frontend-33
 * @created 2026-03-20
 */

export interface ProductPool {
  id: string;
  tenant_id: string;
  shop_id: string;
  product_id: string;
  name: string;
  category: string;
  price: number;
  cost_price: number;
  profit: number;
  roi: number;
  sales_volume: number;
  rating: number;
  review_count: number;
  competition_level: 'LOW' | 'MEDIUM' | 'HIGH';
  trend: 'UP' | 'DOWN' | 'STABLE';
  source_platform: string;
  image_url: string;
  selection_score: number;
  tags: string[];
  selected_at?: string;
}

export interface SelectionRule {
  id: string;
  tenant_id: string;
  shop_id: string;
  name: string;
  category: string[];
  min_roi: number;
  max_roi: number;
  min_profit: number;
  max_price: number;
  min_sales_volume: number;
  max_competition_level: 'LOW' | 'MEDIUM' | 'HIGH';
  min_rating: number;
  trend_filter: 'ALL' | 'UP' | 'STABLE';
  enabled: boolean;
  last_run_at?: string;
  selected_count: number;
}

export interface ListingTask {
  id: string;
  tenant_id: string;
  shop_id: string;
  product_id: string;
  product_name: string;
  target_platforms: string[];
  status: 'PENDING' | 'PROCESSING' | 'COMPLETED' | 'FAILED';
  progress: number;
  error_message?: string;
  created_at: string;
  completed_at?: string;
}

export interface AutoListingConfig {
  id: string;
  tenant_id: string;
  shop_id: string;
  enabled: boolean;
  batch_size: number;
  interval_hours: number;
  target_platforms: string[];
  auto_pricing: boolean;
  auto_inventory: boolean;
  auto_description: boolean;
  auto_images: boolean;
}

export const MOCK_CATEGORIES = ['工业自动化', '电子元器件', '工具设备', '仪器仪表', '安防设备'];

export const MOCK_PLATFORMS = ['Amazon', 'eBay', 'Shopee', 'TikTok', 'Shopify'];

export const mockProductPool: ProductPool[] = [
  {
    id: '1',
    tenant_id: 'tenant-001',
    shop_id: 'shop-001',
    product_id: 'P-TEMP-001',
    name: '工业温度传感器 Pro',
    category: '工业自动化',
    price: 89.99,
    cost_price: 45.00,
    profit: 44.99,
    roi: 99.98,
    sales_volume: 1250,
    rating: 4.8,
    review_count: 342,
    competition_level: 'LOW',
    trend: 'UP',
    source_platform: 'Amazon',
    image_url: 'https://via.placeholder.com/80x80?text=Product',
    selection_score: 95,
    tags: ['高ROI', '低竞争', '上升趋势'],
  },
  {
    id: '2',
    tenant_id: 'tenant-001',
    shop_id: 'shop-001',
    product_id: 'P-PRES-002',
    name: '压力传感器 Digital',
    category: '工业自动化',
    price: 129.99,
    cost_price: 65.00,
    profit: 64.99,
    roi: 99.98,
    sales_volume: 890,
    rating: 4.6,
    review_count: 256,
    competition_level: 'MEDIUM',
    trend: 'UP',
    source_platform: 'Amazon',
    image_url: 'https://via.placeholder.com/80x80?text=Product',
    selection_score: 88,
    tags: ['高ROI', '上升趋势'],
  },
  {
    id: '3',
    tenant_id: 'tenant-001',
    shop_id: 'shop-001',
    product_id: 'P-FLOW-003',
    name: '流量计 Ultra',
    category: '仪器仪表',
    price: 299.99,
    cost_price: 150.00,
    profit: 149.99,
    roi: 99.99,
    sales_volume: 456,
    rating: 4.9,
    review_count: 189,
    competition_level: 'LOW',
    trend: 'STABLE',
    source_platform: 'eBay',
    image_url: 'https://via.placeholder.com/80x80?text=Product',
    selection_score: 92,
    tags: ['高ROI', '低竞争', '高评分'],
  },
  {
    id: '4',
    tenant_id: 'tenant-001',
    shop_id: 'shop-001',
    product_id: 'P-SENS-004',
    name: '智能传感器套件',
    category: '电子元器件',
    price: 199.99,
    cost_price: 120.00,
    profit: 79.99,
    roi: 66.66,
    sales_volume: 678,
    rating: 4.5,
    review_count: 145,
    competition_level: 'MEDIUM',
    trend: 'UP',
    source_platform: 'Shopee',
    image_url: 'https://via.placeholder.com/80x80?text=Product',
    selection_score: 85,
    tags: ['上升趋势'],
  },
  {
    id: '5',
    tenant_id: 'tenant-001',
    shop_id: 'shop-001',
    product_id: 'P-CTRL-005',
    name: 'PLC控制器 Industrial',
    category: '工业自动化',
    price: 499.99,
    cost_price: 280.00,
    profit: 219.99,
    roi: 78.57,
    sales_volume: 234,
    rating: 4.7,
    review_count: 98,
    competition_level: 'HIGH',
    trend: 'STABLE',
    source_platform: 'Amazon',
    image_url: 'https://via.placeholder.com/80x80?text=Product',
    selection_score: 78,
    tags: ['高利润'],
  },
];

export const mockSelectionRules: SelectionRule[] = [
  {
    id: '1',
    tenant_id: 'tenant-001',
    shop_id: 'shop-001',
    name: '高ROI低竞争策略',
    category: ['工业自动化', '仪器仪表'],
    min_roi: 80,
    max_roi: 150,
    min_profit: 40,
    max_price: 500,
    min_sales_volume: 500,
    max_competition_level: 'MEDIUM',
    min_rating: 4.5,
    trend_filter: 'UP',
    enabled: true,
    last_run_at: '2026-03-19 14:30:00',
    selected_count: 156,
  },
  {
    id: '2',
    tenant_id: 'tenant-001',
    shop_id: 'shop-001',
    name: '稳定增长策略',
    category: ['电子元器件', '工具设备'],
    min_roi: 60,
    max_roi: 100,
    min_profit: 30,
    max_price: 300,
    min_sales_volume: 800,
    max_competition_level: 'HIGH',
    min_rating: 4.6,
    trend_filter: 'STABLE',
    enabled: true,
    last_run_at: '2026-03-19 10:15:00',
    selected_count: 234,
  },
];

export const mockListingTasks: ListingTask[] = [
  {
    id: '1',
    tenant_id: 'tenant-001',
    shop_id: 'shop-001',
    product_id: 'P-TEMP-001',
    product_name: '工业温度传感器 Pro',
    target_platforms: ['Amazon', 'eBay'],
    status: 'COMPLETED',
    progress: 100,
    created_at: '2026-03-19 15:00:00',
    completed_at: '2026-03-19 15:05:00',
  },
  {
    id: '2',
    tenant_id: 'tenant-001',
    shop_id: 'shop-001',
    product_id: 'P-PRES-002',
    product_name: '压力传感器 Digital',
    target_platforms: ['Amazon', 'Shopee'],
    status: 'PROCESSING',
    progress: 65,
    created_at: '2026-03-19 15:10:00',
  },
  {
    id: '3',
    tenant_id: 'tenant-001',
    shop_id: 'shop-001',
    product_id: 'P-FLOW-003',
    product_name: '流量计 Ultra',
    target_platforms: ['eBay'],
    status: 'PENDING',
    progress: 0,
    created_at: '2026-03-19 15:15:00',
  },
];

export const mockAutoListingConfig: AutoListingConfig = {
  id: '1',
  tenant_id: 'tenant-001',
  shop_id: 'shop-001',
  enabled: true,
  batch_size: 10,
  interval_hours: 6,
  target_platforms: ['Amazon', 'eBay'],
  auto_pricing: true,
  auto_inventory: true,
  auto_description: true,
  auto_images: true,
};

export function getMockProductPool(params?: {
  category?: string;
  min_roi?: number;
  max_roi?: number;
  competition_level?: string;
  trend?: string;
  keyword?: string;
}): ProductPool[] {
  let result = [...mockProductPool];

  if (params?.category) {
    result = result.filter(item => item.category === params.category);
  }
  if (params?.min_roi) {
    result = result.filter(item => item.roi >= params.min_roi!);
  }
  if (params?.max_roi) {
    result = result.filter(item => item.roi <= params.max_roi!);
  }
  if (params?.competition_level) {
    result = result.filter(item => item.competition_level === params.competition_level);
  }
  if (params?.trend) {
    result = result.filter(item => item.trend === params.trend);
  }
  if (params?.keyword) {
    const keyword = params.keyword.toLowerCase();
    result = result.filter(item =>
      item.name.toLowerCase().includes(keyword) ||
      item.product_id.toLowerCase().includes(keyword)
    );
  }

  return result.sort((a, b) => b.selection_score - a.selection_score);
}

export function getMockSelectionRules(enabled?: boolean): SelectionRule[] {
  let result = [...mockSelectionRules];
  if (enabled !== undefined) {
    result = result.filter(item => item.enabled === enabled);
  }
  return result;
}

export function getMockListingTasks(status?: string): ListingTask[] {
  let result = [...mockListingTasks];
  if (status) {
    result = result.filter(item => item.status === status);
  }
  return result;
}

export function createMockSelectionRule(data: Partial<SelectionRule>): SelectionRule {
  const newRule: SelectionRule = {
    id: `${Date.now()}`,
    tenant_id: 'tenant-001',
    shop_id: 'shop-001',
    name: data.name || '新规则',
    category: data.category || [],
    min_roi: data.min_roi || 0,
    max_roi: data.max_roi || 100,
    min_profit: data.min_profit || 0,
    max_price: data.max_price || 1000,
    min_sales_volume: data.min_sales_volume || 0,
    max_competition_level: data.max_competition_level || 'HIGH',
    min_rating: data.min_rating || 0,
    trend_filter: data.trend_filter || 'ALL',
    enabled: true,
    selected_count: 0,
  };
  mockSelectionRules.push(newRule);
  return newRule;
}

export function updateMockSelectionRule(id: string, data: Partial<SelectionRule>): SelectionRule | null {
  const index = mockSelectionRules.findIndex(item => item.id === id);
  if (index === -1) return null;
  mockSelectionRules[index] = { ...mockSelectionRules[index], ...data };
  return mockSelectionRules[index];
}

export function deleteMockSelectionRule(id: string): boolean {
  const index = mockSelectionRules.findIndex(item => item.id === id);
  if (index === -1) return false;
  mockSelectionRules.splice(index, 1);
  return true;
}

export function createMockListingTask(data: Partial<ListingTask>): ListingTask {
  const newTask: ListingTask = {
    id: `${Date.now()}`,
    tenant_id: 'tenant-001',
    shop_id: 'shop-001',
    product_id: data.product_id || '',
    product_name: data.product_name || '',
    target_platforms: data.target_platforms || [],
    status: 'PENDING',
    progress: 0,
    created_at: new Date().toISOString().replace('T', ' ').slice(0, 19),
  };
  mockListingTasks.unshift(newTask);
  return newTask;
}

export function updateMockListingTask(id: string, data: Partial<ListingTask>): ListingTask | null {
  const index = mockListingTasks.findIndex(item => item.id === id);
  if (index === -1) return null;
  mockListingTasks[index] = { ...mockListingTasks[index], ...data };
  return mockListingTasks[index];
}

export function deleteMockListingTask(id: string): boolean {
  const index = mockListingTasks.findIndex(item => item.id === id);
  if (index === -1) return false;
  mockListingTasks.splice(index, 1);
  return true;
}

export function getMockAutoListingConfig(): AutoListingConfig {
  return { ...mockAutoListingConfig };
}

export function updateMockAutoListingConfig(data: Partial<AutoListingConfig>): AutoListingConfig {
  Object.assign(mockAutoListingConfig, data);
  return { ...mockAutoListingConfig };
}
