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

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

export interface DesignActionsParams {
  skills: Record<string, number>;
}

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

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

  const efficiencyBonus = getEnchantingEfficiencyBonus();
  const totalCapacityCost = CraftingDesign.calculateDesignCapacityCost(effects, efficiencyBonus);

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

  const computedEffects = computeEffects();
  const hasEnchantMastery = hasSpecial(computedEffects, SPECIAL_EFFECTS.ENCHANT_MASTERY);

  let updates: Partial<CraftingState> = {};

  if (!state.designProgress) {
    updates = {
      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: () => CraftingState,
  set: (fn: (state: CraftingState) => Partial<CraftingState>) => void
) {
  const state = get();
  if (state.designProgress2 && !state.designProgress) {
    set(() => ({ designProgress2: null }));
  } else {
    set(() => ({
      designProgress: null,
    }));
  }
}

export function saveDesign(
  design: EnchantmentDesign,
  get: () => CraftingState,
  set: (fn: (state: CraftingState) => Partial<CraftingState>) => void
) {
  const state = get();
  if (state.designProgress2 && state.designProgress2.designId === design.id) {
    set((s) => ({
      completedEnchantmentDesigns: [...s.completedEnchantmentDesigns, design],
      designProgress2: null,
    }));
  } else {
    set((s) => ({
      completedEnchantmentDesigns: [...s.completedEnchantmentDesigns, design],
      designProgress: null,
    }));
  }
}

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