/**
 * Authentication Setup for Accessibility Tests
 *
 * Handles authentication once and saves session state so
 * subsequent tests can reuse it without logging in again.
 */

import { test as setup, expect } from '@playwright/test';
import { mkdirSync } from 'fs';
import path from 'path';

// Fallbacks for convenience — these are non-sensitive test-only defaults that
// match the dev/CI fixture credentials, so developers can run the suite without
// extra setup.  Override via env vars when pointing at a different test server.
const TEST_USERNAME = process.env.TEST_USERNAME || 'test_admin';
const TEST_PASSWORD = process.env.TEST_PASSWORD || 'testpass123';

const authDir = path.join(import.meta.dirname, '.auth');
const authFile = path.join(authDir, 'user.json');

// Ensure .auth directory exists before storageState() call
mkdirSync(authDir, { recursive: true });

// Login on a cold Docker boot can take >30s — registration creates an
// encrypted SQLCipher DB, derives a key from the password, and imports
// 500+ default settings. Give the setup task plenty of headroom; the
// inner waitForURL still bounds the actual wait.
setup.setTimeout(180_000);

setup('authenticate', async ({ page }) => {
  await page.goto('/auth/login', { waitUntil: 'domcontentloaded' });

  await page.fill('input[name="username"]', TEST_USERNAME);
  await page.fill('input[name="password"]', TEST_PASSWORD);

  await Promise.all([
    page.waitForURL('/', { timeout: 120_000 }),
    page.click('button[type="submit"]'),
  ]);

  await expect(page.locator('.ldr-user-info')).toBeVisible({ timeout: 30_000 });

  await page.context().storageState({ path: authFile });
});
