"use client";

import React from "react";

import { Formik, Form, FormikHelpers } from "formik";
import Input from "@/components/ui/Input";
import Button from "@/components/ui/Button";
import { toast } from "sonner";
import { useMutation } from "@tanstack/react-query";
import { changePasswordRequest } from "@/features/auth/services/authApi";
import { useRouter } from "next/navigation";
import * as Yup from "yup";
import {
  YupRequiredString,
  YupStrongPassword,
} from "@/features/shared/schemas";
import { useTranslation } from "react-i18next";
import { cn } from "@/lib/helpers";
import { useLanguageStore } from "@/store/languageStore";
import { getLocalizedApiMessage } from "@/lib/api/errors";
import { splitFormApiErrors } from "@/features/shared/splitFormApiErrors";

export interface ResetPasswordFormProps {
  email?: string;
  token?: string;
  onClose?: () => void;
  onShowLogin?: () => void;
  isModal?: boolean;
}

export default function ResetPasswordForm({
  email = "",
  token = "",
  onClose,
  onShowLogin,
  isModal,
}: ResetPasswordFormProps) {
  const router = useRouter();
  const { t } = useTranslation();
  const selectedLanguage = useLanguageStore((s) => s.language);

  const changePasswordMutation = useMutation({
    mutationFn: changePasswordRequest,
  });

  const initialValues = {
    password: "",
    confirmPassword: "",
  };

  const validationSchema = Yup.object({
    password: YupStrongPassword,
    confirmPassword: YupRequiredString.oneOf(
      [Yup.ref("password")],
      t("COMMON.PASSWORDS.MUST.MATCH"),
    ),
  });

  const handleSubmit = async (
    values: typeof initialValues,
    { resetForm, setFieldError }: FormikHelpers<typeof initialValues>,
  ) => {
    try {
      await changePasswordMutation.mutateAsync({
        password: values.password,
        token,
        email,
      });

      toast.success(t("COMMON.TOAST.PASSWORD_RESET_SUCCESSFUL"));
      resetForm();
      if (onClose && onShowLogin) {
        onClose();
        onShowLogin();
      } else {
        router.push(`/login?email=${encodeURIComponent(email)}`);
      }
    } catch (error) {
      /* A password rule the server rejects shows under the field (PDF
         p.37); only what no field can carry — an expired token, the email —
         is toasted. */
      const { mapped, rest } = splitFormApiErrors(
        error,
        selectedLanguage,
        Object.keys(initialValues),
        setFieldError,
        { password_confirmation: "confirmPassword" },
      );
      if (rest.length > 0) {
        rest.forEach((message) => toast.error(message));
      } else if (mapped === 0) {
        toast.error(
          getLocalizedApiMessage(
            error,
            selectedLanguage,
            t("COMMON.TOAST.PASSWORD_RESET_FAILED"),
          ),
        );
      }
    }
  };

  const isLoading = changePasswordMutation.isPending;

  return (
    <div className={cn(!onClose && "border-t", "border-[#000]")}>
      <div
        className={`${isModal ? "" : "2xl:py-[40px] laptopmain:py-[50px] laptop:py-[40px] lg:py-[40px] py-[40px]"}`}
      >
        {!isModal && (
          <h2 className="2xl:pb-11 lg:pb-5 pb-5 text-center font-bold xs:text-[22px] xs:leading-[26px] text-[28px] leading-[48px] md:text-[32px] md:leading-[52px] lg:text-[30px] lg:leading-[60px] 2xl:text-[50px] xl:leading-[68.09px] tracking-[0px] text-[#29246D]">
            {t("COMMON.RESET_PASSWORD")}
          </h2>
        )}

        <Formik
          initialValues={initialValues}
          validationSchema={validationSchema}
          onSubmit={handleSubmit}
        >
          {() => (
            <div className="flex justify-center">
              <div className="w-[90%] 2xl:w-[658px] lg:w-[658px] md:w-[90%] rounded-lg bg-white">
                <p className="mb-6 text-primary-5 text-center extrasmall:text-base font-[400] text-[20px] leading-[27.24px]">
                  {t("COMMON.PLEASE_ENTER_NEW_PASSWORD")}
                </p>
                <Form>
                  <Input
                    name="password"
                    type="password"
                    label={t("COMMON.ENTER_NEW_PASSWORD")}
                  />
                  <Input
                    name="confirmPassword"
                    type="password"
                    label={t("COMMON.CONFIRM_NEW_PASSWORD")}
                  />

                  <Button
                    className="mx-auto mt-[25px]"
                    variant="primary"
                    size="medium"
                    type="submit"
                    disabled={isLoading}
                  >
                    {t("COMMON.SUBMIT")}
                  </Button>
                </Form>
              </div>
            </div>
          )}
        </Formik>
      </div>
    </div>
  );
}
