import { useGameStore } from '../stores/gameStore';
import { useManaStore, makeInitialElements } from '../stores/manaStore';
import { useCombatStore, makeInitialSpells } from '../stores/combatStore';
import { usePrestigeStore } from '../stores/prestigeStore';
import { useUIStore } from '../stores/uiStore';
import { useDisciplineStore } from '../stores/discipline-slice';
import { useAttunementStore } from '../stores/attunementStore';
import { useCraftingStore } from '../stores/craftingStore';
import { getFloorMaxHP } from '../utils';

export function resetAllStores() {
  useUIStore.setState({
    paused: false,
    gameOver: false,
    victory: false,
    logs: [],
  });

  useGameStore.setState({
    day: 1,
    hour: 0,
    incursionStrength: 0,
    containmentWards: 0,
    initialized: true,
  });

  useManaStore.setState({
    rawMana: 100,
    meditateTicks: 0,
    totalManaGathered: 0,
    elements: makeInitialElements(50, {}),
  });

  useCombatStore.setState({
    currentFloor: 1,
    floorHP: getFloorMaxHP(1),
    floorMaxHP: getFloorMaxHP(1),
    maxFloorReached: 1,
    activeSpell: 'manaBolt',
    currentAction: 'meditate',
    castProgress: 0,
    spireMode: false,
    currentRoom: { roomType: 'combat', enemies: [] },
    clearedFloors: {},
    climbDirection: null,
    isDescending: false,
    startFloor: 1,
    exitFloor: 1,
    currentRoomIndex: 0,
    roomsPerFloor: 5,
    descentPeak: null,
    roomResetState: {},
    clearedRooms: {},
    isDescentComplete: false,
    golemancy: { activeGolems: [], lastSummonFloor: 0, golemDesigns: {}, golemLoadout: [] },
    equipmentSpellStates: [],
    weaponCastProgress: {},
    comboHitCount: 0,
    floorHitCount: 0,
    spells: makeInitialSpells(),
    activityLog: [],
    achievements: { unlocked: [], progress: {} },
    totalSpellsCast: 0,
    totalDamageDealt: 0,
    totalCraftsCompleted: 0,
  });

  usePrestigeStore.setState({
    loopCount: 0,
    insight: 0,
    totalInsight: 0,
    loopInsight: 0,
    prestigeUpgrades: {},
    pactSlots: 1,
    defeatedGuardians: [],
    signedPacts: [],
    signedPactDetails: {},
    pactRitualFloor: null,
    pactRitualProgress: 0,
  });

  useDisciplineStore.setState({
    disciplines: {},
    activeIds: [],
    concurrentLimit: 1,
    totalXP: 0,
    processedPerks: [],
  });

  useAttunementStore.setState({
    attunements: {},
  });

  useCraftingStore.setState({
    designProgress: null,
    designProgress2: null,
    preparationProgress: null,
    applicationProgress: null,
    equipmentCraftingProgress: null,
    completedEnchantmentDesigns: [],
    unlockedEffects: [],
    equippedInstances: {},
    equipmentInstances: {},
    lootInventory: {
      materials: {},
      blueprints: [],
    },
    enchantmentSelection: {
      selectedEquipmentType: null,
      selectedEffects: [],
      designName: '',
      selectedDesign: null,
      selectedEquipmentInstance: null,
    },
    lastError: null,
  });
}

export function tickN(n: number) {
  for (let i = 0; i < n; i++) {
    useGameStore.getState().tick();
  }
}
