import type { SystemId } from '@/lib/rules';
import type { BuildRoute, BuildStep } from './prompts';

/**
 * Deterministic, system-aware build-route advice used when no LLM is configured.
 * Generic archetypes keyed off the system's advancement vocabulary — not a
 * substitute for the LLM's class-specific depth, but always available offline.
 */

const ARCHETYPES: { title: string; summary: string; playstyle: string }[] = [
  { title: 'Offense', summary: 'Lean into raw damage and accuracy.', playstyle: 'Strike first, strike hard' },
  { title: 'Defense', summary: 'Shore up survivability and staying power.', playstyle: 'Outlast the fight' },
  { title: 'Utility', summary: 'Broaden what you can do outside combat.', playstyle: 'A tool for every problem' },
  { title: 'Support', summary: "Amplify and protect your allies.", playstyle: 'Make the party better' },
];

function vocab(system: SystemId) {
  return system === 'pf2e'
    ? { boost: 'ability boosts', feat: 'a class or skill feat', train: 'a skill increase' }
    : { boost: 'an Ability Score Improvement', feat: 'a feat', train: 'proficiency or expertise' };
}

export function deterministicRoutes(_system: SystemId, className: string): BuildRoute[] {
  const cls = className || 'character';
  return ARCHETYPES.map((a) => ({
    title: a.title,
    summary: `${a.summary} (${cls})`,
    playstyle: a.playstyle,
  }));
}

export function deterministicSteps(system: SystemId, className: string, routeTitle: string, nextLevel: number): BuildStep[] {
  const v = vocab(system);
  const cls = className || 'character';
  const byRoute: Record<string, BuildStep[]> = {
    Offense: [
      { label: 'Raise your attack stat', detail: `Put ${v.boost} into your primary attack ability.` },
      { label: 'Pick a damage feat', detail: `Take ${v.feat} that adds damage or extra attacks for a ${cls}.` },
      { label: 'Sharpen accuracy', detail: `Spend ${v.train} on your main weapon or attack proficiency.` },
    ],
    Defense: [
      { label: 'Boost durability', detail: `Invest ${v.boost} in Constitution (and Dexterity for AC).` },
      { label: 'Take a defensive feat', detail: `Choose ${v.feat} that improves AC, saves, or damage mitigation.` },
      { label: 'Train a key save', detail: `Use ${v.train} on your weakest defense.` },
    ],
    Utility: [
      { label: 'Expand your skills', detail: `Apply ${v.train} to skills that fit your concept.` },
      { label: 'Pick a versatile feat', detail: `Take ${v.feat} that grants new options or actions.` },
      { label: 'Round out abilities', detail: `Use ${v.boost} to cover a glaring weakness.` },
    ],
    Support: [
      { label: 'Improve your key ability', detail: `Put ${v.boost} into your spellcasting or support ability.` },
      { label: 'Take a team feat', detail: `Choose ${v.feat} that buffs, heals, or protects allies.` },
      { label: 'Train coordination skills', detail: `Spend ${v.train} on social or healing-adjacent skills.` },
    ],
  };
  const steps = byRoute[routeTitle] ?? [
    { label: 'Pick complementary options', detail: `Choose ${v.feat} and ${v.boost} that support "${routeTitle}".` },
    { label: 'Train toward the goal', detail: `Apply ${v.train} where it advances this plan.` },
  ];
  return [{ label: `Level ${nextLevel} focus`, detail: `Build toward "${routeTitle}" as a ${cls}.` }, ...steps];
}
