import { observer } from "mobx-react";
import Link from "next/link";
import { useMemo, useRef, useState } from "react";
// icons
import { Eye, EyeOff, Info, XCircle } from "lucide-react";
// buzzwareTech imports
import { AUTH_TRACKER_ELEMENTS, E_PASSWORD_STRENGTH } from "@buzzwaretech/constants";
import { useTranslation } from "@buzzwaretech/i18n";
import { Button } from "@buzzwaretech/propel/button";
import { CloseIcon } from "@buzzwaretech/propel/icons";
import { Input, PasswordStrengthIndicator, Spinner } from "@buzzwaretech/ui";
import { getPasswordStrength } from "@buzzwaretech/utils";
// components
import { ForgotPasswordPopover } from "@/components/account/auth-forms/forgot-password-popover";
// constants
// helpers
import { EAuthModes, EAuthSteps } from "@/helpers/authentication.helper";
// services
import { FirebaseAuthService } from "@/services/firebase-auth.service";

type Props = {
  email: string;
  isSMTPConfigured: boolean;
  mode: EAuthModes;
  handleEmailClear: () => void;
  handleAuthStep: (step: EAuthSteps) => void;
  nextPath: string | undefined;
};

type TPasswordFormValues = {
  email: string;
  password: string;
  confirm_password?: string;
};

const defaultValues: TPasswordFormValues = {
  email: "",
  password: "",
};

const firebaseAuthService = new FirebaseAuthService();

export const AuthPasswordForm = observer(function AuthPasswordForm(
  props: Props,
) {
  const {
    email,
    isSMTPConfigured,
    handleAuthStep,
    handleEmailClear,
    mode,
    nextPath,
  } = props;
  // buzzwareTech imports
  const { t } = useTranslation();
  // ref
  const formRef = useRef<HTMLFormElement>(null);
  // states
  const [passwordFormData, setPasswordFormData] = useState<TPasswordFormValues>(
    { ...defaultValues, email },
  );
  const [showPassword, setShowPassword] = useState({
    password: false,
    retypePassword: false,
  });
  const [isSubmitting, setIsSubmitting] = useState(false);
  const [isPasswordInputFocused, setIsPasswordInputFocused] = useState(false);
  const [isRetryPasswordInputFocused, setIsRetryPasswordInputFocused] =
    useState(false);
  const [isBannerMessage, setBannerMessage] = useState(false);

  const handleShowPassword = (key: keyof typeof showPassword) =>
    setShowPassword((prev) => ({ ...prev, [key]: !prev[key] }));

  const handleFormChange = (key: keyof TPasswordFormValues, value: string) =>
    setPasswordFormData((prev) => ({ ...prev, [key]: value }));

  const redirectToUniqueCodeSignIn = async () => {
    handleAuthStep(EAuthSteps.UNIQUE_CODE);
  };

  const passwordSupport =
    mode === EAuthModes.SIGN_IN ? (
      <div className="w-full">
        {isSMTPConfigured ? (
          <Link
            data-ph-element={AUTH_TRACKER_ELEMENTS.FORGOT_PASSWORD_FROM_SIGNIN}
            href={`/accounts/forgot-password?email=${encodeURIComponent(email)}`}
            className="text-11 font-medium text-accent-primary"
          >
            {t("auth.common.forgot_password")}
          </Link>
        ) : (
          <ForgotPasswordPopover />
        )}
      </div>
    ) : (
      passwordFormData.password.length > 0 &&
      getPasswordStrength(passwordFormData.password) !=
        E_PASSWORD_STRENGTH.STRENGTH_VALID && (
        <PasswordStrengthIndicator
          password={passwordFormData.password}
          isFocused={isPasswordInputFocused}
        />
      )
    );

  const isButtonDisabled = useMemo(
    () =>
      !isSubmitting &&
      !!passwordFormData.password &&
      (mode === EAuthModes.SIGN_UP
        ? passwordFormData.password === passwordFormData.confirm_password
        : true)
        ? false
        : true,
    [
      isSubmitting,
      mode,
      passwordFormData.confirm_password,
      passwordFormData.password,
    ],
  );

  const password = passwordFormData?.password ?? "";
  const confirmPassword = passwordFormData?.confirm_password ?? "";
  const renderPasswordMatchError =
    !isRetryPasswordInputFocused || confirmPassword.length >= password.length;

  return (
    <>
      {isBannerMessage && mode === EAuthModes.SIGN_UP && (
        <div className="relative flex items-center gap-2 rounded-md border border-danger-strong/50 bg-danger-subtle p-2">
          <div className="relative flex h-4 w-4 shrink-0 items-center justify-center">
            <Info size={16} className="text-danger-primary" />
          </div>
          <div className="w-full text-13 font-medium text-danger-primary">
            {t("auth.sign_up.errors.password.strength")}
          </div>
          <button
            type="button"
            className="relative ml-auto flex h-6 w-6 cursor-pointer items-center justify-center rounded-xs text-accent-primary/80 transition-all hover:bg-danger-subtle-hover"
            onClick={() => setBannerMessage(false)}
          >
            <CloseIcon className="h-4 w-4 shrink-0 text-danger-primary" />
          </button>
        </div>
      )}
      <form
        ref={formRef}
        className="space-y-4"
        onSubmit={async (event) => {
          event.preventDefault(); // Prevent form from submitting by default
          const isPasswordValid =
            mode === EAuthModes.SIGN_UP
              ? getPasswordStrength(passwordFormData.password) ===
                E_PASSWORD_STRENGTH.STRENGTH_VALID
              : true;
          if (isPasswordValid) {
            setIsSubmitting(true);
            try {
              if (mode === EAuthModes.SIGN_IN) {
                await firebaseAuthService.signInWithEmail(
                  passwordFormData.email,
                  passwordFormData.password,
                );
              } else {
                await firebaseAuthService.signUpWithEmail(
                  passwordFormData.email,
                  passwordFormData.password,
                );
              }
              // Redirect to next path or home on success
              window.location.href = nextPath || "/";
            } catch (err: any) {
              setIsSubmitting(false);
              console.error("Authentication failed:", err);
              alert(
                err?.error ||
                  err?.message ||
                  "Authentication failed. Please check your credentials.",
              );
            }
          } else {
            setBannerMessage(true);
          }
        }}
        onError={() => {
          setIsSubmitting(false);
        }}
      >
        <input type="hidden" value={passwordFormData.email} name="email" />
        {nextPath && <input type="hidden" value={nextPath} name="next_path" />}
        <div className="space-y-1">
          <label htmlFor="email" className="text-13 font-medium text-tertiary">
            {t("auth.common.email.label")}
          </label>
          <div
            className={`relative flex items-center rounded-md border border-strong bg-surface-1`}
          >
            <Input
              id="email"
              name="email"
              type="email"
              value={passwordFormData.email}
              onChange={(e) => handleFormChange("email", e.target.value)}
              placeholder={t("auth.common.email.placeholder")}
              className={`h-10 w-full border-0 disable-autofill-style placeholder:text-placeholder`}
              disabled
            />
            {passwordFormData.email.length > 0 && (
              <button
                type="button"
                className="absolute right-3 size-5"
                onClick={handleEmailClear}
                aria-label={t("aria_labels.auth_forms.clear_email")}
              >
                <XCircle className="size-5 stroke-placeholder" />
              </button>
            )}
          </div>
        </div>

        <div className="space-y-1">
          <label
            htmlFor="password"
            className="text-13 font-medium text-tertiary"
          >
            {mode === EAuthModes.SIGN_IN
              ? t("auth.common.password.label")
              : t("auth.common.password.set_password")}
          </label>
          <div className="relative flex items-center rounded-md bg-surface-1">
            <Input
              type={showPassword?.password ? "text" : "password"}
              id="password"
              name="password"
              value={passwordFormData.password}
              onChange={(e) => handleFormChange("password", e.target.value)}
              placeholder={t("auth.common.password.placeholder")}
              className="h-10 w-full border border-strong !bg-surface-1 pr-12 disable-autofill-style placeholder:text-placeholder"
              onFocus={() => setIsPasswordInputFocused(true)}
              onBlur={() => setIsPasswordInputFocused(false)}
              autoComplete="off"
              autoFocus
            />
            <button
              type="button"
              onClick={() => handleShowPassword("password")}
              className="absolute right-3 grid size-5 place-items-center"
              aria-label={t(
                showPassword?.password
                  ? "aria_labels.auth_forms.hide_password"
                  : "aria_labels.auth_forms.show_password",
              )}
            >
              {showPassword?.password ? (
                <EyeOff className="size-5 stroke-placeholder" />
              ) : (
                <Eye className="size-5 stroke-placeholder" />
              )}
            </button>
          </div>
          {passwordSupport}
        </div>

        {mode === EAuthModes.SIGN_UP && (
          <div className="space-y-1">
            <label
              htmlFor="confirm-password"
              className="text-13 font-medium text-tertiary"
            >
              {t("auth.common.password.confirm_password.label")}
            </label>
            <div className="relative flex items-center rounded-md bg-surface-1">
              <Input
                type={showPassword?.retypePassword ? "text" : "password"}
                id="confirm-password"
                name="confirm_password"
                value={passwordFormData.confirm_password}
                onChange={(e) =>
                  handleFormChange("confirm_password", e.target.value)
                }
                placeholder={t(
                  "auth.common.password.confirm_password.placeholder",
                )}
                className="h-10 w-full border border-strong !bg-surface-1 pr-12 disable-autofill-style placeholder:text-placeholder"
                onFocus={() => setIsRetryPasswordInputFocused(true)}
                onBlur={() => setIsRetryPasswordInputFocused(false)}
                autoComplete="off"
              />
              <button
                type="button"
                className="absolute right-3 grid size-5 place-items-center"
                aria-label={t(
                  showPassword?.retypePassword
                    ? "aria_labels.auth_forms.hide_password"
                    : "aria_labels.auth_forms.show_password",
                )}
                onClick={() => handleShowPassword("retypePassword")}
              >
                {showPassword?.retypePassword ? (
                  <EyeOff className="size-5 stroke-placeholder" />
                ) : (
                  <Eye className="size-5 stroke-placeholder" />
                )}
              </button>
            </div>
            {!!passwordFormData.confirm_password &&
              passwordFormData.password !== passwordFormData.confirm_password &&
              renderPasswordMatchError && (
                <span className="text-13 text-danger-primary">
                  {t("auth.common.password.errors.match")}
                </span>
              )}
          </div>
        )}

        <div className="space-y-2.5">
          {mode === EAuthModes.SIGN_IN ? (
            <>
              <Button
                type="submit"
                variant="primary"
                className="w-full"
                size="xl"
                disabled={isButtonDisabled}
              >
                {isSubmitting ? (
                  <Spinner height="20px" width="20px" />
                ) : isSMTPConfigured ? (
                  t("common.continue")
                ) : (
                  t("common.go_to_workspace")
                )}
              </Button>
              {isSMTPConfigured && (
                <Button
                  type="button"
                  data-ph-element={
                    AUTH_TRACKER_ELEMENTS.SIGN_IN_WITH_UNIQUE_CODE
                  }
                  onClick={redirectToUniqueCodeSignIn}
                  variant="secondary"
                  className="w-full"
                  size="xl"
                >
                  {t("auth.common.sign_in_with_unique_code")}
                </Button>
              )}
            </>
          ) : (
            <Button
              type="submit"
              variant="primary"
              className="w-full"
              size="xl"
              disabled={isButtonDisabled}
            >
              {isSubmitting ? (
                <Spinner height="20px" width="20px" />
              ) : (
                "Create account"
              )}
            </Button>
          )}
        </div>
      </form>
    </>
  );
});
