import { render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { IssueSummary } from '@/components/projects/IssueSummary';
import type { IssueCountSummary } from '@/components/projects/types';

const mockSummary: IssueCountSummary = {
  open: 12,
  in_progress: 8,
  in_review: 3,
  blocked: 2,
  closed: 45,
  total: 70,
};

describe('IssueSummary', () => {
  it('renders issue summary with title', () => {
    render(<IssueSummary summary={mockSummary} />);
    expect(screen.getByText('Issue Summary')).toBeInTheDocument();
  });

  it('displays all status counts', () => {
    render(<IssueSummary summary={mockSummary} />);

    expect(screen.getByText('Open')).toBeInTheDocument();
    expect(screen.getByText('12')).toBeInTheDocument();

    expect(screen.getByText('In Progress')).toBeInTheDocument();
    expect(screen.getByText('8')).toBeInTheDocument();

    expect(screen.getByText('In Review')).toBeInTheDocument();
    expect(screen.getByText('3')).toBeInTheDocument();

    expect(screen.getByText('Blocked')).toBeInTheDocument();
    expect(screen.getByText('2')).toBeInTheDocument();

    expect(screen.getByText('Closed')).toBeInTheDocument();
    expect(screen.getByText('45')).toBeInTheDocument();
  });

  it('renders empty state when summary is null', () => {
    render(<IssueSummary summary={null} />);
    expect(screen.getByText('No issues found')).toBeInTheDocument();
  });

  it('shows loading skeleton when isLoading is true', () => {
    const { container } = render(<IssueSummary summary={null} isLoading />);
    expect(container.querySelectorAll('.animate-pulse').length).toBeGreaterThan(0);
  });

  it('shows View All Issues button with total count', () => {
    const onViewAllIssues = jest.fn();
    render(<IssueSummary summary={mockSummary} onViewAllIssues={onViewAllIssues} />);

    expect(screen.getByRole('button', { name: /view all issues/i })).toBeInTheDocument();
    expect(screen.getByText('View All Issues (70)')).toBeInTheDocument();
  });

  it('calls onViewAllIssues when button is clicked', async () => {
    const user = userEvent.setup();
    const onViewAllIssues = jest.fn();
    render(<IssueSummary summary={mockSummary} onViewAllIssues={onViewAllIssues} />);

    await user.click(screen.getByRole('button', { name: /view all issues/i }));
    expect(onViewAllIssues).toHaveBeenCalledTimes(1);
  });

  it('does not show View All button when onViewAllIssues is not provided', () => {
    render(<IssueSummary summary={mockSummary} />);
    expect(screen.queryByRole('button', { name: /view all issues/i })).not.toBeInTheDocument();
  });

  it('applies custom className', () => {
    render(<IssueSummary summary={mockSummary} className="custom-class" />);
    expect(screen.getByTestId('issue-summary')).toHaveClass('custom-class');
  });

  it('has accessible list role for status items', () => {
    render(<IssueSummary summary={mockSummary} />);
    expect(screen.getByRole('list', { name: /issue counts by status/i })).toBeInTheDocument();
  });
});
