import { test, expect } from '@playwright/test';
import {
  loginAsAdmin,
  navigateToDashboard,
  waitForPageStable
} from '../helpers/systemcore';

/**
 * SystemCore Dashboard and Navigation Tests
 *
 * Tests the SystemCore dashboard and navigation functionality including:
 * - Viewing the dashboard
 * - Navigating to different sections
 * - Verifying breadcrumbs
 * - Testing menu links
 */

test.describe('SystemCore Dashboard & Navigation', () => {
  test.beforeEach(async ({ page }) => {
    await loginAsAdmin(page);
  });

  test('should display SystemCore dashboard', async ({ page }) => {
    // Navigate to /systemcore
    await navigateToDashboard(page);

    // Verify page loads - use .first() since there are multiple cards
    await expect(page.locator('.card').first()).toBeVisible();

    // Verify we're on the SystemCore dashboard
    const currentUrl = page.url();
    expect(currentUrl).toContain('/systemcore');
  });

  test('should navigate to customers from dashboard', async ({ page }) => {
    // Navigate directly to customers page
    await page.goto('/systemcore/customers');
    await waitForPageStable(page);

    // Verify navigation successful
    await expect(page.locator('#customersTable')).toBeVisible();
  });

  test('should navigate to suppliers from dashboard', async ({ page }) => {
    // Navigate directly to suppliers page
    await page.goto('/systemcore/suppliers');
    await waitForPageStable(page);

    // Verify navigation successful
    await expect(page.locator('#suppliersTable')).toBeVisible();
  });

  test('should navigate to products from dashboard', async ({ page }) => {
    // Navigate directly to products page
    await page.goto('/systemcore/products');
    await waitForPageStable(page);

    // Verify navigation successful
    await expect(page.locator('#productsTable')).toBeVisible();
  });

  test('should navigate to sales orders from dashboard', async ({ page }) => {
    // Navigate directly to sales orders page
    await page.goto('/systemcore/sales-orders');
    await waitForPageStable(page);

    // Verify navigation successful
    await expect(page.locator('.card').first()).toBeVisible();
  });

  test('should navigate to purchase orders from dashboard', async ({ page }) => {
    // Navigate directly to purchase orders page
    await page.goto('/systemcore/purchase-orders');
    await waitForPageStable(page);

    // Verify navigation successful
    await expect(page.locator('.card').first()).toBeVisible();
  });

  test('should navigate to payment terms configuration', async ({ page }) => {
    // Navigate to payment terms page directly
    await page.goto('/systemcore/payment-terms');
    await waitForPageStable(page);

    // Verify page loads by checking for the table
    await expect(page.locator('#paymentTermsTable')).toBeVisible();
  });

  test('should navigate to tax configurations', async ({ page }) => {
    // Navigate to tax configurations page directly
    await page.goto('/systemcore/tax-configurations');
    await waitForPageStable(page);

    // Verify page loads by checking for the table
    await expect(page.locator('#taxConfigurationsTable')).toBeVisible();
  });

  test('should navigate to product categories', async ({ page }) => {
    // Navigate to product categories page directly
    await page.goto('/systemcore/product-categories');
    await waitForPageStable(page);

    // Verify page loads by checking for the table
    await expect(page.locator('#productCategoriesTable')).toBeVisible();
  });

  test('should show breadcrumbs on customers page', async ({ page }) => {
    // Navigate to customers
    await page.goto('/systemcore/customers');
    await waitForPageStable(page);

    // Verify breadcrumbs are visible
    const breadcrumb = page.locator('.breadcrumb, nav[aria-label="breadcrumb"]').first();
    if (await breadcrumb.count() > 0) {
      await expect(breadcrumb).toBeVisible();
    }
  });

  test('should navigate back to list from create page', async ({ page }) => {
    // Go to customer create page
    await page.goto('/systemcore/customers/create');
    await waitForPageStable(page);

    // Click cancel to go back
    const cancelButton = page.locator('a:has-text("Cancel"), a.btn-label-secondary').first();
    await cancelButton.click();

    // Verify we're back on the list page
    await page.waitForURL('**/systemcore/customers', { timeout: 10000 });
    await expect(page.locator('#customersTable')).toBeVisible();
  });

  test('should require authentication for SystemCore pages', async ({ page, context }) => {
    // Clear cookies to simulate logged out user
    await context.clearCookies();

    // Try to navigate to SystemCore
    await page.goto('/systemcore/customers');

    // Should be redirected to login
    await page.waitForURL('**/login', { timeout: 10000 });
  });
});
