'use client'

import { Dialog, DialogContent, DialogHeader, DialogTitle } from "@/components/ui/dialog"
import { Button } from "@/components/ui/button"
import { Input } from "@/components/ui/input"
import { useState, useEffect } from "react"
import { useLanguage } from '@/lib/i18n'

interface ReminderDialogProps {
  open: boolean
  onOpenChange: (open: boolean) => void
  currentReminder: Date | null
  onSave: (date: Date) => void
  onRemove: () => void
}

export function ReminderDialog({ 
  open, 
  onOpenChange, 
  currentReminder, 
  onSave, 
  onRemove 
}: ReminderDialogProps) {
  const { t } = useLanguage()
  const [reminderDate, setReminderDate] = useState('')
  const [reminderTime, setReminderTime] = useState('')

  useEffect(() => {
    if (open) {
      if (currentReminder) {
        const date = new Date(currentReminder)
        setReminderDate(date.toISOString().split('T')[0])
        setReminderTime(date.toTimeString().slice(0, 5))
      } else {
        const tomorrow = new Date(Date.now() + 86400000)
        setReminderDate(tomorrow.toISOString().split('T')[0])
        setReminderTime('09:00')
      }
    }
  }, [open, currentReminder])

  const handleSave = () => {
    if (!reminderDate || !reminderTime) return
    
    const dateTimeString = `${reminderDate}T${reminderTime}`
    const date = new Date(dateTimeString)
    
    if (!isNaN(date.getTime())) {
      onSave(date)
      onOpenChange(false)
    }
  }

  return (
    <Dialog open={open} onOpenChange={onOpenChange}>
      <DialogContent
        onInteractOutside={(event) => {
          const target = event.target as HTMLElement;

          const isSonnerElement =
            target.closest('[data-sonner-toast]') ||
            target.closest('[data-sonner-toaster]') ||
            target.closest('[data-icon]') ||
            target.closest('[data-content]') ||
            target.closest('[data-description]') ||
            target.closest('[data-title]') ||
            target.closest('[data-button]');

          if (isSonnerElement) {
            event.preventDefault();
            return;
          }

          if (target.getAttribute('data-sonner-toaster') !== null) {
            event.preventDefault();
            return;
          }
        }}
      >
        <DialogHeader>
          <DialogTitle>{t('reminder.setReminder')}</DialogTitle>
        </DialogHeader>
        <div className="space-y-4 py-4">
          <div className="space-y-2">
            <label htmlFor="reminder-date" className="text-sm font-medium">
              {t('reminder.reminderDate')}
            </label>
            <Input
              id="reminder-date"
              type="date"
              value={reminderDate}
              onChange={(e) => setReminderDate(e.target.value)}
              className="w-full"
            />
          </div>
          <div className="space-y-2">
            <label htmlFor="reminder-time" className="text-sm font-medium">
              {t('reminder.reminderTime')}
            </label>
            <Input
              id="reminder-time"
              type="time"
              value={reminderTime}
              onChange={(e) => setReminderTime(e.target.value)}
              className="w-full"
            />
          </div>
        </div>
        <div className="flex justify-between">
          <div>
            {currentReminder && (
              <Button variant="outline" onClick={() => { onRemove(); onOpenChange(false); }}>
                {t('reminder.removeReminder')}
              </Button>
            )}
          </div>
          <div className="flex gap-2">
            <Button variant="ghost" onClick={() => onOpenChange(false)}>
              {t('reminder.cancel')}
            </Button>
            <Button onClick={handleSave}>
              {t('reminder.save')}
            </Button>
          </div>
        </div>
      </DialogContent>
    </Dialog>
  )
}
