import { test, expect } from '@playwright/test';
import { RoleAuthHelper, PublicFormHelper } from '../../helpers/helpers';

test.describe('Arabic / RTL Layout', () => {
  test.describe.configure({ mode: 'serial' });

  test('language switch sets Arabic cookie', async ({ page }) => {
    await page.goto('/core/public/submit/');
    await page.waitForLoadState('domcontentloaded');

    const langLink = page.locator('a[href*="set-language"][href*="ar"]');
    const langVisible = await langLink.isVisible().catch(() => false);

    if (langVisible) {
      await langLink.first().click();
      await page.waitForLoadState('domcontentloaded');
    } else {
      await page.goto('/core/set-language/?language=ar', { waitUntil: 'domcontentloaded' });
    }

    const cookies = await page.context().cookies();
    const langCookie = cookies.find(c => c.name === 'django_language');
    expect(langCookie?.value).toBe('ar');
  });

  test('HTML dir attribute switches to RTL', async ({ page }) => {
    await page.goto('/core/set-language/?language=ar', { waitUntil: 'domcontentloaded' });

    await page.goto('/core/public/submit/');
    await page.waitForLoadState('domcontentloaded');

    const dir = await page.getAttribute('html', 'dir');
    expect(dir).toBe('rtl');
  });

  test('login page renders in Arabic', async ({ page }) => {
    await page.context().addCookies([
      { name: 'django_language', value: 'ar', domain: 'localhost', path: '/' },
    ]);

    await page.goto('/core/public/submit/');
    await page.waitForLoadState('domcontentloaded');

    const htmlDir = await page.getAttribute('html', 'dir');
    expect(htmlDir).toBe('rtl');

    const body = await page.textContent('body');
    expect(body).toBeTruthy();
  });

  test('public complaint form in Arabic', async ({ page }) => {
    await page.context().addCookies([
      { name: 'django_language', value: 'ar', domain: 'localhost', path: '/' },
    ]);

    const helper = new PublicFormHelper(page);
    await helper.goToPublicLanding();

    const htmlDir = await page.getAttribute('html', 'dir');
    expect(htmlDir).toBe('rtl');

    await helper.selectFormType('complaint');
    await page.waitForLoadState('domcontentloaded');

    const bodyText = await page.textContent('body');
    expect(bodyText).toBeTruthy();
    expect(bodyText.length).toBeGreaterThan(0);
  });

  test('Arabic form submission works', async ({ page }) => {
    await page.context().addCookies([
      { name: 'django_language', value: 'ar', domain: 'localhost', path: '/' },
    ]);

    const helper = new PublicFormHelper(page);
    await helper.goToPublicLanding();
    await helper.selectFormType('complaint');

    await page.fill('#id_complainant_name', `مريض عربي ${Date.now()}`);
    await page.selectOption('#id_relation_to_patient', 'patient');
    await page.fill('#id_mobile_number', '0501234567');
    await page.fill('#id_patient_name', 'اسم المريض');
    await page.fill('#id_national_id', '1234567890');
    await page.fill('#id_incident_date', '2025-01-15');
    await page.fill('#id_complaint_details', 'شكوى من مريض عربي');
    await page.click('#submit_btn');

    await page.waitForLoadState('domcontentloaded');
    const bodyText = await page.textContent('body');
    expect(bodyText).not.toContain('Server Error');
    expect(bodyText).not.toContain('Traceback');
  });
});
