/**
 * E2E tests for careers page
 * Ensures careers page loads correctly and jobs table exists
 */

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

test.describe('Careers Page', () => {
  test.describe('English version', () => {
    test('should load careers page successfully', async ({ page }) => {
      // Navigate to careers page
      await page.goto('/en/careers');

      // Wait for page to load
      await page.waitForLoadState('networkidle');

      // Verify page title
      await expect(page).toHaveTitle(/Join the Mawidi Team/i);

      // Verify main heading exists
      const heading = page.locator('h1');
      await expect(heading).toContainText('Join the Mawidi Team');
    });

    test('should display all sections', async ({ page }) => {
      await page.goto('/en/careers');
      await page.waitForLoadState('networkidle');

      // Verify Culture section
      await expect(page.getByRole('heading', { name: 'Our Culture' })).toBeVisible();

      // Verify Benefits section
      await expect(page.getByRole('heading', { name: 'Why Work With Us' })).toBeVisible();

      // Verify Open Positions section
      await expect(page.getByRole('heading', { name: 'Open Positions' })).toBeVisible();

      // Verify Hiring Process section
      await expect(page.getByRole('heading', { name: 'Our Hiring Process' })).toBeVisible();
    });

    test('should display empty state when no jobs exist', async ({ page }) => {
      await page.goto('/en/careers');
      await page.waitForLoadState('networkidle');

      // Check for empty state message
      const emptyState = page.getByText(/no open positions at the moment/i);
      await expect(emptyState).toBeVisible();
    });

    test('should have working CTA buttons', async ({ page }) => {
      await page.goto('/en/careers');
      await page.waitForLoadState('networkidle');

      // Verify "View Open Positions" button exists and scrolls to section
      const viewPositionsButton = page.getByRole('link', { name: /view open positions/i });
      await expect(viewPositionsButton).toBeVisible();
      await expect(viewPositionsButton).toHaveAttribute('href', '#open-positions');

      // Verify Contact Us link
      const contactLink = page.getByRole('link', { name: /contact us/i }).first();
      await expect(contactLink).toBeVisible();
    });

    test('should not have console errors about missing jobs table', async ({ page }) => {
      const consoleErrors: string[] = [];

      page.on('console', (msg) => {
        if (msg.type() === 'error') {
          consoleErrors.push(msg.text());
        }
      });

      await page.goto('/en/careers');
      await page.waitForLoadState('networkidle');

      // Filter for jobs table errors
      const jobsTableErrors = consoleErrors.filter(
        (err) => err.includes('jobs') && err.includes('PGRST205')
      );

      // Should have NO errors about missing jobs table
      expect(jobsTableErrors).toHaveLength(0);
    });
  });

  test.describe('Arabic version', () => {
    test('should load Arabic careers page successfully', async ({ page }) => {
      await page.goto('/ar/careers');
      await page.waitForLoadState('networkidle');

      // Verify page title contains Arabic text or Mawidi
      await expect(page).toHaveTitle(/Mawidi/);

      // Verify RTL direction
      const main = page.locator('main');
      await expect(main).toHaveAttribute('dir', 'rtl');
    });

    test('should display Arabic content', async ({ page }) => {
      await page.goto('/ar/careers');
      await page.waitForLoadState('networkidle');

      // Check for Arabic heading (should contain right-to-left text)
      const heading = page.locator('h1');
      await expect(heading).toBeVisible();
    });
  });

  test.describe('Navigation', () => {
    test('should have language switcher working', async ({ page }) => {
      // Start on English version
      await page.goto('/en/careers');

      // Click Arabic language link
      const arLink = page.getByRole('link', { name: 'AR' });
      await arLink.click();

      // Should navigate to Arabic version
      await expect(page).toHaveURL('/ar/careers');
    });

    test('should have working navigation menu', async ({ page }) => {
      await page.goto('/en/careers');

      // Verify navigation links exist
      await expect(page.getByRole('link', { name: 'Home' })).toBeVisible();
      await expect(page.getByRole('link', { name: 'About' })).toBeVisible();
      await expect(page.getByRole('link', { name: 'Pricing' })).toBeVisible();
      await expect(page.getByRole('link', { name: 'Contact' })).toBeVisible();
    });
  });
});
