import * as React from 'react'
import { ChevronLeft, ChevronRight } from 'lucide-react'
import { DayPicker } from 'react-day-picker'

import { cn } from '@/lib/utils'
import { buttonVariants } from './button'

export type CalendarProps = React.ComponentProps<typeof DayPicker>

function Calendar({
  className,
  classNames,
  showOutsideDays = true,
  ...props
}: CalendarProps) {
  return (
    <DayPicker
      showOutsideDays={showOutsideDays}
      className={cn('p-3', className)}
      classNames={{
        months: 'flex flex-col sm:flex-row gap-4',
        month: 'flex flex-col gap-3',
        // Row layout: prev-nav · dropdowns (Month / Year) · next-nav.
        // Works for both button and dropdown caption layouts.
        caption: 'flex items-center justify-between px-1 pt-1',
        caption_label: 'text-sm font-medium text-text',
        // Dropdown caption — react-day-picker v8 renders each dropdown
        // as an invisible <select> layered over a visible caption_label
        // span. We position the <select> absolutely + transparent so
        // the label is what's actually drawn, while clicks still open
        // the native picker. The top-level CaptionLabel ("January 2026"
        // in full) sits inside a .vhidden wrapper — absolutely clipped
        // so it stays screen-reader only.
        caption_dropdowns: 'flex items-center justify-center gap-2',
        dropdown_month: 'relative inline-flex items-center rounded-md px-1 text-sm font-medium text-text hover:bg-surface-2',
        dropdown_year: 'relative inline-flex items-center rounded-md px-1 text-sm font-medium text-text hover:bg-surface-2',
        dropdown:
          'absolute inset-0 z-10 cursor-pointer appearance-none bg-transparent opacity-0',
        dropdown_icon: 'ml-1 h-3 w-3 opacity-60',
        vhidden:
          '!absolute !-m-px !h-px !w-px !overflow-hidden !whitespace-nowrap !border-0 !p-0 ![clip:rect(0,0,0,0)]',
        nav: 'flex items-center gap-1',
        nav_button: cn(
          buttonVariants({ variant: 'ghost' }),
          'h-6 w-6 p-0 text-text-muted hover:text-text'
        ),
        nav_button_previous: '',
        nav_button_next: '',
        table: 'w-full border-collapse',
        head_row: 'flex',
        head_cell:
          'text-text-muted rounded-md w-7 font-normal text-[0.7rem]',
        row: 'flex w-full mt-1',
        cell: cn(
          'relative p-0 text-center text-sm focus-within:relative focus-within:z-20',
          '[&:has([aria-selected])]:bg-surface-2 [&:has([aria-selected].day-outside)]:bg-surface-2/50'
        ),
        day: cn(
          buttonVariants({ variant: 'ghost' }),
          'h-7 w-7 p-0 font-normal aria-selected:opacity-100'
        ),
        day_range_end: 'day-range-end',
        day_selected:
          'bg-primary text-bg hover:bg-primary hover:text-bg focus:bg-primary focus:text-bg',
        day_today: 'text-star',
        day_outside:
          'day-outside text-text-faint aria-selected:bg-surface-2/50 aria-selected:text-text-faint',
        day_disabled: 'text-text-faint opacity-50',
        day_range_middle:
          'aria-selected:bg-surface-2 aria-selected:text-text',
        day_hidden: 'invisible',
        ...classNames,
      }}
      components={{
        IconLeft: () => <ChevronLeft className="h-4 w-4" />,
        IconRight: () => <ChevronRight className="h-4 w-4" />,
      }}
      {...props}
    />
  )
}
Calendar.displayName = 'Calendar'

export { Calendar }
