// 麵包配方資料庫 (2026 更新版)
// 每個配方: perPieceFlour (每顆總粉量 g), ingredients (佔粉比 %), extras (含料), split (分割參考重量 g)
// 註: 粉類總和 = 100%，水/酵母/鹽以粉重為基準

const RECIPES = [
  // ── 歐式麵包 (主力)
  {
    id: 'country',
    name: '鄉村',
    category: '歐式主力',
    emoji: '🌾',
    perPieceFlour: 350,
    splitWeight: 670,
    bakedWeight: 590,
    flours: [
      { name: 'Caputo 綠粉', ratio: 0.10 },
      { name: '裸麥', ratio: 0.05 },
      { name: 'SPELT 斯貝爾特', ratio: 0.10 },
      { name: 'T65', ratio: 0.20 },
      { name: '紅粉', ratio: 0.20 },
      { name: '富士', ratio: 0.35 },
    ],
    liquids: [
      { name: '鹽', ratio: 0.02 },
      { name: '酵母', ratio: 0.20 },
      { name: '水', ratio: 0.70 },
    ],
    extras: [],
    companions: [{ id: 'mini-country' }],
  },
  {
    id: 'mini-country',
    name: '小鄉村',
    category: '歐式主力',
    emoji: '🌾',
    perPieceFlour: 125,
    splitWeight: 239,
    flours: [
      { name: 'Caputo 綠粉', ratio: 0.10 },
      { name: '裸麥', ratio: 0.05 },
      { name: 'SPELT 斯貝爾特', ratio: 0.10 },
      { name: 'T65', ratio: 0.20 },
      { name: '紅粉', ratio: 0.20 },
      { name: '富士', ratio: 0.35 },
    ],
    liquids: [
      { name: '鹽', ratio: 0.02 },
      { name: '酵母', ratio: 0.20 },
      { name: '水', ratio: 0.70 },
    ],
    extras: [],
  },
  {
    id: 'farmer',
    name: '農夫',
    category: '歐式主力',
    emoji: '🌾',
    perPieceFlour: 350,
    splitWeight: 670,
    flours: [
      { name: '日清傳奇', ratio: 0.10 },
      { name: '全麥（布爾）', ratio: 0.10 },
      { name: '裸麥', ratio: 0.20 },
      { name: 'T65', ratio: 0.10 },
      { name: 'Spelt', ratio: 0.10 },
      { name: '富士', ratio: 0.20 },
      { name: '紅粉', ratio: 0.20 },
    ],
    liquids: [
      { name: '鹽', ratio: 0.02 },
      { name: '酵母', ratio: 0.20 },
      { name: '水', ratio: 0.72 },
    ],
    extras: [],
    companions: [{ id: 'mini-farmer' }],
  },
  {
    id: 'mini-farmer',
    name: '小農夫',
    category: '歐式主力',
    emoji: '🌾',
    perPieceFlour: 125,
    flours: [
      { name: '日清傳奇', ratio: 0.10 },
      { name: '全麥（布爾）', ratio: 0.10 },
      { name: '裸麥', ratio: 0.20 },
      { name: 'T65', ratio: 0.10 },
      { name: 'Spelt', ratio: 0.10 },
      { name: '富士', ratio: 0.20 },
      { name: '紅粉', ratio: 0.20 },
    ],
    liquids: [
      { name: '鹽', ratio: 0.02 },
      { name: '酵母', ratio: 0.20 },
      { name: '水', ratio: 0.72 },
    ],
    extras: [],
  },
  {
    id: 'pillow',
    name: '枕頭',
    category: '歐式主力',
    emoji: '🛏️',
    perPieceFlour: 600,
    splitWeight: 1164,
    flours: [
      { name: '豐盟', ratio: 0.50 },
      { name: '卡美尼亞', ratio: 0.30 },
      { name: '全麥', ratio: 0.20 },
    ],
    liquids: [
      { name: '鹽', ratio: 0.02 },
      { name: '酵母', ratio: 0.20 },
      { name: '水', ratio: 0.72 },
    ],
    extras: [],
  },
  {
    id: 'french',
    name: '法國',
    category: '歐式主力',
    emoji: '🥖',
    perPieceFlour: 199.8,
    splitWeight: 380,
    flours: [
      { name: 'T55', ratio: 0.50 },
      { name: 'T65', ratio: 0.40 },
      { name: '日清傳奇', ratio: 0.10 },
    ],
    liquids: [
      { name: '鹽', ratio: 0.02 },
      { name: '酵母', ratio: 0.25 },
      { name: '水', ratio: 0.67 },
    ],
    extras: [],
  },

  // ── 堅果果乾類
  {
    id: 'walnut',
    name: '核桃',
    category: '堅果果乾',
    emoji: '🌰',
    perPieceFlour: 350,
    splitWeight: 730,
    bakedWeight: 645,
    flours: [
      { name: 'Caputo 綠粉', ratio: 0.10 },
      { name: '全麥(布爾)', ratio: 0.10 },
      { name: 'Spelt', ratio: 0.20 },
      { name: 'T65', ratio: 0.10 },
      { name: '紅粉', ratio: 0.25 },
      { name: '富士', ratio: 0.25 },
    ],
    liquids: [
      { name: '鹽', ratio: 0.02 },
      { name: '酵母', ratio: 0.20 },
      { name: '水', ratio: 0.72 },
    ],
    extras: [
      { name: '核桃', ratio: 0.15 },
    ],
companions:[{id:'pecan-cranberry'},{id:'olive-provence'}],
  },
  {
    id: 'pecan-cranberry',
    name: '胡桃蔓越莓',
    category: '堅果果乾',
    emoji: '🫐',
    perPieceFlour: 250,
    splitWeight: 510,
    bakedWeight: 440,
    flours: [
      { name: 'Caputo 綠粉', ratio: 0.10 },
      { name: '全麥(布爾)', ratio: 0.10 },
      { name: 'T65', ratio: 0.10 },
      { name: 'Spelt', ratio: 0.20 },
      { name: '紅粉', ratio: 0.25 },
      { name: '富士', ratio: 0.25 },
    ],
    liquids: [
      { name: '鹽', ratio: 0.02 },
      { name: '酵母', ratio: 0.20 },
      { name: '水', ratio: 0.72 },
    ],
    extras: [
      { name: '胡桃', ratio: 0.08 },
      { name: '蔓越莓', ratio: 0.10 },
    ],
  },
  {
    id: 'olive-provence',
    name: '普羅旺斯橄欖',
    category: '堅果果乾',
    emoji: '🫒',
    perPieceFlour: 250,
    splitWeight: 510,
    bakedWeight: 440,
    flours: [
      { name: 'Caputo 綠粉', ratio: 0.10 },
      { name: '全麥(布爾)', ratio: 0.10 },
      { name: 'T65', ratio: 0.10 },
      { name: 'Spelt', ratio: 0.20 },
      { name: '紅粉', ratio: 0.25 },
      { name: '富士', ratio: 0.25 },
    ],
    liquids: [
      { name: '鹽', ratio: 0.02 },
      { name: '酵母', ratio: 0.20 },
      { name: '水', ratio: 0.72 },
    ],
    extras: [
      { name: '綠橄欖', ratio: 0.05 },
      { name: '黑橄欖', ratio: 0.05 },
      { name: '普羅旺斯香料 (茶匙)', ratio: 0.01, unit: '茶匙' },
    ],
  },

  // ── 辣味/鹹味
  {
    id: 'cheese-chili',
    name: '芝士辣椒',
    category: '鹹味',
    emoji: '🌶️',
    perPieceFlour: 250,
    splitWeight: 572.5,
    bakedWeight: 490,
    flours: [
      { name: '日清傳奇', ratio: 0.10 },
      { name: '全麥(布爾)', ratio: 0.10 },
      { name: 'T65', ratio: 0.20 },
      { name: '紅粉', ratio: 0.30 },
      { name: '富士', ratio: 0.30 },
    ],
    liquids: [
      { name: '鹽', ratio: 0.02 },
      { name: '酵母', ratio: 0.20 },
      { name: '水', ratio: 0.72 },
    ],
    extras: [
      { name: '芝士丁', ratio: 0.25 },
      { name: '墨西哥辣椒', ratio: 0.10 },
      { name: '黑胡椒', ratio: 0.0025 },
    ],
    companions: [
      { id: 'cheese-edamame' },
      { id: 'napoli' },
    ],
  },
  {
    id: 'cheese-edamame',
    name: '乾酪毛豆',
    category: '鹹味',
    emoji: '🫛',
    perPieceFlour: 250,
    splitWeight: 598,
    flours: [
      { name: '日清傳奇', ratio: 0.10 },
      { name: '全麥(布爾)', ratio: 0.10 },
      { name: 'T65', ratio: 0.20 },
      { name: '紅粉', ratio: 0.30 },
      { name: '富士', ratio: 0.30 },
    ],
    liquids: [
      { name: '鹽', ratio: 0.02 },
      { name: '酵母', ratio: 0.20 },
      { name: '水', ratio: 0.72 },
    ],
    extras: [
      { name: '乾酪', ratio: 0.15 },
      { name: '毛豆', ratio: 0.30 },
      { name: '黑胡椒', ratio: 0.002 },
    ],
  },
  {
    id: 'napoli',
    name: '拿波里',
    category: '鹹味',
    emoji: '🍅',
    perPieceFlour: 250,
    splitWeight: 560,
    flours: [
      { name: '日清傳奇', ratio: 0.10 },
      { name: '全麥(布爾)', ratio: 0.10 },
      { name: 'T65', ratio: 0.20 },
      { name: '紅粉', ratio: 0.30 },
      { name: '富士', ratio: 0.30 },
    ],
    liquids: [
      { name: '鹽', ratio: 0.02 },
      { name: '酵母', ratio: 0.20 },
      { name: '水', ratio: 0.72 },
    ],
    extras: [
      { name: '乾酪', ratio: 0.10 },
      { name: '紅切達絲', ratio: 0.10 },
      { name: '油漬番茄', ratio: 0.10 },
      { name: '義大利香料', ratio: 0.005 },
      { name: '綜合大蒜粉', ratio: 0.01 },
      { name: '黑胡椒', ratio: 0.0025 },
    ],
  },

  // ── 客製 Wendy
  {
    id: 'wendy-black-olive',
    name: '客製・黑橄欖 (Wendy)',
    category: '客製',
    emoji: '🫒',
    perPieceFlour: 125,
    splitWeight: 258.4,
    note: '加胡椒3次',
    flours: [
      { name: '全麥(布爾）', ratio: 0.15 },
      { name: 'T65', ratio: 0.20 },
      { name: '富士', ratio: 0.30 },
      { name: '紅粉', ratio: 0.30 },
      { name: '裸麥', ratio: 0.05 },
    ],
    liquids: [
      { name: '鹽', ratio: 0.0075 },
      { name: '酵母', ratio: 0.20 },
      { name: '水', ratio: 0.66 },
    ],
    extras: [
      { name: '黑橄欖', ratio: 0.20 },
    ],
  },
  {
    id: 'wendy-green-olive',
    name: '客製・綠橄欖 (Wendy)',
    category: '客製',
    emoji: '🫒',
    perPieceFlour: 125,
    splitWeight: 258.4,
    flours: [
      { name: '全麥(布爾）', ratio: 0.15 },
      { name: 'T65', ratio: 0.20 },
      { name: '富士', ratio: 0.30 },
      { name: '紅粉', ratio: 0.30 },
      { name: '裸麥', ratio: 0.05 },
    ],
    liquids: [
      { name: '鹽', ratio: 0.0075 },
      { name: '酵母', ratio: 0.20 },
      { name: '水', ratio: 0.66 },
    ],
    extras: [
      { name: '綠橄欖', ratio: 0.20 },
    ],
  },
  {
    id: 'wendy-cheese-lemon',
    name: '客製・檸檬乾酪 (Wendy)',
    category: '客製',
    emoji: '🍋',
    perPieceFlour: 125,
    splitWeight: 283.4,
    note: '胡椒加3次',
    flours: [
      { name: '全麥(布爾）', ratio: 0.15 },
      { name: 'T65', ratio: 0.20 },
      { name: '富士', ratio: 0.30 },
      { name: '紅粉', ratio: 0.30 },
      { name: '裸麥', ratio: 0.05 },
    ],
    liquids: [
      { name: '鹽', ratio: 0.0075 },
      { name: '酵母', ratio: 0.20 },
      { name: '水', ratio: 0.66 },
    ],
    extras: [
      { name: '乾酪', ratio: 0.20 },
      { name: '檸檬', ratio: 0.20 },
    ],
  },

  // ── 甜味
  {
    id: 'choc-orange',
    name: '巧克力橘皮',
    category: '甜味',
    emoji: '🍫',
    perPieceFlour: 250,
    splitWeight: 550,
    flours: [
      { name: 'T65', ratio: 0.20 },
      { name: '紅粉', ratio: 0.40 },
      { name: '富士', ratio: 0.40 },
    ],
    liquids: [
      { name: '可可粉', ratio: 0.03 },
      { name: '鹽', ratio: 0.02 },
      { name: '酵母', ratio: 0.20 },
      { name: '水', ratio: 0.75 },
    ],
    extras: [
      { name: '橘皮', ratio: 0.15 },
      { name: '巧克力豆', ratio: 0.10 },
    ],
  },

  // ── 其他
  {
    id: 'granola',
    name: '穀麥脆脆',
    category: '其他',
    emoji: '🥣',
    perPieceFlour: 0,
    noteOnly: true,
    batchUnit: 580,
    absolute: [
      { name: '燕麥', g: 130 },
      { name: '黑麥片', g: 50 },
      { name: 'T65', g: 95 },
      { name: '黑糖', g: 80 },
      { name: '香草精', g: 10 },
      { name: '鹽巴', g: 8 },
      { name: '奶油', g: 100 },
      { name: '楓糖漿', g: 115 },
      { name: '杏仁', g: 95 },
      { name: '糙米米香', g: 20 },
    ],
  },
];

// 分組
const CATEGORIES = ['歐式主力', '堅果果乾', '鹹味', '客製', '甜味', '其他'];

function getRecipe(id) {
  return RECIPES.find(r => r.id === id);
}

function calcRecipe(recipe, n) {
  if (!recipe || n <= 0) return { totalFlour: 0, sections: [] };

  if (recipe.absolute) {
    return {
      totalFlour: 0,
      totalDough: recipe.batchUnit * n,
      sections: [{
        title: '材料',
        items: recipe.absolute.map(i => ({
          name: i.name,
          amount: i.g * n,
          precise: false,
        })),
      }],
    };
  }

  const totalFlour = recipe.perPieceFlour * n;

  const flourItems = recipe.flours.map(f => ({
    name: f.name,
    amount: totalFlour * f.ratio,
    ratio: f.ratio,
    precise: false,
  }));

  const liquidItems = recipe.liquids.map(l => ({
    name: l.name,
    amount: totalFlour * l.ratio,
    ratio: l.ratio,
    precise: true,
  }));

  const extraItems = (recipe.extras || []).map(e => ({
    name: e.name,
    amount: totalFlour * e.ratio,
    ratio: e.ratio,
    precise: true,
    unit: e.unit,
  }));

  const sections = [
    { title: '粉類', items: flourItems, totalLabel: '總粉量', total: totalFlour },
    { title: '液體與酵', items: liquidItems },
  ];
  if (extraItems.length) {
    sections.push({ title: '配料', items: extraItems });
  }

  return { totalFlour, sections };
}

function fmt(amount, precise) {
  if (amount == null || isNaN(amount)) return '—';
  if (precise) {
    const r = Math.round(amount * 10) / 10;
    return r.toFixed(1);
  }
  return Math.round(amount).toString();
}

Object.assign(window, { RECIPES, CATEGORIES, getRecipe, calcRecipe, fmt });
