'use client';

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 '@/components/ui/button';

export type CalendarProps = React.ComponentProps<typeof DayPicker>;

function Calendar({
  className,
  classNames,
  showOutsideDays = true,
  captionLayout,
  ...props
}: CalendarProps) {
  const usesDropdowns =
    captionLayout === 'dropdown' ||
    captionLayout === 'dropdown-months' ||
    captionLayout === 'dropdown-years';

  return (
    <DayPicker
      showOutsideDays={showOutsideDays}
      captionLayout={captionLayout}
      className={cn('p-3 font-sans', className)}
      classNames={{
        months: 'flex flex-col sm:flex-row space-y-4 sm:space-x-4 sm:space-y-0',
        month: 'space-y-3',
        month_caption: 'flex justify-center pt-1 relative items-center gap-1 px-9 min-h-9',
        // Hide label when dropdowns are shown — prevents duplicate month/year text
        caption_label: cn(
          'text-sm font-semibold tracking-tight text-foreground',
          usesDropdowns && 'hidden'
        ),
        dropdowns: 'flex items-center justify-center gap-1.5',
        months_dropdown: 'relative',
        years_dropdown: 'relative',
        dropdown:
          'h-8 appearance-none rounded-md border border-border bg-card pl-2.5 pr-7 text-xs font-medium text-foreground shadow-sm cursor-pointer focus:outline-none focus:ring-2 focus:ring-ring/40',
        nav: 'flex items-center',
        button_previous: cn(
          buttonVariants({ variant: 'outline' }),
          'h-7 w-7 bg-transparent p-0 opacity-70 hover:opacity-100 hover:bg-muted transition-colors absolute left-1 top-1/2 -translate-y-1/2'
        ),
        button_next: cn(
          buttonVariants({ variant: 'outline' }),
          'h-7 w-7 bg-transparent p-0 opacity-70 hover:opacity-100 hover:bg-muted transition-colors absolute right-1 top-1/2 -translate-y-1/2'
        ),
        month_grid: 'w-full border-collapse space-y-1',
        weekdays: 'flex',
        weekday:
          'text-muted-foreground rounded-md w-9 font-medium text-[0.7rem] uppercase tracking-wide',
        week: 'flex w-full mt-1.5',
        day: cn(
          'relative h-9 w-9 p-0 text-center text-sm focus-within:relative focus-within:z-20',
          '[&:has([aria-selected])]:bg-primary/10',
          'first:[&:has([aria-selected])]:rounded-l-lg last:[&:has([aria-selected])]:rounded-r-lg',
          '[&:has([aria-selected].day-outside)]:bg-primary/5'
        ),
        day_button: cn(
          buttonVariants({ variant: 'ghost' }),
          'h-9 w-9 p-0 font-normal rounded-lg transition-colors',
          'hover:bg-primary/10 hover:text-foreground',
          'aria-selected:opacity-100'
        ),
        range_end: 'day-range-end',
        selected:
          'bg-primary text-primary-foreground hover:bg-primary hover:text-primary-foreground focus:bg-primary focus:text-primary-foreground shadow-sm',
        today:
          'bg-accent text-accent-foreground ring-1 ring-inset ring-primary/30 font-semibold',
        outside:
          'day-outside text-muted-foreground opacity-40 aria-selected:bg-primary/10 aria-selected:text-muted-foreground aria-selected:opacity-40',
        disabled: 'text-muted-foreground opacity-35 cursor-not-allowed hover:bg-transparent',
        range_middle:
          'aria-selected:bg-primary/10 aria-selected:text-foreground rounded-none',
        hidden: 'invisible',
        ...classNames,
      }}
      components={{
        Chevron: ({ orientation, ...chevronProps }) =>
          orientation === 'right' ? (
            <ChevronRight className="h-4 w-4" {...chevronProps} />
          ) : (
            <ChevronLeft className="h-4 w-4" {...chevronProps} />
          ),
      }}
      {...props}
    />
  );
}
Calendar.displayName = 'Calendar';

export { Calendar };
