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

export function DepositStage1({ onNext, onSelectPlan }: { onNext: () => void; onSelectPlan: (plan: InvestmentPlan) => void }) {
    const { plans } = usePage<PageProps<{ plans: InvestmentPlan[] }>>().props;

    const form = useForm<Stage1FormValues>({
        resolver: zodResolver(stage1Schema),
        defaultValues: {
            plan: plans[0].name,
        },
    });

    function onSubmit(values: Stage1FormValues) {
        const plan = plans.find((plan) => plan.name === values.plan);
        onSelectPlan(plan as InvestmentPlan);
        onNext();
    }

    return (
        <div className="flex-1 p-4">
            <h3 className="mb-6 flex items-center gap-2 text-lg font-semibold text-gray-800">
                <BarChart4 />
                Choose an investment plan
            </h3>
            <Form {...form}>
                <form onSubmit={form.handleSubmit(onSubmit)} className="space-y-6">
                    <FormField
                        control={form.control}
                        name="plan"
                        render={({ field }) => (
                            <FormItem>
                                <FormLabel className="text-gray-500">- Select a plan :</FormLabel>
                                <FormControl>
                                    <div className="grid grid-cols-1 gap-4 md:grid-cols-2">
                                        {plans.map((plan) => (
                                            <div
                                                key={plan.name}
                                                className={cn(`cursor-pointer rounded-lg border border-gray-200 p-4`, {
                                                    'border-green-500 ring-[1.5px] ring-green-500': field.value === plan.name,
                                                })}
                                                onClick={() => field.onChange(plan.name)}
                                            >
                                                <div className="mb-2 flex items-center justify-between">
                                                    <h4 className="font-semibold">{plan.name}</h4>
                                                    <SquareDashedMousePointer className="h-5 w-5 text-gray-400" />
                                                </div>
                                                <p className="text-sm text-gray-600">{plan.range}</p>
                                                <p className="text-sm font-bold text-green-500">{plan.percentage}%</p>
                                            </div>
                                        ))}
                                    </div>
                                </FormControl>
                                <FormMessage />
                            </FormItem>
                        )}
                    />
                    <div className="flex justify-end">
                        <Button type="submit" className="flex bg-green-500 hover:bg-green-500/90 items-center gap-2">
                            Continue <ArrowRight className="h-4 w-4" />
                        </Button>
                    </div>
                </form>
            </Form>
        </div>
    );
}
