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

function PublicRelations() {
  const router = useRouter();
  const { request, loading } = useAxios();

  const user = useUser();

  const formik = useFormik({
    initialValues: {
      bio: "",
    },
    validationSchema: schema,
    onSubmit: async (values, { setSubmitting }) => {
      try {
        await request("PATCH", "/verify/conversation", {
          conversation_projects: true,
          bio: values.bio,
        });
        router.push("/settings/edit");
      } catch (err: any) {
        toast.error(err?.message || "خطای نامشخصی رخ داد.");
      } finally {
        setSubmitting(false);
      }
    },
  });
  useEffect(() => {
    formik.setValues({
      bio: user?.bio ?? "",
    });
  }, [user]);

  return (
    <Container>
      <div className="p-4 flex flex-col items-center h-screen justify-center">
        <span className="text-xl font-bold text-foreground">بیو</span>
        <div className="mt-5"></div>
        <form
          onSubmit={formik.handleSubmit}
          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={formik.handleChange}
            onBlur={formik.handleBlur}
          />
          {formik.touched.bio && formik.errors.bio && (
            <small className="text-red-500 mt-2 block">
              {formik.errors.bio}
            </small>
          )}

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

export default PublicRelations;
