"use client";

import { useRouter } from "next/navigation";
import { useCallback, useEffect, useState } from "react";
import { useTranslation } from "react-i18next";
import { Response, Workspace } from "@formbricks/database/prisma-browser";
import type { TRendererCustomCss } from "@formbricks/types/custom-css";
import { getLinkSurveyCardMaxWidth } from "@formbricks/types/styling";
import { TSurvey, TSurveyStyling } from "@formbricks/types/surveys/types";
import { TUserLocale } from "@formbricks/types/user";
import { TWorkspaceStyling } from "@formbricks/types/workspace";
import { cn } from "@/lib/cn";
import { getFormattedErrorMessage } from "@/lib/utils/error-message";
import { validateSurveyPinAction } from "@/modules/survey/link/actions";
import { SurveyClientWrapper } from "@/modules/survey/link/components/survey-client-wrapper";
import { useAppLocale } from "@/modules/survey/link/hooks/use-app-locale";
import { isLinkSurveyGoneError } from "@/modules/survey/link/lib/public-access";
import { OTPInput } from "@/modules/ui/components/otp-input";

interface PinScreenProps {
  surveyId: string;
  workspace: Pick<Workspace, "styling" | "logo" | "linkSurveyBranding" | "customHeadScripts">;
  singleUseId?: string;
  /** The single-use link as it appears in the URL, which `validateSurveyPinAction` checks again. */
  suId?: string;
  suToken?: string;
  singleUseResponse?: Pick<Response, "id" | "finished">;
  publicDomain: string;
  IMPRINT_URL?: string;
  PRIVACY_URL?: string;
  TERMS_URL?: string;
  IS_FORMBRICKS_CLOUD: boolean;
  verifiedEmail?: string;
  languageCode: string;
  /** Locale for the gate's own chrome, resolved server-side from `?lang=` or Accept-Language. */
  locale: TUserLocale;
  isEmbed: boolean;
  isPreview: boolean;
  contactId?: string;
  canReadUserIdFromUrl?: boolean;
  recaptchaSiteKey?: string;
  isSpamProtectionEnabled?: boolean;
  responseCount?: number;
  styling: TWorkspaceStyling | TSurveyStyling;
  /** The app's release version, which the survey renderer is loaded under. */
  rendererVersion?: string;
}

export const PinScreen = (props: Readonly<PinScreenProps>) => {
  const {
    surveyId,
    workspace,
    publicDomain,
    singleUseId,
    suId,
    suToken,
    singleUseResponse,
    IMPRINT_URL,
    PRIVACY_URL,
    TERMS_URL,
    IS_FORMBRICKS_CLOUD,
    verifiedEmail,
    languageCode,
    locale,
    isEmbed,
    isPreview,
    contactId,
    canReadUserIdFromUrl = false,
    recaptchaSiteKey,
    isSpamProtectionEnabled = false,
    responseCount,
    styling,
    rendererVersion,
  } = props;

  const [localPinEntry, setLocalPinEntry] = useState<string>("");
  const [loading, setLoading] = useState<boolean>(false);
  const { t } = useTranslation();
  const router = useRouter();
  const isLocaleReady = useAppLocale(locale);
  const [error, setError] = useState("");
  const [survey, setSurvey] = useState<TSurvey>();
  const [pinAuthToken, setPinAuthToken] = useState<string | undefined>();
  const [customCss, setCustomCss] = useState<TRendererCustomCss | undefined>();
  const isCardless = styling.cardArrangement?.linkSurveys === "cardless";
  const linkSurveyCardMaxWidth = getLinkSurveyCardMaxWidth(styling.linkSurveyCardWidth);

  const resetState = useCallback(() => {
    setError("");
    setLoading(false);
    setLocalPinEntry("");
  }, []);

  useEffect(() => {
    if (error) {
      const timeout = setTimeout(() => {
        resetState();
      }, 2 * 1000);
      return () => {
        clearTimeout(timeout);
      };
    }
  }, [error, resetState]);

  useEffect(() => {
    const validateSurveyPin = async () => {
      const validPinRegex = /^\d{4}$/;
      const isValidPin = validPinRegex.test(localPinEntry);
      if (isValidPin) {
        setLoading(true);
        try {
          const response = await validateSurveyPinAction({ surveyId, pin: localPinEntry, suId, suToken });
          if (response?.data?.survey) {
            setSurvey(response.data.survey);
            setPinAuthToken(response.data.pinAuthToken ?? undefined);
            setCustomCss(response.data.customCss);
          } else {
            setError(getFormattedErrorMessage(response));
            // "Not found" means the survey stopped taking responses (closed, archived or gone) after this
            // page loaded (ENG-4032), so no PIN can ever be accepted here. The boxes only show an error as a
            // red border, so re-render the page and let the server show what a fresh load would.
            if (isLinkSurveyGoneError(response?.serverError, surveyId)) {
              router.refresh();
            }
          }
        } catch {
          // A rejected call (offline, or an action id a newer deploy no longer serves) must still release the
          // PIN boxes, or they stay disabled for good.
          // Only its presence is shown (a red border), so the text is never rendered.
          setError("UNEXPECTED_ERROR");
        } finally {
          setLoading(false);
        }
      }
    };

    void validateSurveyPin();
  }, [localPinEntry, router, surveyId, suId, suToken]);

  if (!survey) {
    // The gate is nothing but translated chrome, so it waits for its locale instead of asking for the
    // PIN in the browser's language and switching a frame later.
    if (!isLocaleReady) return null;

    return (
      <div
        className={cn(
          "mx-auto flex h-full w-full items-center justify-center",
          isCardless && "px-4 py-12 sm:px-6"
        )}
        style={{ maxWidth: linkSurveyCardMaxWidth }}>
        <div className="flex flex-col items-center justify-center">
          <div className="my-4 font-semibold">
            <h4>{t("s.enter_pin")}</h4>
          </div>
          <OTPInput
            disabled={Boolean(error) || loading}
            value={localPinEntry}
            onChange={(value) => {
              setLocalPinEntry(value);
            }}
            valueLength={4}
            inputBoxClassName={cn({ "border-red-400": Boolean(error) })}
          />
        </div>
      </div>
    );
  }

  return (
    <SurveyClientWrapper
      survey={survey}
      workspace={workspace}
      styling={styling}
      publicDomain={publicDomain}
      responseCount={responseCount}
      languageCode={languageCode}
      isEmbed={isEmbed}
      singleUseId={singleUseId}
      singleUseResponseId={singleUseResponse?.id}
      contactId={contactId}
      canReadUserIdFromUrl={canReadUserIdFromUrl}
      recaptchaSiteKey={recaptchaSiteKey}
      isSpamProtectionEnabled={isSpamProtectionEnabled}
      isPreview={isPreview}
      verifiedEmail={verifiedEmail}
      IMPRINT_URL={IMPRINT_URL}
      PRIVACY_URL={PRIVACY_URL}
      TERMS_URL={TERMS_URL}
      IS_FORMBRICKS_CLOUD={IS_FORMBRICKS_CLOUD}
      pinAuthToken={pinAuthToken}
      customCss={customCss}
      rendererVersion={rendererVersion}
    />
  );
};
