// ─── Enchantment Design Actions ────────────────────────────────────────────

import type { GameState, EnchantmentDesign, DesignEffect } from '../types';
import * as CraftingUtils from '../crafting-utils';
import * as CraftingDesign from '../crafting-design';
import { computeEffects } from '../effects/upgrade-effects';
import { hasSpecial, SPECIAL_EFFECTS } from '../effects/special-effects';

export function startDesigningEnchantment(
  name: string,
  equipmentTypeId: string,
  effects: DesignEffect[],
  get: () => GameState,
  set: (fn: (state: GameState) => Partial<GameState>) => void
): boolean {
  const state = get();
  const enchantingLevel = state.skills.enchanting || 0;
  const validation = CraftingDesign.validateDesignEffects(
    effects,
    equipmentTypeId,
    enchantingLevel
  );
  if (!validation.valid) return false;

  const equipType = CraftingUtils.getEquipmentType(equipmentTypeId);
  if (!equipType) return false;

  const efficiencyBonus = ((state.skillUpgrades || {})['efficientEnchant'] || [])?.length * 0.05 || 0;
  const totalCapacityCost = CraftingDesign.calculateDesignCapacityCost(effects, efficiencyBonus);

  if (totalCapacityCost > equipType.baseCapacity) {
    return false;
  }

  const computedEffects = computeEffects(state.skillUpgrades || {}, state.skillTiers || {});
  const hasEnchantMastery = hasSpecial(computedEffects, SPECIAL_EFFECTS.ENCHANT_MASTERY);

  let updates: any = {};

  if (!state.designProgress) {
    updates = {
      currentAction: 'design' as const,
      designProgress: {
        designId: CraftingUtils.generateDesignId(),
        progress: 0,
        required: CraftingDesign.calculateDesignTime(effects),
        name,
        equipmentType: equipmentTypeId,
        effects,
      },
    };
  } else if (hasEnchantMastery && !state.designProgress2) {
    updates = {
      designProgress2: {
        designId: CraftingUtils.generateDesignId(),
        progress: 0,
        required: CraftingDesign.calculateDesignTime(effects),
        name,
        equipmentType: equipmentTypeId,
        effects,
      },
    };
  } else {
    return false;
  }

  set(() => updates);
  return true;
}

export function cancelDesign(
  get: () => GameState,
  set: (fn: (state: GameState) => Partial<GameState>) => void
) {
  const state = get();
  if (state.designProgress2 && !state.designProgress) {
    set(() => ({ designProgress2: null }));
  } else {
    set(() => ({
      currentAction: 'meditate' as const,
      designProgress: null,
    }));
  }
}

export function saveDesign(
  design: EnchantmentDesign,
  get: () => GameState,
  set: (fn: (state: GameState) => Partial<GameState>) => void
) {
  const state = get();
  if (state.designProgress2 && state.designProgress2.designId === design.id) {
    set((state) => ({
      enchantmentDesigns: [...state.enchantmentDesigns, design],
      designProgress2: null,
    }));
  } else {
    set((state) => ({
      enchantmentDesigns: [...state.enchantmentDesigns, design],
      designProgress: null,
      currentAction: 'meditate' as const,
    }));
  }
}

export function deleteDesign(
  designId: string,
  set: (fn: (state: GameState) => Partial<GameState>) => void
) {
  set((state) => ({
    enchantmentDesigns: state.enchantmentDesigns.filter(d => d.id !== designId),
  }));
}
