"use client"

import * as React from "react"
import type { DateRange } from "react-day-picker"

import { Calendar } from "@/components/ui/calendar"
import {
  DatePickerField,
  DateRangePickerField,
  DateTextInputField,
} from "@/components/ui/date-picker-field"
import { Field, FieldDescription, FieldLabel } from "@/components/ui/field"

export function CalendarPreview() {
  const [date, setDate] = React.useState<Date | undefined>(new Date())
  return (
    <Calendar
      mode="single"
      selected={date}
      onSelect={setDate}
      className="rounded-md border"
    />
  )
}

export function CalendarDropdownCaptionPreview() {
  const [date, setDate] = React.useState<Date | undefined>(new Date())
  return (
    <Calendar
      mode="single"
      selected={date}
      onSelect={setDate}
      captionLayout="dropdown"
      fromYear={2020}
      toYear={2032}
      className="rounded-md border"
    />
  )
}

export function CalendarRangePreview() {
  const [range, setRange] = React.useState<DateRange | undefined>(() => ({
    from: new Date(),
    to: new Date(Date.now() + 7 * 24 * 60 * 60 * 1000),
  }))
  return (
    <Calendar
      mode="range"
      selected={range}
      onSelect={setRange}
      numberOfMonths={1}
      className="rounded-md border"
    />
  )
}

export function CalendarTwoMonthPreview() {
  const [range, setRange] = React.useState<DateRange | undefined>(() => ({
    from: new Date(),
    to: new Date(Date.now() + 14 * 24 * 60 * 60 * 1000),
  }))
  return (
    <Calendar
      mode="range"
      selected={range}
      onSelect={setRange}
      numberOfMonths={2}
      captionLayout="dropdown"
      fromYear={2020}
      toYear={2032}
      className="rounded-md border"
    />
  )
}

export function DatePickerPreview() {
  const [date, setDate] = React.useState<Date | undefined>(new Date())
  return (
    <Field orientation="vertical" className="max-w-xs">
      <FieldLabel htmlFor="ds-date">Due date</FieldLabel>
      <DatePickerField id="ds-date" value={date} onChange={setDate} />
      <FieldDescription>Popover calendar with dropdown month/year caption.</FieldDescription>
    </Field>
  )
}

export function DateRangePickerPreview() {
  const [range, setRange] = React.useState<DateRange | undefined>(() => ({
    from: new Date(),
    to: new Date(Date.now() + 7 * 24 * 60 * 60 * 1000),
  }))
  return (
    <div className="flex flex-col gap-4 sm:flex-row sm:items-start">
      <Field orientation="vertical" className="max-w-xs">
        <FieldLabel htmlFor="ds-range-1">One month</FieldLabel>
        <DateRangePickerField
          id="ds-range-1"
          value={range}
          onChange={setRange}
          numberOfMonths={1}
        />
      </Field>
      <Field orientation="vertical" className="max-w-sm">
        <FieldLabel htmlFor="ds-range-2">Two months</FieldLabel>
        <DateRangePickerField
          id="ds-range-2"
          value={range}
          onChange={setRange}
          numberOfMonths={2}
        />
      </Field>
    </div>
  )
}

export function DateTextInputPreview() {
  const [value, setValue] = React.useState("06/24/2026")
  return (
    <Field orientation="vertical" className="max-w-xs">
      <FieldLabel htmlFor="ds-date-text">Screening date</FieldLabel>
      <DateTextInputField
        id="ds-date-text"
        aria-label="Screening date"
        value={value}
        onValueChange={setValue}
      />
      <FieldDescription>Masked text input plus calendar icon for hub filters.</FieldDescription>
    </Field>
  )
}
