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

import AuthPageLayout, {
  AuthFormFooter,
  AuthPageContent,
} from "@/components/auth/AuthPageLayout";
import React 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";
import AuthUserDetails from "@/components/auth/AuthUserDetails";
import Image from "next/image";

// Validation schema using Yup
const schema = yup.object().shape({
  selectedButton: yup.string().required("جنسیت الزامی است"),
});

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

  const formik = useFormik({
    initialValues: {
      selectedButton: "",
    },
    validationSchema: schema,
    onSubmit: async (values, { setSubmitting }) => {
      try {
        await request("POST", "/verify/gender", {
          mobile: mobile,
          gender: values.selectedButton,
        });
        if (expertise == "مدلینگ") {
          router.push("/verify/sizes");
        } else {
          router.push("/verify/services");
        }
      } catch (err: any) {
        toast.error(err?.message || "خطای نامشخصی رخ داد.");
      } finally {
        setSubmitting(false);
      }
    },
  });

  return (
    <AuthPageLayout>
      <form
        onSubmit={formik.handleSubmit}
        className="flex w-full flex-1 flex-col items-center"
      >
        <AuthPageContent>
          <span className="text-xl font-bold">جنسیت</span>
          <div className="mt-5">
            <AuthUserDetails />
          </div>

          <div className="w-full max-w-sm flex flex-col items-center">
          <div className="flex w-full gap-4">
            <AuthNextButton
              type="button"
              onClick={() => formik.setFieldValue("selectedButton", "female")}
              className={`mt-5 w-full max-w-[250px] !border-[#FC8EAC] flex  items-center justify-center flex-row-reverse gap-2 ${
                formik.values.selectedButton === "female"
                  ? "!bg-[#FC8EAC]  !text-white"
                  : ""
              }`}
            >
              <Image
                src={"/images/icons/woman.svg"}
                width={25}
                height={25}
                alt="gender icon"
              />
              خانم
            </AuthNextButton>
            <AuthNextButton
              type="button"
              onClick={() => formik.setFieldValue("selectedButton", "male")}
              className={`mt-5 w-full max-w-[250px] !border-[#3E8BFF] flex  items-center justify-center flex-row-reverse gap-2 ${
                formik.values.selectedButton === "male"
                  ? "!bg-[#3E8BFF]  !text-white"
                  : ""
              }`}
            >
              <Image
                src={"/images/icons/man.svg"}
                width={25}
                height={25}
                alt="gender icon"
              />
              آقا
            </AuthNextButton>
          </div>

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

        <AuthFormFooter onSkip={() => router.push("/verify/sizes")}>
          <AuthNextButton type="submit" loading={loading} disabled={loading}>
            ثبت و ادامه
          </AuthNextButton>
        </AuthFormFooter>
      </form>
    </AuthPageLayout>
  );
}

export default Gender;
