import React from 'react';
import { Calendar } from 'lucide-react';
import { DateRange } from '../types';

interface DateRangePickerProps {
  dateRange: DateRange;
  onChange: (range: DateRange) => void;
}

export const DateRangePicker: React.FC<DateRangePickerProps> = ({ dateRange, onChange }) => {
  const formatDateForInput = (date: Date) => {
    return date.toISOString().split('T')[0];
  };

  const handleStartChange = (e: React.ChangeEvent<HTMLInputElement>) => {
    const newStart = new Date(e.target.value);
    if (!isNaN(newStart.getTime())) {
      onChange({ ...dateRange, start: newStart });
    }
  };

  const handleEndChange = (e: React.ChangeEvent<HTMLInputElement>) => {
    const newEnd = new Date(e.target.value);
    if (!isNaN(newEnd.getTime())) {
      onChange({ ...dateRange, end: newEnd });
    }
  };

  return (
    <div className="flex items-center gap-2 bg-white border border-slate-200 px-3 py-2 rounded-lg shadow-sm hover:border-slate-300 transition-colors">
      <Calendar size={16} className="text-slate-500 shrink-0" />
      <div className="flex items-center gap-2 text-sm">
        <input
          type="date"
          value={formatDateForInput(dateRange.start)}
          onChange={handleStartChange}
          className="bg-transparent text-slate-700 font-medium outline-none cursor-pointer w-28 md:w-auto"
        />
        <span className="text-slate-400">até</span>
        <input
          type="date"
          value={formatDateForInput(dateRange.end)}
          onChange={handleEndChange}
          className="bg-transparent text-slate-700 font-medium outline-none cursor-pointer w-28 md:w-auto"
        />
      </div>
    </div>
  );
};