/**
 * Authentication Setup for Playwright Tests
 *
 * This file handles authentication once and saves the session state
 * so subsequent tests can reuse it without logging in again.
 * This prevents rate limiting issues from multiple login attempts.
 */

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(__dirname, '../.auth');
const authFile = path.join(authDir, 'user.json');

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

setup('authenticate', async ({ page }) => {
  // Navigate to login page
  await page.goto('/auth/login');

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

  // Submit the form
  await page.click('button[type="submit"]');

  // Wait for navigation to complete (authentication successful)
  await page.waitForURL('/', { timeout: 60000 });

  // Verify we're logged in by checking for user info display
  await expect(page.locator('.ldr-user-info')).toBeVisible();

  // Save the authentication state
  await page.context().storageState({ path: authFile });
});
