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

import Container from "@/components/elements/Container";
import React, { useEffect } 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 { useUser } from "@/hooks/useUser";

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

function CooperationType() {
  const router = useRouter();
  const { request, loading } = useAxios();
  const user = useUser();

  const formik = useFormik({
    initialValues: {
      selectedButton: null as boolean | null,
    },
    validationSchema: schema,
    onSubmit: async (values, { setSubmitting }) => {
      try {
        await request("PATCH", "/verify/cooperation_type", {
          cooperation_abroad: values.selectedButton,
          cooperation_type: "all",
        });
        router.push("/settings/edit");
      } catch (err: any) {
        toast.error(err?.message || "خطای نامشخصی رخ داد.");
      } finally {
        setSubmitting(false);
      }
    },
  });

  useEffect(() => {
    formik.setValues({ selectedButton: user?.cooperation_abroad ?? null });
  }, [user?.cooperation_abroad]);

  return (
    <Container>
      <div className="p-4 flex flex-col items-center h-screen justify-center">
        <span className="text-xl font-bold text-foreground">نوع همکاری</span>

        <p className="my-10 text-sm font-bold">
          آیا مایل به همکاری خارج از محل سکونت خود هستید؟
        </p>
        <form
          onSubmit={formik.handleSubmit}
          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>

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

          <AuthNextButton type="submit" className="mt-20" loading={loading} disabled={loading}>
            ویرایش
          </AuthNextButton>
        </form>
      </div>
    </Container>
  );
}

export default CooperationType;
