import { expect, test } from '@playwright/test'

test.beforeEach(async ({ page }) => {
  await page.goto('/')
  await page.evaluate(() => localStorage.clear())
  await page.reload()
})

test('core planning journey persists after reload', async ({ page }) => {
  await expect(page.getByRole('heading', { name: /good progress starts/i })).toBeVisible()
  await page.getByRole('button', { name: 'Learning goals' }).click()
  await page.getByRole('button', { name: 'New goal' }).click()
  await page.getByLabel('Goal title').fill('Research progressive enhancement')
  await page.getByLabel('Category').fill('Web platform')
  await page.getByLabel('Target date').fill('2026-12-01')
  await page.getByRole('button', { name: 'Save goal' }).click()
  await expect(page.getByRole('heading', { name: 'Research progressive enhancement' })).toBeVisible()
  await page.reload()
  await page.getByRole('button', { name: 'Learning goals' }).click()
  await expect(page.getByRole('heading', { name: 'Research progressive enhancement' })).toBeVisible()
})

test('task search and status interaction work', async ({ page }) => {
  await page.getByLabel('Primary navigation').getByRole('button', { name: /All tasks/ }).click()
  await page.getByLabel('Search tasks').fill('Firefox')
  const task = page.locator('.task-row').filter({ hasText: 'Run Firefox regression suite' })
  await expect(task).toBeVisible()
  await task.getByRole('button', { name: /complete run firefox/i }).click()
  await expect(task).toHaveClass(/complete/)
})

test('required viewport has no page-level horizontal overflow', async ({ page }) => {
  const dimensions = await page.evaluate(() => ({ scrollWidth: document.documentElement.scrollWidth, clientWidth: document.documentElement.clientWidth }))
  expect(dimensions.scrollWidth).toBeLessThanOrEqual(dimensions.clientWidth)
  await expect(page.locator('.topbar')).toBeVisible()
  await expect(page.locator('.dashboard-grid')).toBeVisible()
})
