import { test, expect } from '@playwright/test';
import { loginAsAdmin, waitForBlazor } from '../helpers/auth.helper';
import { ROUTES } from '../helpers/test-data';

/**
 * EN: Admin dashboard and navigation E2E tests.
 * VI: E2E tests cho admin dashboard va dieu huong.
 */
test.describe('Admin Dashboard', () => {
  test.beforeEach(async ({ page }) => {
    await loginAsAdmin(page);
  });

  // -----------------------------------------------------------------------
  // 1. Dashboard loads after login
  // -----------------------------------------------------------------------
  test('should display admin dashboard after login', async ({ page }) => {
    await expect(page).toHaveURL(/\/admin/);

    // Dashboard should show main content area with MudBlazor layout
    const mainContent = page.locator('.mud-main-content, .mud-layout-content, [class*="dashboard"]').first();
    await expect(mainContent).toBeVisible({ timeout: 10_000 });
  });

  // -----------------------------------------------------------------------
  // 2. Sidebar navigation is visible
  // -----------------------------------------------------------------------
  test('should show sidebar navigation', async ({ page }) => {
    // Admin layout has a 2-level sidebar (MudDrawer / MudNavMenu)
    const sidebar = page.locator('.mud-drawer, .mud-navmenu, [class*="sidebar"]').first();
    await expect(sidebar).toBeVisible({ timeout: 10_000 });

    // Sidebar should contain navigation links
    const navLinks = page.locator('.mud-nav-link, .mud-list-item, [class*="nav-link"]');
    const count = await navLinks.count();
    expect(count).toBeGreaterThan(0);
  });

  // -----------------------------------------------------------------------
  // 3. Navigate to shop / store management
  // -----------------------------------------------------------------------
  test('should navigate to store management', async ({ page }) => {
    await page.goto(ROUTES.ADMIN_STORES);
    await waitForBlazor(page);

    await expect(page).toHaveURL(/\/admin\/stores/);

    // Should display a table or list of stores/shops
    const content = page.locator(
      '.mud-table, .mud-data-grid, [class*="store"], [class*="shop"]',
    ).first();
    await expect(content).toBeVisible({ timeout: 10_000 });
  });

  // -----------------------------------------------------------------------
  // 4. Navigate to reports section
  // -----------------------------------------------------------------------
  test('should navigate to EOD reports page', async ({ page }) => {
    await page.goto(ROUTES.ADMIN_EOD_REPORT);
    await waitForBlazor(page);

    await expect(page).toHaveURL(/\/admin\/reports\/eod/);

    // Reports page should have content
    const content = page.locator('.mud-main-content, .mud-layout-content, [class*="report"]').first();
    await expect(content).toBeVisible({ timeout: 10_000 });
  });

  // -----------------------------------------------------------------------
  // 5. User profile / settings accessible
  // -----------------------------------------------------------------------
  test('should navigate to admin settings', async ({ page }) => {
    await page.goto(ROUTES.ADMIN_SETTINGS);
    await waitForBlazor(page);

    await expect(page).toHaveURL(/\/admin\/settings/);

    const content = page.locator('.mud-main-content, .mud-layout-content, [class*="settings"]').first();
    await expect(content).toBeVisible({ timeout: 10_000 });
  });
});
