import type { EquipSlot, ItemDef, PlayerClass, Stats, WeaponInfo } from './claudeCraftTypes'

const WAR: PlayerClass[] = ['warrior', 'paladin', 'shaman']
const MAG: PlayerClass[] = ['mage', 'priest', 'warlock', 'druid']
const ROG: PlayerClass[] = ['rogue', 'hunter']

type Quality = NonNullable<ItemDef['quality']>

function weapon(
  id: string,
  name: string,
  quality: Quality,
  weaponInfo: WeaponInfo,
  stats: Partial<Stats>,
  requiredClass: PlayerClass[] | undefined,
  sellValue: number,
): ItemDef {
  return { id, kind: 'weapon', name, quality, requiredClass, sellValue, slot: 'mainhand', stats, weapon: weaponInfo }
}

function armor(
  id: string,
  name: string,
  slot: Exclude<EquipSlot, 'mainhand'>,
  quality: Quality,
  stats: Partial<Stats>,
  requiredClass: PlayerClass[] | undefined,
  sellValue: number,
): ItemDef {
  return { id, kind: 'armor', name, quality, requiredClass, sellValue, slot, stats }
}

function junk(id: string, name: string, sellValue: number): ItemDef {
  return { id, kind: 'junk', name, quality: 'poor', sellValue }
}

export const CLAUDECRAFT_ITEMS: Record<string, ItemDef> = {
  worn_sword: weapon('worn_sword', 'Worn Shortsword', 'common', { min: 2, max: 5, speed: 2 }, {}, undefined, 10),
  gnarled_staff: weapon('gnarled_staff', 'Gnarled Staff', 'common', { min: 3, max: 6, speed: 2.9 }, { int: 1 }, MAG, 12),
  rusty_dagger: weapon('rusty_dagger', 'Rusty Dagger', 'common', { min: 2, max: 4, speed: 1.8, dagger: true }, {}, ROG, 10),
  recruit_tunic: armor('recruit_tunic', "Recruit's Tunic", 'chest', 'common', { armor: 20 }, WAR, 5),
  apprentice_robe: armor('apprentice_robe', "Apprentice's Robe", 'chest', 'common', { armor: 8 }, MAG, 5),
  footpad_jerkin: armor('footpad_jerkin', "Footpad's Jerkin", 'chest', 'common', { armor: 14 }, ROG, 5),

  bone_fragments: junk('bone_fragments', 'Bone Fragments', 16),
  linen_scrap: junk('linen_scrap', 'Linen Scrap', 12),
  spider_leg: junk('spider_leg', 'Spider Leg', 10),
  deepfen_pearl: junk('deepfen_pearl', 'Deepfen Pearl', 80),
  cracked_wyrm_scale: junk('cracked_wyrm_scale', 'Cracked Wyrm Scale', 90),

  oiled_boots: armor('oiled_boots', 'Oiled Leather Boots', 'feet', 'uncommon', { armor: 25, agi: 1 }, undefined, 80),
  quilted_trousers: armor('quilted_trousers', 'Quilted Trousers', 'legs', 'uncommon', { armor: 30, sta: 2 }, undefined, 90),
  greyjaw_hide_boots: armor('greyjaw_hide_boots', 'Greyjaw Hide Boots', 'feet', 'uncommon', { armor: 28, agi: 1, sta: 1 }, undefined, 130),
  cryptbone_greaves: armor('cryptbone_greaves', 'Cryptbone Greaves', 'legs', 'uncommon', { armor: 75, sta: 3, str: 2 }, WAR, 220),
  cryptbone_helm: armor('cryptbone_helm', 'Cryptbone Helm', 'helmet', 'uncommon', { armor: 82, sta: 3, str: 2 }, WAR, 240),
  cryptbone_pauldrons: armor('cryptbone_pauldrons', 'Cryptbone Pauldrons', 'shoulder', 'uncommon', { armor: 70, sta: 2, str: 2 }, WAR, 230),

  trollhide_leggings: armor('trollhide_leggings', 'Trollhide Leggings', 'legs', 'uncommon', { armor: 55, sta: 3, str: 2 }, undefined, 280),
  marshstrider_boots: armor('marshstrider_boots', 'Marshstrider Boots', 'feet', 'uncommon', { armor: 40, agi: 2, sta: 2 }, undefined, 250),
  fenmist_robe: armor('fenmist_robe', 'Fenmist Robe', 'chest', 'uncommon', { armor: 45, int: 5, spi: 3 }, MAG, 350),
  eelskin_tunic: armor('eelskin_tunic', 'Eelskin Tunic', 'chest', 'uncommon', { armor: 80, agi: 5 }, ROG, 350),
  tidescale_vest: armor('tidescale_vest', 'Tidescale Vest', 'chest', 'rare', { armor: 145, sta: 5, str: 3 }, WAR, 900),
  drowned_prayer_leggings: armor('drowned_prayer_leggings', 'Drowned Prayer Leggings', 'legs', 'rare', { armor: 58, int: 6, spi: 3 }, MAG, 850),
  drowned_prayer_sandals: armor('drowned_prayer_sandals', 'Drowned Prayer Sandals', 'feet', 'rare', { armor: 42, int: 5, spi: 3 }, MAG, 780),
  eelscale_treads: armor('eelscale_treads', 'Eelscale Treads', 'feet', 'rare', { armor: 62, agi: 6, sta: 2 }, ROG, 780),
  mistveil_cord: armor('mistveil_cord', 'Mistveil Cord', 'waist', 'uncommon', { armor: 28, int: 3, spi: 2 }, MAG, 280),
  mistveil_grips: armor('mistveil_grips', 'Mistveil Grips', 'gloves', 'uncommon', { armor: 32, int: 3, spi: 2 }, MAG, 300),
  tideguard_greaves: armor('tideguard_greaves', 'Tideguard Greaves', 'legs', 'rare', { armor: 130, sta: 5, str: 3 }, WAR, 840),
  tideguard_sabatons: armor('tideguard_sabatons', 'Tideguard Sabatons', 'feet', 'rare', { armor: 105, sta: 4, str: 2 }, WAR, 760),
  eelscale_leggings: armor('eelscale_leggings', 'Eelscale Leggings', 'legs', 'rare', { armor: 78, agi: 7, sta: 3 }, ROG, 840),

  boundstone_helm: armor('boundstone_helm', 'Boundstone Helm', 'helmet', 'uncommon', { armor: 122, sta: 4, str: 3 }, WAR, 520),
  boundstone_girdle: armor('boundstone_girdle', 'Boundstone Girdle', 'waist', 'uncommon', { armor: 96, sta: 3, str: 2 }, WAR, 440),
  gravewyrm_mantle: armor('gravewyrm_mantle', 'Gravewyrm Mantle', 'shoulder', 'uncommon', { armor: 50, int: 4, spi: 3 }, MAG, 480),
  gravewyrm_gauntlets: armor('gravewyrm_gauntlets', 'Gravewyrm Gauntlets', 'gloves', 'uncommon', { armor: 76, agi: 4, sta: 2 }, ROG, 480),
  boneplate_vest: armor('boneplate_vest', 'Boneplate Vest', 'chest', 'uncommon', { armor: 170, sta: 5, str: 4 }, WAR, 700),
  revenant_silk_robe: armor('revenant_silk_robe', 'Revenant Silk Robe', 'chest', 'uncommon', { armor: 62, int: 7, spi: 4 }, MAG, 700),
  nightwalk_jerkin: armor('nightwalk_jerkin', 'Nightwalk Jerkin', 'chest', 'uncommon', { armor: 108, agi: 7, sta: 3 }, ROG, 700),
  zealotsbane_blade: weapon('zealotsbane_blade', "Zealotsbane Blade", 'rare', { min: 22, max: 36, speed: 2.4 }, { str: 7, sta: 3 }, WAR, 1600),
  korgaths_chainwraps: armor('korgaths_chainwraps', "Korgath's Chainwraps", 'gloves', 'rare', { armor: 126, sta: 5, str: 3 }, WAR, 1100),
  staff_of_velkhar: weapon('staff_of_velkhar', 'Staff of Velkhar', 'rare', { min: 24, max: 40, speed: 3 }, { int: 10, spi: 4 }, MAG, 1800),
  shadowmeld_tunic: armor('shadowmeld_tunic', 'Shadowmeld Tunic', 'chest', 'rare', { armor: 130, agi: 9, sta: 4 }, ROG, 1300),
  wyrmcult_grand_robe: armor('wyrmcult_grand_robe', 'Wyrmcult Grand Robe', 'chest', 'rare', { armor: 74, int: 11, spi: 5 }, MAG, 1300),
  gravewyrm_sabatons: armor('gravewyrm_sabatons', 'Gravewyrm Sabatons', 'feet', 'rare', { armor: 132, sta: 5, str: 4 }, WAR, 1200),
  wyrmcult_soulsteps: armor('wyrmcult_soulsteps', 'Wyrmcult Soulsteps', 'feet', 'rare', { armor: 48, int: 7, spi: 4 }, MAG, 1100),
  wyrmshadow_treads: armor('wyrmshadow_treads', 'Wyrmshadow Treads', 'feet', 'rare', { armor: 76, agi: 8, sta: 3 }, ROG, 1100),
  emberwood_staff: weapon('emberwood_staff', 'Emberwood Staff', 'rare', { min: 23, max: 39, speed: 3 }, { int: 9, spi: 5 }, MAG, 1600),
  boneguard_breastplate: armor('boneguard_breastplate', 'Boneguard Breastplate', 'chest', 'rare', { armor: 210, sta: 7, str: 4 }, WAR, 1600),
  gravewyrm_stalkers_treads: armor('gravewyrm_stalkers_treads', "Gravewyrm Stalker's Treads", 'feet', 'rare', { armor: 84, agi: 9, sta: 4 }, ROG, 1300),
  deathlord_legguards: armor('deathlord_legguards', 'Deathlord Legguards', 'legs', 'epic', { armor: 235, sta: 10, str: 7 }, WAR, 3000),
  necromancers_soulsteps: armor('necromancers_soulsteps', "Necromancer's Soulsteps", 'feet', 'epic', { armor: 64, int: 13, spi: 8 }, MAG, 3000),
  wyrmshadow_legguards: armor('wyrmshadow_legguards', 'Wyrmshadow Legguards', 'legs', 'epic', { armor: 118, agi: 13, sta: 6 }, ROG, 3000),
  cultist_flayer: weapon('cultist_flayer', 'Cultist Flayer', 'rare', { min: 18, max: 29, speed: 1.7, dagger: true }, { agi: 10, sta: 4 }, ROG, 2000),
  wyrmfang_greatblade: weapon('wyrmfang_greatblade', 'Wyrmfang Greatblade', 'epic', { min: 32, max: 52, speed: 2.8 }, { str: 12, sta: 7 }, WAR, 4200),
  staff_of_the_gravewyrm: weapon('staff_of_the_gravewyrm', 'Staff of the Gravewyrm', 'epic', { min: 34, max: 56, speed: 3 }, { int: 14, spi: 8 }, MAG, 4200),
  fang_of_korzul: weapon('fang_of_korzul', 'Fang of Korzul', 'epic', { min: 22, max: 35, speed: 1.7, dagger: true }, { agi: 14, sta: 6 }, ROG, 4200),
  deathlord_warplate: armor('deathlord_warplate', 'Deathlord Warplate', 'chest', 'epic', { armor: 290, sta: 12, str: 9 }, WAR, 4300),
  necromancers_starshroud: armor('necromancers_starshroud', "Necromancer's Starshroud", 'chest', 'epic', { armor: 92, int: 15, spi: 9 }, MAG, 4300),
  wyrmshadow_harness: armor('wyrmshadow_harness', 'Wyrmshadow Harness', 'chest', 'epic', { armor: 150, agi: 15, sta: 7 }, ROG, 4300),
  deathlords_dread_visage: armor('deathlords_dread_visage', "Deathlord's Dread Visage", 'helmet', 'epic', { armor: 240, sta: 10, str: 7 }, WAR, 3600),
  necromancers_soulspire_mantle: armor('necromancers_soulspire_mantle', "Necromancer's Soulspire Mantle", 'shoulder', 'epic', { armor: 78, int: 13, spi: 7 }, MAG, 3600),
  wyrmshadow_talongrips: armor('wyrmshadow_talongrips', 'Wyrmshadow Talongrips', 'gloves', 'epic', { armor: 112, agi: 12, sta: 6 }, ROG, 3600),

  deathless_heartwood: weapon('deathless_heartwood', 'Deathless Heartwood', 'legendary', { min: 42, max: 68, speed: 3 }, { int: 20, spi: 12, sta: 8 }, MAG, 9000),
  kingsbane_last_oath: weapon("kingsbane_last_oath", "Kingsbane, Last Oath", 'legendary', { min: 40, max: 64, speed: 2.7 }, { str: 18, sta: 10 }, WAR, 9000),
  crownforged_dreadhelm: armor('crownforged_dreadhelm', 'Crownforged Dreadhelm', 'helmet', 'epic', { armor: 320, sta: 14, str: 10 }, WAR, 5200),
  crownforged_warspaulders: armor('crownforged_warspaulders', 'Crownforged Warspaulders', 'shoulder', 'epic', { armor: 280, sta: 12, str: 9 }, WAR, 5000),
  nighttalon_crown: armor('nighttalon_crown', 'Nighttalon Crown', 'helmet', 'epic', { armor: 165, agi: 16, sta: 8 }, ROG, 5200),
  nighttalon_shoulderguards: armor('nighttalon_shoulderguards', 'Nighttalon Shoulderguards', 'shoulder', 'epic', { armor: 145, agi: 14, sta: 7 }, ROG, 5000),
  soulflame_cowl: armor('soulflame_cowl', 'Soulflame Cowl', 'helmet', 'epic', { armor: 105, int: 17, spi: 9 }, MAG, 5200),
  soulflame_mantle: armor('soulflame_mantle', 'Soulflame Mantle', 'shoulder', 'epic', { armor: 94, int: 15, spi: 8 }, MAG, 5000),
  stormcallers_crown: armor('stormcallers_crown', "Stormcaller's Crown", 'helmet', 'epic', { armor: 250, int: 13, sta: 8 }, ['shaman'], 5200),
  stormcallers_spaulders: armor('stormcallers_spaulders', "Stormcaller's Spaulders", 'shoulder', 'epic', { armor: 225, int: 12, sta: 7 }, ['shaman'], 5000),
}

export function getClaudeCraftItem(itemId: string) {
  return CLAUDECRAFT_ITEMS[itemId] ?? null
}

export function getItemStatsText(item: ItemDef) {
  const stats = item.stats ?? {}
  const parts = [
    stats.armor ? `${stats.armor} Armor` : '',
    stats.str ? `+${stats.str} Str` : '',
    stats.agi ? `+${stats.agi} Agi` : '',
    stats.sta ? `+${stats.sta} Sta` : '',
    stats.int ? `+${stats.int} Int` : '',
    stats.spi ? `+${stats.spi} Spi` : '',
  ].filter(Boolean)
  if (item.weapon) parts.unshift(`${item.weapon.min}-${item.weapon.max} Damage, ${item.weapon.speed.toFixed(1)} Speed`)
  return parts.join(' / ') || 'No combat stats'
}
