import { test, expect } from '@playwright/test';
import {
  loginAsAdmin,
  navigateToReports,
  navigateToTimeReport,
  navigateToBudgetReport,
  navigateToResourceReport,
  waitForPageStable
} from './helpers/pmcore';

/**
 * PM Core Reports and Analytics Tests
 *
 * Tests the PM Core reporting functionality including:
 * - Reports dashboard/index page
 * - Time tracking report
 * - Budget report
 * - Resource utilization report
 * - Filter controls and data visualization
 */

test.describe('Reports and Analytics', () => {
  test.beforeEach(async ({ page }) => {
    await loginAsAdmin(page);
  });

  test('should display reports index page', async ({ page }) => {
    await test.step('Navigate to reports index', async () => {
      await navigateToReports(page);
    });

    await test.step('Verify reports dashboard loads', async () => {
      await waitForPageStable(page);

      const heading = page.locator('h4, h5').filter({ hasText: /reports|analytics/i }).first();
      await expect(heading).toBeVisible({ timeout: 10000 });

      const cards = page.locator('.card');
      const cardCount = await cards.count();
      expect(cardCount).toBeGreaterThan(0);
    });

    await test.step('Verify report type links are present', async () => {
      const timeReportLink = page.locator('a[href*="/reports/time"]').first();
      if (await timeReportLink.count() > 0) {
        await expect(timeReportLink).toBeVisible();
      }

      const budgetReportLink = page.locator('a[href*="/reports/budget"]').first();
      if (await budgetReportLink.count() > 0) {
        await expect(budgetReportLink).toBeVisible();
      }

      const resourceReportLink = page.locator('a[href*="/reports/resource"]').first();
      if (await resourceReportLink.count() > 0) {
        await expect(resourceReportLink).toBeVisible();
      }
    });
  });

  test('should navigate to time tracking report', async ({ page }) => {
    await test.step('Start from reports index', async () => {
      await navigateToReports(page);
      await waitForPageStable(page);
    });

    await test.step('Click time report link', async () => {
      const timeReportLink = page.locator('a[href*="/reports/time"]').first();
      await timeReportLink.click();

      await page.waitForURL('**/projects/reports/time', { timeout: 15000 });
      await waitForPageStable(page);
    });

    await test.step('Verify navigated to time tracking report page', async () => {
      expect(page.url()).toContain('/projects/reports/time');

      const heading = page.locator('h4, h5').filter({ hasText: /time/i }).first();
      await expect(heading).toBeVisible();
    });
  });

  test('should display time tracking report', async ({ page }) => {
    await test.step('Navigate to time tracking report', async () => {
      await navigateToTimeReport(page);
      await waitForPageStable(page);
    });

    await test.step('Verify page structure loads', async () => {
      await expect(page.locator('.card').first()).toBeVisible();

      const heading = page.locator('h4, h5').filter({ hasText: /time/i }).first();
      await expect(heading).toBeVisible();
    });

    await test.step('Verify data/charts load', async () => {
      await page.waitForTimeout(2000);

      const chartExists = await page.locator('.apexcharts-canvas, #timeReportChart, [id*="chart"]').count() > 0;
      const tableExists = await page.locator('table, .dataTables_wrapper').count() > 0;

      expect(chartExists || tableExists).toBeTruthy();
    });
  });

  test('should navigate to budget report', async ({ page }) => {
    await test.step('Start from reports index', async () => {
      await navigateToReports(page);
      await waitForPageStable(page);
    });

    await test.step('Click budget report link', async () => {
      const budgetReportLink = page.locator('a[href*="/reports/budget"]').first();
      await budgetReportLink.click();

      await page.waitForURL('**/projects/reports/budget', { timeout: 15000 });
      await waitForPageStable(page);
    });

    await test.step('Verify navigated to budget report page', async () => {
      expect(page.url()).toContain('/projects/reports/budget');

      const heading = page.locator('h4, h5').filter({ hasText: /budget/i }).first();
      await expect(heading).toBeVisible();
    });
  });

  test('should display budget report', async ({ page }) => {
    await test.step('Navigate to budget report', async () => {
      await navigateToBudgetReport(page);
      await waitForPageStable(page);
    });

    await test.step('Verify page structure loads', async () => {
      await expect(page.locator('.card').first()).toBeVisible();

      const heading = page.locator('h4, h5').filter({ hasText: /budget/i }).first();
      await expect(heading).toBeVisible();
    });

    await test.step('Verify budget data/charts load', async () => {
      await page.waitForTimeout(2000);

      const chartExists = await page.locator('.apexcharts-canvas, #budgetChart, [id*="chart"]').count() > 0;
      const tableExists = await page.locator('table, .dataTables_wrapper').count() > 0;
      const statsExists = await page.locator('.card-body').filter({ hasText: /budget|spent|remaining/i }).count() > 0;

      expect(chartExists || tableExists || statsExists).toBeTruthy();
    });
  });

  test('should navigate to resource utilization report', async ({ page }) => {
    await test.step('Start from reports index', async () => {
      await navigateToReports(page);
      await waitForPageStable(page);
    });

    await test.step('Click resource report link', async () => {
      const resourceReportLink = page.locator('a[href*="/reports/resource"]').first();
      await resourceReportLink.click();

      await page.waitForURL('**/projects/reports/resource', { timeout: 15000 });
      await waitForPageStable(page);
    });

    await test.step('Verify navigated to resource report page', async () => {
      expect(page.url()).toContain('/projects/reports/resource');

      const heading = page.locator('h4, h5').filter({ hasText: /resource/i }).first();
      await expect(heading).toBeVisible();
    });
  });

  test('should display resource utilization report', async ({ page }) => {
    await test.step('Navigate to resource utilization report', async () => {
      await navigateToResourceReport(page);
      await waitForPageStable(page);
    });

    await test.step('Verify page structure loads', async () => {
      await expect(page.locator('.card').first()).toBeVisible();

      const heading = page.locator('h4, h5').filter({ hasText: /resource/i }).first();
      await expect(heading).toBeVisible();
    });

    await test.step('Verify resource data loads', async () => {
      await page.waitForTimeout(2000);

      const chartExists = await page.locator('.apexcharts-canvas, #resourceChart, [id*="chart"]').count() > 0;
      const tableExists = await page.locator('table, .dataTables_wrapper').count() > 0;
      const resourceCards = await page.locator('.card-body').filter({ hasText: /utilization|capacity|allocation/i }).count() > 0;

      expect(chartExists || tableExists || resourceCards).toBeTruthy();
    });
  });
});
