// src/features/flyer/BulkImportSummary.test.tsx
import React from 'react';
import { render, screen, fireEvent } from '@testing-library/react';
import { describe, it, expect, vi, beforeEach } from 'vitest';
import { BulkImportSummary } from './BulkImportSummary';

describe('BulkImportSummary', () => {
  const mockOnDismiss = vi.fn();

  const mockSummary = {
    processed: ['flyer1.pdf', 'flyer2.jpg'],
    skipped: ['flyer1.pdf'],
    errors: [{ fileName: 'flyer3.png', message: 'Invalid format' }],
  };

  beforeEach(() => {
    vi.clearAllMocks();
  });

  it('should render all sections when data is provided for each', () => {
    render(<BulkImportSummary summary={mockSummary} onDismiss={mockOnDismiss} />);

    // Check titles and counts
    expect(screen.getByRole('heading', { name: /bulk import report/i })).toBeInTheDocument();
    expect(screen.getByText('Processed: 2, Skipped: 1, Errors: 1')).toBeInTheDocument();

    // Check section headers
    expect(screen.getByText(/successfully processed \(2\)/i)).toBeInTheDocument();
    expect(screen.getByText(/skipped duplicates \(1\)/i)).toBeInTheDocument();
    expect(screen.getByText(/errors \(1\)/i)).toBeInTheDocument();

    // Check content
    // Use getAllByText since 'flyer1.pdf' appears in multiple sections.
    expect(screen.getAllByText('flyer1.pdf')).toHaveLength(2);
    expect(screen.getByText('flyer2.jpg')).toBeInTheDocument();
    // Be more specific to find the error message content.
    expect(screen.getByText('flyer3.png:')).toBeInTheDocument();
    expect(screen.getByText('Invalid format')).toBeInTheDocument();
  });

  it('should not render sections with no data', () => {
    const partialSummary = {
      processed: ['flyer1.pdf'],
      skipped: [],
      errors: [],
    };
    render(<BulkImportSummary summary={partialSummary} onDismiss={mockOnDismiss} />);

    expect(screen.getByText(/successfully processed \(1\)/i)).toBeInTheDocument();
    // Make the query more specific to the section heading to avoid matching the summary line.
    expect(screen.queryByRole('heading', { name: /skipped duplicates/i })).not.toBeInTheDocument();
    expect(screen.queryByRole('heading', { name: /errors/i })).not.toBeInTheDocument();
  });

  it('should render an empty state message when no files were processed', () => {
    const emptySummary = {
      processed: [],
      skipped: [],
      errors: [],
    };
    render(<BulkImportSummary summary={emptySummary} onDismiss={mockOnDismiss} />);

    expect(screen.getByText(/no new files were found to process/i)).toBeInTheDocument();
    expect(screen.queryByText(/successfully processed/i)).not.toBeInTheDocument();
  });

  it('should call onDismiss when the close button is clicked', () => {
    render(<BulkImportSummary summary={mockSummary} onDismiss={mockOnDismiss} />);

    const closeButton = screen.getByRole('button', { name: /dismiss summary/i });
    fireEvent.click(closeButton);

    expect(mockOnDismiss).toHaveBeenCalledTimes(1);
  });
});