// ─── Golem Component Types ──────────────────────────────────────────────
// Component-based construction system: Core + Frame + Mind Circuit + Enchantments.
// Replaces the legacy predefined GolemDef system.

import type { SpellCost } from '../../types';

// ─── Mana Cost Helpers ───────────────────────────────────────────────────

export function elemCost(element: string, amount: number): SpellCost {
  return { type: 'element', element, amount };
}

export function rawCost(amount: number): SpellCost {
  return { type: 'raw', amount };
}

export interface GolemManaCost {
  type: 'raw' | 'element';
  element?: string;
  amount: number;
}

// ─── Unlock Requirements ─────────────────────────────────────────────────

export interface GolemUnlockRequirement {
  type: 'attunement_level' | 'mana_unlocked' | 'dual_attunement' | 'guardian_pact';
  attunement?: string;
  level?: number;
  manaType?: string;
  attunements?: string[];
  levels?: number[];
}

// ─── Core Definition ─────────────────────────────────────────────────────

export type CoreId = 'basic' | 'intermediate' | 'advanced' | 'guardian';

export interface CoreDefinition {
  id: CoreId;
  tier: 1 | 2 | 3 | 4;
  name: string;
  description: string;
  /** Mana types available (basic = [earth], guardian = guardian-specific) */
  manaTypes: string[];
  manaCapacity: number;
  manaRegen: number;
  maxRoomDuration: number;
  summonCost: GolemManaCost[];
  /** Primary mana type for upkeep calculation */
  primaryManaType: string;
  tierMultiplier: number; // For enchantment capacity: 1.0 / 1.5 / 2.0 / 3.0
  unlockRequirement: GolemUnlockRequirement;
}

// ─── Frame Definition ────────────────────────────────────────────────────

export type FrameId = 'earth' | 'sand' | 'frost' | 'crystal' | 'steel' | 'shadowglass' | 'crystalSteelHybrid';

export type FrameSpecial = 'none' | 'aoe' | 'slow' | 'guardianConstruct';

export interface FrameDefinition {
  id: FrameId;
  name: string;
  description: string;
  baseDamage: number;
  attackSpeed: number;       // Attacks per in-game hour
  armorPierce: number;       // 0–1 fraction of enemy armor bypassed
  magicAffinity: number;     // 0.0–1.0+, spell damage efficiency
  aoeTargets: number;        // 1 = single target, >1 = AoE
  /** Element for elemental matchup (derived from unlock mana type) */
  element?: string;
  specialEffect: FrameSpecial;
  summonCost: GolemManaCost[];
  unlockRequirement: GolemUnlockRequirement;
}

// ─── Mind Circuit Definition ────────────────────────────────────────────

export type MindCircuitId = 'simple' | 'intermediate' | 'advanced' | 'guardian';

export type CircuitBehavior = 'basicOnly' | 'castSpell1' | 'alternate2' | 'cycleAll';

export interface MindCircuitDefinition {
  id: MindCircuitId;
  name: string;
  description: string;
  spellSlots: number;
  behavior: CircuitBehavior;
  summonCost: GolemManaCost[];
  unlockRequirement: GolemUnlockRequirement;
}

// ─── Golem Enchantment Definition ───────────────────────────────────────

export interface GolemEnchantmentDefinition {
  id: string;
  name: string;
  description: string;
  effect: string;
  capacityCost: number;
  summonCost: GolemManaCost[];
  unlockRequirement: GolemUnlockRequirement;
}

// ─── Golem Design (Player-Created) ──────────────────────────────────────

export interface GolemDesign {
  id: string;                    // Player-assigned or auto-generated
  name: string;                  // Player-defined name
  core: CoreDefinition;
  frame: FrameDefinition;
  mindCircuit: MindCircuitDefinition;
  enchantments: GolemEnchantmentDefinition[];  // Optional, 0-N
  /** Player-selected mana types for cores that support choice */
  selectedManaTypes: string[];
  /** Player-selected spell IDs for mind circuits with spell slots */
  selectedSpells: string[];
  /**
   * Runtime override for Guardian Core summon cost.
   * When core is 'guardian', this replaces the placeholder earth cost
   * with guardian-specific mana costs derived from the signed pact.
   * Spec §4.3: Guardian Core uses guardian-specific mana types.
   */
  guardianSummonCost?: GolemManaCost[];
  /**
   * Runtime override for Guardian Core primary mana type.
   * Spec §4.3: Provides all mana types granted by the chosen Guardian.
   */
  guardianPrimaryManaType?: string;
}

// ─── Serialized Design (for storage/transmission) ──────────────────────

export interface SerializedDesign {
  id: string;
  name: string;
  coreId: string;
  frameId: string;
  mindCircuitId: string;
  enchantmentIds: string[];
  selectedManaTypes: string[];
  selectedSpells: string[];
  guardianSummonCost?: GolemManaCost[];
  guardianPrimaryManaType?: string;
}

// ─── Computed Design Stats (derived from components) ────────────────────

export interface ComputedGolemStats {
  maxRoomDuration: number;
  totalSummonCost: GolemManaCost[];
  upkeepCostPerHour: GolemManaCost[];
  manaCapacity: number;
  manaRegen: number;
  baseDamage: number;
  attackSpeed: number;
  armorPierce: number;
  magicAffinity: number;
  aoeTargets: number;
  spellSlots: number;
  availableManaTypes: string[];
  enchantmentCapacity: number;
  specialEffect: FrameSpecial;
}

