import { useState } from 'react';
import { X, CheckCircle, FileText } from 'lucide-react';
import { tasksApi, Task } from '../../lib/tasksApi';

interface CompleteTaskModalProps {
    task: Task;
    onClose: () => void;
    onSuccess: () => void;
}

export default function CompleteTaskModal({ task, onClose, onSuccess }: CompleteTaskModalProps) {
    const [notes, setNotes] = useState('');
    const [isSubmitting, setIsSubmitting] = useState(false);

    const handleSubmit = async (e: React.FormEvent) => {
        e.preventDefault();
        setIsSubmitting(true);
        try {
            await tasksApi.complete(task.id, { notes });
            onSuccess();
            onClose();
        } catch (error) {
            console.error('Failed to complete task', error);
        } finally {
            setIsSubmitting(false);
        }
    };

    return (
        <div className="fixed inset-0 z-50 flex items-center justify-center p-4 bg-black/50 backdrop-blur-sm animate-fade-in">
            <div className="bg-[var(--color-bg-elevated)] w-full max-w-md rounded-2xl shadow-2xl overflow-hidden animate-scale-in">
                <div className="p-6 border-b border-[var(--color-border-default)] flex justify-between items-center">
                    <h2 className="text-xl font-bold dark:text-white flex items-center gap-2">
                        <CheckCircle className="text-[var(--color-primary)]" />
                        Complete Task
                    </h2>
                    <button onClick={onClose} className="text-[var(--color-text-tertiary)] hover:text-slate-700 dark:hover:text-slate-300">
                        <X size={24} />
                    </button>
                </div>

                <form onSubmit={handleSubmit} className="p-6 space-y-4">
                    <div className="bg-[var(--color-bg-tertiary)] p-4 rounded-xl border border-[var(--color-border-default)]">
                        <h3 className="font-bold text-[var(--color-text-primary)]">{task.title}</h3>
                        {task.description && (
                            <p className="text-sm text-[var(--color-text-tertiary)] mt-1">{task.description}</p>
                        )}
                    </div>

                    <div>
                        <label className="block text-sm font-medium text-[var(--color-text-secondary)] mb-2">
                            Completion Notes (Optional)
                        </label>
                        <textarea
                            value={notes}
                            onChange={(e) => setNotes(e.target.value)}
                            className="w-full p-3 rounded-lg bg-[var(--color-bg-elevated)] border border-[var(--color-border-default)] dark:text-white h-32 resize-none"
                            placeholder="e.g. Used 50gal instead of 40gal..."
                        />
                    </div>

                    <button
                        type="submit"
                        disabled={isSubmitting}
                        className="w-full py-3 bg-[var(--color-primary)] hover:bg-[var(--color-primary-hover)] text-white font-bold rounded-xl shadow-lg flex items-center justify-center gap-2 disabled:opacity-50"
                    >
                        Mark Completed
                    </button>
                </form>
            </div>
        </div>
    );
}
