/** Static per-class guidance shown during character creation. No AI, no async. */
export interface ClassTip {
  role: string;
  /** e.g. "Strength (attacks) · Charisma (spells, auras)" */
  primaryAbilities: string;
  beginner: boolean;
  skillSuggestions: string;
  abilityTip?: string;
}

const TIPS_5E: Record<string, ClassTip> = {
  barbarian: {
    role: 'A fearless, rage-fuelled warrior who charges into melee and soaks up punishment. Very simple to play — hit things, don\'t die.',
    primaryAbilities: 'Strength (attacks, grapple) · Constitution (HP and Rage)',
    beginner: true,
    skillSuggestions: 'Athletics, Intimidation, Perception, Survival',
    abilityTip: 'Put your highest score in Strength, second in Constitution. Barbarians don\'t wear heavy armour — your AC = 10 + DEX + CON, so Constitution doubly matters.',
  },
  bard: {
    role: 'A jack-of-all-trades who inspires allies, casts spells, and out-talks everyone. Works well in any role.',
    primaryAbilities: 'Charisma (spells, Bardic Inspiration) · Dexterity (AC, initiative)',
    beginner: false,
    skillSuggestions: 'Persuasion, Deception, Performance, Insight',
    abilityTip: 'Charisma is everything for a Bard. Second priority: Dexterity for AC, then Constitution for concentration spells.',
  },
  cleric: {
    role: 'A divine spellcaster who heals allies and smites enemies. Flexible — some subclasses (War, Forge) fight in the front line.',
    primaryAbilities: 'Wisdom (all spells, Channel Divinity) · Strength or Dexterity (depending on build)',
    beginner: true,
    skillSuggestions: 'Medicine, Religion, Insight, Persuasion',
    abilityTip: 'Wisdom first — it powers every Cleric spell. Front-line Clerics want Strength second; support Clerics want Constitution for concentration.',
  },
  druid: {
    role: 'A nature magic caster who can transform into animals (Wild Shape). Powerful but benefits from reading the options.',
    primaryAbilities: 'Wisdom (spells, Wild Shape save DC) · Constitution (concentration spells)',
    beginner: false,
    skillSuggestions: 'Nature, Perception, Survival, Insight',
    abilityTip: 'Wisdom is your core stat. Constitution keeps concentration spells alive. Low Strength is fine — use Wild Shape for physical challenges.',
  },
  fighter: {
    role: 'The most reliable front-line warrior. Gets more attacks than anyone, can wear any armour, and is very forgiving to play.',
    primaryAbilities: 'Strength (melee attacks) or Dexterity (ranged / finesse builds) · Constitution (HP, Second Wind)',
    beginner: true,
    skillSuggestions: 'Athletics, Perception, Intimidation, History',
    abilityTip: 'Pick a focus: heavy-armour melee (Strength + Constitution) or light-armour finesse (Dexterity + Constitution). Don\'t try to split them.',
  },
  monk: {
    role: 'A fast unarmed martial artist who darts around the battlefield punching multiple enemies per turn.',
    primaryAbilities: 'Dexterity (AC, attacks, finesse weapons) · Wisdom (Unarmored Defense, ki abilities)',
    beginner: false,
    skillSuggestions: 'Athletics, Acrobatics, Stealth, Insight',
    abilityTip: 'Dexterity and Wisdom both feed your AC (= DEX + WIS) — invest in both. Ki points fuel everything; Constitution helps you take a hit while concentrating.',
  },
  paladin: {
    role: 'A holy warrior who hits hard, heals allies, and radiates powerful protective auras. One of the strongest front-liners.',
    primaryAbilities: 'Strength (melee attacks, armour) · Charisma (spells, auras, Lay on Hands, Divine Smite bonus)',
    beginner: false,
    skillSuggestions: 'Persuasion, Athletics, Religion, Insight',
    abilityTip: 'You need two high stats: Strength for hitting things and Charisma for everything magical. Constitution can be your dump stat — heavy armour and d10 HP cover it.',
  },
  ranger: {
    role: 'A skilled hunter and tracker who excels at a favoured terrain or enemy type. Best at skirmishing from range.',
    primaryAbilities: 'Dexterity (ranged and finesse attacks, AC) or Strength (melee) · Wisdom (spells)',
    beginner: false,
    skillSuggestions: 'Perception, Stealth, Survival, Athletics',
    abilityTip: 'Dexterity for bows and light armour is the most common build. Wisdom powers your spells — prioritise it over Charisma. Constitution for survivability.',
  },
  rogue: {
    role: 'A sneaky striker who deals huge Sneak Attack damage on one hit per turn, plus the best skill coverage in the game.',
    primaryAbilities: 'Dexterity (attacks, Sneak Attack with finesse/ranged, AC, Stealth) · Intelligence or Charisma (subclass)',
    beginner: true,
    skillSuggestions: 'Stealth, Perception, Sleight of Hand, Deception, Athletics',
    abilityTip: 'Dexterity is your core stat. You only need one hit per turn for big damage — prioritise staying alive (and unseen) over maximising damage output.',
  },
  sorcerer: {
    role: 'A blaster mage with innate magical power. Fewer spells than a Wizard but Metamagic bends the rules dramatically.',
    primaryAbilities: 'Charisma (spell attacks and save DCs) · Constitution (concentration, survivability)',
    beginner: true,
    skillSuggestions: 'Persuasion, Arcana, Intimidation, Insight',
    abilityTip: 'Charisma is everything. Constitution second — you are in a robe with 6 HP per level and will be targeted. Pick up the Mage Armour spell early.',
  },
  warlock: {
    role: 'A pact-powered caster who recharges spell slots on a short rest — unique among casters. Eldritch Blast is your bread and butter.',
    primaryAbilities: 'Charisma (spells, Eldritch Blast invocations) · Constitution (concentration)',
    beginner: false,
    skillSuggestions: 'Arcana, Intimidation, Deception, History',
    abilityTip: 'Charisma first. Until high level you only have 1–2 spell slots — lean on Eldritch Blast with Invocations to stay useful every round.',
  },
  wizard: {
    role: 'The most versatile spellcaster — prepare different spells each day for any situation. Deeply rewarding, needs spell knowledge.',
    primaryAbilities: 'Intelligence (all spells, Arcane Recovery) · Constitution (concentration spells)',
    beginner: false,
    skillSuggestions: 'Arcana, History, Investigation, Medicine',
    abilityTip: 'Intelligence powers everything. Constitution second — Concentration spells are your strongest tools and you need to maintain them after taking a hit.',
  },
};

const TIPS_PF2E: Record<string, ClassTip> = {
  alchemist: {
    role: 'A tinkerer who brews bombs, mutagens, and elixirs. Highly customisable but needs careful resource management between encounters.',
    primaryAbilities: 'Intelligence (formula DC, items per day) · Dexterity (AC in light armour)',
    beginner: false,
    skillSuggestions: 'Crafting, Arcana, Medicine, Thievery',
  },
  barbarian: {
    role: 'A rage-fuelled melee brawler with huge hit points. Your Instinct (animal, giant, spirit, etc.) defines your flavour and bonus.',
    primaryAbilities: 'Strength (attacks) · Constitution (HP — Rage adds CON to max HP)',
    beginner: true,
    skillSuggestions: 'Athletics, Intimidation, Survival',
    abilityTip: 'Strength first. Constitution second — Rage adds your Constitution modifier to HP on top of the base die. Dexterity for unarmoured builds.',
  },
  bard: {
    role: 'A charismatic performer with occult spellcasting and composition cantrips that buff allies every round. Excellent support.',
    primaryAbilities: 'Charisma (spells, composition cantrips) · Dexterity (AC, Reflex)',
    beginner: false,
    skillSuggestions: 'Performance, Deception, Diplomacy, Occultism',
    abilityTip: 'Charisma is your foundation. Dexterity second for AC in light armour. Constitution for keeping concentration spells up.',
  },
  champion: {
    role: 'A heavily armoured divine warrior whose class Reaction reduces damage taken by allies — uniquely powerful in a party.',
    primaryAbilities: 'Strength (attacks) · Charisma (divine spells, Lay on Hands, Champion reaction) · Constitution (HP)',
    beginner: true,
    skillSuggestions: 'Religion, Athletics, Intimidation, Medicine',
    abilityTip: 'Strength and Charisma both matter. Heavy armour covers weak Dexterity. Your Champion Reaction scales with Charisma.',
  },
  cleric: {
    role: 'A divine spellcaster whose god determines your offensive or support role. The Divine Font gives extra heal/harm spells per day.',
    primaryAbilities: 'Wisdom (spells, divine font) · Strength or Dexterity (deity weapon / unarmoured)',
    beginner: true,
    skillSuggestions: 'Religion, Medicine, Diplomacy, Insight',
    abilityTip: 'Wisdom is your core stat. Warpriest Clerics add Strength; Cloistered Clerics need Constitution for sustained spellcasting.',
  },
  druid: {
    role: 'A nature magic caster who can transform into animals or call on elemental forces. Very versatile with powerful spells.',
    primaryAbilities: 'Wisdom (spells, Wild Shape save DC) · Constitution (HP in Wild Shape forms)',
    beginner: false,
    skillSuggestions: 'Nature, Survival, Medicine, Athletics',
    abilityTip: 'Wisdom is your primary stat. Constitution for HP when in Wild Shape. Dexterity for AC when not transformed.',
  },
  fighter: {
    role: 'The best weapon combatant in the game — higher proficiency than any other class. Multiple attacks and powerful stances.',
    primaryAbilities: 'Strength (melee / thrown attacks) or Dexterity (ranged / finesse attacks)',
    beginner: true,
    skillSuggestions: 'Athletics, Intimidation, Acrobatics, Perception',
    abilityTip: 'Pick Strength for melee or Dexterity for ranged/finesse. Constitution for hit points. Fighters don\'t need a third stat to shine.',
  },
  monk: {
    role: 'An unarmed martial artist with extreme mobility. Ki abilities let you do things no weapon fighter can — flying kicks, stunning strikes.',
    primaryAbilities: 'Strength or Dexterity (attacks and movement) · Wisdom (Unarmored Defense, ki)',
    beginner: false,
    skillSuggestions: 'Athletics, Acrobatics, Perception, Stealth',
    abilityTip: 'Your AC = 10 + DEX + WIS, so both stats matter. Decide early: Strength-based martial arts or Dexterity-based finesse.',
  },
  oracle: {
    role: 'A divine spontaneous caster with a Curse that grows as you cast higher-rank spells. Dramatic power with real tradeoffs.',
    primaryAbilities: 'Charisma (all spells) · Constitution (HP, curse management)',
    beginner: false,
    skillSuggestions: 'Religion, Occultism, Diplomacy, Medicine',
    abilityTip: 'Charisma first. Constitution second — your curse adds drawbacks that are harder to manage at low HP. Monitor your curse rank carefully.',
  },
  ranger: {
    role: 'A skilled hunter and outlander. Hunt Prey gives a persistent bonus against your current target every combat — keep track of it.',
    primaryAbilities: 'Dexterity (ranged attacks, AC) or Strength (melee) · Wisdom (spells)',
    beginner: false,
    skillSuggestions: 'Nature, Survival, Stealth, Athletics',
    abilityTip: 'Choose a focus: ranged (Dexterity + bow) or melee (Strength + weapon). Wisdom for your ranger spells. Perception is trained free.',
  },
  rogue: {
    role: 'A skill master and precision striker. Sneak Attack triggers when the target is Flat-Footed — positioning and flanking matter.',
    primaryAbilities: 'Dexterity (attacks, AC, Stealth) · Intelligence or Charisma (Racket choice)',
    beginner: true,
    skillSuggestions: 'Stealth, Thievery, Deception, Perception, Acrobatics',
    abilityTip: 'Dexterity is your core stat. Your second stat depends on your Racket: Scoundrel needs Charisma, Mastermind needs Intelligence, Thief needs Dexterity first.',
  },
  sorcerer: {
    role: 'A spontaneous arcane or occult caster with innate power from a magical bloodline. More flexible casting than prepared classes.',
    primaryAbilities: 'Charisma (all spells) · Constitution (concentration and survivability)',
    beginner: true,
    skillSuggestions: 'Arcana, Intimidation, Deception, Occultism',
    abilityTip: 'Charisma is everything. Constitution second — you have light armour and will be targeted. Your bloodline gives you bonus spells automatically.',
  },
  swashbuckler: {
    role: 'A dashing duelist who builds Panache through daring deeds, then spends it on devastating Finishers. Style and substance.',
    primaryAbilities: 'Dexterity (attacks, AC) · Charisma (Panache, some class feats)',
    beginner: false,
    skillSuggestions: 'Acrobatics, Athletics, Deception, Performance',
    abilityTip: 'Dexterity for everything combat-related. Charisma for Panache and social moments. Invest in whichever skill triggers your Style.',
  },
  witch: {
    role: 'A patron-bonded spellcaster with powerful hexes and a familiar that delivers your touch spells. Protect your familiar.',
    primaryAbilities: 'Intelligence, Wisdom, or Charisma (depends on patron) · Constitution for spellcasting',
    beginner: false,
    skillSuggestions: 'Arcana, Occultism, Nature, Diplomacy',
    abilityTip: 'Your patron determines your casting stat — check your patron carefully. Constitution second to keep casting without disruption.',
  },
  wizard: {
    role: 'A prepared arcane caster with more spells over a career than any other class. Thorough preparation wins battles.',
    primaryAbilities: 'Intelligence (all spells, Arcane Thesis) · Constitution (concentration spells)',
    beginner: false,
    skillSuggestions: 'Arcana, Occultism, Society, History',
    abilityTip: 'Intelligence first, always. Constitution second for concentration spells. Dexterity for AC matters early when you have no armour.',
  },
};

export function getClassTip(system: string, classSlug: string): ClassTip | undefined {
  const tips = system === 'pf2e' ? TIPS_PF2E : TIPS_5E;
  return tips[classSlug.toLowerCase()];
}

/**
 * Returns a synergy note if the race's meta string contains ability boosts that
 * match the class's key abilities. Both systems' meta formats are handled:
 *   5e:   "+2 DEX, +1 WIS · 30 ft"
 *   PF2e: "+Con/+Wis · 10 HP"
 */
export function raceSynergyNote(raceMeta: string, keyAbilities: string[]): string | null {
  if (!raceMeta || !keyAbilities.length) return null;
  const boosted = new Set<string>();
  // Match "+2 DEX" or "+Wis" or "+Con" patterns
  for (const m of raceMeta.matchAll(/\+\d*([A-Za-z]{3})/g)) {
    const ab = m[1];
    if (ab) boosted.add(ab.toLowerCase());
  }
  const matches = keyAbilities.filter((k) => boosted.has(k.toLowerCase()));
  if (!matches.length) return null;
  const names: Record<string, string> = {
    str: 'Strength', dex: 'Dexterity', con: 'Constitution',
    int: 'Intelligence', wis: 'Wisdom', cha: 'Charisma',
  };
  const label = matches.map((k) => names[k] ?? k).join(' and ');
  return `This ancestry boosts ${label} — a natural fit for this class's primary abilities.`;
}
