import { computeMaxMana, computeClickMana } from '../utils';
import { useUIStore } from './uiStore';
import { usePrestigeStore } from './prestigeStore';
import { useManaStore } from './manaStore';
import { useCombatStore } from './combatStore';
import { computeDisciplineEffects } from '../effects/discipline-effects';
import { useDisciplineStore } from './discipline-slice';

export const createResetGame = (set: (state: any) => void, initialState: any) => () => {
  // Clear all persisted state
  if (typeof window !== 'undefined') {
    localStorage.removeItem('mana-loop-ui-storage');
    localStorage.removeItem('mana-loop-prestige-storage');
    localStorage.removeItem('mana-loop-mana-storage');
    localStorage.removeItem('mana-loop-combat-storage');
    localStorage.removeItem('mana-loop-game-storage');
    localStorage.removeItem('mana-loop-crafting-storage');
    localStorage.removeItem('mana-loop-attunement-storage');
    localStorage.removeItem('mana-loop-discipline-store');
  }

  const startFloor = 1;

  useUIStore.getState().reset();
  usePrestigeStore.getState().resetPrestige();
  useManaStore.getState().resetMana({}, {}, {}, {});
  useCombatStore.getState().resetCombat(startFloor);

  set({
    ...initialState,
    initialized: true,
  });
};

export const createGatherMana = () => () => {
  const prestigeState = usePrestigeStore.getState();
  const disciplineEffects = computeDisciplineEffects(useDisciplineStore.getState() as any);

  // Compute click mana with discipline bonuses (mana-channeling → clickManaMultiplier)
  const cm = computeClickMana(
    { skills: {} },
    disciplineEffects,
  );

  const max = computeMaxMana(
    {
      skills: {},
      prestigeUpgrades: prestigeState.prestigeUpgrades,
      skillUpgrades: {},
      skillTiers: {}
    },
    undefined,
    disciplineEffects,
  );

  useManaStore.getState().gatherMana(cm, max);
};
