import { describe, it, expect, vi } from 'vitest'
import { render, screen } from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import { DateRangePicker } from './DateRangePicker'

describe('DateRangePicker', () => {
  it('renders two datetime picker triggers', () => {
    render(
      <DateRangePicker
        value={{ start: new Date('2026-03-19T10:00'), end: new Date('2026-03-19T11:00') }}
        onChange={() => {}}
      />,
    )
    // DateTimePicker renders button triggers with formatted date text
    const buttons = screen.getAllByRole('button')
    // At least 2 buttons are the from/to date picker triggers (plus preset pills)
    expect(buttons.length).toBeGreaterThanOrEqual(2)
  })

  it('renders preset buttons', () => {
    render(
      <DateRangePicker
        value={{ start: new Date(), end: new Date() }}
        onChange={() => {}}
      />,
    )
    expect(screen.getByText('Last 1h')).toBeInTheDocument()
    expect(screen.getByText('Today')).toBeInTheDocument()
  })

  it('calls onChange when a preset is clicked', async () => {
    const onChange = vi.fn()
    const user = userEvent.setup()
    render(
      <DateRangePicker
        value={{ start: new Date(), end: new Date() }}
        onChange={onChange}
      />,
    )
    await user.click(screen.getByText('Last 1h'))
    expect(onChange).toHaveBeenCalledWith(
      expect.objectContaining({ start: expect.any(Date), end: expect.any(Date) }),
    )
  })
})
