/* 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";
import { useUser } from "@/hooks/useUser";

// Validation schema using Yup
const schema = yup.object().shape({
  password: yup
    .string()
    .required("کلمه عبور نمی\u200Cتواند خالی باشد")
    .min(8, "کلمه عبور نباید کوتاه تر از 8 کاراکتر باشد")
    .matches(
      /^(?=.*[a-zA-Zآ-ی])(?=.*\d).{8,}$/,
      "کلمه عبور باید شامل حروف و اعداد باشد"
    ),
  confirmPassword: yup
    .string()
    .required("تکرار کلمه عبور نمی\u200Cتواند خالی باشد")
    .oneOf(
      [yup.ref("password")],
      "تکرار کلمه عبور باید با کلمه عبور مطابقت داشته باشد"
    )
    .required("تکرار کلمه عبور الزامی است"),
});

function PasswordPage() {
  const router = useRouter();
  const user = useUser();
  const { request, loading } = useAxios();
  const formik = useFormik({
    initialValues: {
      password: "",
      confirmPassword: "",
    },
    validationSchema: schema,
    onSubmit: async (values) => {
      try {
        (await request("PATCH", "/login/change_password", {
          mobile: user?.mobile,
          new_password: values.password,
        })) as IVerifyOtp;
        router.push("/settings/edit");
      } 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="password"
            type="password"
            placeholder="کلمه عبور"
            className={`border mt-4 ${
              formik.touched.password && formik.errors.password
                ? "border-red-500 dark:border-red-500"
                : "border-gray-300"
            }`}
            value={formik.values.password}
            onChange={formik.handleChange}
            onBlur={formik.handleBlur}
          />
          {formik.touched.password && formik.errors.password && (
            <small className="text-red-500 mt-2 block">
              {formik.errors.password}
            </small>
          )}

          <AuthInput
            name="confirmPassword"
            type="password"
            placeholder="تکرار کلمه عبور"
            className={`border mt-4 ${
              formik.touched.confirmPassword && formik.errors.confirmPassword
                ? "border-red-500 dark:border-red-500"
                : "border-gray-300"
            }`}
            value={formik.values.confirmPassword}
            onChange={formik.handleChange}
            onBlur={formik.handleBlur}
          />
          {formik.touched.confirmPassword && formik.errors.confirmPassword && (
            <small className="text-red-500 mt-2 block">
              {formik.errors.confirmPassword}
            </small>
          )}

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

export default PasswordPage;
