import '@testing-library/jest-dom'
import { render, fireEvent, act } from '@testing-library/react'
import { vi } from 'vitest'

import { PktCalendar, IPktCalendar } from './Calendar'

const createCalendar = (props: Partial<IPktCalendar> = {}) => {
  return render(<PktCalendar {...props} />)
}

describe('PktCalendar', () => {
  describe('Keyboard navigation', () => {
    test('handles arrow key navigation', () => {
      const { container } = createCalendar()

      const firstDate = container.querySelector(
        '.pkt-calendar__date:not(.pkt-calendar__date--disabled)',
      ) as HTMLElement

      act(() => {
        firstDate?.focus()
        fireEvent.keyDown(firstDate!, { key: 'ArrowRight' })
      })

      // Check that focus moved (implementation dependent)
      expect(document.activeElement).toBeTruthy()
    })

    test('handles enter key for date selection', () => {
      const { container } = createCalendar()

      const firstDate = container.querySelector(
        '.pkt-calendar__date:not(.pkt-calendar__date--disabled)',
      ) as HTMLElement

      act(() => {
        firstDate?.focus()
        fireEvent.keyDown(firstDate!, { key: 'Enter' })
      })

      expect(firstDate).toHaveClass('pkt-calendar__date--selected')
    })

    test('handles escape key', () => {
      const onClose = vi.fn()
      const { container } = createCalendar({ onClose })

      const calendarEl = container.querySelector('.pkt-calendar') as HTMLElement
      fireEvent.keyDown(calendarEl, { key: 'Escape' })

      // Should trigger close callback
      expect(onClose).toHaveBeenCalled()
    })
  })

  describe('Edge cases and error handling', () => {
    test('handles invalid date formats gracefully', () => {
      const { container } = createCalendar({ earliest: 'invalid-date', latest: 'also-invalid' })

      // Should not crash and render normally
      expect(container.querySelector('.pkt-calendar')).toBeInTheDocument()
      const calendarBody = container.querySelector('.pkt-calendar__body')
      expect(calendarBody).toBeInTheDocument()
    })

    test('handles empty selected dates array', () => {
      const { container } = createCalendar({ selected: [] })

      const selectedDates = container.querySelectorAll('.pkt-calendar__date--selected')
      expect(selectedDates.length).toBe(0)
    })

    test('handles conflicting properties gracefully', () => {
      // Both multiple and range shouldn't typically be used together
      const { container } = createCalendar({ multiple: true, range: true })

      // Should still render without errors
      expect(container.querySelector('.pkt-calendar')).toBeInTheDocument()
    })

    test('handles very early dates', () => {
      const { container } = createCalendar({ currentmonth: '1900-01-01' })

      expect(container.querySelector('.pkt-calendar')).toBeInTheDocument()
      const calendarBody = container.querySelector('.pkt-calendar__body')
      expect(calendarBody).toBeInTheDocument()
    })

    test('handles far future dates', () => {
      const { container } = createCalendar({ currentmonth: '2100-12-31' })

      expect(container.querySelector('.pkt-calendar')).toBeInTheDocument()
      const calendarBody = container.querySelector('.pkt-calendar__body')
      expect(calendarBody).toBeInTheDocument()
    })
  })
})
