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

test.describe('Authentication', () => {
  test.beforeEach(async ({ page }) => {
    // Navigate to home page before each test
    await page.goto('/');
  });

  test('should navigate to sign in page', async ({ page }) => {
    // Click sign in link or button
    const signInLink = page.getByRole('link', { name: /sign in/i }).or(page.getByText(/sign in/i)).first();
    if (await signInLink.isVisible()) {
      await signInLink.click();
      await expect(page).toHaveURL(/.*auth.*/i);
    }
  });

  test('should sign in with valid credentials', async ({ page }) => {
    // Navigate to auth page
    await page.goto('/auth');
    
    // Fill sign in form (adjust selectors based on your actual form)
    const emailInput = page.locator('input[type="email"]').or(page.locator('input[name="email"]')).first();
    const passwordInput = page.locator('input[type="password"]').first();
    const submitButton = page.getByRole('button', { name: /sign in/i }).or(page.locator('button[type="submit"]')).first();
    
    if (await emailInput.isVisible()) {
      await emailInput.fill('test@example.com');
      await passwordInput.fill('testpassword123');
      await submitButton.click();
      
      // Wait for redirect after successful sign in
      await page.waitForURL(/.*app.*/, { timeout: 10000 });
      
      // Verify user is logged in (check for user menu or profile)
      const userMenu = page.locator('[data-testid="user-menu"]').or(page.getByText(/test@example.com/i)).first();
      await expect(userMenu).toBeVisible({ timeout: 5000 });
    }
  });

  test('should show error with invalid credentials', async ({ page }) => {
    await page.goto('/auth');
    
    const emailInput = page.locator('input[type="email"]').or(page.locator('input[name="email"]')).first();
    const passwordInput = page.locator('input[type="password"]').first();
    const submitButton = page.getByRole('button', { name: /sign in/i }).or(page.locator('button[type="submit"]')).first();
    
    if (await emailInput.isVisible()) {
      await emailInput.fill('invalid@example.com');
      await passwordInput.fill('wrongpassword');
      await submitButton.click();
      
      // Wait for error message
      const errorMessage = page.getByText(/invalid|error|incorrect/i).first();
      await expect(errorMessage).toBeVisible({ timeout: 5000 });
    }
  });

  test('should redirect to sign in when accessing protected route', async ({ page }) => {
    // Try to access protected route (e.g., /app)
    await page.goto('/app');
    
    // Should redirect to auth page
    await expect(page).toHaveURL(/.*auth.*/, { timeout: 5000 });
  });

  test('should sign out successfully', async ({ page }) => {
    // First sign in
    await page.goto('/auth');
    const emailInput = page.locator('input[type="email"]').or(page.locator('input[name="email"]')).first();
    const passwordInput = page.locator('input[type="password"]').first();
    const submitButton = page.getByRole('button', { name: /sign in/i }).or(page.locator('button[type="submit"]')).first();
    
    if (await emailInput.isVisible()) {
      await emailInput.fill('test@example.com');
      await passwordInput.fill('testpassword123');
      await submitButton.click();
      await page.waitForURL(/.*app.*/, { timeout: 10000 });
      
      // Click sign out
      const signOutButton = page.getByRole('button', { name: /sign out/i }).or(page.getByText(/sign out/i)).first();
      if (await signOutButton.isVisible()) {
        await signOutButton.click();
        
        // Should redirect to landing page
        await expect(page).toHaveURL(/.*\/$/, { timeout: 5000 });
      }
    }
  });
});

