import { Button } from '@/components/ui/button';
import { Input } from '@/components/ui/input';
import { Label } from '@/components/ui/label';
import InputError from '@/components/InputError';
import { useForm } from '@inertiajs/react';
import { LoaderCircle } from 'lucide-react';
import { FormEventHandler } from 'react';
import ComponentTitle from '@/components/ui/ComponentTitle';

interface MilestoneFormData {
    target: string;
    description: string;
    [key: string]: string;
}

interface AddMilestoneFormProps {
    onSuccess?: () => void;
    onCancel?: () => void;
}

export default function AddMilestoneForm({ onSuccess, onCancel }: AddMilestoneFormProps) {
    const { data, setData, post, processing, errors, reset } = useForm<MilestoneFormData>({
        target: '',
        description: '',
    });

    const submit: FormEventHandler = (e) => {
        e.preventDefault();

        post(route('milestones.store'), {
            onSuccess: () => {
                reset();
                if (onSuccess) {
                    onSuccess();
                }
            },
        });
    };

    return (
        <div className="w-full">
            <div className="space-y-4">
                <ComponentTitle>ADD MILESTONE</ComponentTitle>
                <form onSubmit={submit} className="space-y-4">
                    <div>
                        <Label htmlFor="target" className="text-red-400 font-mono text-xs uppercase tracking-wider">&gt; Target Number</Label>
                        <Input
                            id="target"
                            type="number"
                            step="1"
                            min="1"
                            placeholder="1500"
                            value={data.target}
                            onChange={(e) => setData('target', e.target.value)}
                            className="bg-black border-red-500 text-red-400 focus:border-red-300 font-mono text-sm rounded-none border-2 focus:ring-0 focus:outline-none placeholder:text-red-400/40 transition-all glow-red"
                        />
                        <InputError message={errors.target} />
                    </div>

                    <div>
                        <Label htmlFor="description" className="text-red-400 font-mono text-xs uppercase tracking-wider">&gt; Description</Label>
                        <Input
                            id="description"
                            type="text"
                            placeholder="First milestone"
                            value={data.description}
                            onChange={(e) => setData('description', e.target.value)}
                            className="bg-black border-red-500 text-red-400 focus:border-red-300 font-mono text-sm rounded-none border-2 focus:ring-0 focus:outline-none placeholder:text-red-400/40 transition-all glow-red"
                        />
                        <InputError message={errors.description} />
                    </div>

                    <div className="flex gap-3 pt-2">
                        <Button
                            type="submit"
                            disabled={processing}
                            className="flex-1 bg-red-500 hover:bg-red-500 text-black font-mono text-sm font-bold border-red-500 rounded-none border-2 uppercase tracking-wider transition-all glow-red"
                        >
                            {processing && <LoaderCircle className="mr-2 h-4 w-4 animate-spin" />}
                            [EXECUTE]
                        </Button>
                        {onCancel && (
                            <Button
                                type="button"
                                variant="outline"
                                onClick={onCancel}
                                className="flex-1 bg-black border-red-500 text-red-400 hover:bg-red-950 hover:text-red-300 font-mono text-sm font-bold rounded-none border-2 uppercase tracking-wider transition-all glow-red"
                            >
                                [ABORT]
                            </Button>
                        )}
                    </div>
                </form>
            </div>
        </div>
    );
}
