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

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

test('v2 surfaces: feats tracking, multiclass slot calculator, signals bell, World nav', async ({ page }) => {
  await page.getByRole('button', { name: '+ New campaign' }).first().click();
  await page.locator('input[data-autofocus]').fill('V2 Test');
  await page.getByRole('button', { name: 'Create' }).click();
  await page.getByLabel('Primary').getByRole('link', { name: 'Characters' }).click();
  await createCharacter(page, 'Vex');

  // Feats: add one and confirm it becomes a tracked record.
  await page.getByPlaceholder(/Great Weapon Master/).fill('Sentinel');
  await page.getByPlaceholder(/Great Weapon Master/).press('Enter');
  await expect(page.getByLabel('Feat name')).toHaveValue('Sentinel');

  // Multiclass slot calculator: 5 full-caster levels → standard 4/3/2 table.
  await page.getByText('Multiclass slot calculator').click();
  await page.getByLabel('Full caster levels').fill('5');
  await page.getByRole('button', { name: 'Apply slots' }).click();
  await expect(page.getByLabel('Level 1 max slots')).toHaveValue('4');
  await expect(page.getByLabel('Level 3 max slots')).toHaveValue('2');

  // Ambient Signals bell opens and renders its panel.
  await page.getByRole('button', { name: /Signals/ }).click();
  await expect(page.getByText('Signals', { exact: true })).toBeVisible();
  await page.keyboard.press('Escape');

  // The previously-orphaned routes are now reachable from the rail's World group.
  await page.getByLabel('Primary').getByRole('link', { name: 'Notes' }).click();
  await expect(page).toHaveURL(/\/notes/);
  await page.getByLabel('Primary').getByRole('link', { name: 'Quests' }).click();
  await expect(page).toHaveURL(/\/quests/);
});
