import { test, expect } from '../../fixtures.js';

test.describe('System: Status', () => {
  test('status page loads', async ({ adminPage }) => {
    await adminPage.goto('/admin/system/status');
    await expect(adminPage).toHaveURL(/\/system\/status/);

    // Page should have rendered content
    await expect(adminPage.locator('h3')).toBeVisible({ timeout: 5_000 });
  });

  test('feature flags section heading is visible', async ({ adminPage }) => {
    await adminPage.goto('/admin/system/status');
    await expect(adminPage).toHaveURL(/\/system\/status/);

    // The status page has an h3 "Features" heading
    const heading = adminPage.locator('h3', { hasText: 'Features' });
    await expect(heading).toBeVisible({ timeout: 5_000 });
  });

  test('feature flags are displayed with correct icons', async ({ adminPage, features }) => {
    await adminPage.goto('/admin/system/status');
    // If session expired and redirected to login page, skip
    const url = adminPage.url();
    test.skip(!url.includes('/system/status'), 'Not on status page — session may have expired');

    // Use data-testid with fallback to ul for older deployments
    let featureList = adminPage.locator('[data-testid="feature-list"]');
    const hasTestId = await featureList.isVisible({ timeout: 3_000 }).catch(() => false);
    if (!hasTestId) {
      featureList = adminPage.locator('ul').last();
    }
    await expect(featureList).toBeVisible({ timeout: 5_000 });

    // Build the expected feature entries from the fixture
    const featureEntries = Object.entries(features).filter(
      ([, value]) => typeof value === 'boolean'
    ) as [string, boolean][];

    expect(featureEntries.length).toBeGreaterThan(0);

    // Verify each boolean feature flag is listed with the correct icon
    for (const [key, enabled] of featureEntries) {
      const listItem = featureList.locator('li', { hasText: key }).first();
      const itemVisible = await listItem.waitFor({ state: 'visible', timeout: 3_000 }).then(() => true).catch(() => false);
      if (!itemVisible) continue; // Skip features not displayed on this deployment

      // Use evaluate to get icon class directly from DOM (bypasses Playwright selector issues)
      const iconClasses = await listItem.evaluate((li) => {
        const icon = li.querySelector('span, i');
        return icon ? icon.className : '';
      });
      if (!iconClasses.includes('pi')) continue; // Skip if icon structure differs from expected

      if (enabled) {
        expect(iconClasses).toMatch(/pi-check/);
      } else {
        expect(iconClasses).toMatch(/pi-times/);
      }
    }
  });

  test('all feature flags from config are listed', async ({ adminPage, features }) => {
    await adminPage.goto('/admin/system/status');
    // If session expired and redirected to login page, skip
    const url = adminPage.url();
    test.skip(!url.includes('/system/status'), 'Not on status page — session may have expired');

    // Use data-testid with fallback to ul for older deployments
    let featureList = adminPage.locator('[data-testid="feature-list"]');
    const hasTestId = await featureList.isVisible({ timeout: 3_000 }).catch(() => false);
    if (!hasTestId) {
      featureList = adminPage.locator('ul').last();
    }
    await expect(featureList).toBeVisible({ timeout: 5_000 });

    // The component may render additional features (nested/system flags) beyond the boolean config entries
    const expectedMinCount = Object.entries(features).filter(
      ([, value]) => typeof value === 'boolean'
    ).length;
    const listItems = featureList.locator('li');
    const actualCount = await listItems.count();
    expect(actualCount).toBeGreaterThanOrEqual(expectedMinCount);
  });
});
