// ─── Fabricator Equipment Recipes ──────────────────────────────────────────────
// Core and elemental recipes. Branch recipes are in separate files.
// Material recipes are in fabricator-material-recipes.ts.

import type { FabricatorRecipe } from './fabricator-recipe-types';
export type { FabricatorRecipe, MANA_TYPE_LABELS } from './fabricator-recipe-types';
export { MATERIAL_RECIPES } from './fabricator-material-recipes';
export { WIZARD_BRANCH_RECIPES } from './fabricator-wizard-recipes';
export { PHYSICAL_BRANCH_RECIPES } from './fabricator-physical-recipes';

import { WIZARD_BRANCH_RECIPES } from './fabricator-wizard-recipes';
import { PHYSICAL_BRANCH_RECIPES } from './fabricator-physical-recipes';

export const FABRICATOR_RECIPES: FabricatorRecipe[] = [
  // ─── Earth Gear (Compacted Earth — high defense) ──────────────────────
  {
    id: 'earthHelm',
    name: 'Earthen Helm',
    description: 'A sturdy helm carved from compacted stone.',
    manaType: 'earth',
    equipmentTypeId: 'wizardHat',
    slot: 'head',
    materials: { manaCrystalDust: 4, earthShard: 2 },
    manaCost: 200,
    craftTime: 3,
    rarity: 'uncommon',
    gearTrait: '+25 Earth Mana Capacity',
    bonusEnchantments: [
      { effectId: 'earth_cap_10', stacks: 1, actualCost: 30 },
    ],
  },
  {
    id: 'earthChest',
    name: 'Stoneguard Armor',
    description: 'Heavy stone plates layered over leather. Slow but nearly impenetrable.',
    manaType: 'earth',
    equipmentTypeId: 'scholarRobe',
    slot: 'body',
    materials: { manaCrystalDust: 8, earthShard: 4, elementalCore: 1 },
    manaCost: 500,
    craftTime: 6,
    rarity: 'rare',
    gearTrait: '+50 Earth Mana Capacity, +10% Earth Mana Regen',
    bonusEnchantments: [
      { effectId: 'earth_cap_10', stacks: 1, actualCost: 30 },
      { effectId: 'mana_regen_1', stacks: 1, actualCost: 15 },
    ],
  },
  {
    id: 'earthBoots',
    name: 'Stonegreaves',
    description: 'Boots reinforced with compacted earth. Firm footing in any battle.',
    manaType: 'earth',
    equipmentTypeId: 'travelerBoots',
    slot: 'feet',
    materials: { manaCrystalDust: 3, earthShard: 1 },
    manaCost: 150,
    craftTime: 2,
    rarity: 'uncommon',
    gearTrait: '+20 Earth Mana Capacity',
    bonusEnchantments: [
      { effectId: 'earth_cap_10', stacks: 1, actualCost: 30 },
    ],
  },

  // ─── Metal Gear (Fire+Earth — balanced offense/defense) ──────────────
  {
    id: 'metalBlade',
    name: 'Metal Blade',
    description: 'A blade forged from condensed metal mana. Sharp and durable.',
    manaType: 'metal',
    equipmentTypeId: 'steelBlade',
    slot: 'mainHand',
    materials: { manaCrystalDust: 6, metalShard: 3, elementalCore: 2 },
    manaCost: 400,
    craftTime: 5,
    rarity: 'rare',
    gearTrait: '+15% Enchantment Power, +20 Metal Mana Capacity',
    bonusEnchantments: [
      { effectId: 'damage_5', stacks: 1, actualCost: 15 },
      { effectId: 'metal_cap_10', stacks: 1, actualCost: 30 },
    ],
  },
  {
    id: 'metalShield',
    name: 'Metal Spell Focus',
    description: 'A metal orb that amplifies spellcasting. Boosts enchantment power.',
    manaType: 'metal',
    equipmentTypeId: 'metalSpellFocus',
    slot: 'offHand',
    materials: { manaCrystalDust: 7, metalShard: 4, elementalCore: 1 },
    manaCost: 450,
    craftTime: 5,
    rarity: 'rare',
    gearTrait: '+15% Enchantment Capacity',
    bonusEnchantments: [
      { effectId: 'mana_cap_50', stacks: 1, actualCost: 20 },
    ],
  },
  {
    id: 'metalGloves',
    name: 'Metalweave Gauntlets',
    description: 'Gauntlets woven with metal mana threads. Protective yet dexterous.',
    manaType: 'metal',
    equipmentTypeId: 'spellweaveGloves',
    slot: 'hands',
    materials: { manaCrystalDust: 4, metalShard: 2 },
    manaCost: 250,
    craftTime: 3,
    rarity: 'uncommon',
    gearTrait: '+10% Enchantment Power, +15 Metal Mana Capacity',
    bonusEnchantments: [
      { effectId: 'metal_cap_10', stacks: 1, actualCost: 30 },
    ],
  },

  // ─── Crystal Gear (Sand+Sand+Light — high enchantment capacity) ──────
  {
    id: 'crystalWand',
    name: 'Crystal Focus Wand',
    description: 'A wand with a pure crystal core. Exceptional mana conductivity.',
    manaType: 'crystal',
    equipmentTypeId: 'crystalWand',
    slot: 'mainHand',
    materials: { manaCrystalDust: 10, crystalShard: 5, elementalCore: 3 },
    manaCost: 600,
    craftTime: 6,
    rarity: 'epic',
    gearTrait: '+40% enchantment capacity',
    bonusEnchantments: [
      { effectId: 'mana_cap_50', stacks: 1, actualCost: 20 },
      { effectId: 'mana_regen_1', stacks: 1, actualCost: 15 },
    ],
  },
  {
    id: 'crystalRing',
    name: 'Crystal Ring',
    description: 'A ring set with a mana crystal. Amplifies enchantment effects.',
    manaType: 'crystal',
    equipmentTypeId: 'silverRing',
    slot: 'accessory1',
    materials: { manaCrystalDust: 5, crystalShard: 3, elementalCore: 1 },
    manaCost: 350,
    craftTime: 3,
    rarity: 'rare',
    gearTrait: '+15% enchantment capacity',
    bonusEnchantments: [
      { effectId: 'mana_cap_50', stacks: 1, actualCost: 20 },
    ],
  },
  {
    id: 'crystalAmulet',
    name: 'Crystal Pendant',
    description: 'An amulet housing a crystal shard. Enhances all enchantments worn.',
    manaType: 'crystal',
    equipmentTypeId: 'silverAmulet',
    slot: 'accessory2',
    materials: { manaCrystalDust: 6, crystalShard: 3, elementalCore: 2 },
    manaCost: 400,
    craftTime: 4,
    rarity: 'rare',
    gearTrait: '+10% all enchantment effects',
    bonusEnchantments: [
      { effectId: 'crystal_cap_10', stacks: 1, actualCost: 30 },
    ],
  },

  // ─── Sand Gear (Earth+Water — lightweight, agile) ────────────────────
  {
    id: 'sandBoots',
    name: 'Sandstrider Boots',
    description: 'Boots infused with sand mana. Light as air, silent as dust.',
    manaType: 'sand',
    equipmentTypeId: 'travelerBoots',
    slot: 'feet',
    materials: { manaCrystalDust: 3, sandShard: 1 },
    manaCost: 120,
    craftTime: 2,
    rarity: 'uncommon',
    gearTrait: '+15% cast speed, +10% evasion',
    bonusEnchantments: [
      { effectId: 'attack_speed_10', stacks: 1, actualCost: 22 },
    ],
  },
  {
    id: 'sandGloves',
    name: 'Sandweave Gloves',
    description: 'Gloves woven from sand mana. Nimble fingers for delicate enchanting.',
    manaType: 'sand',
    equipmentTypeId: 'spellweaveGloves',
    slot: 'hands',
    materials: { manaCrystalDust: 3, sandShard: 2 },
    manaCost: 140,
    craftTime: 2,
    rarity: 'uncommon',
    gearTrait: '+10% cast speed',
    bonusEnchantments: [
      { effectId: 'attack_speed_10', stacks: 1, actualCost: 22 },
    ],
  },
  {
    id: 'sandVest',
    name: 'Sandcloth Vest',
    description: 'A light vest woven from sand mana. Offers minimal protection but maximum mobility.',
    manaType: 'sand',
    equipmentTypeId: 'scholarRobe',
    slot: 'body',
    materials: { manaCrystalDust: 5, sandShard: 2, elementalCore: 1 },
    manaCost: 300,
    craftTime: 4,
    rarity: 'rare',
    gearTrait: '+20% cast speed, +5% evasion',
    bonusEnchantments: [
      { effectId: 'attack_speed_10', stacks: 1, actualCost: 22 },
      { effectId: 'sand_cap_10', stacks: 1, actualCost: 30 },
    ],
  },

  // ─── Branch recipes ───────────────────────────────────────────────────
  ...WIZARD_BRANCH_RECIPES,
  ...PHYSICAL_BRANCH_RECIPES,
];

// ─── Helpers ──────────────────────────────────────────────────────────────────

export function getRecipesByManaType(manaType: string): FabricatorRecipe[] {
  return FABRICATOR_RECIPES.filter(r => r.manaType === manaType);
}

export function getRecipeById(id: string): FabricatorRecipe | undefined {
  return FABRICATOR_RECIPES.find(r => r.id === id);
}

export function canCraftRecipe(
  recipe: FabricatorRecipe,
  materials: Record<string, number>,
  manaAmount: number,
  manaType?: string,
  costReduction?: number,
): { canCraft: boolean; missingMaterials: Record<string, number>; missingMana: number } {
  const reduction = costReduction ?? 0;
  const missingMaterials: Record<string, number> = {};
  let canCraft = true;

  for (const [matId, rawRequired] of Object.entries(recipe.materials)) {
    const required = reduction > 0
      ? Math.max(1, Math.ceil(rawRequired * (1 - reduction / 100)))
      : rawRequired;
    const available = materials[matId] || 0;
    if (available < required) {
      missingMaterials[matId] = required - available;
      canCraft = false;
    }
  }

  const missingMana = Math.max(0, recipe.manaCost - manaAmount);
  if (missingMana > 0) {
    canCraft = false;
  }

  return { canCraft, missingMaterials, missingMana };
}
