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('level-up advisor offers routes and expands a chosen one (deterministic)', async ({ page }) => {
  await page.getByLabel('Settings').click();
  await page.getByRole('button', { name: 'Load sample campaign' }).click();
  await expect(page.getByRole('heading', { name: 'Sample: Lost Mine' })).toBeVisible();

  // Open Lia (level 3 Fighter) and start a level-up.
  await page.getByLabel('Primary').getByRole('link', { name: 'Characters' }).click();
  await page.getByRole('link', { name: /Lia the Brave/ }).click();
  await page.getByRole('button', { name: 'Level up' }).click();

  const advisor = page.getByTestId('levelup-advisor');
  await page.getByRole('button', { name: 'Suggest build routes' }).click();
  await expect(advisor).toBeVisible();

  // Four system-aware routes; choose one and see concrete steps for the next level.
  await expect(advisor.getByText('Offense')).toBeVisible();
  await expect(advisor.getByRole('button', { name: 'Choose' })).toHaveCount(4);
  await advisor.locator('li').filter({ hasText: 'Offense' }).getByRole('button', { name: 'Choose' }).click();
  await expect(advisor.getByText('Level 4 focus')).toBeVisible();
});

test('assistant page renders the campaign insights section', async ({ page }) => {
  await page.getByLabel('Settings').click();
  await page.getByRole('button', { name: 'Load sample campaign' }).click();
  await page.getByRole('link', { name: 'Assistant' }).click();
  await expect(page.getByRole('heading', { name: 'Campaign insights' })).toBeVisible();
  // Sample has a single encounter → not enough for a trend yet.
  await expect(page.getByText(/No trends detected yet/)).toBeVisible();
});
