import { test, expect } from '../fixtures';

/**
 * Report Generation Tests
 * Covers: PDF/CSV generation, scheduled reports, report management
 * Target: 100% coverage on critical paths
 */

test.describe('Report Generation @reports @critical', () => {
  
  test('should generate PDF report', async ({ authenticatedPage, testData }) => {
    // Create scenario with data
    const scenario = await testData.createScenario({
      name: 'PDF Report Test',
      region: 'us-east-1',
      tags: [],
    });
    await testData.addScenarioLogs(scenario.id, 50);
    
    await authenticatedPage.goto(`/scenarios/${scenario.id}/reports`);
    
    // Generate PDF report
    await authenticatedPage.click('[data-testid="generate-report-button"]');
    await authenticatedPage.selectOption('[data-testid="report-format-select"]', 'pdf');
    await authenticatedPage.click('[data-testid="include-logs-checkbox"]');
    await authenticatedPage.click('[data-testid="generate-now-button"]');
    
    // Wait for generation
    await authenticatedPage.waitForSelector('[data-testid="report-ready"]', { timeout: 30000 });
    
    // Download
    const [download] = await Promise.all([
      authenticatedPage.waitForEvent('download'),
      authenticatedPage.click('[data-testid="download-report-button"]'),
    ]);
    
    expect(download.suggestedFilename()).toMatch(/\.pdf$/);
  });

  test('should generate CSV report', async ({ authenticatedPage, testData }) => {
    const scenario = await testData.createScenario({
      name: 'CSV Report Test',
      region: 'us-east-1',
      tags: [],
    });
    await testData.addScenarioLogs(scenario.id, 100);
    
    await authenticatedPage.goto(`/scenarios/${scenario.id}/reports`);
    
    await authenticatedPage.click('[data-testid="generate-report-button"]');
    await authenticatedPage.selectOption('[data-testid="report-format-select"]', 'csv');
    await authenticatedPage.click('[data-testid="generate-now-button"]');
    
    await authenticatedPage.waitForSelector('[data-testid="report-ready"]', { timeout: 30000 });
    
    const [download] = await Promise.all([
      authenticatedPage.waitForEvent('download'),
      authenticatedPage.click('[data-testid="download-report-button"]'),
    ]);
    
    expect(download.suggestedFilename()).toMatch(/\.csv$/);
  });

  test('should show report generation progress', async ({ authenticatedPage, testData }) => {
    const scenario = await testData.createScenario({
      name: 'Progress Test',
      region: 'us-east-1',
      tags: [],
    });
    
    await authenticatedPage.goto(`/scenarios/${scenario.id}/reports`);
    await authenticatedPage.click('[data-testid="generate-report-button"]');
    await authenticatedPage.click('[data-testid="generate-now-button"]');
    
    // Check progress indicator
    await expect(authenticatedPage.locator('[data-testid="generation-progress"]')).toBeVisible();
    
    // Wait for completion
    await authenticatedPage.waitForSelector('[data-testid="report-ready"]', { timeout: 60000 });
  });

  test('should list generated reports', async ({ authenticatedPage, testData }) => {
    const scenario = await testData.createScenario({
      name: 'List Reports Test',
      region: 'us-east-1',
      tags: [],
    });
    
    // Generate a few reports
    await testData.createReport(scenario.id, 'pdf');
    await testData.createReport(scenario.id, 'csv');
    
    await authenticatedPage.goto(`/scenarios/${scenario.id}/reports`);
    
    // Check list
    await expect(authenticatedPage.locator('[data-testid="reports-list"]')).toBeVisible();
    const reportItems = await authenticatedPage.locator('[data-testid="report-item"]').count();
    expect(reportItems).toBeGreaterThanOrEqual(2);
  });

  test('should delete report', async ({ authenticatedPage, testData }) => {
    const scenario = await testData.createScenario({
      name: 'Delete Report Test',
      region: 'us-east-1',
      tags: [],
    });
    const report = await testData.createReport(scenario.id, 'pdf');
    
    await authenticatedPage.goto(`/scenarios/${scenario.id}/reports`);
    
    await authenticatedPage.click(`[data-testid="delete-report-${report.id}"]`);
    await authenticatedPage.click('[data-testid="confirm-delete-button"]');
    
    await expect(authenticatedPage.locator('[data-testid="delete-success-toast"]')).toBeVisible();
    await expect(authenticatedPage.locator(`[data-testid="report-item-${report.id}"]`)).not.toBeVisible();
  });
});

test.describe('Scheduled Reports @reports @scheduled', () => {
  
  test('should schedule daily report', async ({ authenticatedPage, testData }) => {
    const scenario = await testData.createScenario({
      name: 'Scheduled Report Test',
      region: 'us-east-1',
      tags: [],
    });
    
    await authenticatedPage.goto(`/scenarios/${scenario.id}/reports/schedule`);
    
    // Configure schedule
    await authenticatedPage.fill('[data-testid="schedule-name-input"]', 'Daily Cost Report');
    await authenticatedPage.selectOption('[data-testid="schedule-frequency-select"]', 'daily');
    await authenticatedPage.selectOption('[data-testid="schedule-format-select"]', 'pdf');
    await authenticatedPage.fill('[data-testid="schedule-time-input"]', '09:00');
    await authenticatedPage.fill('[data-testid="schedule-email-input"]', 'test@example.com');
    
    await authenticatedPage.click('[data-testid="save-schedule-button"]');
    
    await expect(authenticatedPage.locator('[data-testid="schedule-created-success"]')).toBeVisible();
  });

  test('should schedule weekly report', async ({ authenticatedPage, testData }) => {
    const scenario = await testData.createScenario({
      name: 'Weekly Report Test',
      region: 'us-east-1',
      tags: [],
    });
    
    await authenticatedPage.goto(`/scenarios/${scenario.id}/reports/schedule`);
    
    await authenticatedPage.fill('[data-testid="schedule-name-input"]', 'Weekly Summary');
    await authenticatedPage.selectOption('[data-testid="schedule-frequency-select"]', 'weekly');
    await authenticatedPage.selectOption('[data-testid="schedule-day-select"]', 'monday');
    await authenticatedPage.selectOption('[data-testid="schedule-format-select"]', 'csv');
    
    await authenticatedPage.click('[data-testid="save-schedule-button"]');
    
    await expect(authenticatedPage.locator('[data-testid="schedule-created-success"]')).toBeVisible();
  });

  test('should list scheduled reports', async ({ authenticatedPage, testData }) => {
    const scenario = await testData.createScenario({
      name: 'List Scheduled Test',
      region: 'us-east-1',
      tags: [],
    });
    
    await testData.createScheduledReport(scenario.id, {
      name: 'Daily Report',
      frequency: 'daily',
      format: 'pdf',
    });
    
    await authenticatedPage.goto(`/scenarios/${scenario.id}/reports/schedule`);
    
    await expect(authenticatedPage.locator('[data-testid="scheduled-reports-list"]')).toBeVisible();
  });

  test('should edit scheduled report', async ({ authenticatedPage, testData }) => {
    const scenario = await testData.createScenario({
      name: 'Edit Schedule Test',
      region: 'us-east-1',
      tags: [],
    });
    
    const schedule = await testData.createScheduledReport(scenario.id, {
      name: 'Original Name',
      frequency: 'daily',
      format: 'pdf',
    });
    
    await authenticatedPage.goto(`/scenarios/${scenario.id}/reports/schedule`);
    await authenticatedPage.click(`[data-testid="edit-schedule-${schedule.id}"]`);
    
    await authenticatedPage.fill('[data-testid="schedule-name-input"]', 'Updated Name');
    await authenticatedPage.selectOption('[data-testid="schedule-frequency-select"]', 'weekly');
    
    await authenticatedPage.click('[data-testid="save-schedule-button"]');
    
    await expect(authenticatedPage.locator('[data-testid="schedule-updated-success"]')).toBeVisible();
  });

  test('should delete scheduled report', async ({ authenticatedPage, testData }) => {
    const scenario = await testData.createScenario({
      name: 'Delete Schedule Test',
      region: 'us-east-1',
      tags: [],
    });
    
    const schedule = await testData.createScheduledReport(scenario.id, {
      name: 'To Delete',
      frequency: 'daily',
      format: 'pdf',
    });
    
    await authenticatedPage.goto(`/scenarios/${scenario.id}/reports/schedule`);
    await authenticatedPage.click(`[data-testid="delete-schedule-${schedule.id}"]`);
    await authenticatedPage.click('[data-testid="confirm-delete-button"]');
    
    await expect(authenticatedPage.locator('[data-testid="schedule-deleted-success"]')).toBeVisible();
  });
});

test.describe('Report Templates @reports', () => {
  
  test('should create custom report template', async ({ authenticatedPage }) => {
    await authenticatedPage.goto('/reports/templates');
    
    await authenticatedPage.click('[data-testid="create-template-button"]');
    await authenticatedPage.fill('[data-testid="template-name-input"]', 'Custom Template');
    await authenticatedPage.fill('[data-testid="template-description-input"]', 'My custom report layout');
    
    // Select sections
    await authenticatedPage.check('[data-testid="include-summary-checkbox"]');
    await authenticatedPage.check('[data-testid="include-charts-checkbox"]');
    await authenticatedPage.check('[data-testid="include-logs-checkbox"]');
    
    await authenticatedPage.click('[data-testid="save-template-button"]');
    
    await expect(authenticatedPage.locator('[data-testid="template-created-success"]')).toBeVisible();
  });

  test('should use template for report generation', async ({ authenticatedPage, testData }) => {
    const scenario = await testData.createScenario({
      name: 'Template Report Test',
      region: 'us-east-1',
      tags: [],
    });
    
    // Create template
    const template = await testData.createReportTemplate({
      name: 'Executive Summary',
      sections: ['summary', 'charts'],
    });
    
    await authenticatedPage.goto(`/scenarios/${scenario.id}/reports`);
    await authenticatedPage.click('[data-testid="generate-report-button"]');
    await authenticatedPage.selectOption('[data-testid="report-template-select"]', template.id);
    await authenticatedPage.click('[data-testid="generate-now-button"]');
    
    await authenticatedPage.waitForSelector('[data-testid="report-ready"]', { timeout: 30000 });
  });
});
