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('maps: upload, place a token, toggle fog', async ({ page }) => {
  await page.getByRole('button', { name: '+ New campaign' }).first().click();
  await page.locator('input[data-autofocus]').fill('VTT');
  await page.getByRole('button', { name: 'Create' }).click();
  await page.getByRole('link', { name: 'Dashboard' }).click();
  await page.getByRole('link', { name: 'Maps' }).click();

  // Upload an image into the hidden file input
  await page.locator('input[type="file"]').setInputFiles('public/pwa-512x512.png');

  // Editor loads (Move tool button present)
  await expect(page.getByRole('button', { name: 'Move' })).toBeVisible();

  // Add a token
  await page.getByRole('button', { name: '+ Token' }).click();
  await expect(page.getByTitle(/double-click to remove/)).toBeVisible();

  // Toggle fog on (no crash)
  await page.getByText('Fog', { exact: true }).locator('input').check();
  await expect(page.getByRole('button', { name: 'Reveal all' })).toBeVisible();
});
