import { test, expect } from '@playwright/test';
import { loginAs, TEST_USER } from './helpers/auth.js';

test.describe('Projects page', () => {
  test.beforeEach(async ({ page }) => {
    await loginAs(page, TEST_USER.email, TEST_USER.password);
    const url = page.url();
    test.skip(!url.includes('/chat'), 'No seeded test user — skipping authenticated tests');
  });

  test('projects page loads with heading', async ({ page }) => {
    await page.goto('/projects');
    await expect(page.getByRole('heading', { name: /projects/i })).toBeVisible({ timeout: 10_000 });
  });

  test('shows empty state or project cards when loaded', async ({ page }) => {
    await page.goto('/projects');
    // Wait for loading state to clear
    await expect(page.getByText(/loading projects/i)).not.toBeVisible({ timeout: 10_000 });

    const hasProjects = await page
      .locator('[class*="grid"]')
      .isVisible()
      .catch(() => false);
    const hasEmpty = await page
      .getByText(/no projects yet/i)
      .isVisible()
      .catch(() => false);

    expect(hasProjects || hasEmpty).toBe(true);
  });

  test('shows Active Mission section', async ({ page }) => {
    await page.goto('/projects');
    await expect(page.getByRole('heading', { name: /active mission/i })).toBeVisible({
      timeout: 10_000,
    });
  });

  test('sidebar navigation is present', async ({ page }) => {
    await page.goto('/projects');
    await expect(page.getByRole('link', { name: /projects/i }).first()).toBeVisible();
  });
});
