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

test.use({ storageState: { cookies: [], origins: [] } });

test.describe.configure({ mode: 'serial' });

test.describe('Public Pages - No Auth Required', () => {
  test('health check endpoint returns OK', async ({ request }) => {
    const response = await request.get('/health/');
    expect(response.status()).toBe(200);
  });

  test('public submission landing page loads', async ({ page }) => {
    await page.goto('/core/public/submit/');
    await page.waitForSelector('.selection-card');
    await expect(page.locator('.selection-card')).toHaveCount(3);
    await expect(page.locator('.selection-card[data-type="complaint"]')).toBeVisible();
    await expect(page.locator('.selection-card[data-type="observation"]')).toBeVisible();
    await expect(page.locator('.selection-card[data-type="inquiry"]')).toBeVisible();
  });

  test('login page loads with form fields', async ({ page }) => {
    await page.goto('/accounts/login/');
    await page.waitForSelector('#email');
    await expect(page.locator('#email')).toBeVisible();
    await expect(page.locator('#password')).toBeVisible();
    await expect(page.locator('button[type="submit"]')).toBeVisible();
    await expect(page.locator('h1')).toContainText('Welcome to PX360');
  });

  test('password reset page loads', async ({ page }) => {
    await page.goto('/accounts/password/reset/');
    await page.waitForSelector('#id_email');
    await expect(page.locator('#id_email')).toBeVisible();
    await expect(page.locator('button[type="submit"]')).toContainText('Send Reset Link');
  });

  test('public complaint track page loads', async ({ page }) => {
    await page.goto('/complaints/public/track/');
    await page.waitForSelector('input[name="reference_number"]');
    await expect(page.locator('input[name="reference_number"]')).toBeVisible();
    await expect(page.locator('button[type="submit"]')).toContainText('Track Status');
  });

  test('public observation track page loads', async ({ page }) => {
    await page.goto('/observations/track/');
    await page.waitForSelector('input[name="tracking_code"]');
    await expect(page.locator('input[name="tracking_code"]')).toBeVisible();
    await expect(page.locator('button[type="submit"]')).toContainText('Track');
  });

  test('public observation submit page loads', async ({ page }) => {
    await page.goto('/observations/new/');
    await page.waitForSelector('textarea[name="description"]');
    await expect(page.locator('textarea[name="description"]')).toBeVisible();
    await expect(page.locator('.severity-option')).toHaveCount(0);
  });

  test('unauthenticated access redirects to login', async ({ page }) => {
    await page.goto('/');
    await page.waitForURL(/\/accounts\/login\/?/, { timeout: 10000 });
    await expect(page.locator('#email')).toBeVisible();
  });

  test('API docs page is accessible', async ({ page }) => {
    const response = await page.goto('/api/docs/');
    expect(response?.status()).toBe(200);
  });
});
