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

test.describe('Communication Request Tests', () => {
  test.setTimeout(120000);
  test.describe.configure({ mode: 'serial' });

  test('source user raises communication request', async ({ page }) => {
    const auth = new RoleAuthHelper(page);
    await auth.login('source_user');
    await page.waitForTimeout(800);

    await page.goto('/px-sources/communication-requests/new/');
    await page.waitForLoadState('domcontentloaded');
    await page.waitForTimeout(2500);

    const nameField = page.locator('input[name="patient_name"], #id_patient_name');
    if (await nameField.count() > 0) { await nameField.fill(`E2E Test Patient ${Date.now()}`); await page.waitForTimeout(300); }
    const phoneField = page.locator('input[name="patient_phone"], #id_patient_phone');
    if (await phoneField.count() > 0) { await phoneField.fill('0509998877'); await page.waitForTimeout(300); }
    const msgField = page.locator('textarea[name="message"], #id_message');
    if (await msgField.count() > 0) { await msgField.fill(`E2E communication request test ${Date.now()}. Please contact patient family.`); await page.waitForTimeout(300); }
    const reasonField = page.locator('select[name="reason"], #id_reason');
    if (await reasonField.count() > 0) { await reasonField.selectOption({ index: 1 }); await page.waitForTimeout(300); }

    await submitContentForm(page);
    expect(page.url()).not.toContain('/new/');
    await page.waitForTimeout(800);
  });

  test('admin can see communication request list', async ({ page }) => {
    const auth = new RoleAuthHelper(page);
    await auth.login('hospital_admin');
    await page.waitForTimeout(800);

    await page.goto('/px-sources/communication-requests/manage/');
    await page.waitForLoadState('domcontentloaded');
    await page.waitForTimeout(2500);

    const pageText = (await page.textContent('body')) || '';
    expect(pageText.length).toBeGreaterThan(0);
    await page.waitForTimeout(800);
  });

  test('admin handles communication request', async ({ page }) => {
    const auth = new RoleAuthHelper(page);
    await auth.login('hospital_admin');
    await page.waitForTimeout(800);

    await page.goto('/px-sources/communication-requests/manage/');
    await page.waitForLoadState('domcontentloaded');
    await page.waitForTimeout(2500);

    const pageText = (await page.textContent('body')) || '';
    if (pageText.includes('404') || pageText.includes('403') || pageText.includes("don't have permission")) {
      test.skip();
      return;
    }

    const links = page.locator('a[href*="communication-requests"]');
    const count = await links.count();
    if (count > 0) {
      const firstHref = await links.first().getAttribute('href') || '';
      if (firstHref) {
        await page.goto(firstHref);
        await page.waitForLoadState('domcontentloaded');
        await page.waitForTimeout(2500);

        const statusField = page.locator('select[name="status"], #id_status');
        if (await statusField.count() > 0) {
          const opts = await statusField.locator('option').count();
          if (opts > 1) { await statusField.selectOption({ index: 1 }); await page.waitForTimeout(400); }
        }
        const notesField = page.locator('textarea[name="resolution_notes"], #id_resolution_notes, textarea[name="notes"], #id_notes');
        if (await notesField.count() > 0) { await notesField.fill('E2E test - admin has contacted patient.'); await page.waitForTimeout(400); }

        await submitContentForm(page);
      }
    }
    expect(page.url()).toBeTruthy();
    await page.waitForTimeout(800);
  });
});
