"use server";

import { isAPIError } from "better-auth/api";
import { headers } from "next/headers";
import { z } from "zod";
import { logger } from "@formbricks/logger";
import {
  INVALID_PASSWORD_RESET_TOKEN_ERROR_CODE,
  InvalidInputError,
  InvalidPasswordResetTokenError,
  OperationNotAllowedError,
  PASSWORD_COMPROMISED_ERROR_CODE,
} from "@formbricks/types/errors";
import { ZUserPassword } from "@formbricks/types/user";
import { PASSWORD_RESET_DISABLED } from "@/lib/constants";
import { actionClient } from "@/lib/utils/action-client";
import { isUniqueConstraintError } from "@/lib/utils/prisma-error";
import { auth } from "@/modules/auth/lib/auth";
import { isPasswordCompromisedError } from "@/modules/auth/lib/better-auth-hibp";
import { applyIPRateLimit } from "@/modules/core/rate-limit/helpers";
import { rateLimitConfigs } from "@/modules/core/rate-limit/rate-limit-configs";

const ZResetPasswordAction = z.object({
  token: z.string().min(1),
  password: ZUserPassword,
});

export const resetPasswordAction = actionClient
  .inputSchema(ZResetPasswordAction)
  .action(async ({ parsedInput }) => {
    if (PASSWORD_RESET_DISABLED) {
      throw new OperationNotAllowedError("Password reset is disabled");
    }

    await applyIPRateLimit(rateLimitConfigs.auth.resetPassword);

    try {
      await auth.api.resetPassword({
        body: { token: parsedInput.token, newPassword: parsedInput.password },
        headers: await headers(),
      });
    } catch (error) {
      // A breached password is rejected by the HIBP plugin with its own APIError — surface it as the
      // stable compromised-password code (the reset form maps it to a localized message). This MUST
      // precede the catch-all below, which would otherwise mislabel it as an invalid/expired token.
      if (isPasswordCompromisedError(error)) {
        throw new InvalidInputError(PASSWORD_COMPROMISED_ERROR_CODE);
      }
      // Better Auth rejects an invalid/expired/already-used token with an APIError; surface it as the
      // existing invalid-reset-token error so the form shows the same message. The post-reset side
      // effects — session revocation (revokeSessionsOnPasswordReset), the security notification email,
      // and the audit event (onPasswordReset) — are all handled inside Better Auth / auth.ts.
      if (isAPIError(error)) {
        throw new InvalidPasswordResetTokenError(
          INVALID_PASSWORD_RESET_TOKEN_ERROR_CODE,
          "invalid_or_expired"
        );
      }
      // ENG-3258: Better Auth could not see the user's credential row and tried to create a second one.
      // The token is already spent by then, so this is an invalid link, not a server error. Logged
      // because the sign-in / reset-request heal should have made it impossible.
      if (isUniqueConstraintError(error)) {
        logger.error({ error }, "Password reset collided with an existing credential account");
        throw new InvalidPasswordResetTokenError(
          INVALID_PASSWORD_RESET_TOKEN_ERROR_CODE,
          "credential_account_conflict"
        );
      }
      throw error;
    }

    return { success: true };
  });
