/**
 * Seller Analytics Test
 * 
 * Tags: @seller @p2 @analytics
 * 
 * Tests seller analytics functionality:
 * - View analytics dashboard
 * - View sales analytics
 * - View product analytics
 * - Verify charts and visualizations
 */

import { test, expect } from '@playwright/test';
import { 
  printTestCase, 
  printSuccess, 
  printWarning, 
  ensureAuthenticated 
} from '../../helpers/test-helpers';

test.describe('06_Seller_Portal - Analytics', () => {
  test.use({ storageState: 'auth.json' });
  
  test.beforeEach(async ({ page }) => {
    await ensureAuthenticated(page);
  });
  
  test('Test Case 1: Navigate to analytics page', async ({ page }) => {
    printTestCase(1, 'Navigate to Analytics Page');
    
    await page.goto('/seller/analytics');
    await page.waitForLoadState('networkidle');
    
    // Verify analytics page loaded
    const analyticsHeader = page.locator('h1:has-text("Analytics"), h6:has-text("Analytics"), .card-header:has-text("Analytics")');
    const headerVisible = await analyticsHeader.first().isVisible({ timeout: 5000 }).catch(() => false);
    
    if (headerVisible) {
      printSuccess('Analytics page loaded');
    } else {
      printWarning('Analytics header not found');
    }
    
    // Verify URL
    const currentUrl = page.url();
    if (currentUrl.includes('/seller/analytics')) {
      printSuccess('On seller analytics page');
    }
  });
  
  test('Test Case 2: View sales analytics', async ({ page }) => {
    printTestCase(2, 'View Sales Analytics');
    
    await page.goto('/seller/analytics');
    await page.waitForLoadState('networkidle');
    
    // Look for sales metrics
    const salesMetrics = page.locator('[class*="sales"], .card:has-text("Sales")');
    if (await salesMetrics.first().isVisible({ timeout: 3000 }).catch(() => false)) {
      printSuccess('Sales analytics section found');
      
      // Check for sales chart
      const salesChart = page.locator('canvas, svg, [class*="chart"]');
      if (await salesChart.first().isVisible().catch(() => false)) {
        printSuccess('Sales chart/visualization found');
      }
    } else {
      printWarning('Sales analytics section not found');
    }
  });
  
  test('Test Case 3: View product analytics', async ({ page }) => {
    printTestCase(3, 'View Product Analytics');
    
    await page.goto('/seller/analytics/products');
    await page.waitForLoadState('networkidle');
    
    // Look for product metrics
    const productMetrics = page.locator('[class*="product"], .card:has-text("Product")');
    if (await productMetrics.first().isVisible({ timeout: 3000 }).catch(() => false)) {
      printSuccess('Product analytics section found');
    } else {
      printWarning('Product analytics section not found - may not be implemented');
    }
  });
  
  test('Test Case 4: Verify analytics charts', async ({ page }) => {
    printTestCase(4, 'Verify Analytics Charts');
    
    await page.goto('/seller/analytics');
    await page.waitForLoadState('networkidle');
    
    // Look for charts (canvas or SVG elements)
    const charts = page.locator('canvas, svg[class*="chart"]');
    const chartCount = await charts.count();
    
    if (chartCount > 0) {
      printSuccess(`Found ${chartCount} chart(s)/visualization(s)`);
    } else {
      printWarning('No charts found');
    }
  });
  
  test('Test Case 5: Test date range filter', async ({ page }) => {
    printTestCase(5, 'Test Date Range Filter');
    
    await page.goto('/seller/analytics');
    await page.waitForLoadState('networkidle');
    
    // Look for date picker or date range inputs
    const dateInputs = page.locator('input[type="date"], input[placeholder*="date" i]');
    const dateInputCount = await dateInputs.count();
    
    if (dateInputCount > 0) {
      printSuccess(`Found ${dateInputCount} date filter(s)`);
      
      // Try setting a date range
      const firstDateInput = dateInputs.first();
      const futureDate = new Date();
      futureDate.setDate(futureDate.getDate() - 30); // 30 days ago
      const dateString = futureDate.toISOString().split('T')[0];
      
      await firstDateInput.fill(dateString);
      await page.waitForTimeout(1500);
      printSuccess('Date filter applied');
    } else {
      printWarning('No date filters found');
    }
  });
  
  test('Test Case 6: Verify key performance indicators', async ({ page }) => {
    printTestCase(6, 'Verify Key Performance Indicators');
    
    await page.goto('/seller/analytics');
    await page.waitForLoadState('networkidle');
    
    // Look for KPI cards or metrics
    const kpiCards = page.locator('.kpi, .metric, [class*="performance"], .stats-card');
    const kpiCount = await kpiCards.count();
    
    if (kpiCount > 0) {
      printSuccess(`Found ${kpiCount} KPI indicator(s)`);
      
      // Check for percentage indicators
      const percentageIndicators = page.locator('[class*="percent"], :has-text("%")');
      const percentCount = await percentageIndicators.count();
      
      if (percentCount > 0) {
        printSuccess(`Found ${percentCount} percentage metric(s)`);
      }
    } else {
      printWarning('No KPI indicators found');
    }
  });
});

