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 PriceUpdateFormData {
    date: string;
    price: string;
    [key: string]: string;
}

interface UpdatePriceFormProps {
    currentPrice?: number;
    className?: string;
    onSuccess?: () => void;
    onCancel?: () => void;
}

export default function UpdatePriceForm({ currentPrice, className, onSuccess, onCancel }: UpdatePriceFormProps) {
    const { data, setData, post, processing, errors } = useForm<PriceUpdateFormData>({
        date: new Date().toISOString().split('T')[0], // Today's date in YYYY-MM-DD format
        price: currentPrice?.toString() || '100.00',
    });

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

        post(route('pricing.update'), {
            onSuccess: () => {
                // Keep the date, reset only price if needed
                // User might want to update same day multiple times
                if (onSuccess) {
                    onSuccess();
                }
            },
            onError: (errors) => {
                console.error('Price update failed:', errors);
            }
        });
    };

    return (
        <div className="w-full">
            <div className="space-y-4">
                <ComponentTitle>UPDATE PRICE</ComponentTitle>
                {currentPrice && (
                    <p className="text-sm text-red-400/60 font-mono">
                        [CURRENT] €{currentPrice.toFixed(4)}
                    </p>
                )}
                <form onSubmit={submit} className="space-y-4">
                    <div>
                        <Label htmlFor="date" className="text-red-400 font-mono text-xs uppercase tracking-wider">&gt; Price Date</Label>
                        <Input
                            id="date"
                            type="date"
                            value={data.date}
                            onChange={(e) => setData('date', e.target.value)}
                            max={new Date().toISOString().split('T')[0]}
                            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 transition-all glow-red"
                        />
                        <InputError message={errors.date} />
                    </div>

                    <div>
                        <Label htmlFor="price" className="text-red-400 font-mono text-xs uppercase tracking-wider">&gt; Asset Price (€)</Label>
                        <Input
                            id="price"
                            type="number"
                            step="0.0001"
                            min="0"
                            placeholder="123.4567"
                            value={data.price}
                            onChange={(e) => setData('price', 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 focus:shadow-[0_0_10px_rgba(239,68,68,0.5)] placeholder:text-red-400/40 transition-all"
                        />
                        <p className="text-xs text-red-400/60 mt-1 font-mono">
                            [UNIT] price per share
                        </p>
                        <InputError message={errors.price} />
                    </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>
    );
}
