import React from 'react';
import { describe, it, expect, vi } from 'vitest';
import { render, screen, fireEvent } from '@testing-library/react';
import { Pagination } from '@/components/ui/Pagination';

describe('Pagination', () => {
  it('renders page info and navigation', () => {
    render(
      <Pagination currentPage={2} totalPages={5} total={100} pageSize={20} onPageChange={() => {}} />
    );
    expect(screen.getByText('21–40')).toBeInTheDocument();
    expect(screen.getByText('100')).toBeInTheDocument();
  });

  it('calls onPageChange with next page', () => {
    const onPageChange = vi.fn();
    render(
      <Pagination currentPage={1} totalPages={3} total={60} pageSize={20} onPageChange={onPageChange} />
    );
    fireEvent.click(screen.getByLabelText('Weiter'));
    expect(onPageChange).toHaveBeenCalledWith(2);
  });

  it('calls onPageChange with previous page', () => {
    const onPageChange = vi.fn();
    render(
      <Pagination currentPage={2} totalPages={3} total={60} pageSize={20} onPageChange={onPageChange} />
    );
    fireEvent.click(screen.getByLabelText('Zurück'));
    expect(onPageChange).toHaveBeenCalledWith(1);
  });

  it('disables previous on first page', () => {
    render(
      <Pagination currentPage={1} totalPages={3} total={60} pageSize={20} onPageChange={() => {}} />
    );
    expect(screen.getByLabelText('Zurück')).toBeDisabled();
  });

  it('disables next on last page', () => {
    render(
      <Pagination currentPage={3} totalPages={3} total={60} pageSize={20} onPageChange={() => {}} />
    );
    expect(screen.getByLabelText('Weiter')).toBeDisabled();
  });

  it('returns null when totalPages <= 1', () => {
    const { container } = render(
      <Pagination currentPage={1} totalPages={1} total={5} pageSize={20} onPageChange={() => {}} />
    );
    expect(container.firstChild).toBeNull();
  });

  it('has aria-current on current page button', () => {
    render(
      <Pagination currentPage={2} totalPages={5} total={100} pageSize={20} onPageChange={() => {}} />
    );
    const currentBtn = screen.getByLabelText('Seite 2');
    expect(currentBtn).toHaveAttribute('aria-current', 'page');
  });
});
