import { test, expect } from '@playwright/test';
import { RoleAuthHelper } from '../../helpers/helpers';
import { DEPT_ID } from '../../helpers/api-helper';

test.describe('Champion Dashboard Tests', () => {
  test.setTimeout(120000);

  test('login as champion and view department detail', async ({ page }) => {
    const auth = new RoleAuthHelper(page);
    await auth.login('champion');
    await page.waitForTimeout(800);

    await page.waitForURL(/organizations|dashboard/, { timeout: 10000 }).catch(() => {});
    await page.waitForLoadState('domcontentloaded');
    await page.waitForTimeout(2500);

    const url = page.url();
    expect(url).toMatch(/organizations|dashboard|px-sources/);
    await page.waitForTimeout(800);
  });

  test('view department detail page', async ({ page }) => {
    const auth = new RoleAuthHelper(page);
    await auth.login('champion');
    await page.waitForTimeout(800);

    await page.goto(`/organizations/departments/${DEPT_ID}/`);
    await page.waitForLoadState('domcontentloaded');
    await page.waitForTimeout(2500);

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

  test('check pending requests on department page', async ({ page }) => {
    const auth = new RoleAuthHelper(page);
    await auth.login('champion');
    await page.waitForTimeout(800);

    await page.goto(`/organizations/departments/${DEPT_ID}/`);
    await page.waitForLoadState('domcontentloaded');
    await page.waitForTimeout(2500);

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