// ─── Achievement Definitions ───────────────────────────────────────────────────

import type { AchievementDef } from '../types';

export const ACHIEVEMENTS: Record<string, AchievementDef> = {
  // ─── Combat Achievements ───
  firstBlood: {
    id: 'firstBlood',
    name: 'First Blood',
    desc: 'Clear your first floor',
    category: 'combat',
    requirement: { type: 'floor', value: 2 },
    reward: { insight: 10 },
  },
  floorClimber: {
    id: 'floorClimber',
    name: 'Floor Climber',
    desc: 'Reach floor 10',
    category: 'combat',
    requirement: { type: 'floor', value: 10 },
    reward: { insight: 25, manaBonus: 10 },
  },
  spireAssault: {
    id: 'spireAssault',
    name: 'Spire Assault',
    desc: 'Reach floor 25',
    category: 'combat',
    requirement: { type: 'floor', value: 25 },
    reward: { insight: 50, damageBonus: 0.05 },
  },
  towerConqueror: {
    id: 'towerConqueror',
    name: 'Tower Conqueror',
    desc: 'Reach floor 50',
    category: 'combat',
    requirement: { type: 'floor', value: 50 },
    reward: { insight: 100, manaBonus: 50, damageBonus: 0.1 },
  },
  spireMaster: {
    id: 'spireMaster',
    name: 'Spire Master',
    desc: 'Reach floor 75',
    category: 'combat',
    requirement: { type: 'floor', value: 75 },
    reward: { insight: 200, damageBonus: 0.15, title: 'Spire Master' },
  },
  apexReached: {
    id: 'apexReached',
    name: 'Apex Reached',
    desc: 'Reach floor 100',
    category: 'combat',
    requirement: { type: 'floor', value: 100 },
    reward: { insight: 500, manaBonus: 200, damageBonus: 0.25, title: 'Apex Climber' },
  },

  // ─── Damage Achievements ───
  hundredDamage: {
    id: 'hundredDamage',
    name: 'Heavy Hitter',
    desc: 'Deal 100 damage in a single hit',
    category: 'combat',
    requirement: { type: 'damage', value: 100 },
    reward: { insight: 20 },
  },
  thousandDamage: {
    id: 'thousandDamage',
    name: 'Devastating Blow',
    desc: 'Deal 1,000 damage in a single hit',
    category: 'combat',
    requirement: { type: 'damage', value: 1000 },
    reward: { insight: 75, damageBonus: 0.03 },
  },
  tenThousandDamage: {
    id: 'tenThousandDamage',
    name: 'Apocalypse Now',
    desc: 'Deal 10,000 damage in a single hit',
    category: 'combat',
    requirement: { type: 'damage', value: 10000 },
    reward: { insight: 200, damageBonus: 0.05, title: 'Apocalypse Bringer' },
  },

  // ─── Pact Achievements ───
  pactSeeker: {
    id: 'pactSeeker',
    name: 'Pact Seeker',
    desc: 'Sign your first guardian pact',
    category: 'progression',
    requirement: { type: 'pact', value: 1 },
    reward: { insight: 30 },
  },
  pactCollector: {
    id: 'pactCollector',
    name: 'Pact Collector',
    desc: 'Sign 5 guardian pacts',
    category: 'progression',
    requirement: { type: 'pact', value: 5 },
    reward: { insight: 100, manaBonus: 25 },
  },
  pactMaster: {
    id: 'pactMaster',
    name: 'Pact Master',
    desc: 'Sign all guardian pacts',
    category: 'progression',
    requirement: { type: 'pact', value: 9 },
    reward: { insight: 500, damageBonus: 0.2, title: 'Pact Master' },
  },

  // ─── Magic Achievements ───
  spellCaster: {
    id: 'spellCaster',
    name: 'Spell Caster',
    desc: 'Cast 100 spells',
    category: 'magic',
    requirement: { type: 'spells', value: 100 },
    reward: { insight: 25 },
  },
  spellWeaver: {
    id: 'spellWeaver',
    name: 'Spell Weaver',
    desc: 'Cast 1,000 spells',
    category: 'magic',
    requirement: { type: 'spells', value: 1000 },
    reward: { insight: 75, regenBonus: 0.5 },
  },
  spellStorm: {
    id: 'spellStorm',
    name: 'Spell Storm',
    desc: 'Cast 10,000 spells',
    category: 'magic',
    requirement: { type: 'spells', value: 10000 },
    reward: { insight: 200, regenBonus: 1, title: 'Storm Caller' },
  },

  // ─── Mana Achievements ───
  manaPool: {
    id: 'manaPool',
    name: 'Mana Pool',
    desc: 'Accumulate 1,000 total mana',
    category: 'magic',
    requirement: { type: 'mana', value: 1000 },
    reward: { insight: 20 },
  },
  manaLake: {
    id: 'manaLake',
    name: 'Mana Lake',
    desc: 'Accumulate 100,000 total mana',
    category: 'magic',
    requirement: { type: 'mana', value: 100000 },
    reward: { insight: 100, manaBonus: 50 },
  },
  manaOcean: {
    id: 'manaOcean',
    name: 'Mana Ocean',
    desc: 'Accumulate 10,000,000 total mana',
    category: 'magic',
    requirement: { type: 'mana', value: 10000000 },
    reward: { insight: 500, manaBonus: 200, title: 'Mana Ocean' },
  },

  // ─── Crafting Achievements ───
  enchanter: {
    id: 'enchanter',
    name: 'Enchanter',
    desc: 'Complete your first enchantment',
    category: 'crafting',
    requirement: { type: 'craft', value: 1 },
    reward: { insight: 30 },
  },
  masterEnchanter: {
    id: 'masterEnchanter',
    name: 'Master Enchanter',
    desc: 'Complete 10 enchantments',
    category: 'crafting',
    requirement: { type: 'craft', value: 10 },
    reward: { insight: 100, unlockEffect: 'efficiencyBoost' },
  },
  legendaryEnchanter: {
    id: 'legendaryEnchanter',
    name: 'Legendary Enchanter',
    desc: 'Complete 50 enchantments',
    category: 'crafting',
    requirement: { type: 'craft', value: 50 },
    reward: { insight: 300, title: 'Legendary Enchanter' },
  },

  // ─── Special Achievements ───
  speedRunner: {
    id: 'speedRunner',
    name: 'Speed Runner',
    desc: 'Reach floor 50 in under 5 days',
    category: 'special',
    requirement: { type: 'time', value: 5, subType: 'floor50' },
    reward: { insight: 200, title: 'Speed Demon' },
    hidden: true,
  },
  perfectionist: {
    id: 'perfectionist',
    name: 'Perfectionist',
    desc: 'Reach floor 100 without any guardian pacts',
    category: 'special',
    requirement: { type: 'floor', value: 100, subType: 'noPacts' },
    reward: { insight: 1000, title: 'Perfect Climber' },
    hidden: true,
  },
  survivor: {
    id: 'survivor',
    name: 'Survivor',
    desc: 'Complete a loop during full incursion (day 30+)',
    category: 'special',
    requirement: { type: 'time', value: 30 },
    reward: { insight: 300, manaBonus: 100, title: 'Survivor' },
  },
};

// Category colors for UI
export const ACHIEVEMENT_CATEGORY_COLORS: Record<string, string> = {
  combat: '#EF4444',     // Red
  progression: '#F59E0B', // Amber
  crafting: '#8B5CF6',   // Purple
  magic: '#3B82F6',      // Blue
  special: '#EC4899',    // Pink
};

// Get achievements by category
export function getAchievementsByCategory(): Record<string, AchievementDef[]> {
  const result: Record<string, AchievementDef[]> = {};
  
  for (const achievement of Object.values(ACHIEVEMENTS)) {
    if (!result[achievement.category]) {
      result[achievement.category] = [];
    }
    result[achievement.category].push(achievement);
  }
  
  return result;
}

// Check if an achievement should be revealed
export function isAchievementRevealed(
  achievement: AchievementDef,
  progress: number
): boolean {
  if (!achievement.hidden) return true;
  // Reveal hidden achievements when at 50% progress
  return progress >= achievement.requirement.value * 0.5;
}
