import { Button } from '@/components/ui/button';
import { Form, FormControl, FormField, FormItem, FormLabel, FormMessage } from '@/components/ui/form';
import { Stage3FormValues, stage3Schema } from '@/lib/validations';
import { PageProps, PaymentMethod } from '@/types';
import { zodResolver } from '@hookform/resolvers/zod';
import { usePage } from '@inertiajs/react';
import { ArrowRight, SquareDashedMousePointer } from 'lucide-react';
import { useForm } from 'react-hook-form';

export function DepositStage3({
    onNext,
    onBack,
    onSelectPaymentMethod,
}: {
    onNext: () => void;
    onBack: () => void;
    onSelectPaymentMethod: (method: PaymentMethod) => void;
}) {
    const { paymentMethods } = usePage<PageProps<{ paymentMethods: PaymentMethod[] }>>().props;
    const form = useForm<Stage3FormValues>({
        resolver: zodResolver(stage3Schema),
        defaultValues: {
            paymentMethod: paymentMethods[0].name,
        },
    });

    function onSubmit(values: Stage3FormValues) {
        const method = paymentMethods.find((method) => method.name === values.paymentMethod);
        onSelectPaymentMethod(method as PaymentMethod);
        onNext();
    }

    return (
        <div className="flex-1 p-4">
            <h3 className="mb-6 text-lg font-semibold text-gray-800">Choose your payment method</h3>
            <Form {...form}>
                <form onSubmit={form.handleSubmit(onSubmit)} className="space-y-6">
                    <FormField
                        control={form.control}
                        name="paymentMethod"
                        render={({ field }) => (
                            <FormItem>
                                <FormLabel className="text-gray-500">- Payment method :</FormLabel>
                                <FormControl>
                                    <div className="grid grid-cols-1 gap-4 md:grid-cols-2">
                                        {paymentMethods.map((method) => (
                                            <div
                                                key={method.name}
                                                className={`cursor-pointer rounded-lg border p-4 ${
                                                    field.value === method.name ? 'border-green-500 ring-[1.5px] ring-green-500' : 'border-gray-200'
                                                }`}
                                                onClick={() => field.onChange(method.name)}
                                            >
                                                <div className="mb-2 flex items-center justify-between">
                                                    <p className="text-sm font-medium text-gray-600">Direct deposit from your Wallet</p>
                                                    <SquareDashedMousePointer className="h-5 w-5 text-gray-400" />
                                                </div>
                                                <h4 className="text-base font-semibold">{method.name}</h4>
                                            </div>
                                        ))}
                                    </div>
                                </FormControl>
                                <FormMessage />
                            </FormItem>
                        )}
                    />
                    <div className="flex justify-between">
                        <Button variant="outline" onClick={onBack} className="flex items-center gap-2 bg-transparent">
                            <ArrowRight className="h-4 w-4 rotate-180" /> Back
                        </Button>
                        <Button type="submit" className="flex items-center gap-2 bg-green-500 hover:bg-green-500/90">
                            Continue <ArrowRight className="h-4 w-4" />
                        </Button>
                    </div>
                </form>
            </Form>
        </div>
    );
}
