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

test.describe('PX Admin Role', () => {
  test.describe.configure({ mode: 'parallel' });

  test('login succeeds and redirects to hospital select', async ({ page }) => {
    const auth = new RoleAuthHelper(page);
    await auth.login('px_admin');

    expect(page.url()).toContain('select-hospital');
  });

  test('hospital select page shows hospital cards', async ({ page }) => {
    const auth = new RoleAuthHelper(page);
    await auth.login('px_admin');

    await page.waitForSelector('.hospital-card, .bg-amber-50', { timeout: 10000 });
    const hasHospitals = await page.locator('.hospital-card').count().then(c => c > 0);
    expect(hasHospitals).toBeTruthy();
  });

  test('can access config dashboard', async ({ page }) => {
    const auth = new RoleAuthHelper(page);
    await auth.login('px_admin');

    const response = await page.goto('/config/');
    expect(response?.status()).toBeLessThan(400);
  });

  test('can access SLA config', async ({ page }) => {
    const auth = new RoleAuthHelper(page);
    await auth.login('px_admin');

    const response = await page.goto('/config/sla/');
    expect(response?.status()).toBeLessThan(400);
  });


  test('can access complaints list', async ({ page }) => {
    const auth = new RoleAuthHelper(page);
    await auth.login('px_admin');

    const response = await page.goto('/complaints/');
    expect(response?.status()).toBeLessThan(400);
  });

  test('can access surveys page', async ({ page }) => {
    const auth = new RoleAuthHelper(page);
    await auth.login('px_admin');

    const response = await page.goto('/surveys/templates/');
    expect(response?.status()).toBeLessThan(400);
  });

  test('can access observations page', async ({ page }) => {
    const auth = new RoleAuthHelper(page);
    await auth.login('px_admin');

    const response = await page.goto('/observations/');
    expect(response?.status()).toBeLessThan(400);
  });

  test('can access organizations page', async ({ page }) => {
    const auth = new RoleAuthHelper(page);
    await auth.login('px_admin');

    await page.goto('/organizations/');
    await page.waitForLoadState('domcontentloaded');
    expect(page.url()).not.toContain('login');
  });

  test('can access API docs', async ({ page }) => {
    const auth = new RoleAuthHelper(page);
    await auth.login('px_admin');

    const response = await page.goto('/api/docs/');
    expect(response?.status()).toBe(200);
  });

  test('can access AI engine', async ({ page }) => {
    const auth = new RoleAuthHelper(page);
    await auth.login('px_admin');

    const response = await page.goto('/ai-engine/');
    expect(response?.status()).toBeLessThan(400);
  });

  test('can access physician ratings', async ({ page }) => {
    const auth = new RoleAuthHelper(page);
    await auth.login('px_admin');

    const response = await page.goto('/physicians/');
    expect(response?.status()).toBeLessThan(400);
  });
});
