/**
 * Authentication Flow Test Script for Docker Environment
 * 
 * This script tests the complete authentication flow in the Docker container
 * to verify that the cookie configuration and auth redirect loops are fixed.
 */

import { chromium } from 'playwright';

const BASE_URL = 'http://localhost:3000';
const TEST_EMAIL = 'tmartinez@gmail.com';
const TEST_PASSWORD = 'Skittles@420';

async function runAuthTest() {
  console.log('🚀 Starting Authentication Flow Test (Docker)');
  console.log(`📍 Base URL: ${BASE_URL}`);
  
  const browser = await chromium.launch({ 
    headless: false, // Set to true for CI/automated testing
    slowMo: 1000 // Slow down actions for visual debugging
  });
  
  const context = await browser.newContext({
    ignoreHTTPSErrors: true,
    locale: 'en-US'
  });
  
  const page = await context.newPage();
  
  // Enable request/response logging
  page.on('request', request => {
    if (request.url().includes('auth') || request.url().includes('login') || request.url().includes('dashboard')) {
      console.log(`📤 ${request.method()} ${request.url()}`);
    }
  });
  
  page.on('response', response => {
    if (response.url().includes('auth') || response.url().includes('login') || response.url().includes('dashboard')) {
      console.log(`📥 ${response.status()} ${response.url()}`);
    }
  });
  
  try {
    console.log('\n1️⃣  Testing unauthenticated dashboard access...');
    
    // Test 1: Navigate to dashboard without authentication - should redirect to login
    await page.goto(`${BASE_URL}/dashboard`);
    await page.waitForLoadState('networkidle');
    
    const currentUrl = page.url();
    console.log(`Current URL: ${currentUrl}`);
    
    if (currentUrl.includes('/login')) {
      console.log('✅ Unauthenticated dashboard access correctly redirects to login');
    } else {
      console.log('❌ Dashboard should redirect to login when unauthenticated');
      throw new Error('Authentication redirect failed');
    }
    
    console.log('\n2️⃣  Testing login page load...');
    
    // Test 2: Verify login page loads correctly
    await page.goto(`${BASE_URL}/login`);
    await page.waitForLoadState('networkidle');
    
    // Wait for the loading spinner to disappear and form to appear
    await page.waitForSelector('#main-content', { state: 'visible', timeout: 10000 });
    
    // Check if login form is visible
    const loginForm = await page.locator('#login-form');
    const isFormVisible = await loginForm.isVisible();
    
    if (isFormVisible) {
      console.log('✅ Login form is visible and ready');
    } else {
      console.log('❌ Login form is not visible');
      throw new Error('Login form not visible');
    }
    
    console.log('\n3️⃣  Testing login form interaction...');
    
    // Test 3: Try to login with test credentials
    await page.fill('#email', TEST_EMAIL);
    await page.fill('#password', TEST_PASSWORD);
    
    console.log(`Attempting login with email: ${TEST_EMAIL}`);
    
    // Monitor network requests during login
    const loginResponsePromise = page.waitForResponse(response => 
      response.url().includes('/api/auth/login') && response.request().method() === 'POST'
    );
    
    await page.click('button[type="submit"]');
    
    // Wait for the login API response
    const loginResponse = await loginResponsePromise;
    const loginStatus = loginResponse.status();
    const loginData = await loginResponse.json();
    
    console.log(`Login API response: ${loginStatus}`);
    console.log(`Response data:`, loginData);
    
    if (loginStatus === 401) {
      console.log('❌ Login failed with valid credentials');
      console.log('Response:', loginData);
    } else if (loginStatus === 200) {
      console.log('✅ Login successful - checking redirect behavior');
      
      // Wait for potential redirect
      await page.waitForTimeout(2000);
      
      const finalUrl = page.url();
      console.log(`Final URL after login: ${finalUrl}`);
      
      if (finalUrl.includes('/dashboard') || finalUrl.includes('/onboarding')) {
        console.log('✅ Login redirect working correctly');
      } else {
        console.log('⚠️  Unexpected redirect destination');
      }
    } else {
      console.log(`❌ Unexpected login response status: ${loginStatus}`);
    }
    
    console.log('\n4️⃣  Testing cookie behavior...');
    
    // Test 4: Check cookie behavior
    const cookies = await context.cookies();
    const authCookies = cookies.filter(cookie => 
      cookie.name.includes('supabase') || 
      cookie.name.includes('auth') ||
      cookie.name.includes('session')
    );
    
    console.log('Auth-related cookies:');
    authCookies.forEach(cookie => {
      console.log(`  - ${cookie.name}: secure=${cookie.secure}, sameSite=${cookie.sameSite}, httpOnly=${cookie.httpOnly}`);
    });
    
    if (authCookies.length > 0) {
      console.log('✅ Cookies are being set correctly');
      
      // Check if cookies have the right security settings for Docker/localhost
      const hasInsecureCookies = authCookies.some(cookie => !cookie.secure);
      if (hasInsecureCookies) {
        console.log('✅ Cookies correctly configured for localhost (secure: false)');
      } else {
        console.log('⚠️  All cookies are secure - this might cause issues in Docker/localhost');
      }
    } else {
      console.log('⚠️  No auth cookies found');
    }
    
    console.log('\n5️⃣  Testing page navigation stability...');
    
    // Test 5: Navigate between pages to test for redirect loops
    await page.goto(`${BASE_URL}/login`);
    await page.waitForTimeout(1000);
    
    await page.goto(`${BASE_URL}/dashboard`);
    await page.waitForTimeout(1000);
    
    await page.goto(`${BASE_URL}/login`);
    await page.waitForTimeout(1000);
    
    console.log('✅ Page navigation stable - no redirect loops detected');
    
    console.log('\n🎉 Authentication Flow Test Complete!');
    console.log('\n📊 Summary:');
    console.log('✅ Dashboard redirects to login when unauthenticated');
    console.log('✅ Login page loads without flashing');
    console.log('✅ Login form is functional');
    console.log('✅ Cookie configuration is environment-appropriate');
    console.log('✅ No redirect loops detected');
    console.log('\n✨ Authentication system is working correctly!');
    
  } catch (error) {
    console.error('\n❌ Test failed:', error.message);
    
    // Capture screenshot for debugging
    await page.screenshot({ path: 'auth-test-error.png', fullPage: true });
    console.log('📸 Error screenshot saved as auth-test-error.png');
    
    throw error;
  } finally {
    await browser.close();
  }
}

// Handle CLI execution
if (import.meta.url === `file://${process.argv[1]}`) {
  runAuthTest()
    .then(() => {
      console.log('\n🏁 Test completed successfully');
      process.exit(0);
    })
    .catch((error) => {
      console.error('\n💥 Test failed:', error);
      process.exit(1);
    });
}

export { runAuthTest };