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

// Each test starts from a clean IndexedDB so runs are deterministic.
test.beforeEach(async ({ page }) => {
  await page.goto('/');
  await page.evaluate(async () => {
    indexedDB.deleteDatabase('ttrpg-manager');
    localStorage.clear();
  });
  await page.reload();
});

test('full core flow: campaign → character → dice → combat → compendium', async ({ page }) => {
  // --- Campaign ---
  await expect(page.getByRole('heading', { name: 'Campaigns' })).toBeVisible();
  await page.getByRole('button', { name: '+ New campaign' }).first().click();
  await page.getByLabel('').first().waitFor(); // modal open
  await page.locator('input[data-autofocus]').fill('Curse of Strahd');
  await page.getByRole('button', { name: 'Create' }).click();

  // Card appears and becomes active
  await expect(page.getByRole('heading', { name: 'Curse of Strahd' })).toBeVisible();

  // --- Character ---
  await page.getByRole('link', { name: 'Characters' }).click();
  await expect(page.getByRole('heading', { name: 'Characters' })).toBeVisible();
  await createCharacter(page, 'Ireena');

  // On the sheet: set STR to 16 and expect +3 modifier
  await page.getByLabel('STR score').fill('16');
  await expect(page.getByText('+3').first()).toBeVisible();

  // --- Dice ---
  await page.getByRole('link', { name: 'Dice' }).click();
  await page.getByRole('button', { name: 'Roll' }).click();
  // a result total renders (1..20 for default 1d20)
  await expect(page.locator('text=/= \\d+/')).toBeVisible();

  // --- Combat ---
  await page.getByRole('link', { name: 'Combat' }).click();
  await page.getByRole('button', { name: '+ New encounter' }).first().click();
  await page.locator('input[data-autofocus]').fill('Ambush');
  await page.getByRole('button', { name: 'Create' }).click();
  await page.getByLabel('Name').fill('Goblin');
  await page.getByRole('button', { name: 'Add', exact: true }).click();
  await expect(page.getByText('Goblin')).toBeVisible();
  await page.getByRole('button', { name: 'Start combat' }).click();
  await expect(page.getByRole('button', { name: 'End' })).toBeVisible(); // combat active

  // --- Compendium ---
  await page.getByRole('link', { name: 'Compendium' }).click();
  await expect(page.getByText(/results/)).toBeVisible();
  await page.getByPlaceholder('Search bestiary…').fill('goblin');
  await page.getByRole('button', { name: /^Goblin/ }).first().click();
  await expect(page.getByRole('heading', { name: 'Goblin', exact: true })).toBeVisible();
});
