import {
  campaignsRepo,
  charactersRepo,
  encountersRepo,
  notesRepo,
  npcsRepo,
  questsRepo,
} from '@/lib/db/repositories';
import { addCombatant } from '@/lib/combat/engine';
import { newId } from '@/lib/ids';

/** Seed a small demo D&D 5e campaign so new users have something to explore. */
export async function seedSampleCampaign(): Promise<string> {
  const campaign = await campaignsRepo.create({
    name: 'Sample: Lost Mine',
    system: '5e',
    description: 'A demo campaign to explore the app. Safe to delete.',
  });

  const hero = await charactersRepo.create(campaign.id, {
    system: '5e', name: 'Lia the Brave', kind: 'pc', ancestry: 'Half-Elf', className: 'Fighter', level: 3,
  });
  await charactersRepo.update(hero.id, {
    abilities: { str: 16, dex: 14, con: 15, int: 10, wis: 12, cha: 8 },
    hp: { current: 28, max: 28, temp: 0 },
    skillRanks: { athletics: 'trained', perception: 'trained' },
    saveRanks: { str: 'trained', con: 'trained' },
  });
  await charactersRepo.create(campaign.id, {
    system: '5e', name: 'Pip Underbough', kind: 'pc', ancestry: 'Halfling', className: 'Rogue', level: 3,
  });

  await npcsRepo.create(campaign.id, 'Sildar Hallwinter');
  await questsRepo.create(campaign.id, 'Find Gundren Rockseeker');
  await notesRepo.create(campaign.id, 'Phandalin');

  const enc = await encountersRepo.create(campaign.id, 'Goblin Ambush');
  const withGoblins = [1, 2, 3].reduce(
    (e, _) =>
      addCombatant(e, {
        id: newId(), name: 'Goblin', kind: 'monster', initiative: 12, initBonus: 2, ac: 15,
        hp: { current: 7, max: 7, temp: 0 }, conditions: [], notes: '', cr: 0.25,
      }),
    enc,
  );
  await encountersRepo.save(withGoblins);

  return campaign.id;
}
