// Import modules after mocking
import * as Sentry from "@sentry/nextjs";
import { afterAll, afterEach, beforeEach, describe, expect, test, vi } from "vitest";
import { logger } from "@formbricks/logger";
// Import after mocking
import {
  type TRateLimitReservation,
  checkRateLimit,
  peekRateLimit,
  reserveRateLimit,
  settleRateLimit,
} from "./rate-limit";
import { TRateLimitConfig } from "./types/rate-limit";

const { mockEval, mockGet, mockRedisClient, mockCache } = vi.hoisted(() => {
  const _mockEval = vi.fn();
  const _mockGet = vi.fn();
  const _mockRedisClient = {
    evalSha: _mockEval,
    get: _mockGet,
  };

  const _mockCache = {
    getRedisClient: vi.fn().mockResolvedValue(_mockRedisClient),
    runRedisOperation: vi.fn(async (operation: (client: typeof _mockRedisClient) => Promise<unknown>) =>
      operation(_mockRedisClient)
    ),
  };

  return {
    mockEval: _mockEval,
    mockGet: _mockGet,
    mockRedisClient: _mockRedisClient,
    mockCache: _mockCache,
  };
});

const cacheWithClient = <T>(client: T) => ({
  getRedisClient: vi.fn().mockResolvedValue(client),
  runRedisOperation: vi.fn((operation: (redis: T) => Promise<unknown>) => operation(client)),
});

// Mock all dependencies (will use the hoisted mocks above)
vi.mock("@/lib/cache", () => ({
  cache: mockCache,
}));

vi.mock("@/lib/constants", () => ({
  REDIS_URL: "redis://localhost:6379",
  RATE_LIMITING_DISABLED: false,
  SENTRY_DSN: "https://test@sentry.io/test",
}));
vi.mock("@formbricks/logger", () => ({
  logger: {
    info: vi.fn(),
    debug: vi.fn(),
    warn: vi.fn(),
    error: vi.fn(),
  },
}));
vi.mock("@sentry/nextjs", () => ({
  addBreadcrumb: vi.fn(),
  captureException: vi.fn(),
}));

const { mockRecordRateLimiterUnavailable } = vi.hoisted(() => ({
  mockRecordRateLimiterUnavailable: vi.fn(),
}));
vi.mock("./rate-limit-metrics", () => ({
  recordRateLimiterUnavailable: mockRecordRateLimiterUnavailable,
}));

describe("checkRateLimit", () => {
  const testConfig: TRateLimitConfig = {
    interval: 300, // 5 minutes
    allowedPerInterval: 5,
    namespace: "test",
  };

  beforeEach(() => {
    vi.clearAllMocks();
    // Reset the mock to return our mock client
    mockCache.getRedisClient.mockResolvedValue(mockRedisClient);
  });

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

  // Ensure mocks don't leak to other test suites (e.g. load tests)
  afterAll(() => {
    vi.resetModules();
    vi.resetAllMocks();
  });

  test("should allow request when under limit", async () => {
    // Mock Redis returning count of 2, which is under limit of 5
    mockEval.mockResolvedValue([2, 1]);

    const result = await checkRateLimit(testConfig, "test-user");

    expect(result.ok).toBe(true);
    if (result.ok) {
      expect(result.data.allowed).toBe(true);
    }
  });

  test("should deny request when over limit", async () => {
    // Mock Redis returning count of 6, which is over limit of 5
    mockEval.mockResolvedValue([6, 0]);

    const result = await checkRateLimit(testConfig, "test-user");

    expect(result.ok).toBe(true);
    if (result.ok) {
      expect(result.data.allowed).toBe(false);
    }
    expect(logger.error).toHaveBeenCalledWith(
      expect.objectContaining({
        violationId: expect.stringMatching(/^[0-9a-f-]{36}$/),
        namespace: "test",
      }),
      "Rate limit exceeded"
    );

    const firstViolation = vi.mocked(logger.error).mock.calls.at(-1)?.[0] as { violationId: string };
    await checkRateLimit(testConfig, "test-user");
    const secondViolation = vi.mocked(logger.error).mock.calls.at(-1)?.[0] as { violationId: string };
    expect(secondViolation.violationId).not.toBe(firstViolation.violationId);
  });

  test("should fail open when Redis is unavailable", async () => {
    // Mock Redis throwing an error
    mockEval.mockRejectedValue(new Error("Redis connection failed"));

    const result = await checkRateLimit(testConfig, "test-user");

    expect(result.ok).toBe(true);
    if (result.ok) {
      expect(result.data.allowed).toBe(true);
    }
  });

  test("should fail open when rate limiting is disabled", async () => {
    vi.resetModules();
    vi.doMock("@/lib/constants", () => ({
      REDIS_URL: "redis://localhost:6379",
      RATE_LIMITING_DISABLED: true,
      SENTRY_DSN: "https://test@sentry.io/test",
    }));

    // Dynamic import after mocking
    const { checkRateLimit: checkRateLimitMocked } = await import("./rate-limit");
    const result = await checkRateLimitMocked(testConfig, "test-user");

    expect(result.ok).toBe(true);
    if (result.ok) {
      expect(result.data.allowed).toBe(true);
    }
  });

  test("should fail open when Redis is not configured", async () => {
    vi.resetModules();
    vi.doMock("@/lib/cache", () => ({
      cache: {
        getRedisClient: vi.fn().mockResolvedValue(null),
      },
    }));

    // Dynamic import after mocking
    const { checkRateLimit: checkRateLimitMocked } = await import("./rate-limit");
    const result = await checkRateLimitMocked(testConfig, "test-user");

    expect(result.ok).toBe(true);
    if (result.ok) {
      expect(result.data.allowed).toBe(true);
    }
  });

  test("should generate correct Redis key with window alignment", async () => {
    mockEval.mockResolvedValue([1, 1]);

    await checkRateLimit(testConfig, "test-user");

    expect(mockEval).toHaveBeenCalledWith(expect.stringMatching(/^[0-9a-f]{40}$/), {
      keys: [expect.stringMatching(/^fb:rate_limit:test:test-user:\d+$/)],
      arguments: ["5", expect.any(String), "1"],
    });
  });

  test("should use provided namespace", async () => {
    const configWithCustomNamespace: TRateLimitConfig = {
      interval: 300,
      allowedPerInterval: 5,
      namespace: "custom",
    };

    mockEval.mockResolvedValue([1, 1]);

    await checkRateLimit(configWithCustomNamespace, "test-user");

    expect(mockEval).toHaveBeenCalledWith(expect.stringMatching(/^[0-9a-f]{40}$/), {
      keys: [expect.stringMatching(/^fb:rate_limit:custom:test-user:\d+$/)],
      arguments: ["5", expect.any(String), "1"],
    });
  });

  test("should calculate correct TTL for window expiration", async () => {
    mockEval.mockResolvedValue([1, 1]);

    await checkRateLimit(testConfig, "test-user");

    // TTL should be between 0 and 300 seconds (window interval)
    const ttlUsed = Number.parseInt(mockEval.mock.calls[0][1].arguments[1]);
    expect(ttlUsed).toBeGreaterThan(0);
    expect(ttlUsed).toBeLessThanOrEqual(300);
  });

  test("should count multiple recipients in one atomic request", async () => {
    mockEval.mockResolvedValue([5, 1]);

    const result = await checkRateLimit(testConfig, "test-user", 5);

    expect(result).toEqual({ ok: true, data: { allowed: true, retryAfter: undefined } });
    expect(mockEval).toHaveBeenCalledWith(expect.stringMatching(/^[0-9a-f]{40}$/), {
      keys: [expect.any(String)],
      arguments: ["5", expect.any(String), "5"],
    });
  });

  test("should return an exact-window receipt for a weighted reservation", async () => {
    mockEval.mockResolvedValue([5, 1]);

    const result = await reserveRateLimit(testConfig, "test-user", 5);

    expect(result.ok).toBe(true);
    if (!result.ok) return;
    expect(result.data).toEqual({
      allowed: true,
      retryAfter: undefined,
      reservation: {
        identifier: "test-user",
        key: expect.stringMatching(/^fb:rate_limit:test:test-user:\d+$/),
        namespace: "test",
        requested: 5,
        settled: false,
      },
    });
  });

  test("should atomically release only unused reserved units", async () => {
    const reservation: TRateLimitReservation = {
      identifier: "test-user",
      key: "fb:rate_limit:test:test-user:123",
      namespace: "test",
      requested: 5,
      settled: false,
    };
    mockEval.mockResolvedValue([2, 3]);

    await settleRateLimit(reservation, 2);

    expect(mockEval).toHaveBeenCalledWith(expect.stringMatching(/^[0-9a-f]{40}$/), {
      keys: [reservation.key],
      arguments: ["3"],
    });
    expect(reservation.settled).toBe(true);
  });

  test("should settle a receipt only once", async () => {
    const reservation: TRateLimitReservation = {
      identifier: "test-user",
      key: "fb:rate_limit:test:test-user:123",
      namespace: "test",
      requested: 5,
      settled: false,
    };
    mockEval.mockResolvedValue([2, 3]);

    await settleRateLimit(reservation, 2);
    await settleRateLimit(reservation, 2);

    expect(mockEval).toHaveBeenCalledOnce();
  });

  test("should not touch Redis when all reserved units succeed", async () => {
    const reservation: TRateLimitReservation = {
      identifier: "test-user",
      key: "fb:rate_limit:test:test-user:123",
      namespace: "test",
      requested: 5,
      settled: false,
    };

    await settleRateLimit(reservation, 5);

    expect(mockEval).not.toHaveBeenCalled();
    expect(reservation.settled).toBe(true);
  });

  test.each([-1, 1.5, 6])("should reject invalid successful usage %s", async (successful) => {
    const reservation: TRateLimitReservation = {
      identifier: "test-user",
      key: "fb:rate_limit:test:test-user:123",
      namespace: "test",
      requested: 5,
      settled: false,
    };

    await expect(settleRateLimit(reservation, successful)).rejects.toThrow(
      "Successful rate limit usage must be an integer within the reserved amount"
    );
    expect(mockEval).not.toHaveBeenCalled();
    expect(reservation.settled).toBe(false);
  });

  test.each([0, -1, 1.5])("should reject invalid usage %s", async (requested) => {
    await expect(checkRateLimit(testConfig, "test-user", requested)).rejects.toThrow(
      "Rate limit usage must be a positive integer"
    );
    expect(mockEval).not.toHaveBeenCalled();
  });

  test("should not call Sentry when SENTRY_DSN is not configured", async () => {
    vi.resetModules();

    // Re-mock all dependencies after resetModules
    vi.doMock("@/lib/constants", () => ({
      REDIS_URL: "redis://localhost:6379",
      RATE_LIMITING_DISABLED: false,
      SENTRY_DSN: undefined,
    }));

    const mockAddBreadcrumb = vi.fn();
    const mockCaptureException = vi.fn();
    vi.doMock("@sentry/nextjs", () => ({
      addBreadcrumb: mockAddBreadcrumb,
      captureException: mockCaptureException,
    }));

    vi.doMock("@formbricks/logger", () => ({
      logger: {
        info: vi.fn(),
        debug: vi.fn(),
        warn: vi.fn(),
        error: vi.fn(),
      },
    }));

    vi.doMock("@/lib/cache", () => ({
      cache: cacheWithClient({ evalSha: vi.fn().mockResolvedValue([6, 0]) }),
    }));

    // Dynamic import after mocking
    const { checkRateLimit: checkRateLimitMocked } = await import("./rate-limit");

    await checkRateLimitMocked(testConfig, "test-user");

    // Verify Sentry functions were not called
    expect(mockAddBreadcrumb).not.toHaveBeenCalled();
  });

  test("should call Sentry when SENTRY_DSN is configured and rate limit exceeded", async () => {
    vi.resetModules();

    // Re-mock all dependencies after resetModules
    vi.doMock("@/lib/constants", () => ({
      REDIS_URL: "redis://localhost:6379",
      RATE_LIMITING_DISABLED: false,
      SENTRY_DSN: "https://test@sentry.io/test",
    }));

    const mockAddBreadcrumb = vi.fn();
    const mockCaptureException = vi.fn();
    vi.doMock("@sentry/nextjs", () => ({
      addBreadcrumb: mockAddBreadcrumb,
      captureException: mockCaptureException,
    }));

    vi.doMock("@formbricks/logger", () => ({
      logger: {
        info: vi.fn(),
        debug: vi.fn(),
        warn: vi.fn(),
        error: vi.fn(),
      },
    }));

    vi.doMock("@/lib/cache", () => ({
      cache: cacheWithClient({ evalSha: vi.fn().mockResolvedValue([6, 0]) }),
    }));

    // Dynamic import after mocking
    const { checkRateLimit: checkRateLimitMocked } = await import("./rate-limit");

    await checkRateLimitMocked(testConfig, "test-user");

    // Verify Sentry breadcrumb was added
    expect(mockAddBreadcrumb).toHaveBeenCalledWith({
      message: "Rate limit exceeded",
      level: "warning",
      data: expect.objectContaining({
        identifier: "test-user",
        currentCount: 6,
        limit: 5,
        namespace: "test",
      }),
    });
  });

  test("should call Sentry when SENTRY_DSN is configured and Redis error occurs", async () => {
    vi.resetModules();

    // Re-mock all dependencies after resetModules
    vi.doMock("@/lib/constants", () => ({
      REDIS_URL: "redis://localhost:6379",
      RATE_LIMITING_DISABLED: false,
      SENTRY_DSN: "https://test@sentry.io/test",
    }));

    const mockAddBreadcrumb = vi.fn();
    const mockCaptureException = vi.fn();
    vi.doMock("@sentry/nextjs", () => ({
      addBreadcrumb: mockAddBreadcrumb,
      captureException: mockCaptureException,
    }));

    vi.doMock("@formbricks/logger", () => ({
      logger: {
        info: vi.fn(),
        debug: vi.fn(),
        warn: vi.fn(),
        error: vi.fn(),
      },
    }));

    const redisError = new Error("Redis connection failed");
    vi.doMock("@/lib/cache", () => ({
      cache: cacheWithClient({ evalSha: vi.fn().mockRejectedValue(redisError) }),
    }));

    // Dynamic import after mocking
    const { checkRateLimit: checkRateLimitMocked } = await import("./rate-limit");

    await checkRateLimitMocked(testConfig, "test-user");

    // Verify Sentry exception was captured
    expect(mockCaptureException).toHaveBeenCalledWith(
      redisError,
      expect.objectContaining({
        tags: {
          component: "rate-limiter",
          namespace: "test",
        },
        extra: expect.objectContaining({
          error: redisError,
          identifier: "test-user",
          namespace: "test",
        }),
      })
    );
  });
});

describe("peekRateLimit", () => {
  const testConfig: TRateLimitConfig = {
    interval: 300,
    allowedPerInterval: 5,
    namespace: "test",
  };

  beforeEach(() => {
    vi.clearAllMocks();
    mockCache.getRedisClient.mockResolvedValue(mockRedisClient);
  });

  test("should allow request when current usage is below the limit", async () => {
    mockGet.mockResolvedValue("2");

    const result = await peekRateLimit(testConfig, "test-user");

    expect(result.ok).toBe(true);
    if (result.ok) {
      expect(result.data.allowed).toBe(true);
    }
    expect(mockGet).toHaveBeenCalledWith(expect.stringMatching(/^fb:rate_limit:test:test-user:\d+$/));
    expect(mockEval).not.toHaveBeenCalled();
  });

  test("should deny request when current usage reached the limit", async () => {
    mockGet.mockResolvedValue("5");

    const result = await peekRateLimit(testConfig, "test-user");

    expect(result.ok).toBe(true);
    if (result.ok) {
      expect(result.data.allowed).toBe(false);
      expect(result.data.retryAfter).toBeGreaterThan(0);
    }
    expect(logger.error).toHaveBeenCalledWith(
      expect.objectContaining({ violationId: expect.any(String), namespace: "test" }),
      "Rate limit exceeded"
    );
  });

  test("should treat missing redis key as zero usage", async () => {
    mockGet.mockResolvedValue(null);

    const result = await peekRateLimit(testConfig, "test-user");

    expect(result.ok).toBe(true);
    if (result.ok) {
      expect(result.data.allowed).toBe(true);
    }
  });

  test("should allow request without touching Redis when rate limiting is disabled", async () => {
    vi.resetModules();
    vi.doMock("@/lib/constants", () => ({
      REDIS_URL: "redis://localhost:6379",
      RATE_LIMITING_DISABLED: true,
      SENTRY_DSN: "https://test@sentry.io/test",
    }));

    const { peekRateLimit: peekRateLimitMocked } = await import("./rate-limit");
    const result = await peekRateLimitMocked(testConfig, "test-user");

    expect(result.ok).toBe(true);
    if (result.ok) {
      expect(result.data.allowed).toBe(true);
    }
    expect(mockGet).not.toHaveBeenCalled();
  });

  test("should fail open when Redis is not configured", async () => {
    vi.resetModules();
    // Re-assert rate limiting is enabled so we reach the Redis-client check (doMock from the
    // previous test persists across resetModules).
    vi.doMock("@/lib/constants", () => ({
      REDIS_URL: "redis://localhost:6379",
      RATE_LIMITING_DISABLED: false,
      SENTRY_DSN: "https://test@sentry.io/test",
    }));
    vi.doMock("@/lib/cache", () => ({
      cache: {
        getRedisClient: vi.fn().mockResolvedValue(null),
      },
    }));

    const { peekRateLimit: peekRateLimitMocked } = await import("./rate-limit");
    const result = await peekRateLimitMocked(testConfig, "test-user");

    expect(result.ok).toBe(true);
    if (result.ok) {
      expect(result.data.allowed).toBe(true);
    }
  });

  test("should fail open and report to Sentry when the Redis read throws", async () => {
    vi.resetModules();

    vi.doMock("@/lib/constants", () => ({
      REDIS_URL: "redis://localhost:6379",
      RATE_LIMITING_DISABLED: false,
      SENTRY_DSN: "https://test@sentry.io/test",
    }));

    const mockCaptureException = vi.fn();
    vi.doMock("@sentry/nextjs", () => ({
      addBreadcrumb: vi.fn(),
      captureException: mockCaptureException,
    }));

    vi.doMock("@formbricks/logger", () => ({
      logger: {
        info: vi.fn(),
        debug: vi.fn(),
        warn: vi.fn(),
        error: vi.fn(),
      },
    }));

    const redisError = new Error("Redis connection failed");
    vi.doMock("@/lib/cache", () => ({
      cache: cacheWithClient({ get: vi.fn().mockRejectedValue(redisError) }),
    }));

    const { peekRateLimit: peekRateLimitMocked } = await import("./rate-limit");
    const result = await peekRateLimitMocked(testConfig, "test-user");

    // Fail open: rate-limit read errors must not block the request.
    expect(result.ok).toBe(true);
    if (result.ok) {
      expect(result.data.allowed).toBe(true);
    }
    expect(mockCaptureException).toHaveBeenCalledWith(
      redisError,
      expect.objectContaining({
        tags: {
          component: "rate-limiter",
          namespace: "test",
        },
        extra: expect.objectContaining({
          error: redisError,
          identifier: "test-user",
          namespace: "test",
        }),
      })
    );
  });
});

describe("when the limiter cannot reach Redis", () => {
  const openConfig: TRateLimitConfig = { interval: 300, allowedPerInterval: 5, namespace: "test" };
  const closedConfig: TRateLimitConfig = { ...openConfig, failClosed: true };
  const checks = [
    ["checkRateLimit", checkRateLimit],
    ["reserveRateLimit", reserveRateLimit],
    ["peekRateLimit", peekRateLimit],
  ] as const;

  beforeEach(() => {
    vi.clearAllMocks();
    mockEval.mockReset();
    mockGet.mockReset();
    mockCache.getRedisClient.mockResolvedValue(mockRedisClient);
  });

  test.each(checks)("%s refuses a failClosed limit when there is no Redis client", async (_, check) => {
    mockCache.getRedisClient.mockResolvedValue(null);

    const result = await check(closedConfig, "test-user");

    expect(result.ok).toBe(false);
    expect(logger.error).toHaveBeenCalledWith(
      expect.objectContaining({ namespace: "test", failClosed: true }),
      "Redis unavailable"
    );
    expect(mockRecordRateLimiterUnavailable).toHaveBeenCalledWith({
      failMode: "closed",
      reason: "no_client",
    });
  });

  test.each(checks)("%s refuses a failClosed limit when the Redis call throws", async (_, check) => {
    const redisError = new Error("Redis connection failed");
    mockEval.mockRejectedValue(redisError);
    mockGet.mockRejectedValue(redisError);

    const result = await check(closedConfig, "test-user");

    expect(result.ok).toBe(false);
    expect(Sentry.captureException).toHaveBeenCalledWith(redisError, expect.anything());
    expect(mockRecordRateLimiterUnavailable).toHaveBeenCalledWith({ failMode: "closed", reason: "error" });
  });

  test.each(checks)("%s still lets a limit without failClosed through", async (_, check) => {
    mockCache.getRedisClient.mockResolvedValue(null);

    const result = await check(openConfig, "test-user");

    expect(result).toEqual({ ok: true, data: { allowed: true } });
    expect(mockRecordRateLimiterUnavailable).toHaveBeenCalledWith({ failMode: "open", reason: "no_client" });
  });
});
