// ─── Crafting Store ─────────────────────────────────────────────────────
import { create } from 'zustand';
import { persist } from 'zustand/middleware';
import type { EquipmentSlot } from '../types/equipmentSlot';
import type { CraftingStore, CraftingState } from './craftingStore.types';

import { useManaStore } from './manaStore';
import { useCombatStore } from './combatStore';

import * as ApplicationActions from '../crafting-actions/application-actions';
import * as PreparationActions from '../crafting-actions/preparation-actions';
import { equipItem as equipItemAction, unequipItem as unequipItemAction } from '../crafting-actions/equipment-actions';
import { startDesigningEnchantment as startDesignAction, cancelDesign as cancelDesignAction, saveDesign as saveDesignAction, deleteDesign as deleteDesignAction } from '../crafting-actions/design-actions';
import { useAttunementStore } from './attunementStore';
import { ErrorCode } from '../utils/result';
import { createSafeStorage } from '../utils/safe-persist';
import { createDefaultCraftingState } from './crafting-initial-state';
import { craftMaterial as craftMaterialAction } from '../crafting-actions/crafting-material-actions';
import { processEquipmentCraftingTick } from './crafting-equipment-tick';
import { startCraftingEquipment, cancelEquipmentCrafting, startFabricatorCrafting } from './pipelines/equipment-crafting';

export const useCraftingStore = create<CraftingStore>()(
  persist(
    (set, get) => {
      const defaultState = createDefaultCraftingState();
      return {
        ...defaultState,

        // Actions
        setDesignProgress: (progress) => set({ designProgress: progress }),
        setDesignProgress2: (progress) => set({ designProgress2: progress }),
        setPreparationProgress: (progress) => set({ preparationProgress: progress }),
        setApplicationProgress: (progress) => set({ applicationProgress: progress }),
        setEquipmentCraftingProgress: (progress) => set({ equipmentCraftingProgress: progress }),

        // Enchantment design actions — delegated to design-actions.ts
        startDesigningEnchantment: (name, equipmentTypeId, effects) => {
          const attunements = useAttunementStore.getState();
          const enchanterLevel = attunements.attunements?.enchanter?.level ?? 0;
          const success = startDesignAction(
            name,
            equipmentTypeId,
            effects,
            { skills: { enchanting: enchanterLevel } },
            get,
            set as unknown as (fn: (state: CraftingState) => Partial<CraftingState>) => void
          );
          if (success) {
            useCombatStore.setState({ currentAction: 'design' });
          }
          return success;
        },

        cancelDesign: (slot?: 1 | 2) => {
          if (slot === 2) {
            const state = get();
            if (state.designProgress2) {
              set({ designProgress2: null });
            }
          } else if (slot === 1) {
            const state = get();
            if (state.designProgress) {
              set({ designProgress: null });
              useCombatStore.setState({ currentAction: 'meditate' });
            }
          } else {
            cancelDesignAction(get, set as unknown as (fn: (state: CraftingState) => Partial<CraftingState>) => void);
            const state = get();
            // If both slots are now null, reset to meditate
            if (!state.designProgress && !state.designProgress2) {
              useCombatStore.setState({ currentAction: 'meditate' });
            }
          }
        },

        deleteDesign: (designId) => {
          deleteDesignAction(designId, set as unknown as (fn: (state: CraftingState) => Partial<CraftingState>) => void);
        },

        saveDesign: (design) => {
          saveDesignAction(design, get, set as unknown as (fn: (state: CraftingState) => Partial<CraftingState>) => void);
          useCombatStore.setState({ currentAction: 'meditate' });
        },

        // Enchantment application actions
        startApplying: (equipmentInstanceId, designId) => {
          const currentAction = useCombatStore.getState().currentAction;
          const result = ApplicationActions.startApplying(
            equipmentInstanceId,
            designId,
            get,
            set as unknown as (partial: Partial<CraftingState>) => void,
            currentAction
          );
          if (result) {
            useCombatStore.setState({ currentAction: 'enchant' });
          }
          return result;
        },

        pauseApplication: () => {
          ApplicationActions.pauseApplication(get, set as unknown as (partial: Partial<CraftingState>) => void);
        },

        resumeApplication: () => {
          ApplicationActions.resumeApplication(get, set as unknown as (partial: Partial<CraftingState>) => void);
        },

        cancelApplication: () => {
          ApplicationActions.cancelApplication(get, set as unknown as (partial: Partial<CraftingState>) => void);
          useCombatStore.setState({ currentAction: 'meditate' });
        },

        // Preparation actions
        startPreparing: (equipmentInstanceId) => {
          const rawMana = useManaStore.getState().rawMana;
          const result = PreparationActions.startPreparing(
            equipmentInstanceId,
            rawMana,
            get,
            set
          );
          if (result) {
            useCombatStore.setState({ currentAction: 'prepare' });
            set({ lastError: null });
          } else {
            const state = get();
            const instance = state.equipmentInstances[equipmentInstanceId];
            let message = 'Cannot start preparation';
            if (!instance) {
              message = `Equipment instance not found: ${equipmentInstanceId}`;
            } else if (instance.tags?.includes('Ready for Enchantment')) {
              message = 'Equipment is already prepared';
            } else {
              message = 'Insufficient mana for preparation';
            }
            set({ lastError: { code: ErrorCode.INVALID_INPUT, message, timestamp: Date.now() } });
          }
          return result;
        },

        cancelPreparation: () => {
          PreparationActions.cancelPreparation(get, set);
          useCombatStore.setState({ currentAction: 'meditate' });
        },

        startCraftingEquipment: (blueprintId: string) => startCraftingEquipment(blueprintId, get, set),

        cancelEquipmentCrafting: () => cancelEquipmentCrafting(get, set),

        // Fabricator crafting — uses elemental mana instead of raw mana
        startFabricatorCrafting: (recipeId: string) => startFabricatorCrafting(recipeId, get, set),

        // Material crafting — instant crafting of materials
        craftMaterial: (recipeId: string) => {
          const state = get();
          const result = craftMaterialAction(recipeId, state.lootInventory.materials);
          if (!result.success) return false;
          if (result.newMaterials) {
            set((s) => ({ lootInventory: { ...s.lootInventory, materials: result.newMaterials! } }));
          }
          return true;
        },

        // Enchantment selection actions
        setSelectedEquipmentType: (type) => {
          set((s) => ({ enchantmentSelection: { ...s.enchantmentSelection, selectedEquipmentType: type }}));
        },
        setSelectedEffects: (effects) => {
          set((s) => ({ enchantmentSelection: { ...s.enchantmentSelection, selectedEffects: effects } }));
        },
        setDesignName: (name) => {
          set((s) => ({ enchantmentSelection: { ...s.enchantmentSelection, designName: name } }));
        },
        setSelectedDesign: (id) => {
          set((s) => ({ enchantmentSelection: { ...s.enchantmentSelection, selectedDesign: id } }));
        },
        setSelectedEquipmentInstance: (id) => {
          set((s) => ({ enchantmentSelection: { ...s.enchantmentSelection, selectedEquipmentInstance: id } }));
        },
        resetEnchantmentSelection: () => {
          set((s) => ({
            enchantmentSelection: {
              selectedEquipmentType: null,
              selectedEffects: [],
              designName: '',
              selectedDesign: null,
              selectedEquipmentInstance: null,
            },
          }));
        },

        // Loot inventory actions
        deleteMaterial: (materialId: string, amount: number) => {
          set((state) => {
            const newMaterials = { ...state.lootInventory.materials };
            const currentAmount = newMaterials[materialId] || 0;
            const newAmount = Math.max(0, currentAmount - amount);

            if (newAmount <= 0) {
              delete newMaterials[materialId];
            } else {
              newMaterials[materialId] = newAmount;
            }

            return {
              lootInventory: {
                ...state.lootInventory,
                materials: newMaterials,
              },
            };
          });
        },

        deleteEquipmentInstance: (instanceId: string) => {
          set((state) => {
            let newEquipped = { ...state.equippedInstances };
            for (const [slot, id] of Object.entries(newEquipped)) {
              if (id === instanceId) {
                newEquipped[slot as EquipmentSlot] = null;
              }
            }

            const newInstances = { ...state.equipmentInstances };
            delete newInstances[instanceId];

            return {
              equippedInstances: newEquipped,
              equipmentInstances: newInstances,
            };
          });
        },

        equipItem: (instanceId: string, slot: EquipmentSlot) => {
          return equipItemAction(instanceId, slot, get, set);
        },

        unequipItem: (slot: EquipmentSlot) => {
          unequipItemAction(slot, set);
        },

        clearLastError: () => set({ lastError: null }),

        setActiveCraftingSubTab: (tab) => set({ activeCraftingSubTab: tab }),

        unlockEffects: (effectIds: string[]) => {
          set((state) => {
            const existing = new Set(state.unlockedEffects);
            let changed = false;
            for (const id of effectIds) {
              if (!existing.has(id)) {
                existing.add(id);
                changed = true;
              }
            }
            if (!changed) return state;
            return { unlockedEffects: Array.from(existing) };
          });
        },

        unlockRecipes: (recipeIds: string[]) => {
          set((state) => {
            const existing = new Set(state.unlockedRecipes);
            let changed = false;
            for (const id of recipeIds) {
              if (!existing.has(id)) {
                existing.add(id);
                changed = true;
              }
            }
            if (!changed) return state;
            return { unlockedRecipes: Array.from(existing) };
          });
        },

        processEquipmentCraftingTick: (): { completed: boolean; logMessage?: string } => {
          const state = get();
          return processEquipmentCraftingTick(state, set as unknown as (partial: Partial<CraftingState>) => void);
        },

        resetCrafting: () => {
          set(createDefaultCraftingState());
        },
      };
    },
    {
      storage: createSafeStorage(),
      name: 'mana-loop-crafting',
      version: 1,
      partialize: (state) => ({
        designProgress: state.designProgress,
        designProgress2: state.designProgress2,
        preparationProgress: state.preparationProgress,
        applicationProgress: state.applicationProgress,
        equipmentCraftingProgress: state.equipmentCraftingProgress,
        completedEnchantmentDesigns: state.completedEnchantmentDesigns,
        unlockedEffects: state.unlockedEffects,
        unlockedRecipes: state.unlockedRecipes,
        equipmentInstances: state.equipmentInstances,
        equippedInstances: state.equippedInstances,
        lootInventory: state.lootInventory,
        enchantmentSelection: state.enchantmentSelection,
        lastError: state.lastError,
        activeCraftingSubTab: state.activeCraftingSubTab,
      }),
    }
  )
);
