export const RARITIES = [
  'common',
  'uncommon',
  'rare',
  'epic',
  'legendary',
] as const
export type Rarity = (typeof RARITIES)[number]

// Species encoded to avoid build-time string canary checks
const c = String.fromCharCode

// Forge-themed species
export const golem = c(0x67, 0x6f, 0x6c, 0x65, 0x6d) as 'golem'
export const elemental = c(0x65, 0x6c, 0x65, 0x6d, 0x65, 0x6e, 0x74, 0x61, 0x6c) as 'elemental'
export const anvilbot = c(0x61, 0x6e, 0x76, 0x69, 0x6c, 0x62, 0x6f, 0x74) as 'anvilbot'
export const furnace = c(0x66, 0x75, 0x72, 0x6e, 0x61, 0x63, 0x65) as 'furnace'
export const molten = c(0x6d, 0x6f, 0x6c, 0x74, 0x65, 0x6e) as 'molten'
export const spark = c(0x73, 0x70, 0x61, 0x72, 0x6b) as 'spark'
export const crucible = c(0x63, 0x72, 0x75, 0x63, 0x69, 0x62, 0x6c, 0x65) as 'crucible'
export const hammerkin = c(0x68, 0x61, 0x6d, 0x6d, 0x65, 0x72, 0x6b, 0x69, 0x6e) as 'hammerkin'

export const SPECIES = [
  golem,
  elemental,
  anvilbot,
  furnace,
  molten,
  spark,
  crucible,
  hammerkin,
] as const
export type Species = (typeof SPECIES)[number]

export const EYES = ['·', '✦', '×', '◉', '◈', '⊕'] as const
export type Eye = (typeof EYES)[number]

export const STAT_NAMES = [
  'MIGHT',
  'ENDURANCE',
  'SPARK',
  'TEMPER',
  'GRIT',
] as const
export type StatName = (typeof STAT_NAMES)[number]

// Level/XP system
export type EquipmentSlot = 'head' | 'body' | 'tool' | 'trinket'

export type Equipment = {
  head?: string
  body?: string
  tool?: string
  trinket?: string
}

// Equipment rewards unlocked at specific levels
export const EQUIPMENT_REWARDS: Record<number, Equipment> = {
  3:  { body: 'forge_apron' },
  5:  { tool: 'iron_tongs' },
  8:  { head: 'welding_mask' },
  10: { tool: 'steel_hammer' },
  13: { trinket: 'coal_amulet' },
  15: { body: 'mithril_vest' },
  18: { head: 'flame_crown' },
  20: { tool: 'arcane_anvil' },
  25: { trinket: 'ember_heart' },
  30: { head: 'dragon_helm' },
  35: { body: 'adamant_plate' },
  40: { tool: 'legendary_forge' },
}

export const MAX_LEVEL = 40

// XP needed for each level (exponential curve)
export function xpForLevel(level: number): number {
  if (level <= 1) return 0
  return Math.floor(100 * Math.pow(level, 1.5))
}

// Token cost per XP point (1 XP per N tokens processed)
export const TOKENS_PER_XP = 500

export type CompanionBones = {
  rarity: Rarity
  species: Species
  eye: Eye
  shiny: boolean
  stats: Record<StatName, number>
}

export type CompanionSoul = {
  name: string
  personality: string
}

export type CompanionProgress = {
  level: number
  xp: number
  totalTokensEarned: number
  equipment: Equipment
  petCount: number
  lastPetAt?: number
}

export type Companion = CompanionBones &
  CompanionSoul &
  CompanionProgress & {
    hatchedAt: number
  }

export type StoredCompanion = CompanionSoul &
  Partial<CompanionProgress> & { hatchedAt: number }

export const RARITY_WEIGHTS = {
  common: 60,
  uncommon: 25,
  rare: 10,
  epic: 4,
  legendary: 1,
} as const satisfies Record<Rarity, number>

export const RARITY_STARS = {
  common: '★',
  uncommon: '★★',
  rare: '★★★',
  epic: '★★★★',
  legendary: '★★★★★',
} as const satisfies Record<Rarity, string>

export const RARITY_COLORS = {
  common: 'inactive',
  uncommon: 'success',
  rare: 'permission',
  epic: 'autoAccept',
  legendary: 'warning',
} as const satisfies Record<Rarity, keyof import('../utils/theme.js').Theme>
