// src/app/tab4/tab4.page.ts
import { Component } from '@angular/core';
import { CloudObject, CloudQuery } from 'src/lib/ncloud';

type NutritionKey = 'protein' | 'carbs' | 'fat';

@Component({
  selector: 'app-tab4',
  templateUrl: 'tab4.page.html',
  styleUrls: ['tab4.page.scss'],
  standalone: false,
})
export class Tab4Page {
  currentPhase = '增肌期';
  phaseConfig = {
    color: '#FF6B35',
    tip: '建议每日热量盈余 300kcal'
  };

  trainingData = {
    calories: 875,
    sets: 5,
    accuracy: 92,
    exercise: '深蹲训练'
  };

  // 类型安全的数据结构
  nutritionKeys: NutritionKey[] = ['protein', 'carbs', 'fat'];
  nutritionGoals: Record<NutritionKey, { target: number; current: number }> = {
    protein: { target: 158, current: 120 },
    carbs: { target: 240, current: 180 },
    fat: { target: 62, current: 45 }
  };

  restaurants = [
    {
      name: '奥力给健身厨房',
      rating: 4.8,
      distance: 1.2,
      tags: ['蛋白质达标', '562kcal/套餐', '30分钟达'],
      dish: {
        name: '增肌冠军碗',
        protein: 42,
        carbs: '慢碳黑米',
        veggies: '双倍西兰花',
        price: 58,
        originalPrice: 78,
        image: 'assets/images/chicken-bowl.jpg'
      }
    }
  ];

  constructor() {
    this.loadFoodStore()
  }

  storeList: CloudObject[] = []
  async loadFoodStore() {
    let query = new CloudQuery("FoodStore")
    this.storeList = await query.find();
  }

  getProgress(goal: number, current: number): string {
    const percent = (current / goal) * 100;
    return `${percent}%`;
  }

  async importStore() {
    /**
     数据范式 FoodStore
     name String 名称
     rating Number 评分
     distance Number距离 公里数
     tags Array<string> 标签
     dish Object 招牌菜

    示例数据：
      {
      name: '奥力给健身厨房',
      rating: 4.8,
      distance: 1.2,
      tags: ['蛋白质达标', '562kcal/套餐', '30分钟达'],
      dish: {
        name: '增肌冠军碗',
        protein: 42,
        carbs: '慢碳黑米',
        veggies: '双倍西兰花',
        price: 58,
        originalPrice: 78,
        image: 'assets/images/chicken-bowl.jpg'
      }
    }
     */
    const restaurantData = [
      {
        name: '蛋白力量厨房',
        rating: 4.7,
        distance: 0.8,
        tags: ['高蛋白', '低脂', '30分钟达'],
        dish: {
          name: '蛋白力量碗',
          protein: 45,
          carbs: '糙米',
          veggies: '混合蔬菜',
          price: 52,
          originalPrice: 68,
          image: 'assets/images/protein-bowl.jpg'
        }
      },
      {
        name: '低碳轻食坊',
        rating: 4.9,
        distance: 1.5,
        tags: ['低碳水', '生酮友好', '45分钟达'],
        dish: {
          name: '牛油果鸡肉沙拉',
          protein: 38,
          carbs: '低碳蔬菜',
          veggies: '牛油果+菠菜',
          price: 65,
          originalPrice: 85,
          image: 'assets/images/avocado-salad.jpg'
        }
      },
      {
        name: '增肌战士餐厅',
        rating: 4.6,
        distance: 2.1,
        tags: ['增肌套餐', '高热量', '60分钟达'],
        dish: {
          name: '战士增肌餐',
          protein: 55,
          carbs: '红薯+糙米',
          veggies: '三色蔬菜',
          price: 68,
          originalPrice: 88,
          image: 'assets/images/gain-bowl.jpg'
        }
      },
      {
        name: '素食健身屋',
        rating: 4.5,
        distance: 1.8,
        tags: ['全素食', '植物蛋白', '40分钟达'],
        dish: {
          name: '豆腐能量碗',
          protein: 35,
          carbs: '藜麦',
          veggies: '烤蔬菜',
          price: 48,
          originalPrice: 65,
          image: 'assets/images/tofu-bowl.jpg'
        }
      },
      {
        name: '均衡营养站',
        rating: 4.8,
        distance: 0.5,
        tags: ['营养均衡', '定制化', '25分钟达'],
        dish: {
          name: '均衡营养餐',
          protein: 40,
          carbs: '可选糙米/红薯',
          veggies: '自选蔬菜',
          price: 55,
          originalPrice: 75,
          image: 'assets/images/balance-meal.jpg'
        }
      },
      {
        name: '减脂实验室',
        rating: 4.7,
        distance: 1.3,
        tags: ['低卡路里', '减脂专用', '35分钟达'],
        dish: {
          name: '实验室减脂餐',
          protein: 30,
          carbs: '魔芋米',
          veggies: '羽衣甘蓝',
          price: 45,
          originalPrice: 60,
          image: 'assets/images/lowcal-meal.jpg'
        }
      },
      {
        name: '健身快餐车',
        rating: 4.4,
        distance: 0.9,
        tags: ['快速出餐', '便携包装', '20分钟达'],
        dish: {
          name: '健身快餐盒',
          protein: 35,
          carbs: '糙米',
          veggies: '西兰花+胡萝卜',
          price: 42,
          originalPrice: 58,
          image: 'assets/images/fast-meal.jpg'
        }
      },
      {
        name: '运动员厨房',
        rating: 4.9,
        distance: 2.3,
        tags: ['专业运动员', '精准营养', '50分钟达'],
        dish: {
          name: '运动员特餐',
          protein: 50,
          carbs: '定制碳水',
          veggies: '有机蔬菜',
          price: 75,
          originalPrice: 95,
          image: 'assets/images/athlete-meal.jpg'
        }
      },
      {
        name: '健康餐大师',
        rating: 4.7,
        distance: 1.7,
        tags: ['五星主厨', '精致摆盘', '40分钟达'],
        dish: {
          name: '大师特制餐',
          protein: 42,
          carbs: '黑米+藜麦',
          veggies: '时令蔬菜',
          price: 62,
          originalPrice: 82,
          image: 'assets/images/chef-special.jpg'
        }
      },
      {
        name: '健身外卖王',
        rating: 4.6,
        distance: 0.7,
        tags: ['超高性价比', '量大实惠', '30分钟达'],
        dish: {
          name: '健身王套餐',
          protein: 38,
          carbs: '糙米',
          veggies: '混合蔬菜',
          price: 38,
          originalPrice: 55,
          image: 'assets/images/value-meal.jpg'
        }
      }
    ];
    const FoodStore = new CloudObject("FoodStore");
    const query = new CloudQuery("FoodStore");

    for (const restaurant of restaurantData) {
      try {
        // 检查是否已存在同名餐厅
        query.equalTo("name", restaurant.name);
        const existing = await query.first();

        if (existing) {
          console.log(`餐厅 "${restaurant.name}" 已存在，跳过保存`);
          continue;
        }

        // 创建新餐厅对象
        const newRestaurant = new CloudObject("FoodStore");
        newRestaurant.set(restaurant);

        // 保存到数据库
        await newRestaurant.save();
        console.log(`餐厅 "${restaurant.name}" 保存成功`);
      } catch (error) {
        console.error(`保存餐厅 "${restaurant.name}" 时出错:`, error);
      }
    }

    console.log("所有餐厅数据处理完成");
  }
}