import { CloudObject } from "src/lib/ncloud";

// 食谱分类数据生成
async function seedRecipeCategories() {
  const categories = [
    { name: '中式菜系', icon: 'utensils' },
    { name: '西式料理', icon: 'fork-knife' },
    { name: '减脂轻食', icon: 'carrot' },
    { name: '甜点烘焙', icon: 'cake' },
    { name: '汤羹粥品', icon: 'bowl' },
    { name: '素食主义', icon: 'leaf' }
  ];

  const createdCategories = [];
  
  for (const category of categories) {
    const categoryObj = new CloudObject('RecipeCategory');
    categoryObj.set(category);
    await categoryObj.save();
    createdCategories.push(categoryObj);
    console.log(`Created category: ${category.name}`);
  }

  return createdCategories;
}

// 食谱数据生成
async function seedRecipes(categories: CloudObject[]) {
  const recipes = [
    // 减脂轻食类
  {
    title: '香煎鸡胸肉配时蔬',
    imageUrl: '/assets/food/jxr.jpg',
    description: '低脂高蛋白的健身餐首选，鸡胸肉嫩滑多汁，搭配新鲜时蔬，营养均衡且美味',
    prepTime: '10分钟',
    cookTime: '20分钟',
    difficulty: '简单',
    servings: 2,
    rating: 4.8,
    ingredients: [
      { name: '鸡胸肉', amount: '300g' },
      { name: '西兰花', amount: '200g' },
      { name: '胡萝卜', amount: '1根' },
      { name: '橄榄油', amount: '2汤匙' },
      { name: '盐和黑胡椒', amount: '适量' }
    ],
    steps: [
      '鸡胸肉用盐和黑胡椒腌制10分钟',
      '西兰花和胡萝卜切小块焯水',
      '平底锅加热橄榄油，放入鸡胸肉煎至两面金黄',
      '加入蔬菜翻炒均匀即可'
    ],
    category: categories.find(c => c.get('name') === '减脂轻食')
  },
  {
    title: '藜麦蔬菜沙拉',
    imageUrl: '/assets/food/lmssl.jpg',
    description: '超级食物藜麦与新鲜蔬菜的完美组合，富含膳食纤维和植物蛋白',
    prepTime: '15分钟',
    cookTime: '15分钟',
    difficulty: '简单',
    servings: 2,
    rating: 4.6,
    ingredients: [
      { name: '藜麦', amount: '100g' },
      { name: '樱桃番茄', amount: '10颗' },
      { name: '黄瓜', amount: '1根' },
      { name: '柠檬汁', amount: '2汤匙' },
      { name: '橄榄油', amount: '1汤匙' }
    ],
    steps: [
      '藜麦煮熟后放凉',
      '蔬菜洗净切丁',
      '混合所有食材',
      '加入柠檬汁和橄榄油拌匀'
    ],
    category: categories.find(c => c.get('name') === '减脂轻食')
  },

  // 西式料理类
  {
    title: '奶油蘑菇意面',
    imageUrl: '/assets/food/nymg.jpg',
    description: '经典意大利风味，奶油香浓，蘑菇鲜美，搭配弹牙意面口感绝佳',
    prepTime: '15分钟',
    cookTime: '30分钟',
    difficulty: '中等',
    servings: 2,
    rating: 4.9,
    ingredients: [
      { name: '意大利面', amount: '250g' },
      { name: '蘑菇', amount: '200g' },
      { name: '淡奶油', amount: '200ml' },
      { name: '大蒜', amount: '3瓣' },
      { name: '帕玛森奶酪', amount: '适量' }
    ],
    steps: [
      '意大利面按包装说明煮熟',
      '大蒜切末，蘑菇切片',
      '锅中加热橄榄油，炒香蒜末',
      '加入蘑菇炒至软化',
      '倒入淡奶油煮至浓稠',
      '拌入煮好的意面，撒上奶酪'
    ],
    category: categories.find(c => c.get('name') === '西式料理')
  },
  {
    title: '经典牛排配薯泥',
    imageUrl: '/assets/food/np.jpg',
    description: '三分熟完美牛排搭配绵密土豆泥，高端餐厅级别的家庭料理',
    prepTime: '20分钟',
    cookTime: '15分钟',
    difficulty: '中等',
    servings: 2,
    rating: 4.7,
    ingredients: [
      { name: '牛排', amount: '2块(约300g)' },
      { name: '土豆', amount: '500g' },
      { name: '黄油', amount: '50g' },
      { name: '迷迭香', amount: '2枝' },
      { name: '海盐', amount: '适量' }
    ],
    steps: [
      '牛排室温回温，两面撒盐',
      '土豆煮熟压成泥，加入黄油',
      '高温煎锅煎牛排，每面2分钟',
      '加入迷迭香提香',
      '静置5分钟后切片'
    ],
    category: categories.find(c => c.get('name') === '西式料理')
  },

  // 中式菜系类
  {
    title: '红烧肉',
    imageUrl: '/assets/food/hsr.jpg',
    description: '经典本帮菜，肥而不腻，入口即化，酱香浓郁的家常美味',
    prepTime: '20分钟',
    cookTime: '90分钟',
    difficulty: '中等',
    servings: 4,
    rating: 4.7,
    ingredients: [
      { name: '五花肉', amount: '500g' },
      { name: '生姜', amount: '1块' },
      { name: '八角', amount: '2颗' },
      { name: '老抽', amount: '2汤匙' },
      { name: '冰糖', amount: '30g' }
    ],
    steps: [
      '五花肉切块焯水',
      '锅中放少量油，加入冰糖炒至融化',
      '放入肉块翻炒上色',
      '加入生姜、八角和适量水',
      '小火炖煮1小时至肉质酥烂'
    ],
    category: categories.find(c => c.get('name') === '中式菜系')
  },
  {
    title: '宫保鸡丁',
    imageUrl: '/assets/food/gbjd.jpg',
    description: '川菜经典，鸡肉嫩滑，花生香脆，麻辣酸甜的完美平衡',
    prepTime: '15分钟',
    cookTime: '10分钟',
    difficulty: '中等',
    servings: 3,
    rating: 4.8,
    ingredients: [
      { name: '鸡胸肉', amount: '400g' },
      { name: '花生米', amount: '100g' },
      { name: '干辣椒', amount: '10个' },
      { name: '花椒', amount: '1茶匙' },
      { name: '生抽', amount: '2汤匙' }
    ],
    steps: [
      '鸡肉切丁腌制',
      '花生米炸至金黄',
      '爆香辣椒和花椒',
      '快速翻炒鸡丁',
      '加入调味汁收干'
    ],
    category: categories.find(c => c.get('name') === '中式菜系')
  },

  // 甜点烘焙类
  {
    title: '提拉米苏',
    imageUrl: '/assets/food/tlms.jpg',
    description: '意大利经典甜点，咖啡酒香与马斯卡彭奶酪的完美融合，口感层次丰富',
    prepTime: '30分钟',
    cookTime: '0分钟',
    difficulty: '中等',
    servings: 6,
    rating: 4.9,
    ingredients: [
      { name: '马斯卡彭奶酪', amount: '250g' },
      { name: '手指饼干', amount: '200g' },
      { name: '浓缩咖啡', amount: '100ml' },
      { name: '鸡蛋', amount: '3个' },
      { name: '可可粉', amount: '适量' }
    ],
    steps: [
      '蛋黄加糖打发，拌入马斯卡彭奶酪',
      '蛋白打发后拌入奶酪糊',
      '手指饼干蘸咖啡液铺在容器底部',
      '铺一层奶酪糊，重复一层饼干一层奶酪',
      '冷藏4小时，撒可可粉'
    ],
    category: categories.find(c => c.get('name') === '甜点烘焙')
  },
  {
    title: '巧克力熔岩蛋糕',
    imageUrl: '/assets/food/qklry.jpg',
    description: '外酥内软的巧克力甜点，切开后热巧克力浆缓缓流出，视觉与味觉的双重享受',
    prepTime: '20分钟',
    cookTime: '12分钟',
    difficulty: '中等',
    servings: 4,
    rating: 4.9,
    ingredients: [
      { name: '黑巧克力', amount: '200g' },
      { name: '黄油', amount: '100g' },
      { name: '鸡蛋', amount: '3个' },
      { name: '细砂糖', amount: '80g' },
      { name: '低筋面粉', amount: '50g' }
    ],
    steps: [
      '巧克力和黄油隔水融化',
      '鸡蛋和糖打发至蓬松',
      '混合巧克力液和蛋糊',
      '筛入面粉轻轻拌匀',
      '倒入模具210°C烤12分钟'
    ],
    category: categories.find(c => c.get('name') === '甜点烘焙')
  },

  // 汤羹粥品类
  {
    title: '番茄蛋花汤',
    imageUrl: '/assets/food/fqdh.jpg',
    description: '家常快手汤品，酸甜开胃，蛋花柔滑，五分钟即可完成的营养美味',
    prepTime: '5分钟',
    cookTime: '10分钟',
    difficulty: '简单',
    servings: 4,
    rating: 4.5,
    ingredients: [
      { name: '番茄', amount: '2个' },
      { name: '鸡蛋', amount: '2个' },
      { name: '葱花', amount: '适量' },
      { name: '盐', amount: '适量' }
    ],
    steps: [
      '番茄切块，鸡蛋打散',
      '水烧开放入番茄煮软',
      '慢慢倒入蛋液形成蛋花',
      '加盐调味，撒葱花'
    ],
    category: categories.find(c => c.get('name') === '汤羹粥品')
  },
  {
    title: '皮蛋瘦肉粥',
    imageUrl: '/assets/food/pdsrz.jpg',
    description: '广式经典粥品，米粒开花绵密，皮蛋Q弹，肉丝鲜嫩，早餐夜宵皆宜',
    prepTime: '10分钟',
    cookTime: '40分钟',
    difficulty: '简单',
    servings: 4,
    rating: 4.7,
    ingredients: [
      { name: '大米', amount: '150g' },
      { name: '皮蛋', amount: '2个' },
      { name: '猪里脊', amount: '100g' },
      { name: '姜丝', amount: '适量' },
      { name: '白胡椒粉', amount: '少许' }
    ],
    steps: [
      '大米浸泡30分钟后煮粥',
      '肉丝用调料腌制',
      '皮蛋切块',
      '粥煮至绵密时加入配料',
      '再煮10分钟调味'
    ],
    category: categories.find(c => c.get('name') === '汤羹粥品')
  },

  // 素食主义类
  {
    title: '素食沙拉',
    imageUrl: '/assets/food/ssl.jpg',
    description: '清新爽口的全素沙拉，多种蔬菜搭配柠檬油醋汁，健康低卡无负担',
    prepTime: '15分钟',
    cookTime: '0分钟',
    difficulty: '简单',
    servings: 2,
    rating: 4.6,
    ingredients: [
      { name: '生菜', amount: '100g' },
      { name: '黄瓜', amount: '1根' },
      { name: '樱桃番茄', amount: '10个' },
      { name: '橄榄油', amount: '2汤匙' },
      { name: '柠檬汁', amount: '1汤匙' }
    ],
    steps: [
      '所有蔬菜洗净切块',
      '混合橄榄油和柠檬汁制成酱汁',
      '将酱汁淋在蔬菜上拌匀'
    ],
    category: categories.find(c => c.get('name') === '素食主义')
  },
  {
    title: '素三鲜豆腐',
    imageUrl: '/assets/food/ssxd.jpg',
    description: '植物蛋白丰富的素食料理，豆腐外焦里嫩，搭配三种时令鲜蔬',
    prepTime: '15分钟',
    cookTime: '15分钟',
    difficulty: '简单',
    servings: 3,
    rating: 4.5,
    ingredients: [
      { name: '老豆腐', amount: '1块' },
      { name: '香菇', amount: '5朵' },
      { name: '胡萝卜', amount: '1根' },
      { name: '青椒', amount: '1个' },
      { name: '素蚝油', amount: '2汤匙' }
    ],
    steps: [
      '豆腐切块煎至两面金黄',
      '蔬菜切片炒至断生',
      '加入豆腐和调味料',
      '小火焖煮5分钟'
    ],
    category: categories.find(c => c.get('name') === '素食主义')
  }
  ];

  const createdRecipes = [];
  
  for (const recipe of recipes) {
    const recipeObj = new CloudObject('Recipe');
    const recipeData = {
      ...recipe,
      category: recipe.category?.toPointer()
    };
    recipeObj.set(recipeData);
    await recipeObj.save();
    createdRecipes.push(recipeObj);
    console.log(`Created recipe: ${recipe.title}`);
  }

  return createdRecipes;
}

// 历史记录导入
async function seedHistories(recipes: CloudObject[]) {
  // 随机选择5-10个食谱作为历史记录
  const shuffled = [...recipes].sort(() => 0.5 - Math.random());
  const historyCount = 5 + Math.floor(Math.random() * 6); // 5-10条记录
  const historyRecipes = shuffled.slice(0, historyCount);
  
  // 创建历史记录，时间分布在最近30天内
  for (let i = 0; i < historyRecipes.length; i++) {
    const history = new CloudObject('RecipeHistory');
    const date = new Date();
    date.setDate(date.getDate() - Math.floor(Math.random() * 30));
    
    history.set({
      recipe: historyRecipes[i].toPointer(),
      viewedAt: date.toISOString()
    });
    
    await history.save();
    console.log(`Created history record for recipe: ${historyRecipes[i].get('title')} at ${date.toLocaleDateString()}`);
  }
}

// 收藏导入
async function seedFavorites(recipes: CloudObject[]) {
  // 随机选择3-8个食谱作为收藏
  const shuffled = [...recipes].sort(() => 0.5 - Math.random());
  const favoriteCount = 3 + Math.floor(Math.random() * 6); // 3-8条记录
  const favoriteRecipes = shuffled.slice(0, favoriteCount);
  
  // 创建收藏记录
  for (const recipe of favoriteRecipes) {
    const favorite = new CloudObject('RecipeFavorite');
    const date = new Date();
    date.setDate(date.getDate() - Math.floor(Math.random() * 30));
    
    favorite.set({
      recipe: recipe.toPointer(),
      createdAt: date.toISOString()
    });
    
    await favorite.save();
    console.log(`Created favorite record for recipe: ${recipe.get('title')} at ${date.toLocaleDateString()}`);
  }
}

// 每日推荐数据生成
async function seedDailyRecommendations(recipes: CloudObject[]) {
  // 获取最近7天的日期
  const dates = [];
  for (let i = 0; i < 7; i++) {
    const date = new Date();
    date.setDate(date.getDate() - i);
    dates.push(date);
  }

  for (const date of dates) {
    // 随机选择3个食谱
    const shuffled = [...recipes].sort(() => 0.5 - Math.random());
    const recommended = shuffled.slice(0, 3).map(r => r.toPointer());
    
    const recommendation = new CloudObject('DailyRecommendation');
    recommendation.set({
      date: date.toISOString(),
      recommendedRecipes: recommended
    });
    
    await recommendation.save();
    console.log(`Created recommendation for ${date.toLocaleDateString()}`);
  }
}

// 主导入函数
export async function importAllData() {
  try {
    console.log('Starting data import...');
    
    // 1. 先导入分类
    const categories = await seedRecipeCategories();
    
    // 2. 导入食谱数据
    const recipes = await seedRecipes(categories);
    
    // 3. 生成历史记录
    await seedHistories(recipes);
    
    // 4. 生成收藏记录
    await seedFavorites(recipes);
    
    // 5. 生成每日推荐
    await seedDailyRecommendations(recipes);
    
    console.log('Data import completed successfully!');
  } catch (error) {
    console.error('Error during data import:', error);
  }
}