/* eslint-disable @typescript-eslint/no-explicit-any */
"use client";

import { Head, useForm, usePage } from "@inertiajs/react";

import { LoaderCircle } from "lucide-react";

import {
    GoogleReCaptchaProvider,
    useGoogleReCaptcha,
} from "react-google-recaptcha-v3";

import logo from "../../../images/logo-white.png";

import TextLink from "@/components/text-link";
import InputError from "@/components/input-error";

import { Button } from "@/components/ui/button";
import { Input } from "@/components/ui/input";
import { Label } from "@/components/ui/label";

import { login } from "@/routes";

import styles from "./register.module.scss";

function RegisterForm() {
    const { appSettings } =
        usePage().props as any;

    const {
        executeRecaptcha,
    } = useGoogleReCaptcha();

    const {
        data,
        setData,
        post,
        processing,
        errors,
    } = useForm({
        email: "",
        recaptcha_token: "",
    } as any);

    const handleSubmit = async (
        e: React.FormEvent<HTMLFormElement>
    ) => {
        e.preventDefault();

        let token = "";

        if (
            appSettings.enable_recaptcha
        ) {
            if (!executeRecaptcha) {
                return;
            }

            token =
                await executeRecaptcha(
                    "register"
                );
        }

        setData((data: any) => ({
            ...data,
            recaptcha_token: token,
        }));
        post(route("register.send-otp"));
    };

    return (
                <>
            <Head title="Create Account" />

            <div className={styles.auth}>
                {/* Header */}

                <header className={styles.header}>
                    <div className={styles.headerInner}>
                        <TextLink href="/">
                            <img
                                src={logo}
                                alt="TraderPlus"
                            />
                        </TextLink>

                        <TextLink href={login()}>
                            Sign In
                        </TextLink>
                    </div>
                </header>

                {/* Content */}

                <main className={styles.content}>
                    <div className={styles.wrapper}>
                        <div className={styles.copy}>
                            <span>
                                GET STARTED
                            </span>

                            <h1>
                                Create your
                                account
                            </h1>

                            <p>
                                Verify your
                                email address
                                to begin your
                                registration.
                            </p>
                        </div>

                        <form
                            onSubmit={handleSubmit}
                            className={styles.form}
                        >
                            <div className={styles.field}>
                                <Label htmlFor="email">
                                    Email
                                    Address
                                </Label>

                                <Input
                                    id="email"
                                    name="email"
                                    type="email"
                                    placeholder="you@example.com"
                                    autoComplete="email"
                                    autoFocus
                                    required
                                    value={data.email}
                                    onChange={(e) =>
                                        setData(
                                            "email",
                                            e.target.value
                                        )
                                    }
                                />

                                <InputError
                                    message={errors.email}
                                />

                                <InputError
                                    message={
                                        errors.recaptcha_token
                                    }
                                />
                            </div>

                            <Button
                                type="submit"
                                disabled={processing}
                                className={
                                    styles.submitBtn
                                }
                            >
                                {processing && (
                                    <LoaderCircle className="h-4 w-4 animate-spin" />
                                )}

                                Send
                                Verification
                                Code
                            </Button>

                            <div
                                className={
                                    styles.footerText
                                }
                            >
                                Already have
                                an account?{" "}
                                <TextLink
                                    href={login()}
                                >
                                    Log in
                                </TextLink>
                            </div>
                        </form>
                    </div>
                </main>
            </div>
        </>
    );
}

export default function Register() {
    const { appSettings } =
        usePage().props as any;

    if (
        !appSettings.enable_recaptcha
    ) {
        return <RegisterForm />;
    }

    return (
        <GoogleReCaptchaProvider
            reCaptchaKey={
                appSettings.recaptcha_site_key
            }
            scriptProps={{
                async: true,
                defer: true,
                appendTo: "head",
            }}
        >
            <RegisterForm />
        </GoogleReCaptchaProvider>
    );
}