import { zodResolver } from "@hookform/resolvers/zod";
import { useForm } from "react-hook-form";
import { Link, useNavigate } from "react-router";
import { z } from "zod";
import { PasswordInput } from "~/components/custom-ui/password-input";
import { ThemeToggle } from "~/components/theme/theme-toggle";
import { Button, buttonVariants } from "~/components/ui/button";
import { Card, CardContent, CardDescription, CardFooter, CardHeader, CardTitle } from "~/components/ui/card";
import { Form, FormControl, FormField, FormItem, FormLabel, FormMessage } from "~/components/ui/form";
import { Input } from "~/components/ui/input";
import auth from "~/lib/api/client/auth";

const schema = z.object({
    email: z.string().email(),
    displayName: z.string().min(1).optional(),
    username: z
        .string()
        .min(3)
        .regex(/^[a-zA-Z0-9_.]+$/),
    password: z.string().min(8),
});

export default function Register() {
    let navigate = useNavigate();

    const form = useForm<z.infer<typeof schema>>({
        resolver: zodResolver(schema),
    });

    async function onSubmit(values: z.infer<typeof schema>) {
        await auth.register(values);

        navigate("/login");
    }

    return (
        <Card style={{ viewTransitionName: "auth-card-view" }}>
            <CardHeader style={{ viewTransitionName: "auth-card-header-view" }} className="relative">
                <CardTitle>Create an account</CardTitle>
                <CardDescription>Please fill out the form below to create an account.</CardDescription>
                <div
                    className="absolute top-0 right-0 px-6"
                    style={{
                        viewTransitionName: "auth-card-header-mode-toggle-view",
                    }}
                >
                    <ThemeToggle />
                </div>
            </CardHeader>
            <CardContent style={{ viewTransitionName: "auth-card-content-view" }}>
                <Form {...form}>
                    <form
                        onSubmit={form.handleSubmit(onSubmit)}
                        className="space-y-4"
                        style={{ viewTransitionName: "auth-form-view" }}
                    >
                        <FormField
                            control={form.control}
                            name="email"
                            render={({ field }) => (
                                <FormItem
                                    style={{
                                        viewTransitionName: "email-field-view",
                                    }}
                                >
                                    <FormLabel required>Email</FormLabel>
                                    <FormControl>
                                        <Input type="email" placeholder="email@example.com" {...field} />
                                    </FormControl>
                                    <FormMessage />
                                </FormItem>
                            )}
                        />
                        <FormField
                            control={form.control}
                            name="username"
                            render={({ field }) => (
                                <FormItem>
                                    <FormLabel required>Username</FormLabel>
                                    <FormControl>
                                        <Input {...field} />
                                    </FormControl>
                                    <FormMessage />
                                </FormItem>
                            )}
                        />
                        <FormField
                            control={form.control}
                            name="displayName"
                            render={({ field }) => (
                                <FormItem>
                                    <FormLabel>Display Name</FormLabel>
                                    <FormControl>
                                        <Input {...field} />
                                    </FormControl>
                                    <FormMessage />
                                </FormItem>
                            )}
                        />
                        <FormField
                            control={form.control}
                            name="password"
                            render={({ field }) => (
                                <FormItem
                                    style={{
                                        viewTransitionName: "password-field-view",
                                    }}
                                >
                                    <FormLabel required>Password</FormLabel>
                                    <FormControl>
                                        <PasswordInput {...field} />
                                    </FormControl>
                                    <FormMessage />
                                </FormItem>
                            )}
                        />
                        <Button className="w-full" type="submit" style={{ viewTransitionName: "submit-button-view" }}>
                            Register
                        </Button>
                    </form>
                </Form>
            </CardContent>
            <CardFooter style={{ viewTransitionName: "auth-card-footer-view" }}>
                <div className="flex items-center">
                    <span className="text-muted-foreground text-sm">Already have an account?</span>
                    <Link
                        className={buttonVariants({
                            variant: "link",
                            size: "sm",
                        })}
                        to="/login"
                        viewTransition
                    >
                        Log In
                    </Link>
                </div>
            </CardFooter>
        </Card>
    );
}
