"use client";
import SignupForm from "@/components/user/SignupForm";
import { useRouter } from "next/navigation";
import { Flip, toast } from "react-toastify";
import checkPasswordStrength from "@/lib/strongPassCheck";
const SignUpClient = () => {
  const router = useRouter();
  const createUser = async (
    email: string,
    password: string,
    full_name: string,

  ) => {
    try {
      const response = await fetch("/api/user", {
        method: "POST",
        headers: {
          "Content-Type": "application/json",
        },
        body: JSON.stringify({
          full_name,
          email,
          password,

        }),
      });
      return response;
    } catch (error: any) {
      throw new Error("Failed to create user: " + error.message);
    }
  };

  const handleCreateUser = async (
    email: string,
    password: string,
    full_Name: string,

  ) => {
    try {
      const response = await createUser(
        email,
        password,
        full_Name,

      );
      if (response.ok) {
        const passwordStrength = checkPasswordStrength(password);
        let passwordStrengthMessage = "";
        if (passwordStrength === 1) {
          passwordStrengthMessage = "Weak password";
        } else if (passwordStrength === 2) {
          passwordStrengthMessage = "Medium password";
        } else {
          passwordStrengthMessage = "Strong password";
        }
        toast.success("Account created successfully, Verify your mail now. Do check your spam folder if you don't find the verification mail ", {
          position: "top-right",
          autoClose: false,
          hideProgressBar: false,
          closeOnClick: true,
          pauseOnHover: true,
          draggable: true,
          progress: undefined,
          theme: "colored",
          transition: Flip,
        });
        toast.info(passwordStrengthMessage, {
          position: "top-right",
          autoClose: 4000,
          hideProgressBar: false,
          closeOnClick: true,
          pauseOnHover: true,
          draggable: true,
          progress: undefined,
          theme: "colored",
          transition: Flip,
        });
        router.push("/login");
      } else {
        const res = await response.json();
        toast.error(res.message, {
          position: "top-right",
          // autoClose: 4000,
          hideProgressBar: false,
          closeOnClick: true,
          pauseOnHover: true,
          draggable: true,
          progress: undefined,
          theme: "colored",
          transition: Flip,
        });
      }
    } catch (error: any) {
      throw new Error(error.message);
    }
  };
  return (
    <div>
      <div className="flex min-h-svh flex-col items-center justify-center gap-6 bg-muted p-6 md:p-10 bg-gray-200">
        <div className="flex w-full max-w-sm flex-col gap-6">
          <SignupForm onEmailSignup={handleCreateUser} />
        </div>
      </div>
    </div>
  );
};

export default SignUpClient;
