import { test, expect } from '@playwright/test';
import {
  loginSeedUser,
  takeScreenshot,
  setupConsoleErrorTracking,
  setupNetworkTracking,
  assertCleanPage,
} from './utils/test-helpers';

/**
 * All routes from DashboardLayoutClient navItems.
 * Every route listed in the sidebar must load without 5xx or redirect to login.
 */
const ALL_DASHBOARD_ROUTES = [
  { path: '/dashboard', label: 'Overview' },
  { path: '/dashboard/spending', label: 'Spending' },
  { path: '/dashboard/savings', label: 'Savings' },
  { path: '/dashboard/add', label: 'Add Entry' },
  { path: '/dashboard/projections', label: 'Projections' },
  { path: '/dashboard/achievements', label: 'Achievements' },
  { path: '/dashboard/keys', label: 'API Keys' },
  { path: '/dashboard/cache', label: 'Cache' },
  { path: '/dashboard/feedback', label: 'Feedback' },
  { path: '/dashboard/topology', label: 'Topology' },
  { path: '/dashboard/devices', label: 'Devices' },
  { path: '/dashboard/marketplace', label: 'Marketplace' },
  { path: '/dashboard/lan-nodes', label: 'LAN Nodes' },
  { path: '/dashboard/token', label: 'Token' },
  { path: '/dashboard/team', label: 'Team' },
  { path: '/dashboard/settings', label: 'Settings' },
];

test.describe('Journey 6: Dashboard Navigation & Data', () => {
  let consoleErrors: string[];
  let network: ReturnType<typeof setupNetworkTracking>;

  test.beforeEach(async ({ page }) => {
    consoleErrors = setupConsoleErrorTracking(page);
    network = setupNetworkTracking(page);
  });

  test('should navigate through all dashboard pages without errors', async ({ page }) => {
    await loginSeedUser(page);

    for (const route of ALL_DASHBOARD_ROUTES) {
      await page.goto(route.path);
      await page.waitForTimeout(1000);
      const screenshotName = `dash-${route.label.toLowerCase().replace(/\s+/g, '-')}`;
      await takeScreenshot(page, screenshotName);

      // Must still be on the dashboard (not redirected to login)
      expect(page.url()).toContain('/dashboard');
    }

    assertCleanPage(consoleErrors, network);
  });

  test('should directly navigate to every dashboard subpage without redirect', async ({ page }) => {
    await loginSeedUser(page);

    for (const route of ALL_DASHBOARD_ROUTES) {
      await page.goto(route.path);
      await page.waitForTimeout(500);

      // Should stay on the requested path, not redirect to login
      expect(page.url()).toContain(route.path);
      expect(page.url()).not.toContain('/auth/login');
    }

    assertCleanPage(consoleErrors, network);
  });

  test('should display user info in sidebar', async ({ page }) => {
    await loginSeedUser(page);
    await page.goto('/dashboard');
    await page.waitForTimeout(2000);
    await takeScreenshot(page, 'dash-sidebar-user-info');

    // Sidebar should show user email or plan info
    const pageContent = await page.textContent('body');
    expect(pageContent).toBeTruthy();
    assertCleanPage(consoleErrors, network);
  });

  test('should redirect unauthenticated user from dashboard routes to login', async ({ page }) => {
    // Without logging in, dashboard routes should redirect to login
    for (const route of ['/dashboard', '/dashboard/keys', '/dashboard/settings']) {
      await page.goto(route);
      await page.waitForURL('**/auth/login**', { timeout: 10000 });
      expect(page.url()).toContain('/auth/login');
    }
  });
});
