import { test, expect } from '@playwright/test';

test.beforeEach(async ({ page }) => {
  await page.goto('/');
  await page.evaluate(async () => {
    indexedDB.deleteDatabase('ttrpg-manager');
    localStorage.clear();
  });
  await page.reload();
});

test('creation wizard builds a complete character (HP, skills, spells)', async ({ page }) => {
  await page.getByRole('button', { name: '+ New campaign' }).first().click();
  await page.locator('input[data-autofocus]').fill('Wizard Camp');
  await page.getByRole('button', { name: 'Create' }).click();
  await page.getByRole('link', { name: 'Characters' }).click();
  await page.getByRole('button', { name: '+ New character' }).first().click();

  // Class step: a level-3 Wizard (a caster, so a Spells step appears).
  await page.getByLabel('Name').fill('Mira');
  await page.getByLabel('Level', { exact: true }).fill('3');
  await page.getByTestId('class-card').filter({ hasText: 'Wizard' }).click();
  await page.getByRole('button', { name: 'Next' }).click(); // → Origin
  await page.getByRole('button', { name: 'Next' }).click(); // → Abilities (standard array valid)
  await page.getByRole('button', { name: 'Next' }).click(); // → Skills

  const boxes = page.locator('input[type="checkbox"]');
  await boxes.nth(0).check();
  await boxes.nth(1).check();
  await page.getByRole('button', { name: 'Next' }).click(); // → Spells (Wizard is a caster)

  await expect(page.getByPlaceholder('Search spells…')).toBeVisible();
  await page.getByRole('button', { name: 'Next' }).click(); // → Review

  await expect(page.getByText('Max HP')).toBeVisible();
  await expect(page.getByText('Spell slots')).toBeVisible();
  await page.getByRole('button', { name: 'Create character' }).click();

  // Lands on the sheet with real derived HP, not the old 1/1.
  await expect(page.getByText('Hit Points')).toBeVisible();
});
