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

test.describe.configure({ mode: 'serial' });

test.describe('Public Complaint Form', () => {
  test('complaint form loads from landing page', async ({ page }) => {
    const helper = new PublicFormHelper(page);
    await helper.goToPublicLanding();
    await helper.selectFormType('complaint');

    await page.waitForSelector('#id_complainant_name', { timeout: 15000 });
    await expect(page.locator('#id_complainant_name')).toBeVisible();
    await expect(page.locator('#id_patient_name')).toBeVisible();
    await expect(page.locator('#id_national_id')).toBeVisible();
    await expect(page.locator('#id_mobile_number')).toBeVisible();
    await expect(page.locator('#id_incident_date')).toBeVisible();
    await expect(page.locator('#id_complaint_details')).toBeVisible();
    await expect(page.locator('#submit_btn')).toBeVisible();
  });

  test('complaint form has required field validation', async ({ page }) => {
    const helper = new PublicFormHelper(page);
    await helper.goToPublicLanding();
    await helper.selectFormType('complaint');
    await page.waitForSelector('#id_complainant_name', { timeout: 15000 });

    await page.click('#submit_btn');
    await page.waitForLoadState('networkidle');

    const complainantInvalid = await page.locator('#id_complainant_name:invalid').count();
    const patientInvalid = await page.locator('#id_patient_name:invalid').count();
    const nationalIdInvalid = await page.locator('#id_national_id:invalid').count();

    expect(complainantInvalid + patientInvalid + nationalIdInvalid).toBeGreaterThan(0);
  });

  test('complaint form fills all fields correctly', async ({ page }) => {
    const helper = new PublicFormHelper(page);
    await helper.goToPublicLanding();
    await helper.selectFormType('complaint');
    await page.waitForSelector('#id_complainant_name', { timeout: 15000 });

    await helper.fillComplaintForm({
      complainantName: 'Test Complainant',
      relationToPatient: 'relative',
      mobileNumber: '+966501234567',
      patientName: 'Test Patient',
      nationalId: '1234567890',
      incidentDate: '2026-01-15',
      complaintDetails: 'This is a test complaint for E2E testing purposes. The patient experienced delays in the emergency department.',
      email: 'test@example.com',
    });

    await expect(page.locator('#id_complainant_name')).toHaveValue('Test Complainant');
    await expect(page.locator('#id_patient_name')).toHaveValue('Test Patient');
    await expect(page.locator('#id_national_id')).toHaveValue('1234567890');
    await expect(page.locator('#id_mobile_number')).toHaveValue('+966501234567');
    await expect(page.locator('#id_incident_date')).toHaveValue('2026-01-15');
    await expect(page.locator('#id_complaint_details')).toHaveValue(
      'This is a test complaint for E2E testing purposes. The patient experienced delays in the emergency department.'
    );
    await expect(page.locator('#id_email')).toHaveValue('test@example.com');
  });

  test('back button returns to selection from complaint form', async ({ page }) => {
    const helper = new PublicFormHelper(page);
    await helper.goToPublicLanding();
    await helper.selectFormType('complaint');
    await page.waitForSelector('#id_complainant_name', { timeout: 15000 });

    await page.click('button:has-text("Back to Selection")');

    await page.waitForSelector('.selection-card', { timeout: 10000 });
    await expect(page.locator('.selection-card')).toHaveCount(3);
  });

  test('relation to patient has correct options', async ({ page }) => {
    const helper = new PublicFormHelper(page);
    await helper.goToPublicLanding();
    await helper.selectFormType('complaint');
    await page.waitForSelector('#id_relation_to_patient', { timeout: 15000 });

    const options = page.locator('#id_relation_to_patient option');
    const count = await options.count();
    expect(count).toBeGreaterThanOrEqual(4);

    const values = [];
    for (let i = 0; i < count; i++) {
      values.push(await options.nth(i).getAttribute('value'));
    }
    expect(values).toContain('patient');
    expect(values).toContain('relative');
    expect(values).toContain('friend');
    expect(values).toContain('other');
  });
});

test.describe('Public Complaint Tracking', () => {
  test('tracking page loads correctly', async ({ page }) => {
    await page.goto('/complaints/public/track/');
    await page.waitForSelector('input[name="reference_number"]');

    await expect(page.locator('input[name="reference_number"]')).toBeVisible();
    await expect(page.locator('button[type="submit"]')).toContainText('Track Status');
    await expect(page.locator('input[name="reference_number"]')).toHaveAttribute(
      'placeholder',
      /CMP-/
    );
  });

  test('tracking with invalid reference shows error', async ({ page }) => {
    await page.goto('/complaints/public/track/');
    await page.fill('input[name="reference_number"]', 'CMP-INVALID-999999');
    await page.click('button[type="submit"]');
    await page.waitForLoadState('networkidle');

    const hasError = await page.locator('.bg-rose-50').count().then(c => c > 0);
    expect(hasError).toBeTruthy();
  });
});
