// ─── Mana & Regen Utilities ──────────────────────────────────────────────────

import type { GameState } from '../types';
import type { ComputedEffects } from '../effects/upgrade-effects.types';
import { HOURS_PER_TICK } from '../constants';
import { getTotalAttunementRegen, getTotalAttunementConversionDrain } from '../data/attunements';

export function computeMaxMana(
  state: Pick<GameState, 'skills' | 'prestigeUpgrades' | 'skillUpgrades' | 'skillTiers'>,
  effects?: ComputedEffects
): number {
  const pu = state.prestigeUpgrades;
  const base = 
    100 +
    ((state.skills || {}).manaWell || 0) * 100 +
    ((pu || {}).manaWell || 0) * 500;
  
  // Apply upgrade effects if provided
  if (effects) {
    return Math.floor((base + effects.maxManaBonus) * effects.maxManaMultiplier);
  }
  return base;
}

// computeElementMax is now in ../store.ts with support for unlockedManaTypeUpgrades
// This file no longer exports computeElementMax to avoid duplicate export issues
// Import computeElementMax from '../store' instead

export function computeRegen(
  state: Pick<GameState, 'skills' | 'prestigeUpgrades' | 'skillUpgrades' | 'skillTiers' | 'attunements'>,
  effects?: ComputedEffects
): number {
  const pu = state.prestigeUpgrades;
  const temporalBonus = 1 + (pu.temporalEcho || 0) * 0.1;
  const base =
    2 +
    ((state.skills || {}).manaFlow || 0) * 1 +
    ((state.skills || {}).manaSpring || 0) * 2 +
    ((pu || {}).manaFlow || 0) * 0.5;
  
  let regen = base * temporalBonus;
  
  // Add attunement raw mana regen
  const attunementRegen = getTotalAttunementRegen(state.attunements || {});
  regen += attunementRegen;
  
  // Apply upgrade effects if provided
  if (effects) {
    regen = (regen + effects.regenBonus + effects.permanentRegenBonus) * effects.regenMultiplier;
  }
  
  return regen;
}

// Compute the effective regen (raw regen minus conversion drains) for display purposes
export function computeEffectiveRegenForDisplay(
  state: Pick<GameState, 'skills' | 'prestigeUpgrades' | 'skillUpgrades' | 'skillTiers' | 'attunements'>,
  effects?: ComputedEffects
): { rawRegen: number; conversionDrain: number; effectiveRegen: number } {
  // Get the full raw regen (without conversion drain)
  const rawRegen = computeRegen(state, effects);
  
  // Calculate conversion drain
  const conversionDrain = getTotalAttunementConversionDrain(state.attunements || {});
  
  // Effective regen is what actually increases raw mana
  const effectiveRegen = Math.max(0, rawRegen - conversionDrain);
  
  return { rawRegen, conversionDrain, effectiveRegen };
}

/**
 * Compute regen with dynamic special effects (needs current mana, max mana, incursion)
 */
export function computeEffectiveRegen(
  state: Pick<GameState, 'skills' | 'prestigeUpgrades' | 'rawMana' | 'incursionStrength' | 'skillUpgrades' | 'skillTiers' | 'attunements'>,
  effects?: ComputedEffects
): number {
  // Base regen from existing function
  let regen = computeRegen(state, effects);
  
  const incursionStrength = state.incursionStrength || 0;
  
  // Apply incursion penalty
  regen *= (1 - incursionStrength);
  
  return regen;
}

export function computeClickMana(state: Pick<GameState, 'skills'>): number {
  return (
    1 +
    ((state.skills || {}).manaTap || 0) * 1 +
    ((state.skills || {}).manaSurge || 0) * 3
  );
}

// Meditation bonus now affects regen rate directly
export function getMeditationBonus(meditateTicks: number, skills: Record<string, number>, meditationEfficiency: number = 1): number {
  const hasMeditation = skills.meditation === 1;
  const hasDeepTrance = skills.deepTrance === 1;
  const hasVoidMeditation = skills.voidMeditation === 1;
  
  const hours = meditateTicks * HOURS_PER_TICK;
  
  // Base meditation: ramps up over 4 hours to 1.5x
  let bonus = 1 + Math.min(hours / 4, 0.5);
  
  // With Meditation Focus: up to 2.5x after 4 hours
  if (hasMeditation && hours >= 4) {
    bonus = 2.5;
  }
  
  // With Deep Trance: up to 3.0x after 6 hours
  if (hasDeepTrance && hours >= 6) {
    bonus = 3.0;
  }
  
  // With Void Meditation: up to 5.0x after 8 hours
  if (hasVoidMeditation && hours >= 8) {
    bonus = 5.0;
  }
  
  // Apply meditation efficiency from upgrades (Deep Wellspring, etc.)
  bonus *= meditationEfficiency;
  
  return bonus;
}
