import { expect, type Page, test } from '@playwright/test';

async function switchDemoRole(page: Page, roleLabel: string): Promise<void> {
  await page.getByRole('button', { name: /View as:/ }).click();
  await page.getByRole('button', { name: roleLabel }).click();
}

test.describe('app shell smoke paths', () => {
  test.beforeEach(async ({ page }) => {
    await page.goto('/dashboard');
    await expect(page.getByText("You're browsing as a Guest")).toBeVisible();
  });

  test('teacher guest sees staff modules and cannot access director-only modules', async ({ page }) => {
    const navigation = page.getByRole('navigation');

    await expect(page.getByRole('button', { name: /View as: Teacher/ })).toBeVisible();
    await expect(navigation.getByRole('button', { name: 'Classroom Timer' })).toBeVisible();
    await expect(navigation.getByRole('button', { name: 'Director Dashboard' })).toHaveCount(0);
    await expect(navigation.getByRole('button', { name: 'Walk-Through Check-In' })).toHaveCount(0);

    await navigation.getByRole('button', { name: 'Classroom Timer' }).click();
    await expect(page).toHaveURL(/\/classroom-timer$/);
    await expect(page.getByText('Loading classroom timer content...')).toBeVisible();
  });

  test('teacher guest restricted executive route redirects to dashboard', async ({ page }) => {
    await page.goto('/director-dashboard');

    await expect(page).toHaveURL(/\/dashboard$/);
    await expect(page.getByText("You're browsing as a Guest")).toBeVisible();
  });

  test('director guest can reach director dashboard and walk-through module', async ({ page }) => {
    const navigation = page.getByRole('navigation');

    await switchDemoRole(page, 'Director');

    await expect(page.getByRole('button', { name: /View as: Director/ })).toBeVisible();
    await expect(navigation.getByRole('button', { name: 'Director Dashboard' })).toBeVisible();
    await expect(navigation.getByRole('button', { name: 'Walk-Through Check-In' })).toBeVisible();

    await navigation.getByRole('button', { name: 'Walk-Through Check-In' }).click();
    await expect(page).toHaveURL(/\/walkthrough$/);
    await expect(page.getByRole('heading', { name: 'Walk-Through Check-In' })).toBeVisible();
  });

  test('superintendent guest keeps executive module access after navigation', async ({ page }) => {
    const navigation = page.getByRole('navigation');

    await switchDemoRole(page, 'Superintendent');

    await expect(page.getByRole('button', { name: /View as: Superintendent/ })).toBeVisible();
    await navigation.getByRole('button', { name: 'Walk-Through Check-In' }).click();
    await expect(page).toHaveURL(/\/walkthrough$/);
    await expect(page.getByRole('heading', { name: 'Walk-Through Check-In' })).toBeVisible();

    await navigation.getByRole('button', { name: 'Home Dashboard' }).click();
    await expect(page).toHaveURL(/\/dashboard$/);
    await expect(navigation.getByRole('button', { name: 'Director Dashboard' })).toBeVisible();
    await expect(navigation.getByRole('button', { name: 'Walk-Through Check-In' })).toBeVisible();
  });
});
