/* eslint-disable @next/next/no-img-element */
import {toast, ToastContainer} from "react-toastify";
import {useState} from "react";
import Head from "next/head";
import useUser from "@/hooks/useUser";
import Button from "@/components/Low/Button";
import {BsArrowRepeat} from "react-icons/bs";
import Link from "next/link";
import Input from "@/components/Low/Input";
import axios from "axios";

export default function Register() {
	const [name, setName] = useState("");
	const [email, setEmail] = useState("");
	const [password, setPassword] = useState("");
	const [confirmPassword, setConfirmPassword] = useState("");
	const [isLoading, setIsLoading] = useState(false);

	const {mutateUser} = useUser({
		redirectTo: "/",
		redirectIfFound: true,
	});

	const register = (e: any) => {
		e.preventDefault();

		if (confirmPassword !== password) {
			toast.error("Your passwords do not match!", {toastId: "password-not-match"});
			return;
		}

		setIsLoading(true);
		axios
			.post("/api/register", {name, email, password, profilePicture: "/defaultAvatar.png"})
			.then((response) => mutateUser(response.data.user))
			.catch((error) => {
				console.log(error.response.data);

				if (error.response.status === 401) {
					toast.error("There is already a user with that e-mail!");
					return;
				}
				toast.error("There was something wrong, please try again!");
			})
			.finally(() => setIsLoading(false));
	};

	return (
		<>
			<Head>
				<title>Register | IELTS GPT</title>
				<meta name="description" content="Generated by create next app" />
				<meta name="viewport" content="width=device-width, initial-scale=1" />
				<link rel="icon" href="/favicon.ico" />
			</Head>
			<main className="w-full h-[100vh] flex bg-white text-black">
				<ToastContainer />
				<section className="h-full w-fit min-w-fit relative">
					<div className="absolute h-full w-full bg-mti-rose-light z-10 bg-opacity-50" />
					<img src="/people-talking-tablet.png" alt="People smiling looking at a tablet" className="h-full aspect-auto" />
				</section>
				<section className="h-full w-full flex flex-col items-center justify-center gap-12">
					<div className="flex flex-col gap-2 items-center relative">
						<img src="/logo_title.png" alt="EnCoach's Logo" className="w-64 absolute -top-64" />
						<h1 className="font-bold text-4xl">Create new account</h1>
					</div>
					<form className="flex flex-col items-center gap-6 w-1/2" onSubmit={register}>
						<Input type="text" name="name" onChange={(e) => setName(e)} placeholder="Enter your name" required />
						<Input type="email" name="email" onChange={(e) => setEmail(e)} placeholder="Enter email address" required />
						<Input type="password" name="password" onChange={(e) => setPassword(e)} placeholder="Enter your password" required />
						<Input
							type="password"
							name="confirmPassword"
							onChange={(e) => setConfirmPassword(e)}
							placeholder="Confirm your password"
							required
						/>
						<Button className="mt-8 w-full" color="purple" disabled={isLoading}>
							{!isLoading && "Create account"}
							{isLoading && (
								<div className="flex items-center justify-center">
									<BsArrowRepeat className="text-white animate-spin" size={25} />
								</div>
							)}
						</Button>
					</form>
					<Link className="text-mti-purple-light text-sm font-normal" href="/login">
						Sign in instead
					</Link>
				</section>
			</main>
		</>
	);
}
