/**
 * (share/ui/pagination — a11y links, disabled, keyboard, nav, ellipsis).
 */
import React from 'react'
import { render, screen, fireEvent } from '@testing-library/react'
import {
  Pagination,
  PaginationContent,
  PaginationItem,
  PaginationLink,
  PaginationPrevious,
  PaginationNext,
  PaginationEllipsis,
} from '@/share/ui/pagination'

jest.mock('@/share/ui/button', () => ({
  Button: ({ children, asChild, onClick, ...p }: any) =>
    asChild ? (
      <>{children}</>
    ) : (
      <button type="button" onClick={onClick} {...p}>
        {children}
      </button>
    ),
}))
jest.mock('lucide-react', () => ({
  ChevronLeftIcon: () => <span data-testid="chevron-left" />,
  ChevronRightIcon: () => <span data-testid="chevron-right" />,
  MoreHorizontalIcon: () => <span data-testid="ellipsis-icon" />,
}))

describe('[share/ui/pagination]', () => {
  describe('navigation landmark', () => {
    it('renders nav with page link', () => {
      render(
        <Pagination>
          <PaginationContent>
            <PaginationItem>
              <PaginationLink href="#">1</PaginationLink>
            </PaginationItem>
          </PaginationContent>
        </Pagination>,
      )
      expect(
        screen.getByRole('navigation', { name: 'pagination' }),
      ).toBeInTheDocument()
      expect(screen.getByText('1')).toBeInTheDocument()
    })
  })

  describe('PaginationLink states', () => {
    it('marks current page and disabled links', () => {
      render(
        <Pagination>
          <PaginationContent>
            <PaginationItem>
              <PaginationLink href="#" isActive>
                2
              </PaginationLink>
            </PaginationItem>
            <PaginationItem>
              <PaginationLink href="#" disabled>
                3
              </PaginationLink>
            </PaginationItem>
          </PaginationContent>
        </Pagination>,
      )
      const activeLink = screen.getByRole('link', { name: '2', current: 'page' })
      const disabledLink = screen.getByRole('button', { name: '3' })
      expect(activeLink).toHaveAttribute('aria-current', 'page')
      expect(disabledLink).toHaveAttribute('aria-disabled', 'true')
    })

    it('invokes onClick when not disabled', () => {
      const onClick = jest.fn()
      render(
        <Pagination>
          <PaginationContent>
            <PaginationItem>
              <PaginationLink href="#" onClick={onClick}>
                1
              </PaginationLink>
            </PaginationItem>
          </PaginationContent>
        </Pagination>,
      )
      fireEvent.click(screen.getByText('1'))
      expect(onClick).toHaveBeenCalled()
    })

    it('does not invoke onClick when disabled', () => {
      const onClick = jest.fn()
      render(
        <Pagination>
          <PaginationContent>
            <PaginationItem>
              <PaginationLink href="#" disabled onClick={onClick}>
                1
              </PaginationLink>
            </PaginationItem>
          </PaginationContent>
        </Pagination>,
      )
      fireEvent.click(screen.getByText('1'))
      expect(onClick).not.toHaveBeenCalled()
    })
  })

  describe('keyboard interaction', () => {
    it('fires click on Space when enabled', () => {
      const onClick = jest.fn()
      render(
        <Pagination>
          <PaginationContent>
            <PaginationItem>
              <PaginationLink href="#" onClick={onClick}>
                1
              </PaginationLink>
            </PaginationItem>
          </PaginationContent>
        </Pagination>,
      )

      fireEvent.keyDown(screen.getByText('1'), { key: ' ' })
      expect(onClick).toHaveBeenCalled()
    })

    it('does not fire on Enter when disabled', () => {
      const onClick = jest.fn()
      render(
        <Pagination>
          <PaginationContent>
            <PaginationItem>
              <PaginationLink href="#" disabled onClick={onClick}>
                1
              </PaginationLink>
            </PaginationItem>
          </PaginationContent>
        </Pagination>,
      )

      const btn = screen.getByRole('button', { name: '1' })
      fireEvent.keyDown(btn, { key: 'Enter' })
      expect(onClick).not.toHaveBeenCalled()
    })
  })

  describe('Previous / Next', () => {
    it('renders labeled previous link with custom text', () => {
      render(
        <Pagination>
          <PaginationContent>
            <PaginationPrevious href="#" text="Previous" />
          </PaginationContent>
        </Pagination>,
      )
      expect(
        screen.getByRole('link', { name: /go to previous page/i }),
      ).toBeInTheDocument()
      expect(screen.getByText('Previous')).toBeInTheDocument()
    })

    it('renders labeled next link with custom text', () => {
      render(
        <Pagination>
          <PaginationContent>
            <PaginationNext href="#" text="Next" />
          </PaginationContent>
        </Pagination>,
      )
      expect(
        screen.getByRole('link', { name: /go to next page/i }),
      ).toBeInTheDocument()
      expect(screen.getByText('Next')).toBeInTheDocument()
    })

    it('omits visible Previous/Next text when not passed', () => {
      render(
        <Pagination>
          <PaginationContent>
            <PaginationPrevious href="#" />
            <PaginationNext href="#" />
          </PaginationContent>
        </Pagination>,
      )

      expect(
        screen.getByRole('link', { name: /go to previous page/i }),
      ).toBeInTheDocument()
      expect(
        screen.getByRole('link', { name: /go to next page/i }),
      ).toBeInTheDocument()
      expect(screen.queryByText('Previous')).not.toBeInTheDocument()
      expect(screen.queryByText('Next')).not.toBeInTheDocument()
    })
  })

  describe('ellipsis', () => {
    it('renders ellipsis slot and accessible label', () => {
      render(
        <Pagination>
          <PaginationContent>
            <PaginationItem>
              <PaginationEllipsis />
            </PaginationItem>
          </PaginationContent>
        </Pagination>,
      )
      expect(
        document.querySelector('[data-slot="pagination-ellipsis"]'),
      ).toBeInTheDocument()
      expect(screen.getByText('More pages')).toBeInTheDocument()
    })
  })
})
