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

import AuthHead from "@/components/auth/AuthHead";
import Container from "@/components/elements/Container";
import React, { useState } from "react";
import useAxios from "@/hooks/useAxios";
import { useFormik } from "formik";
import * as yup from "yup";
import { useRouter } from "next/navigation";
import AuthNextButton from "@/components/auth/AuthNextButton";
import toast from "react-hot-toast";

// Validation schema using Yup
const schema = yup.object().shape({
  selectedButton: yup.string(),
});

function UsertypePage() {
  const router = useRouter();
  const { request, loading } = useAxios();
  const mobile =
    typeof window !== "undefined" ? localStorage.getItem("mobile") : null;

  // State for managing submit action
  const [submitAction, setSubmitAction] = useState<"register" | "login" | null>(
    null
  );

  const formik = useFormik({
    initialValues: {
      selectedButton: "",
    },
    validationSchema: schema,
    onSubmit: async (values, { setSubmitting }) => {
      try {
        await request("POST", "/register/user_type", {
          mobile: mobile,
          // user_type: values.selectedButton,
          user_type: "user",
        });

        if (submitAction === "register") {
          // localStorage.setItem("usertype", values.selectedButton);
          localStorage.setItem("usertype", "user");
          router.push("/verify/avatar");
        } else if (submitAction === "login") {
          // localStorage.setItem("usertype", values.selectedButton);
          localStorage.setItem("usertype", "user");
          router.push("/");
        }
      } catch (err: any) {
        toast.error(err?.message || "خطای نامشخصی رخ داد.");
      } finally {
        setSubmitting(false);
      }
    },
  });

  return (
    <Container>
      <div className="p-4 flex flex-col items-center h-screen justify-center">
        {/* <AuthHead title="نوع کاربری" /> */}
        <AuthHead title="" />
        <form
          onSubmit={formik.handleSubmit}
          className="w-full max-w-sm flex flex-col items-center"
        >
          {/* User Type Buttons */}
          {/* <AuthNextButton
            type="button"
            onClick={() => formik.setFieldValue("selectedButton", "user")}
            className={`mt-5 w-full py-3 max-w-[250px] ${
              formik.values.selectedButton === "user"
                ? "!bg-[#FC8EAC] !border-[#FC8EAC] !text-white"
                : ""
            }`}
          >
            کاربر
          </AuthNextButton>
          <AuthNextButton
            type="button"
            onClick={() => formik.setFieldValue("selectedButton", "employer")}
            className={`mt-5 w-full py-3 max-w-[250px] ${
              formik.values.selectedButton === "employer"
                ? "!bg-[#FC8EAC] !border-[#FC8EAC] !text-white"
                : ""
            }`}
          >
            کارفرما
          </AuthNextButton> */}

          {/* Error Message */}
          {formik.errors.selectedButton && formik.touched.selectedButton && (
            <div className="text-red-500 mt-2 text-sm">
              {formik.errors.selectedButton}
            </div>
          )}

          {/* Submit Buttons */}
          <div className="flex gap-3">
            <AuthNextButton
              type="submit"
              onClick={() => setSubmitAction("register")}
              className="mt-20 !border-[#0C8002] !text-[#0C8002]"
              disabled={loading}
            >
              {loading ? "در حال ارسال..." : "ادامه ثبت نام"}
            </AuthNextButton>
            <AuthNextButton
              type="submit"
              onClick={() => setSubmitAction("login")}
              className="mt-20 !border-[#FF0000] !text-[#FF0000]"
              disabled={loading}
            >
              {loading ? "در حال ارسال..." : "ورود به برنامه"}
            </AuthNextButton>
          </div>
        </form>
      </div>
    </Container>
  );
}

export default UsertypePage;
