import type { ElementDef, GuardianDef, SpellDef, GameAction } from '@/lib/game/types';
import { useManaStore } from '@/lib/game/stores/manaStore';
import { usePrestigeStore } from '@/lib/game/stores/prestigeStore';
import { useUIStore } from '@/lib/game/stores/uiStore';
import { useCombatStore } from '@/lib/game/stores/combatStore';
import { computeEffects } from '@/lib/game/effects/upgrade-effects';
import { hasSpecial, SPECIAL_EFFECTS } from '@/lib/game/effects/special-effects';
import { getBoonBonuses } from '@/lib/game/utils';

// Define a unified store type that combines all stores
export interface UnifiedStore {
  // From gameStore (coordinator)
  day: number;
  hour: number;
  incursionStrength: number;
  containmentWards: number;
  initialized: boolean;
  tick: () => void;
  resetGame: () => void;
  gatherMana: () => void;
  startNewLoop: () => void;

  // From manaStore
  rawMana: number;
  meditateTicks: number;
  totalManaGathered: number;
  elements: Record<string, { current: number; max: number; unlocked: boolean }>;
  setRawMana: (amount: number) => void;
  addRawMana: (amount: number, max: number) => void;
  spendRawMana: (amount: number) => boolean;
  convertMana: (element: string, amount: number) => boolean;
  unlockElement: (element: string, cost: number) => boolean;
  craftComposite: (target: string, recipe: string[]) => boolean;

  // From prestigeStore
  loopCount: number;
  insight: number;
  totalInsight: number;
  loopInsight: number;
  prestigeUpgrades: Record<string, number>;
  memorySlots: number;
  pactSlots: number;
  memories: Array<{ skillId: string; level: number; tier: number; upgrades: string[] }>;
  defeatedGuardians: number[];
  signedPacts: number[];
  pactRitualFloor: number | null;
  pactRitualProgress: number;
  doPrestige: (id: string) => void;
  addMemory: (memory: { skillId: string; level: number; tier: number; upgrades: string[] }) => void;
  removeMemory: (skillId: string) => void;
  clearMemories: () => void;
  startPactRitual: (floor: number, rawMana: number) => boolean;
  cancelPactRitual: () => void;
  removePact: (floor: number) => void;
  defeatGuardian: (floor: number) => void;

  // From combatStore
  currentFloor: number;
  floorHP: number;
  floorMaxHP: number;
  maxFloorReached: number;
  activeSpell: string;
  currentAction: GameAction;
  castProgress: number;
  spells: Record<string, { learned: boolean; level: number; studyProgress?: number }>;
  setAction: (action: GameAction) => void;
  setSpell: (spellId: string) => void;
  learnSpell: (spellId: string) => void;
  advanceFloor: () => void;

  // From uiStore
  log: string[];
  paused: boolean;
  gameOver: boolean;
  victory: boolean;
  addLog: (message: string) => void;
  togglePause: () => void;
  setPaused: (paused: boolean) => void;
  setGameOver: (gameOver: boolean, victory?: boolean) => void;
}

export interface GameContextValue {
  // Unified store for backward compatibility
  store: UnifiedStore;

  // Individual stores for direct access if needed
  manaStore: ReturnType<typeof useManaStore.getState>;
  prestigeStore: ReturnType<typeof usePrestigeStore.getState>;
  uiStore: ReturnType<typeof useUIStore.getState>;
  combatStore: ReturnType<typeof useCombatStore.getState>;

  // Computed effects from upgrades
  upgradeEffects: ReturnType<typeof computeEffects>;

  // Derived stats
  maxMana: number;
  baseRegen: number;
  clickMana: number;
  floorElem: string;
  floorElemDef: ElementDef | undefined;
  isGuardianFloor: boolean;
  currentGuardian: GuardianDef | undefined;
  activeSpellDef: SpellDef | undefined;
  meditationMultiplier: number;
  incursionStrength: number;
  studySpeedMult: number;
  studyCostMult: number;

  // Effective regen calculations
  effectiveRegenWithSpecials: number;
  manaCascadeBonus: number;
  manaWaterfallBonus: number;
  effectiveRegen: number;

  // Has special flags
  hasManaWaterfall: boolean;
  hasFlowSurge: boolean;
  hasManaOverflow: boolean;
  hasEternalFlow: boolean;

  // DPS calculation
  dps: number;

  // Boons
  activeBoons: ReturnType<typeof getBoonBonuses>;

  // Helpers
  canCastSpell: (spellId: string) => boolean;
  hasSpecial: (effects: ReturnType<typeof computeEffects>, specialId: string) => boolean;
  SPECIAL_EFFECTS: typeof SPECIAL_EFFECTS;
}
