import { render, screen } from '@testing-library/react';
import { describe, it, expect, vi, beforeEach } from 'vitest';

vi.mock('@inertiajs/react', async () => {
  const actual = await vi.importActual('@inertiajs/react');
  return {
    ...actual,
    Head: ({ title }: { title: string }) => <title>{title}</title>,
    usePage: vi.fn(() => ({
      url: '/admin/findings',
      props: {
        auth: { user: { id: 1, name: 'Admin', email: 'admin@test.com', is_admin: true } },
        features: { billing: false, admin: true },
      },
    })),
    Link: ({
      children,
      href,
      className,
    }: {
      children: React.ReactNode;
      href: string;
      className?: string;
    }) => (
      <a href={href} className={className}>
        {children}
      </a>
    ),
    router: {
      get: vi.fn(),
      on: vi.fn(() => vi.fn()),
    },
  };
});

vi.mock('@/Components/theme/use-theme', () => ({
  useTheme: vi.fn(() => ({ theme: 'system', setTheme: vi.fn(), resolvedTheme: 'light' })),
}));

import AdminFindingsIndex from './Index';

const makePagination = <T,>(data: T[]) => ({
  data,
  current_page: 1,
  per_page: 50,
  total: data.length,
  last_page: 1,
  from: data.length > 0 ? 1 : 0,
  to: data.length,
  links: [],
});

const makeFinding = (overrides = {}) => ({
  id: 1,
  analysis_run_id: 10,
  site_name: 'Test Site',
  site_domain: 'test.com',
  dimension_type: 'overall',
  created_at: '2026-01-01T00:00:00Z',
  ...overrides,
});

describe('AdminFindingsIndex', () => {
  beforeEach(() => {
    vi.clearAllMocks();
  });

  it('renders the Findings heading', () => {
    render(<AdminFindingsIndex findings={makePagination([])} filters={{}} />);
    expect(screen.getByRole('heading', { name: 'Findings', level: 1 })).toBeInTheDocument();
  });

  it('shows actionable empty state title when no findings and no filters', () => {
    render(<AdminFindingsIndex findings={makePagination([])} filters={{}} />);
    expect(screen.getByText('No analysis findings yet')).toBeInTheDocument();
    expect(
      screen.getByText('Findings appear here automatically after analysis runs complete.'),
    ).toBeInTheDocument();
  });

  it('shows search-scoped empty state title when filters are active', () => {
    render(<AdminFindingsIndex findings={makePagination([])} filters={{ search: 'no-match' }} />);
    expect(screen.getByText('No findings match your search')).toBeInTheDocument();
    expect(screen.getByText('Try clearing your filters to see all findings.')).toBeInTheDocument();
  });

  it('shows dimension-filter empty state title when dimension filter is active', () => {
    render(
      <AdminFindingsIndex findings={makePagination([])} filters={{ dimension_type: 'device' }} />,
    );
    expect(screen.getByText('No findings match your search')).toBeInTheDocument();
  });

  it('renders a finding row with site and dimension', () => {
    render(<AdminFindingsIndex findings={makePagination([makeFinding()])} filters={{}} />);
    expect(screen.getByText('Test Site')).toBeInTheDocument();
    expect(screen.getByText('test.com')).toBeInTheDocument();
    expect(screen.getByText('overall')).toBeInTheDocument();
  });

  it('renders Export button', () => {
    render(<AdminFindingsIndex findings={makePagination([])} filters={{}} />);
    expect(screen.getByRole('link', { name: /Export/ })).toBeInTheDocument();
  });

  it('renders analysis run link for each finding', () => {
    render(<AdminFindingsIndex findings={makePagination([makeFinding()])} filters={{}} />);
    const runLink = screen.getByText('#10');
    expect(runLink).toBeInTheDocument();
    expect(runLink.closest('a')).toHaveAttribute('href', expect.stringContaining('analysis-runs'));
  });
});
