/* 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({
  bio: yup.string().required("نوشتن bio الزامی است"),
});


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

  const formik = useFormik({
    initialValues: {
      selectedButton: null,
      bio: "",
    },
    validationSchema: schema,
    onSubmit: async (values, { setSubmitting }) => {
      try {
        await request("POST", "/verify/conversation", {
          mobile: mobile,
          conversation_projects: values.selectedButton,
          bio: values.bio,
        });
        router.push("/verify/location");
      } 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">
          <small className="font-bold mt-10"> در مورد خودتان چیزی بگویید</small>
          <textarea
            name="bio"
            placeholder="بیو"
            className={`border mt-4 w-full max-w-[290px] p-3 rounded-2xl border-border-primary-light dark:border-border-primary-dark bg-secondary-light dark:bg-secondary-dark font-medium ${
              formik.touched.bio && formik.errors.bio
                ? "border-red-500 dark:border-red-500"
                : "border-gray-300"
            }`}
            value={formik.values.bio}
            onChange={(e) => {
              if (e.target.value.length <= 500) {
                formik.handleChange(e);
              }
            }}
            onBlur={formik.handleBlur}
          />

          <small className="mt-1 text-gray-500 block text-right">
            {formik.values.bio.length}/500
          </small>

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

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

export default PublicRelations;
