import * as React from "react"
import { cva, type VariantProps } from "class-variance-authority"
import { cn } from "@/lib/utils"

const datePickerVariants = cva(
  "flex h-9 w-full rounded-md border border-input bg-transparent px-3 py-1 text-sm shadow-sm transition-colors focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring disabled:cursor-not-allowed disabled:opacity-50",
  {
    variants: {
      variant: {
        default: "",
        error: "border-destructive focus-visible:ring-destructive",
      },
    },
    defaultVariants: {
      variant: "default",
    },
  }
)

export interface DatePickerProps
  extends Omit<React.InputHTMLAttributes<HTMLInputElement>, 'type' | 'value' | 'onChange'>,
    VariantProps<typeof datePickerVariants> {
  value?: string | null
  onChange?: (date: string | null) => void
}

const DatePicker = React.forwardRef<HTMLInputElement, DatePickerProps>(
  ({ className, variant, value, onChange, ...props }, ref) => {
    const handleChange = (e: React.ChangeEvent<HTMLInputElement>) => {
      const newValue = e.target.value || null
      onChange?.(newValue)
    }

    return (
      <input
        type="date"
        className={cn(datePickerVariants({ variant, className }))}
        ref={ref}
        value={value || ""}
        onChange={handleChange}
        {...props}
      />
    )
  }
)
DatePicker.displayName = "DatePicker"

export { DatePicker, datePickerVariants }