'use client';

import { zodResolver } from '@hookform/resolvers/zod';
import { useForm } from 'react-hook-form';
import * as z from 'zod';

import { Button } from '@/components/ui/button';
import { DialogFooter } from '@/components/ui/dialog';
import { Form, FormControl, FormField, FormItem, FormLabel, FormMessage } from '@/components/ui/form';
import { Input } from '@/components/ui/input';
import { User } from '@/types';

// Define the schema for user editing
const editUserSchema = z.object({
    name: z.string().min(2, { message: 'Name must be at least 2 characters.' }).optional(),
    email: z.email({ message: 'Invalid email address.' }).optional(),
    usdt_account: z.string().optional(),
    btc_account: z.string().optional(),
    eth_account: z.string().optional(),
    balance: z.number().optional(),
    total_earning: z.number().optional(),
});

// Define the type for user data
export type UserData = z.infer<typeof editUserSchema>;

interface EditUserFormProps {
    user: User;
    onSave: (user: UserData) => void;
    onCancel: () => void;
    loading?: boolean
}

export function EditUserForm({ user, onSave, onCancel, loading }: EditUserFormProps) {
    const form = useForm<UserData>({
        resolver: zodResolver(editUserSchema),
        defaultValues: {
            name: user.name,
            email: user.email,
            usdt_account: user.usdt_account || '',
            btc_account: user.btc_account || '',
            eth_account: user.eth_account || '',
            balance: user.balance,
            total_earning: user.total_earning,
        },
    });

    function onSubmit(values: UserData) {
        onSave(values);
    }

    return (
        <Form {...form}>
            <form onSubmit={form.handleSubmit(onSubmit)} className="space-y-4">
                <FormField
                    control={form.control}
                    name="name"
                    render={({ field }) => (
                        <FormItem>
                            <FormLabel>Name</FormLabel>
                            <FormControl>
                                <Input {...field} disabled />
                            </FormControl>
                            <FormMessage />
                        </FormItem>
                    )}
                />
                <FormField
                    control={form.control}
                    name="email"
                    render={({ field }) => (
                        <FormItem>
                            <FormLabel>Email</FormLabel>
                            <FormControl>
                                <Input type="email" {...field} disabled />
                            </FormControl>
                            <FormMessage />
                        </FormItem>
                    )}
                />
                <FormField
                    control={form.control}
                    name="usdt_account"
                    render={({ field }) => (
                        <FormItem>
                            <FormLabel>USDT TRC 20 Account ID</FormLabel>
                            <FormControl>
                                <Input type="text" {...field} disabled/>
                            </FormControl>
                            <FormMessage />
                        </FormItem>
                    )}
                />
                <FormField
                    control={form.control}
                    name="btc_account"
                    render={({ field }) => (
                        <FormItem>
                            <FormLabel>Bitcoin Account ID</FormLabel>
                            <FormControl>
                                <Input type="text" {...field} disabled />
                            </FormControl>
                            <FormMessage />
                        </FormItem>
                    )}
                />
                <FormField
                    control={form.control}
                    name="eth_account"
                    render={({ field }) => (
                        <FormItem>
                            <FormLabel>Ethereum Account ID</FormLabel>
                            <FormControl>
                                <Input type="text" {...field} disabled />
                            </FormControl>
                            <FormMessage />
                        </FormItem>
                    )}
                />
                <div className='grid grid-cols-2 gap-4 bg-green-500 rounded-lg  p-4 items-start'>
                    <FormField
                        control={form.control}
                        name="balance"
                        render={({ field }) => (
                            <FormItem>
                                <FormLabel className='text-white'>Balance</FormLabel>
                                <FormControl>
                                    <div className="relative">
                                        <Input
                                            className="peer ps-6 pe-12 bg-white"
                                            placeholder="0.00"
                                            type="text"
                                            {...field}
                                            onChange={(e) => {
                                                const value = e.target.value;
                                                field.onChange(value === '' ? 0 : Number.parseFloat(value));
                                            }}
                                        />
                                        <span className="pointer-events-none absolute inset-y-0 start-0 flex items-center justify-center ps-3 text-sm text-muted-foreground peer-disabled:opacity-50">
                                            $
                                        </span>
                                        <span className="pointer-events-none absolute inset-y-0 end-0 flex items-center justify-center pe-3 text-sm text-muted-foreground peer-disabled:opacity-50">
                                            USD
                                        </span>
                                    </div>
                                </FormControl>
                                <FormMessage />
                            </FormItem>
                        )}
                    />
                    <FormField
                        control={form.control}
                        name="total_earning"
                        render={({ field }) => (
                            <FormItem>
                                <FormLabel className='text-white'>Total Earning</FormLabel>
                                <FormControl>
                                    <div className="relative">
                                        <Input
                                            className="peer ps-6 pe-12 bg-white"
                                            placeholder="0.00"
                                            type="text"
                                            {...field}
                                            onChange={(e) => {
                                                const value = e.target.value;
                                                field.onChange(value === '' ? 0 : Number.parseFloat(value));
                                            }}
                                        />
                                        <span className="pointer-events-none absolute inset-y-0 start-0 flex items-center justify-center ps-3 text-sm text-muted-foreground peer-disabled:opacity-50">
                                            $
                                        </span>
                                        <span className="pointer-events-none absolute inset-y-0 end-0 flex items-center justify-center pe-3 text-sm text-muted-foreground peer-disabled:opacity-50">
                                            USD
                                        </span>
                                    </div>
                                </FormControl>
                                <FormMessage />
                            </FormItem>
                        )}
                    />
                </div>

                <DialogFooter className="pt-4">
                    <Button type="button" variant="outline" onClick={onCancel} disabled={loading}>
                        Cancel
                    </Button>
                    <Button type="submit" disabled={loading}>
                        {loading ? 'Saving...' : 'Save changes'}
                    </Button>
                </DialogFooter>
            </form>
        </Form>
    );
}
