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

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

test.describe('Public Survey Form', () => {
  test('invalid token page loads', async ({ page }) => {
    const response = await page.goto('/surveys/invalid/');
    expect(response?.status()).toBe(200);
    await expect(page.locator('body')).toContainText(/invalid|token/i);
  });

  test('survey with expired or invalid token shows error', async ({ page }) => {
    const response = await page.goto('/surveys/s/invalid-token-xyz/');
    expect(response?.status()).toBeLessThan(500);
    await page.waitForLoadState('networkidle');

    const hasContent = await page.locator('body').textContent().then(t => t!.length > 0);
    expect(hasContent).toBeTruthy();
  });
});

test.describe('Survey Internal Pages (Auth Required)', () => {
  test('unauthenticated access to survey templates redirects to login', async ({ page }) => {
    await page.goto('/surveys/templates/');
    await page.waitForURL(/\/accounts\/login\/?/, { timeout: 10000 });
    await expect(page.locator('#email')).toBeVisible();
  });

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

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