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

import InertiaPagination from './InertiaPagination';

function makeLink(url: string | null, label: string, active = false) {
  return { url, label, active };
}

describe('InertiaPagination', () => {
  describe('single-page suppression', () => {
    it('renders nothing when lastPage <= 1', () => {
      const { container } = render(
        <InertiaPagination links={[]} currentPage={1} lastPage={1} />,
      );
      expect(container.firstChild).toBeNull();
    });

    it('renders nothing when lastPage is 0', () => {
      const { container } = render(
        <InertiaPagination links={[]} currentPage={1} lastPage={0} />,
      );
      expect(container.firstChild).toBeNull();
    });

    // R6FE-007: an all-null-url links array (Laravel sometimes emits this for
    // a single-page result even when lastPage > 1) must also produce no chrome.
    it('renders nothing when links array has entries but all urls are null', () => {
      const links = [
        makeLink(null, '&laquo; Previous'),
        makeLink(null, '1', true),
        makeLink(null, 'Next &raquo;'),
      ];
      const { container } = render(
        <InertiaPagination links={links} currentPage={1} lastPage={2} />,
      );
      expect(container.firstChild).toBeNull();
    });

    it('renders nothing when links is empty and lastPage > 1 (edge: empty array)', () => {
      // hasAnyUrl = false on an empty array, so also hidden
      const { container } = render(
        <InertiaPagination links={[]} currentPage={1} lastPage={2} />,
      );
      expect(container.firstChild).toBeNull();
    });
  });

  describe('multi-page rendering', () => {
    it('renders Prev and Next buttons when lastPage > 1 and links have urls', () => {
      const links = [
        makeLink(null, '&laquo; Previous'),
        makeLink('/items?page=1', '1', true),
        makeLink('/items?page=2', '/items?page=2'),
        makeLink('/items?page=2', 'Next &raquo;'),
      ];
      render(<InertiaPagination links={links} currentPage={1} lastPage={2} />);
      expect(screen.getByRole('button', { name: /previous/i })).toBeInTheDocument();
      expect(screen.getByRole('link', { name: /next/i })).toBeInTheDocument();
    });

    it('shows disabled Previous button when prevLink url is null but nextLink has a url', () => {
      const links = [
        makeLink(null, '&laquo; Previous'),
        makeLink('/items?page=2', 'Next &raquo;'),
      ];
      render(<InertiaPagination links={links} currentPage={1} lastPage={2} />);
      const prevBtn = screen.getByRole('button', { name: /previous/i });
      expect(prevBtn).toBeDisabled();
    });

    it('shows page label by default', () => {
      const links = [
        makeLink(null, '&laquo; Previous'),
        makeLink('/items?page=2', 'Next &raquo;'),
      ];
      render(<InertiaPagination links={links} currentPage={1} lastPage={3} />);
      expect(screen.getByText('Page 1 of 3')).toBeInTheDocument();
    });

    it('shows items range when perPage and total are provided', () => {
      const links = [
        makeLink(null, 'Previous'),
        makeLink('/items?page=2', 'Next'),
      ];
      render(
        <InertiaPagination links={links} currentPage={1} lastPage={2} perPage={10} total={15} />,
      );
      expect(screen.getByText('Showing 1–10 of 15 items')).toBeInTheDocument();
    });
  });
});
