/**
 * Actual User Login Test
 *
 * Tests login with the real user credentials provided
 * Email: gara2222ge@support.com
 * Password: eGj&tk!@%T8sFMOq#3QP
 *
 * This test validates that the user can successfully:
 * 1. Log in with their credentials
 * 2. Access the dashboard
 * 3. See their profile information
 */

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

const BASE_URL = 'http://localhost:9000';
const ACTUAL_USER_EMAIL = 'gara2222ge@support.com';
const ACTUAL_USER_PASSWORD = 'eGj&tk!@%T8sFMOq#3QP';
const ACTUAL_USER_NAME = 'Mohammad';

test.describe('[ACTUAL USER] Login Flow Validation', () => {
  test('[CRITICAL] Should log in with actual user credentials and access dashboard', async ({ page }) => {
    console.log('=== Testing Login with Actual User Credentials ===');
    console.log(`Email: ${ACTUAL_USER_EMAIL}`);
    console.log(`Name: ${ACTUAL_USER_NAME}`);
    console.log('');

    // Navigate to login page
    await page.goto(`${BASE_URL}/en/login`, { waitUntil: 'networkidle' });
    console.log('✅ Navigated to login page');

    // Verify page loaded correctly
    await expect(page.locator('h2')).toContainText(/Log in|Login/i);
    console.log('✅ Login page loaded');

    // Fill in credentials
    console.log('Filling in email...');
    await page.locator('input[type="text"]').fill(ACTUAL_USER_EMAIL);

    console.log('Filling in password...');
    await page.locator('input[type="password"]').fill(ACTUAL_USER_PASSWORD);

    console.log('Clicking submit button...');

    // Submit the form and wait for navigation
    await Promise.all([
      page.waitForURL('**/dashboard', { timeout: 20000 }), // Wait for dashboard URL
      page.locator('button[type="submit"]').click() // Click submit
    ]);

    console.log('✅ Navigation completed');

    // Verify we're on dashboard
    const currentUrl = page.url();
    console.log(`Current URL: ${currentUrl}`);

    // Take screenshot
    await page.screenshot({ path: 'actual-user-login-result.png', fullPage: true });
    console.log('Screenshot saved: actual-user-login-result.png');

    // Verify URL is dashboard
    await expect(page).toHaveURL(/\/en\/dashboard/);
    console.log('✅ On dashboard page');

    // Wait for page to fully load
    await page.waitForLoadState('networkidle', { timeout: 5000 }).catch(() => {});

    console.log('');
    console.log('✅✅✅ LOGIN SUCCESSFUL - USER CAN ACCESS DASHBOARD ✅✅✅');
  });

  test('Should handle login from Arabic page', async ({ page }) => {
    console.log('=== Testing Login from Arabic Page ===');

    // Navigate to Arabic login page
    await page.goto(`${BASE_URL}/ar/login`);
    console.log('✅ Navigated to Arabic login page');

    // Verify RTL layout
    const formDir = await page.locator('form').getAttribute('dir');
    expect(formDir).toBe('rtl');
    console.log('✅ RTL layout confirmed');

    // Fill credentials
    await page.locator('input[type="text"]').fill(ACTUAL_USER_EMAIL);
    await page.locator('input[type="password"]').fill(ACTUAL_USER_PASSWORD);

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

    // Should redirect to Arabic dashboard
    await expect(page).toHaveURL(/\/ar\/dashboard/, { timeout: 10000 });
    console.log('✅ Redirected to Arabic dashboard');

    // Verify content
    await expect(page.locator(`text=${ACTUAL_USER_NAME}`)).toBeVisible({ timeout: 5000 });
    console.log('✅ Arabic dashboard accessible');
  });

  test('Should persist session across page reloads', async ({ page }) => {
    console.log('=== Testing Session Persistence ===');

    // Login first
    await page.goto(`${BASE_URL}/en/login`);
    await page.locator('input[type="text"]').fill(ACTUAL_USER_EMAIL);
    await page.locator('input[type="password"]').fill(ACTUAL_USER_PASSWORD);
    await page.locator('button[type="submit"]').click();

    // Wait for dashboard
    await expect(page).toHaveURL(/\/en\/dashboard/, { timeout: 10000 });
    console.log('✅ Initial login successful');

    // Reload the page
    await page.reload();
    console.log('Reloaded page...');

    // Should still be on dashboard (session persisted)
    await expect(page).toHaveURL(/\/en\/dashboard/);
    await expect(page.locator(`text=${ACTUAL_USER_NAME}`)).toBeVisible({ timeout: 5000 });
    console.log('✅ Session persisted after reload');

    // Navigate away and back
    await page.goto(`${BASE_URL}/en`);
    console.log('Navigated to home page...');

    await page.goto(`${BASE_URL}/en/dashboard`);
    console.log('Navigated back to dashboard...');

    // Should still be authenticated
    await expect(page).toHaveURL(/\/en\/dashboard/);
    await expect(page.locator(`text=${ACTUAL_USER_NAME}`)).toBeVisible({ timeout: 5000 });
    console.log('✅ Session persisted across navigation');
  });
});
