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

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

  test('login succeeds and redirects to source user dashboard', async ({ page }) => {
    const auth = new RoleAuthHelper(page);
    await auth.login('source_user');

    await page.waitForLoadState('domcontentloaded');
    expect(page.url()).toContain('px-sources');
  });

  test('cannot access main dashboard', async ({ page }) => {
    const auth = new RoleAuthHelper(page);
    await auth.login('source_user');

    await page.goto('/');
    await page.waitForLoadState('domcontentloaded');
    expect(page.url()).toContain('px-sources');
    expect(page.url()).not.toEqual(process.env.E2E_BASE_URL || 'http://localhost:8000/');
  });

  test('cannot access complaints management', async ({ page }) => {
    const auth = new RoleAuthHelper(page);
    await auth.login('source_user');

    await page.goto('/complaints/');
    await page.waitForLoadState('domcontentloaded');
    expect(page.url()).toContain('px-sources');
  });

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

    await page.goto('/surveys/templates/');
    await page.waitForLoadState('domcontentloaded');
    expect(page.url()).toContain('px-sources');
  });

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

    await page.goto('/observations/');
    await page.waitForLoadState('domcontentloaded');
    expect(page.url()).toContain('px-sources');
  });

  test('cannot access config pages', async ({ page }) => {
    const auth = new RoleAuthHelper(page);
    await auth.login('source_user');

    await page.goto('/config/');
    await page.waitForLoadState('domcontentloaded');
    expect(page.url()).toContain('px-sources');
  });

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

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

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

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

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

    const response = await page.goto('/px-sources/dashboard/');
    expect(response?.status()).toBeLessThan(400);
    expect(page.url()).toContain('px-sources');
  });

  test('can access source user inquiry list', async ({ page }) => {
    const auth = new RoleAuthHelper(page);
    await auth.login('source_user');

    const response = await page.goto('/px-sources/inquiries/');
    expect(response?.status()).toBeLessThan(400);
    expect(page.url()).not.toContain('login');
  });

  test('can change password', async ({ page }) => {
    const auth = new RoleAuthHelper(page);
    await auth.login('source_user');

    const response = await page.goto('/accounts/password/change/');
    expect(response?.status()).toBeLessThan(400);
    expect(page.url()).not.toContain('px-sources');
  });

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

    const response = await page.goto('/accounts/settings/');
    expect(response?.status()).toBeLessThan(400);
    expect(page.url()).not.toContain('px-sources');
  });

  test('can logout', async ({ page }) => {
    const auth = new RoleAuthHelper(page);
    await auth.login('source_user');
    await page.waitForLoadState('domcontentloaded');

    await auth.logout();
    expect(page.url()).toContain('login');
  });
});
