import { test, expect } from '@playwright/test';
import {
  loginAsAdmin,
  navigateToReports,
  navigateToSummaryReport,
  navigateToCashFlowReport,
  navigateToCategoryPerformanceReport,
  waitForPageStable
} from './helpers/accountingcore';

/**
 * AccountingCore Reports Tests
 *
 * Tests the AccountingCore reporting functionality including:
 * - Reports index/dashboard page
 * - Summary report (Income & Expense)
 * - Cash flow report
 * - Category performance report
 * - Date range filters
 */

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/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 () => {
      // Look for report cards in main content area (not sidebar)
      const mainContent = page.locator('.layout-page');

      // Check for summary report card
      const summaryCard = mainContent.locator('.card').filter({ hasText: /Income.*Expense.*Summary/i }).first();
      await expect(summaryCard).toBeVisible();

      // Check for cash flow report card
      const cashFlowCard = mainContent.locator('.card').filter({ hasText: /Cash Flow/i }).first();
      await expect(cashFlowCard).toBeVisible();

      // Check for category performance report card
      const categoryCard = mainContent.locator('.card').filter({ hasText: /Category Performance/i }).first();
      await expect(categoryCard).toBeVisible();
    });
  });

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

    await test.step('Click summary report link', async () => {
      // Find the summary report card in main content and click its "View Report" link
      const mainContent = page.locator('.layout-page');
      const summaryCard = mainContent.locator('.card').filter({ hasText: /Income.*Expense.*Summary/i }).first();
      const viewReportLink = summaryCard.locator('a:has-text("View Report")');
      await viewReportLink.click();
      await page.waitForURL('**/accountingcore/reports/summary', { timeout: 15000 });
      await waitForPageStable(page);
    });

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

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

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

    await test.step('Verify income/expense summary cards', async () => {
      await page.waitForTimeout(2000);
      const incomeCard = page.locator('.card-body').filter({ hasText: /total income/i }).first();
      await expect(incomeCard).toBeVisible();

      const expenseCard = page.locator('.card-body').filter({ hasText: /total expense/i }).first();
      await expect(expenseCard).toBeVisible();
    });
  });

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

    await test.step('Click cash flow report link', async () => {
      // Find the cash flow report card in main content and click its "View Report" link
      const mainContent = page.locator('.layout-page');
      const cashFlowCard = mainContent.locator('.card').filter({ hasText: /Cash Flow/i }).first();
      const viewReportLink = cashFlowCard.locator('a:has-text("View Report")');
      await viewReportLink.click();
      await page.waitForURL('**/accountingcore/reports/cashflow', { timeout: 15000 });
      await waitForPageStable(page);
    });

    await test.step('Verify navigated to cash flow report page', async () => {
      expect(page.url()).toContain('/accountingcore/reports/cashflow');
    });
  });

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

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

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

    await test.step('Click category performance link', async () => {
      // Find the category performance report card in main content and click its "View Report" link
      const mainContent = page.locator('.layout-page');
      const categoryCard = mainContent.locator('.card').filter({ hasText: /Category Performance/i }).first();
      const viewReportLink = categoryCard.locator('a:has-text("View Report")');
      await viewReportLink.click();
      await page.waitForURL('**/accountingcore/reports/category-performance', { timeout: 15000 });
      await waitForPageStable(page);
    });

    await test.step('Verify navigated to category performance page', async () => {
      expect(page.url()).toContain('/accountingcore/reports/category-performance');
    });
  });

  test('should have date range filters', async ({ page }) => {
    await test.step('Navigate to summary report', async () => {
      await navigateToSummaryReport(page);
      await waitForPageStable(page);
    });

    await test.step('Verify date filter inputs exist', async () => {
      const startDateInput = page.locator('input#start_date, input[name="start_date"]').first();
      await expect(startDateInput).toBeVisible();

      const endDateInput = page.locator('input#end_date, input[name="end_date"]').first();
      await expect(endDateInput).toBeVisible();
    });
  });
});
