/* 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";

// Validation schema using Yup
const schema = yup.object().shape({
  selectedButton: yup.boolean().required("انتخاب نوع همکاری الزامی است"),
});

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

  const formik = useFormik({
    initialValues: {
      selectedButton: null,
    },
    validationSchema: schema,
    onSubmit: async (values, { setSubmitting }) => {
      try {
        await request("POST", "/verify/cooperation_type", {
          mobile: mobile,
          cooperation_abroad: values.selectedButton,
          cooperation_type: "all",
        });
        router.push("/verify/public-relations");
      } 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>

          <p className="my-10 text-sm font-bold">
            آیا مایل به همکاری خارج از محل سکونت خود هستید؟
          </p>

          <div className="w-full max-w-sm flex flex-col items-center">
          <div className="flex w-full gap-2">
            <AuthNextButton
              type="button"
              onClick={() => formik.setFieldValue("selectedButton", true)}
              className={`mt-5 text-xs w-full max-w-[250px] flex  items-center justify-center flex-row-reverse gap-2 px-1 ${
                formik.values.selectedButton === true
                  ? "!bg-[#FC8EAC] !border-[#FC8EAC] !text-white"
                  : ""
              }`}
            >
              بله خوشحال هم می شم
            </AuthNextButton>
            <AuthNextButton
              type="button"
              onClick={() => formik.setFieldValue("selectedButton", false)}
              className={`mt-5 text-xs w-full max-w-[250px] flex  items-center justify-center flex-row-reverse gap-2 px-1 ${
                formik.values.selectedButton === false
                  ? "!bg-[#FC8EAC] !border-[#FC8EAC] !text-white"
                  : ""
              }`}
            >
              نه شهر خودم رو ترجیح میدم
            </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/public-relations")}>
          <AuthNextButton type="submit" loading={loading} disabled={loading}>
            ثبت و ادامه
          </AuthNextButton>
        </AuthFormFooter>
      </form>
    </AuthPageLayout>
  );
}

export default CooperationType;
