import { format, addDays, startOfYear, endOfYear, subDays } from "date-fns";
import { Calendar as CalendarIcon } from "lucide-react";
import { DateRange } from "react-day-picker";
import { cn } from "@/lib/utils";
import { Button } from "@/components/ui/button";
import { Calendar } from "@/components/ui/calendar";
import {
  Popover,
  PopoverContent,
  PopoverTrigger,
} from "@/components/ui/popover";

interface DateRangePickerProps {
  value: DateRange;
  onChange: (range: DateRange | undefined) => void;
  className?: string;
  future?: boolean;
}

export function DateRangePicker({
  value,
  onChange,
  className,
  future = false,
}: DateRangePickerProps) {
  const today = new Date();

  const presets = future ? [
    {
      label: "Next 30 days",
      range: {
        from: today,
        to: addDays(today, 30),
      },
    },
    {
      label: "Next 90 days",
      range: {
        from: today,
        to: addDays(today, 90),
      },
    },
    {
      label: "Rest of year",
      range: {
        from: today,
        to: endOfYear(today),
      },
    },
  ] : [
    {
      label: "Last 7 days",
      range: {
        from: subDays(today, 7),
        to: today,
      },
    },
    {
      label: "Last 30 days",
      range: {
        from: subDays(today, 30),
        to: today,
      },
    },
    {
      label: "Last 90 days",
      range: {
        from: subDays(today, 90),
        to: today,
      },
    },
    {
      label: "Year to date",
      range: {
        from: startOfYear(today),
        to: today,
      },
    },
  ];

  return (
    <div className={cn("grid gap-1", className)}>
      <Popover>
        <PopoverTrigger asChild className="p-3">
          <Button
            id="date"
            variant={"outline"}
            className={cn(
              "h-8 w-[230px] justify-start text-left font-normal",
              !value && "text-muted-foreground"
            )}
          >
            <CalendarIcon className="h-4 w-4" />
            {value?.from ? (
              value.to ? (
                <>
                  {format(value.from, "LLL d, y")} -{" "}
                  {format(value.to, "LLL d, y")}
                </>
              ) : (
                format(value.from, "LLL dd, y")
              )
            ) : (
              <span>Pick a date range</span>
            )}
          </Button>
        </PopoverTrigger>
        <PopoverContent className="w-auto p-3" align="start">
          <div className="flex gap-2 pb-4">
            {presets.map((preset) => (
              <Button
                key={preset.label}
                variant="outline"
                size="sm"
                onClick={() => onChange(preset.range)}
              >
                {preset.label}
              </Button>
            ))}
          </div>
          <Calendar
            initialFocus
            mode="range"
            defaultMonth={value?.from}
            selected={value}
            onSelect={(range) => {
              if (range) onChange(range);
            }}
            numberOfMonths={2}
          />
        </PopoverContent>
      </Popover>
    </div>
  );
} 