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('assistant flags a bloodied PC and builds an encounter', async ({ page }) => {
  // Sample campaign gives us PCs + monsters
  await page.getByLabel('Settings').click();
  await page.getByRole('button', { name: 'Load sample campaign' }).click();
  await expect(page.getByRole('heading', { name: 'Sample: Lost Mine' })).toBeVisible();

  // Wound a PC so the assistant has something to flag
  await page.getByLabel('Primary').getByRole('link', { name: 'Characters' }).click();
  await page.getByRole('link', { name: /Lia the Brave/ }).click();
  await page.getByLabel('Current HP').fill('3');
  await page.waitForTimeout(450); // let autosave flush

  // Assistant shows a resource suggestion
  await page.getByLabel('Primary').getByRole('link', { name: 'Dashboard' }).click();
  await page.getByLabel('Primary').getByRole('link', { name: 'Assistant' }).click();
  await expect(page.getByRole('heading', { name: 'Assistant' })).toBeVisible();
  await expect(page.getByText(/Lia the Brave is bloodied/)).toBeVisible();

  // Build a Medium encounter → lands in combat with monsters
  await page.getByRole('button', { name: 'Medium' }).click();
  await expect(page.getByRole('button', { name: 'Start combat' })).toBeVisible();
});
