/** Deterministic, per-class action guide for the player "What can I do?" panel. */
export interface ActionStep {
  label: string;
  desc: string;
}

export interface TurnGuide {
  actions: ActionStep[];
  /** Optional level gate (shown when level < minLevel). */
  upgradeAt?: { level: number; note: string };
  /** Reminder shown at all levels. */
  tip?: string;
}

// ---- 5e guides ----

const GUIDE_5E: Record<string, TurnGuide> = {
  barbarian: {
    actions: [
      { label: 'Action', desc: 'Attack — if you aren\'t raging yet, use a Bonus Action to Rage first (it\'s free this turn).' },
      { label: 'Bonus Action', desc: 'Rage (once per combat, free resources). At level 2: Reckless Attack costs no action — just declare it on your first attack roll.' },
      { label: 'Movement', desc: 'Your speed is 30 ft. While raging you can\'t cast spells or concentrate, but you\'re nigh-unstoppable.' },
      { label: 'Reaction', desc: 'Opportunity Attack — if an enemy leaves your reach, you can attack as a reaction.' },
    ],
    upgradeAt: { level: 5, note: 'At level 5 you gain Extra Attack — make two attacks on your Attack action.' },
    tip: 'Rage gives resistance to Bludgeoning, Piercing, and Slashing damage. Never fight without it if you expect more than one hit.',
  },
  bard: {
    actions: [
      { label: 'Action', desc: 'Cast a spell (Vicious Mockery to impose disadvantage costs nothing but a cantrip slot), or use Help.' },
      { label: 'Bonus Action', desc: 'Bardic Inspiration — give a d6 to an ally; they can add it to any roll within the next 10 minutes. At level 5 it becomes a d8.' },
      { label: 'Movement', desc: '30 ft. Stay at range — you have light armour and low hit points.' },
      { label: 'Reaction', desc: 'Cutting Words (once per short rest, subtract your Inspiration die from an enemy\'s attack, check, or damage roll).' },
    ],
    tip: 'Bardic Inspiration is your most powerful tool early on. Give it out before initiative is rolled when possible.',
  },
  cleric: {
    actions: [
      { label: 'Action', desc: 'Cast a spell — Sacred Flame (cantrip, radiant damage) costs no slots. Use higher-level slots for Guiding Bolt or Spirit Guardians.' },
      { label: 'Bonus Action', desc: 'Healing Word is a bonus-action heal — save it to pick up a downed ally. Better than Cure Wounds in most combats.' },
      { label: 'Movement', desc: '30 ft. Channel Divinity is a free action, not a bonus action — use it on the same turn as a spell.' },
      { label: 'Reaction', desc: 'Shield of Faith (concentration) boosts an ally\'s AC as your Action; your Reaction is free for Opportunity Attacks.' },
    ],
    tip: 'Healing Word as a Bonus Action to revive downed allies, then attack or cast on your main Action. Don\'t burn all your spell slots healing mid-combat.',
  },
  druid: {
    actions: [
      { label: 'Action', desc: 'Cast a spell (Shillelagh makes your staff a Wisdom-based melee weapon) or use Wild Shape to become a beast.' },
      { label: 'Bonus Action', desc: 'Wild Shape is a Bonus Action — transform mid-round without losing your main Action. Your gear melds into your new form.' },
      { label: 'Movement', desc: '30 ft (or the Wild Shape form\'s speed — most animals are faster). Wild Shape HP is separate from yours; it\'s a free buffer.' },
      { label: 'Reaction', desc: 'No unique reaction early on. Use Opportunity Attacks in melee or cast Absorb Elements to halve elemental damage.' },
    ],
    upgradeAt: { level: 2, note: 'At level 2 you unlock Wild Shape. Use the CR ¼ CR ½ beast forms as frontline tanks — when they run out of HP you revert, unharmed.' },
    tip: 'Wild Shape HP is a free buffer on top of your own. Get hit in beast form, then revert and cast heals if needed.',
  },
  fighter: {
    actions: [
      { label: 'Action', desc: 'Attack — one attack until level 5. Choose a Fighting Style at level 1 (Great Weapon Master, Archery, Defence, Dueling, etc.).' },
      { label: 'Bonus Action', desc: 'Second Wind (once per short rest) — recover 1d10 + Fighter level HP. At level 3 some subclasses add bonus action attacks (Battle Master, Champion).' },
      { label: 'Movement', desc: '30 ft (35 ft in medium armour with the Mobility feat). You can split movement before and after an attack.' },
      { label: 'Reaction', desc: 'Opportunity Attack — make one attack against any creature that moves out of your reach. Strongly consider the Sentinel feat later.' },
    ],
    upgradeAt: { level: 5, note: 'Level 5: Extra Attack — make two attacks per Attack action. Level 11: Three attacks. Level 20: Four attacks.' },
    tip: 'You have the most Action Surge uses and the broadest weapon/armour access. Don\'t forget Second Wind between fights — it\'s free.',
  },
  monk: {
    actions: [
      { label: 'Action', desc: 'Attack — use Dexterity with unarmed strikes and monk weapons (short sword, any simple weapon). Flurry of Blows can add 2 more hits per turn.' },
      { label: 'Bonus Action', desc: 'Flurry of Blows (1 ki — hit twice), Step of the Wind (1 ki — Dash or Disengage), or Patient Defense (1 ki — Dodge).' },
      { label: 'Movement', desc: '35 ft at level 1, increasing 5 ft every 4 levels. You don\'t need to engage by running in a straight line — use your speed creatively.' },
      { label: 'Reaction', desc: 'Deflect Missiles — catch or redirect incoming ranged weapon attacks. At level 3: Stunning Strike (2 ki on a hit) makes a target lose their next turn on a failed Con save.' },
    ],
    upgradeAt: { level: 5, note: 'Level 5: Extra Attack + Stunning Strike. A Stunning Strike on a melee hit removes the target\'s next action and gives advantage to all attacks against them.' },
    tip: 'Ki points replenish on a short rest. Use them every short rest — hoarding ki is a common new-player mistake.',
  },
  paladin: {
    actions: [
      { label: 'Action', desc: 'Attack. After a hit you can expend a spell slot as a Bonus Action to add Divine Smite (2d8 radiant per slot level, doubled on crits — save slots for crits).' },
      { label: 'Bonus Action', desc: 'Divine Smite after a hit (your choice, after you see the roll). Lay on Hands is an Action. Healing using a spell slot is also an Action.' },
      { label: 'Movement', desc: '30 ft in heavy armour. Your Auras (level 6+) are passive — they don\'t cost actions, they just work.' },
      { label: 'Reaction', desc: 'Opportunity Attack. At level 6: Aura of Protection adds your Charisma modifier to all saving throws for nearby allies — no action needed.' },
    ],
    upgradeAt: { level: 5, note: 'Level 5: Extra Attack + 2nd-level spell slots. Smiting with a 2nd-level slot adds 3d8 radiant — devastating on a crit.' },
    tip: 'Divine Smite is declared after you know the attack hit and whether it\'s a crit (2× dice on a crit). Save your highest spell slots for confirmed crits.',
  },
  ranger: {
    actions: [
      { label: 'Action', desc: 'Attack at range or melee. Mark a creature as your Favoured Enemy for bonus damage. Hunter\'s Mark (concentration, Bonus Action to recast) adds 1d6 per hit.' },
      { label: 'Bonus Action', desc: 'Cast Hunter\'s Mark (1st-level spell, concentration), or move it to a new target when the old one dies.' },
      { label: 'Movement', desc: '30 ft. Ranger subclasses often grant additional Bonus Actions at level 3 (Hunter: Horde Breaker; Beastmaster: command your companion).' },
      { label: 'Reaction', desc: 'Opportunity Attack. At level 7: Evasion — half damage (zero on a success) vs area attacks that target Dexterity saves.' },
    ],
    upgradeAt: { level: 5, note: 'Level 5: Extra Attack. Combine with Hunter\'s Mark for consistent damage every turn.' },
    tip: 'Hunter\'s Mark is concentration — don\'t cast other concentration spells while it\'s active. It moves for free when a target drops, no spell slot needed.',
  },
  rogue: {
    actions: [
      { label: 'Action', desc: 'Attack — one attack per turn (use finesse or ranged weapons for Dexterity-based Sneak Attack). Sneak Attack triggers once per turn when you have advantage or an ally is adjacent to your target.' },
      { label: 'Bonus Action', desc: 'Cunning Action (level 2): Dash, Disengage, or Hide as a Bonus Action. Disengage means you can walk away without triggering Opportunity Attacks.' },
      { label: 'Movement', desc: '30 ft. Use Disengage (Cunning Action) to safely retreat after attacking from range or melee.' },
      { label: 'Reaction', desc: 'Uncanny Dodge (level 5): use your Reaction to halve damage from one attack per round. Evasion (level 7): half/no damage from Dexterity saves.' },
    ],
    upgradeAt: { level: 2, note: 'Level 2: Cunning Action unlocks the Rogue\'s unique mobility. Disengage every turn after attacking to avoid Opportunity Attacks.' },
    tip: 'You only need Sneak Attack once per turn but it can trigger on Opportunity Attacks too. Set up flanking (adjacent ally) to guarantee advantage.',
  },
  sorcerer: {
    actions: [
      { label: 'Action', desc: 'Cast a spell. Fire Bolt and Ray of Frost are damage cantrips that cost no slots. Chromatic Orb (1st slot) deals 3d8 of any element — strong early choice.' },
      { label: 'Bonus Action', desc: 'Quicken Spell (2 Sorcery Points): cast a spell as a Bonus Action instead of an Action — lets you attack twice in one turn with two spells.' },
      { label: 'Movement', desc: '30 ft. Stay far from enemies — use Misty Step (2nd-level slot) to teleport 30 ft if surrounded.' },
      { label: 'Reaction', desc: 'Counterspell (3rd-level slot, level 6): cancel a spell being cast within 60 ft. No other unique reaction early on.' },
    ],
    upgradeAt: { level: 3, note: 'Level 3: Metamagic unlocks. Twinned Spell (double target, 1 point per spell level) and Empowered Spell (1 point to reroll damage dice) are often picked first.' },
    tip: 'Sorcery Points convert to extra spell slots (1 short rest). Don\'t spend all your Sorcery Points on Metamagic — keep some for emergency slots.',
  },
  warlock: {
    actions: [
      { label: 'Action', desc: 'Cast a spell or use Eldritch Blast (cantrip, force damage, scales to 4 beams at level 17). Eldritch Blast + Agonizing Blast invocation is your go-to attack.' },
      { label: 'Bonus Action', desc: 'Hex (1st-level slot, concentration): choose an ability, target has disadvantage on checks with it, and you add 1d6 necrotic per hit. Move it when the target dies for free.' },
      { label: 'Movement', desc: '30 ft. Your spell slots recharge on a short rest — take short rests as often as the group allows to keep up output.' },
      { label: 'Reaction', desc: 'No unique early reaction. Devil\'s Sight invocation lets you see in magical darkness — combine with Darkness spell for advantage on all attacks.' },
    ],
    upgradeAt: { level: 5, note: 'Level 5: 3rd-level spell slots (still only 2). Hypnotic Pattern and Hunger of Hadar are crowd-control standouts at this level.' },
    tip: 'Your spell slots are few but powerful — always at your highest slot level. Short-rest classes (Monk, Fighter) decide short rest frequency; advocate for them if your DM forgets.',
  },
  wizard: {
    actions: [
      { label: 'Action', desc: 'Cast a spell. Fire Bolt and Chill Touch are free cantrips. Hold your best slots (Fireball, Hypnotic Pattern) for fights that matter.' },
      { label: 'Bonus Action', desc: 'No universal bonus action. Misty Step (2nd-level) uses an Action, not a Bonus Action. Some subclass features add bonus action options.' },
      { label: 'Movement', desc: '30 ft. Stand behind your party and out of reach. Blur and Mirror Image are Concentration-free self-protections.' },
      { label: 'Reaction', desc: 'Counterspell (3rd-level slot): cancel a spell being cast. Shield (1st-level slot): add +5 AC until your next turn (excellent use of a reaction slot).' },
    ],
    upgradeAt: { level: 5, note: 'Level 5: Fireball and Lightning Bolt become available. These 3rd-level spells are game-changers — conserve 3rd-level slots for them early on.' },
    tip: 'Copy spells into your spellbook whenever you find scrolls or can access another wizard\'s book. The more spells you know, the more prepared you can be.',
  },
};

// ---- PF2e guides ----

const GUIDE_PF2E: Record<string, TurnGuide> = {
  barbarian: {
    actions: [
      { label: '1 action', desc: 'Strike — make one weapon attack. In PF2e, you can Strike up to 3 times in one turn, but each additional attack takes a -5 / -10 penalty to hit.' },
      { label: '2 actions', desc: 'Enter Rage (free the first time per encounter). Most of your power comes from raging — always start combat in Rage.' },
      { label: '3 actions', desc: 'Double-Strike, Sudden Charge (move + attack), or take the Intimidate action to Demoralize enemies.' },
      { label: 'Reaction', desc: 'Attack of Opportunity (from the Reactive Strike feat — Fighters get it free; Barbarians can buy it). You can also Step away from threats as a free action when not engaged.' },
    ],
    tip: 'In PF2e you have 3 actions and 1 reaction per round. Move, Strike, Strike is a common sequence (with -5 on the second). Rage first to maximise your bonus damage.',
  },
  champion: {
    actions: [
      { label: '1 action', desc: 'Strike — use your deity\'s favoured weapon for extra class synergy.' },
      { label: '2 actions', desc: 'Raise a Shield (1 action) + Strike (1 action) = Block reaction available AND make an attack. Shield\'s AC bonus only applies when raised.' },
      { label: '3 actions', desc: 'Move (1) + Raise Shield (1) + Strike (1) is your bread-and-butter turn.' },
      { label: 'Reaction', desc: 'Champion\'s Reaction — Retributive Strike (Paladin) or Rescuing Reach (Liberator): when an ally within 15 ft is hit, you can interpose and reduce the damage. This is why you exist.' },
    ],
    tip: 'Your Champion\'s Reaction is the most powerful defensive tool in PF2e. Always stay within 15 ft of the party\'s squishiest member. Raise your Shield every single turn.',
  },
  cleric: {
    actions: [
      { label: 'Heal / Harm', desc: 'Heal at 1 action targets only you; 2 actions targets a touched creature; 3 actions is an area burst that heals all allies and damages undead. Pick based on the situation.' },
      { label: 'Cast a Spell', desc: 'Most spells take 2 actions. Divine Spellcasters have access to Harm / Heal, Bless, and weapon spells through their deity.' },
      { label: 'Channel Smite', desc: '2 actions: Strike AND add a Harm/Heal charge to the hit (1 + spell rank damage). Extremely efficient if you want to be in melee.' },
      { label: 'Reaction', desc: 'No class reaction at level 1. Take a shield if you want a Block reaction — it matters against single big hits.' },
    ],
    tip: 'The 3-action Heal burst is PF2e\'s most efficient healing: one 3-action spell heals every ally in range. Decide at the start of combat whether you\'re healing or offending that round.',
  },
  fighter: {
    actions: [
      { label: '1 action', desc: 'Strike — Fighters have the highest weapon proficiency in the game (Legendary by level 17).' },
      { label: '2 actions', desc: 'Power Attack (level 1 feat) — Strike for extra damage with a -2 MAP. Double Slice (two weapon) or Knockdown (felling blow). Use one of these every turn early on.' },
      { label: '3 actions', desc: 'Sudden Charge (2 actions — move 2× speed + Strike), or Intimidating Strike (Strike + Demoralize in one action at level 10).' },
      { label: 'Reaction', desc: 'Reactive Strike — hit any creature that uses a manipulate action, Stands Up, or moves through your reach. Extremely valuable; use it every time it triggers.' },
    ],
    tip: 'Your Reactive Strike is a free extra attack every single round. Build around triggering it: hold position, let enemies come to you, and punish movement.',
  },
  ranger: {
    actions: [
      { label: 'Hunt Prey', desc: '1 action at the start of each combat. Gives +2 to Perception checks and +2 to damage against your Prey for the whole fight. Never skip this.' },
      { label: 'Strike', desc: '1 action. Ranger\'s Flurry (level 1 optional rule) reduces MAP on second Strike by 2 — great for two-weapon and bow builds.' },
      { label: 'Spells', desc: 'You get a small list of ranger spells from level 1 if you take the Spellcasting archetype (or the Druidic Ranger subclass). Many have 1-minute durations — prep before combat.' },
      { label: 'Reaction', desc: 'No class reaction at level 1. Consider the Attack of Opportunity feat chain for melee rangers. Evasive Arrow (ranged) can redirect enemy fire at level 12.' },
    ],
    tip: 'Hunt Prey first, always. Your target has essentially -2 to -4 to their effective AC relative to you. If your Prey drops, use the free Hunt Prey reaction to pick a new one.',
  },
  rogue: {
    actions: [
      { label: 'Strike', desc: 'Sneak Attack (+1d6 per 2 rogue levels) triggers on any flat-footed target. Flanking (you and an ally both adjacent to the target) makes the target flat-footed.' },
      { label: 'Feint', desc: '1 action — Deception check vs target\'s Perception DC. On success: flat-footed to you until end of your next turn. Enables Sneak Attack solo.' },
      { label: 'Skill Actions', desc: 'Tumble Through (Acrobatics), Steal, Disable Device, Recall Knowledge — Rogues have more skill actions than anyone. Use them every turn.' },
      { label: 'Reaction', desc: 'Nimble Dodge (level 1) — add +2 to AC once per round as a Reaction when attacked. Better than many class reactions.' },
    ],
    tip: 'Sneak Attack requires flat-footed — arrange flanking at the start of every fight. Nimble Dodge protects you when flanking fails.',
  },
  sorcerer: {
    actions: [
      { label: 'Cast a Spell (2 actions)', desc: 'Most spells cost 2 actions. Haste is 2 actions on an ally — one of the best buffs in PF2e. Burning Hands, Fear, and Magic Missile are strong early options.' },
      { label: 'Cast a Spell (1 action)', desc: 'Cantrips and some focus spells can be cast for 1 action (with limitations). A cantrip each turn fills your 3rd action after moving and casting.' },
      { label: 'Blood Magic', desc: 'Your bloodline grants a Focus Pool and a Focus Spell. These replenish on 10-minute refocuses — use them freely.' },
      { label: 'Reaction', desc: 'No class reaction. Contingency (level 14 wizard spell) is the gold standard; until then, just move away from danger.' },
    ],
    tip: 'PF2e Sorcerers cast Spontaneously — pick fewer spells but cast them at any level. Choose flexible options like Fear (scales well) over highly situational picks.',
  },
  wizard: {
    actions: [
      { label: 'Cast a Spell (2 actions)', desc: 'Most spells cost 2 actions. At level 1, Grease (difficult terrain in an area), Magic Missile (no roll), and Fear are excellent. Save your best slots for fights that demand them.' },
      { label: 'Recall Knowledge', desc: '1 action — identify a creature\'s weaknesses, immunities, and key abilities. In PF2e this is vital; knowing a troll regenerates fire damage prevents a wasted round.' },
      { label: 'Cantrip', desc: 'Electric Arc hits two adjacent targets for 1d4+INT — the best damage cantrip in PF2e. Use it when you don\'t have a better target for a spell slot.' },
      { label: 'Reaction', desc: 'Counterspell — expend a prepared copy of a spell to cancel the same spell cast against you. Prepare Counterspell-worthy copies of any spell the enemy might cast.' },
    ],
    tip: 'Prepare Recall Knowledge on at least one party member (via the Identifying feature or ask your DM). In PF2e, knowing resistances and weaknesses is almost as valuable as a damage spell.',
  },
  monk: {
    actions: [
      { label: 'Flurry of Blows', desc: '1 action — Strike twice in 1 action (first at -0, second at -4 MAP instead of the usual -5). Your signature move — use it every turn.' },
      { label: 'Ki Strike', desc: '1 action (focus spell) — Strike + add d6 force damage. Replenish with 10-minute refocus.' },
      { label: 'Stunning Fist', desc: '1 action (focus, 1 focus point) — Strike + Constitution save. On failure: Stunned 1 (lost 1 action next turn). An action-economy nightmare for enemies.' },
      { label: 'Reaction', desc: 'Deflect Arrow (level 2 feat) — reduce ranged weapon damage by 1d10+DEX with a Reaction. Consider the Crane Stance for +1 AC Reaction-free.' },
    ],
    tip: 'Monks are an action-economy class — Flurry of Blows gives 2 Strikes for 1 action. Build your kit around actions that generate value: Flurry, a focus spell, and a positioning move.',
  },
};

export function getTurnGuide(system: string, classSlug: string): TurnGuide | undefined {
  const guides = system === 'pf2e' ? GUIDE_PF2E : GUIDE_5E;
  return guides[classSlug.toLowerCase()];
}

/** Generic guide when no class-specific data is available. */
export function getGenericGuide(system: string): TurnGuide {
  if (system === 'pf2e') {
    return {
      actions: [
        { label: '1 action', desc: 'Strike — make one weapon or unarmed attack.' },
        { label: '2 actions', desc: 'Most spells, Raise Shield, or a 2-action activity from your class.' },
        { label: '3 actions', desc: 'Move and still act: 1 Stride (1 action) + 2-action activity, or use a 3-action spell.' },
        { label: 'Reaction', desc: 'Attack of Opportunity (if you have it), Block (with a raised shield), or your class-specific reaction.' },
      ],
      tip: 'PF2e gives you 3 actions and 1 reaction per round. Actions carry a Multiple Attack Penalty (MAP): -5 on the 2nd Strike, -10 on the 3rd.',
    };
  }
  return {
    actions: [
      { label: 'Action', desc: 'Attack, Cast a Spell, Dash, Dodge, Help, or Use an Object.' },
      { label: 'Bonus Action', desc: 'Class or spell abilities that cost a Bonus Action (e.g. second-wind, certain spells). You only have 1 per turn.' },
      { label: 'Movement', desc: 'Move up to your speed at any point during your turn. You can split it before and after your Action.' },
      { label: 'Reaction', desc: 'Opportunity Attack when an enemy leaves your reach, or a spell like Shield / Counterspell if you have one prepared.' },
    ],
    tip: 'You get one Action, one possible Bonus Action, movement, and a Reaction each round. Reactions are used on other people\'s turns.',
  };
}
