'use client';

import { useState } from 'react';
import { DailyCheckbox, DailyCheckboxType } from '@/lib/types';
import { Button } from '@/components/ui/Button';
import { Input } from '@/components/ui/Input';
import { Modal } from '@/components/ui/Modal';
import { TaskSelector } from './TaskSelector';

interface EditCheckboxModalProps {
  checkbox: DailyCheckbox;
  isOpen: boolean;
  onClose: () => void;
  onSave: (text: string, type: DailyCheckboxType, taskId?: string) => Promise<void>;
  saving?: boolean;
}

export function EditCheckboxModal({ 
  checkbox, 
  isOpen, 
  onClose, 
  onSave, 
  saving = false 
}: EditCheckboxModalProps) {
  const [text, setText] = useState(checkbox.text);
  const [type, setType] = useState<DailyCheckboxType>(checkbox.type);
  const [taskId, setTaskId] = useState<string | undefined>(checkbox.taskId);

  const handleSave = async () => {
    if (!text.trim()) return;
    
    try {
      await onSave(text.trim(), type, taskId);
      onClose();
    } catch (error) {
      console.error('Erreur lors de la sauvegarde:', error);
    }
  };

  const handleKeyPress = (e: React.KeyboardEvent) => {
    if (e.key === 'Enter' && !e.shiftKey) {
      e.preventDefault();
      handleSave();
    }
  };

  const resetForm = () => {
    setText(checkbox.text);
    setType(checkbox.type);
    setTaskId(checkbox.taskId);
  };

  const handleClose = () => {
    resetForm();
    onClose();
  };

  return (
    <Modal isOpen={isOpen} onClose={handleClose} title="Modifier la tâche">
      <div className="space-y-4">
        {/* Texte */}
        <div>
          <label className="block text-sm font-medium text-[var(--foreground)] mb-2">
            Description
          </label>
          <Input
            value={text}
            onChange={(e) => setText(e.target.value)}
            onKeyDown={handleKeyPress}
            placeholder="Description de la tâche..."
            className="w-full"
            autoFocus
          />
        </div>

        {/* Type */}
        <div>
          <label className="block text-sm font-medium text-[var(--foreground)] mb-2">
            Type
          </label>
          <div className="flex gap-2">
            <Button
              type="button"
              onClick={() => setType('task')}
              variant="ghost"
              size="sm"
              className={`flex items-center gap-2 border-l-4 ${
                type === 'task' 
                  ? 'border-l-green-500 bg-green-100 dark:bg-green-900/40 text-green-900 dark:text-green-100 font-medium' 
                  : 'border-l-green-300 hover:border-l-green-400 opacity-70 hover:opacity-90'
              }`}
            >
              ✅ Tâche
            </Button>
            <Button
              type="button"
              onClick={() => setType('meeting')}
              variant="ghost"
              size="sm"
              className={`flex items-center gap-2 border-l-4 ${
                type === 'meeting' 
                  ? 'border-l-blue-500 bg-blue-100 dark:bg-blue-900/40 text-blue-900 dark:text-blue-100 font-medium' 
                  : 'border-l-blue-300 hover:border-l-blue-400 opacity-70 hover:opacity-90'
              }`}
            >
              🗓️ Réunion
            </Button>
          </div>
        </div>

        {/* Liaison tâche (seulement pour les tâches) */}
        {type === 'task' && (
          <div>
            <label className="block text-sm font-medium text-[var(--foreground)] mb-2">
              Lier à une tâche (optionnel)
            </label>
            <div className="border border-[var(--border)] rounded-lg p-3">
              <TaskSelector
                selectedTaskId={taskId}
                onTaskSelect={setTaskId}
                disabled={saving}
              />
              {taskId && (
                <div className="mt-2 text-xs text-[var(--muted-foreground)]">
                  Tâche liée : #{taskId.slice(-6)}
                </div>
              )}
            </div>
          </div>
        )}

        {/* Actions */}
        <div className="flex gap-2 justify-end pt-4">
          <Button
            type="button"
            onClick={handleClose}
            variant="ghost"
            disabled={saving}
          >
            Annuler
          </Button>
          <Button
            type="button"
            onClick={handleSave}
            variant="primary"
            disabled={!text.trim() || saving}
          >
            {saving ? 'Sauvegarde...' : 'Sauvegarder'}
          </Button>
        </div>
      </div>
    </Modal>
  );
}
