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

import AuthHead from "@/components/auth/AuthHead";
import AuthInput from "@/components/auth/AuthInput";
import Container from "@/components/elements/Container";
import React from "react";
import useAxios from "@/hooks/useAxios";
import { useFormik } from "formik";
import * as yup from "yup";
import { useRouter } from "next/navigation";
import { IVerifyOtp } from "@/types/types";
import AuthNextButton from "@/components/auth/AuthNextButton";

// Validation schema using Yup
const schema = yup.object().shape({
  family: yup.string().required("نام خانوادگی الزامی است"),
  name: yup.string().required("نام الزامی است"),
});

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

  const formik = useFormik({
    initialValues: {
      family: "",
      name: "",
    },
    validationSchema: schema,
    onSubmit: async (values) => {
      try {
        (await request("POST", "/register/fullname", {
          mobile: mobile,
          first_name: values.name,
          last_name: values.family,
        })) as IVerifyOtp;
        router.push("/register/usertype");
      } catch (err: any) {
        console.log("Unhandled error:", err?.message);
      }
    },
  });

  return (
    <Container>
      <div className="p-4 flex flex-col items-center h-screen justify-center">
        <AuthHead title="نام و نام خانوادگی" />
        <form
          onSubmit={formik.handleSubmit}
          className="w-full max-w-sm flex flex-col items-center"
        >
          <AuthInput
            name="name"
            type="text"
            placeholder="نام"
            className={`border mt-4 ${
              formik.touched.name && formik.errors.name
                ? "border-red-500 dark:border-red-500"
                : "border-gray-300"
            }`}
            value={formik.values.name}
            onChange={formik.handleChange}
            onBlur={formik.handleBlur}
          />
          {formik.touched.name && formik.errors.name && (
            <small className="text-red-500 mt-2 block">
              {formik.errors.name}
            </small>
          )}

          <AuthInput
            name="family"
            type="text"
            placeholder="نام خانوادگی"
            className={`border mt-4 ${
              formik.touched.family && formik.errors.family
                ? "border-red-500 dark:border-red-500"
                : "border-gray-300"
            }`}
            value={formik.values.family}
            onChange={formik.handleChange}
            onBlur={formik.handleBlur}
          />
          {formik.touched.family && formik.errors.family && (
            <small className="text-red-500 mt-2 block">
              {formik.errors.family}
            </small>
          )}

          <AuthNextButton type="submit" className="mt-20" disabled={loading}>
            {loading ? "در حال ارسال..." : "ثبت و ادامه"}
          </AuthNextButton>
        </form>
      </div>
    </Container>
  );
}

export default FullNamePage;
