/**
 * SchoolCard Component Tests
 */

import '@testing-library/jest-dom';
import { render, screen, fireEvent } from '@testing-library/react';
import { SchoolCard } from '@/components/SchoolCard';
import type { School } from '@/lib/types';

const mockSchool = {
  urn: 100001,
  school_name: 'Test Primary School',
  local_authority: 'Westminster',
  school_type: 'Academy',
  address: '123 Test Street',
  postcode: 'SW1A 1AA',
  latitude: 51.5074,
  longitude: -0.1278,
  rwm_expected_pct: 75.5,
  prev_rwm_expected_pct: 70.0,
} as School;

describe('SchoolCard', () => {
  it('renders school information correctly', () => {
    render(<SchoolCard school={mockSchool} />);

    expect(screen.getByText('Test Primary School')).toBeInTheDocument();
    expect(screen.getByText('Westminster')).toBeInTheDocument();
    expect(screen.getByText('Academy')).toBeInTheDocument();
    expect(screen.getByText('75.5%')).toBeInTheDocument();
  });

  it('links to school detail page', () => {
    render(<SchoolCard school={mockSchool} />);

    const link = screen.getByRole('link', { name: /test primary school/i });
    expect(link).toHaveAttribute('href', '/school/100001-test-primary-school');
  });

  it('calls onAddToCompare when the Compare button is clicked', () => {
    const mockAddToCompare = jest.fn();
    render(<SchoolCard school={mockSchool} onAddToCompare={mockAddToCompare} />);

    const addButton = screen.getByText('+ Compare');
    fireEvent.click(addButton);

    expect(mockAddToCompare).toHaveBeenCalledWith(mockSchool);
    expect(mockAddToCompare).toHaveBeenCalledTimes(1);
  });

  it('does not render the Compare button when handler not provided', () => {
    render(<SchoolCard school={mockSchool} />);

    expect(screen.queryByText('+ Compare')).not.toBeInTheDocument();
  });

  it('displays trend indicator for positive change', () => {
    render(<SchoolCard school={mockSchool} />);

    // Should show upward trend (75.5 > 70.0)
    expect(screen.getByTitle('Previous year: 70.0%')).toBeInTheDocument();
  });
});
