import { APIError } from "better-auth/api";
import { afterEach, beforeEach, describe, expect, test, vi } from "vitest";
import { Prisma } from "@formbricks/database/prisma";
import { PrismaErrorType } from "@formbricks/database/types/error";
import {
  INVALID_PASSWORD_RESET_TOKEN_ERROR_CODE,
  InvalidInputError,
  InvalidPasswordResetTokenError,
  OperationNotAllowedError,
  PASSWORD_COMPROMISED_ERROR_CODE,
} from "@formbricks/types/errors";
import { auth } from "@/modules/auth/lib/auth";
import { applyIPRateLimit } from "@/modules/core/rate-limit/helpers";
import { rateLimitConfigs } from "@/modules/core/rate-limit/rate-limit-configs";
import { resetPasswordAction } from "./actions";

const constantsState = vi.hoisted(() => ({
  passwordResetDisabled: false,
}));

// Reset now goes through Better Auth; the post-reset audit/notification/session-revocation live in
// auth.ts (onPasswordReset + revokeSessionsOnPasswordReset), not this action (ENG-1054).
vi.mock("@/modules/auth/lib/auth", () => ({
  auth: { api: { resetPassword: vi.fn() } },
}));

vi.mock("@/modules/core/rate-limit/helpers", () => ({
  applyIPRateLimit: vi.fn(),
}));

vi.mock("next/headers", () => ({
  headers: vi.fn(() => Promise.resolve(new Headers())),
}));

vi.mock("@/lib/constants", () => ({
  get PASSWORD_RESET_DISABLED() {
    return constantsState.passwordResetDisabled;
  },
}));

vi.mock("@/lib/utils/action-client", () => ({
  actionClient: {
    inputSchema: vi.fn().mockReturnThis(),
    action: vi.fn((fn) => fn),
  },
}));

describe("resetPasswordAction", () => {
  const parsedInput = {
    token: "opaque-reset-token",
    password: "Password123",
  };

  beforeEach(() => {
    vi.resetAllMocks();
    constantsState.passwordResetDisabled = false;
  });

  afterEach(() => {
    vi.restoreAllMocks();
  });

  test("resets the password through Better Auth on success", async () => {
    vi.mocked(auth.api.resetPassword, { partial: true }).mockResolvedValue({ status: true });

    const result = await resetPasswordAction({ parsedInput } as unknown as Parameters<
      typeof resetPasswordAction
    >[0]);

    expect(result).toEqual({ success: true });
    expect(auth.api.resetPassword).toHaveBeenCalledWith({
      body: { token: parsedInput.token, newPassword: parsedInput.password },
      headers: expect.any(Headers),
    });
  });

  test("applies the reset-password IP limit before calling Better Auth", async () => {
    vi.mocked(auth.api.resetPassword).mockResolvedValue({ status: true });

    await resetPasswordAction({ parsedInput } as never);

    expect(applyIPRateLimit).toHaveBeenCalledWith(rateLimitConfigs.auth.resetPassword);
    expect(applyIPRateLimit).toHaveBeenCalledBefore(vi.mocked(auth.api.resetPassword));
  });

  test("short-circuits before Better Auth when the IP limit is exceeded", async () => {
    vi.mocked(applyIPRateLimit).mockRejectedValue(
      new Error("Maximum number of requests reached. Please try again later.")
    );

    await expect(resetPasswordAction({ parsedInput } as never)).rejects.toThrow(
      "Maximum number of requests reached. Please try again later."
    );
    expect(auth.api.resetPassword).not.toHaveBeenCalled();
  });

  test("maps a Better Auth APIError (invalid/expired/used token) to the invalid-reset-token error", async () => {
    vi.mocked(auth.api.resetPassword).mockRejectedValue(
      new APIError("BAD_REQUEST", { message: "invalid token", code: "INVALID_TOKEN" })
    );

    // Invoke once and assert on the single promise — this action wraps a mutating auth call, so
    // re-invoking would double the side effect and weaken the test.
    const result = resetPasswordAction({ parsedInput } as unknown as Parameters<
      typeof resetPasswordAction
    >[0]);
    await expect(result).rejects.toBeInstanceOf(InvalidPasswordResetTokenError);
    await expect(result).rejects.toThrow(INVALID_PASSWORD_RESET_TOKEN_ERROR_CODE);
    expect(auth.api.resetPassword).toHaveBeenCalledTimes(1);
  });

  test("maps a compromised-password APIError to the compromised code, not the invalid-token error", async () => {
    // The HIBP plugin rejects a breached password with its own APIError. This must be surfaced as the
    // stable compromised code — NOT swallowed by the catch-all that maps every APIError to a bogus
    // "invalid/expired token" message.
    vi.mocked(auth.api.resetPassword).mockRejectedValue(
      new APIError("BAD_REQUEST", { message: "breached", code: PASSWORD_COMPROMISED_ERROR_CODE })
    );

    const result = resetPasswordAction({ parsedInput } as unknown as Parameters<
      typeof resetPasswordAction
    >[0]);
    await expect(result).rejects.toBeInstanceOf(InvalidInputError);
    await expect(result).rejects.toThrow(PASSWORD_COMPROMISED_ERROR_CODE);
    expect(auth.api.resetPassword).toHaveBeenCalledTimes(1);
  });

  test("maps a unique-constraint violation to the invalid-reset-token error, not a server error", async () => {
    // ENG-3258: Better Auth could not see the credential row, so it tried to create a second one. The
    // token is already spent by then, so the user must get the invalid-link message.
    vi.mocked(auth.api.resetPassword).mockRejectedValue(
      new Prisma.PrismaClientKnownRequestError("Unique constraint failed", {
        code: PrismaErrorType.UniqueConstraintViolation,
        clientVersion: "test",
      })
    );

    const result = resetPasswordAction({ parsedInput } as never);
    await expect(result).rejects.toBeInstanceOf(InvalidPasswordResetTokenError);
    await expect(result).rejects.toThrow(INVALID_PASSWORD_RESET_TOKEN_ERROR_CODE);
  });

  test("propagates an unexpected (non-APIError) error unchanged", async () => {
    vi.mocked(auth.api.resetPassword).mockRejectedValue(new Error("Database error"));

    await expect(
      resetPasswordAction({ parsedInput } as unknown as Parameters<typeof resetPasswordAction>[0])
    ).rejects.toThrow("Database error");
  });

  test("rejects when password reset is disabled and never calls Better Auth", async () => {
    constantsState.passwordResetDisabled = true;

    await expect(
      resetPasswordAction({ parsedInput } as unknown as Parameters<typeof resetPasswordAction>[0])
    ).rejects.toThrow(OperationNotAllowedError);
    expect(applyIPRateLimit).not.toHaveBeenCalled();
    expect(auth.api.resetPassword).not.toHaveBeenCalled();
  });
});
