import { beforeEach, describe, expect, test, vi } from "vitest";
import { MCP_GRANT_REVOKE_TRANSACTION_TIMEOUT_MS } from "@/lib/authzed/mcp-grant-timeouts";
import { cache } from "@/lib/cache";
import {
  type TSsoRecoveryClaim,
  type TSsoRecoveryIntent,
  claimSsoRecoveryIntent,
  consumeSsoRecoveryIntent,
  createSsoRecoveryIntent,
  getSsoRecoveryPairedTtlSeconds,
  readSsoRecoveryIntent,
  refreshSsoRecoveryIntent,
} from "./recovery-intent";

/**
 * `vitestSetup.ts` stubs `crypto.createHash` to the constant `"fake-hash"` for the license checks. This
 * module derives its cache key from `sha256(stateId)`, so under that stub every state id collapses onto
 * one key: two intents would overwrite each other and a key-derivation regression would be invisible.
 * Restore the real implementation for this file — the store below depends on keys actually differing.
 */
vi.mock("crypto", async () => await vi.importActual<typeof import("crypto")>("crypto"));

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

vi.mock("@/lib/cache", () => ({
  cache: { get: vi.fn(), set: vi.fn(), del: vi.fn(), getRedisClient: vi.fn(), runRedisOperation: vi.fn() },
}));

const mockCache = vi.mocked(cache);

/**
 * A real store rather than assertions on `cache.set` call arguments.
 *
 * Reading back through the same key derivation is what makes the round-trip tests bind anything: with
 * `expect(cache.set).toHaveBeenCalledWith(...)` the key could be the raw state id, or a constant, and
 * every test would still pass.
 */
const store = new Map<string, { value: unknown; ttlMs?: number }>();

const LINK_TTL_MS = 60 * 15 * 1000;
// One resend's worth of slack past the link TTL — see INTENT_MAX_LIFETIME_MS.
const MAX_LIFETIME_MS = LINK_TTL_MS * 2;

const intentInput = {
  userId: "cm5q1x2y30000abcdefghijkl",
  email: "matti@formbricks.com",
  provider: "azuread",
  providerAccountId: "00000000-1111-2222-3333-444444444444",
  callbackUrl: "https://test-webapp-url.com/workspaces/abc/surveys",
};

describe("SSO recovery intent", () => {
  beforeEach(() => {
    vi.resetAllMocks();
    store.clear();

    mockCache.set.mockImplementation(async (key: string, value: unknown, ttlMs?: number) => {
      store.set(key, { value, ttlMs });
      return { ok: true, data: undefined };
    });
    mockCache.get.mockImplementation(async (key: string) => ({
      ok: true,
      // The real service JSON round-trips, so mirror that rather than handing back the same object.
      data: store.has(key) ? JSON.parse(JSON.stringify(store.get(key)!.value)) : null,
    }));
    mockCache.del.mockImplementation(async (keys: string[]) => {
      keys.forEach((key) => store.delete(key));
      return { ok: true, data: undefined };
    });
    // Real EXPIRE semantics, which is the whole point of using it: a no-op returning 0 on a key that is
    // no longer there. A stub that always "succeeded" would hide the resurrection this guards against.
    mockCache.getRedisClient.mockResolvedValue({
      expire: vi.fn(async (key: string, seconds: number) => {
        const entry = store.get(key);
        if (!entry) return 0;
        store.set(key, { ...entry, ttlMs: seconds * 1000 });
        return 1;
      }),
      // Real DEL semantics: the number of keys removed, 0 when it was already gone.
      del: vi.fn(async (key: string) => (store.delete(key) ? 1 : 0)),
      // The two claim scripts, with the semantics their Lua gives them in Redis.
      eval: vi.fn(
        async (script: string, { keys, arguments: args }: { keys: string[]; arguments: string[] }) => {
          if (script.includes("EXISTS")) {
            const [intentKey, claimKey] = keys;
            if (!store.has(intentKey)) return 0;
            if (store.has(claimKey)) return 2;
            store.set(claimKey, { value: args[0], ttlMs: Number(args[1]) });
            return 1;
          }
          const [claimKey] = keys;
          if (store.get(claimKey)?.value !== args[0]) return 0;
          store.delete(claimKey);
          return 1;
        }
      ),
    } as never);
    mockCache.runRedisOperation.mockImplementation(async (operation) => {
      const redis = await mockCache.getRedisClient();
      if (!redis) throw new Error("Redis unavailable");
      return operation(redis);
    });
  });

  describe("create", () => {
    test("returns an opaque 43-character base64url state id", async () => {
      await expect(createSsoRecoveryIntent(intentInput)).resolves.toMatch(/^[A-Za-z0-9_-]{43}$/);
    });

    test("round-trips the intent", async () => {
      const stateId = await createSsoRecoveryIntent(intentInput);

      await expect(readSsoRecoveryIntent(stateId)).resolves.toEqual({
        ...intentInput,
        createdAt: expect.any(Number),
      });
    });

    test("mints a distinct state id and key per intent, so two recoveries cannot collide", async () => {
      const first = await createSsoRecoveryIntent(intentInput);
      const second = await createSsoRecoveryIntent({ ...intentInput, userId: "other-user" });

      expect(first).not.toBe(second);
      expect(store.size).toBe(2);
      await expect(readSsoRecoveryIntent(first)).resolves.toMatchObject({ userId: intentInput.userId });
      await expect(readSsoRecoveryIntent(second)).resolves.toMatchObject({ userId: "other-user" });
    });

    test("keys on the hash, never on the state id itself", async () => {
      const stateId = await createSsoRecoveryIntent(intentInput);
      const [key] = [...store.keys()];

      expect(key).not.toContain(stateId);
      expect(key).toMatch(/^fb:sso_recovery:\{[0-9a-f]{64}\}:intent$/);
    });

    test("puts the intent and its claim in one Redis Cluster hash slot", async () => {
      const stateId = await createSsoRecoveryIntent(intentInput);
      await claimSsoRecoveryIntent(stateId);

      // The claim script names both keys in one EVAL; Redis Cluster hashes only the `{...}` part.
      const redis = await mockCache.getRedisClient();
      const [, { keys }] = vi.mocked(redis!.eval).mock.calls.at(-1) as [string, { keys: string[] }];
      const hashTags = keys.map((key) => /\{([^}]+)\}/.exec(key)?.[1]);
      expect(hashTags).toHaveLength(2);
      expect(new Set(hashTags).size).toBe(1);
      expect(hashTags[0]).toMatch(/^[0-9a-f]{64}$/);
    });

    test("stores nothing the URL used to carry in the clear", async () => {
      const stateId = await createSsoRecoveryIntent(intentInput);

      // The state id is the whole of what travels; it must not encode the identity it stands for.
      expect(stateId).not.toContain(intentInput.userId);
      expect(stateId).not.toContain(intentInput.email);
    });

    test("stores with the verification-link TTL, so the link cannot outlive the intent", async () => {
      await createSsoRecoveryIntent(intentInput);

      expect([...store.values()][0].ttlMs).toBe(LINK_TTL_MS);
    });

    test("throws when the write fails, rather than emailing a link that cannot work", async () => {
      mockCache.set.mockResolvedValue({ ok: false, error: { code: "RedisConnectionError" } });

      await expect(createSsoRecoveryIntent(intentInput)).rejects.toThrow(
        "Unable to store the SSO recovery intent"
      );
    });
  });

  describe("read", () => {
    test("returns null for an id that was never issued", async () => {
      await expect(readSsoRecoveryIntent("a".repeat(43))).resolves.toBeNull();
    });

    test.each([
      ["absent", undefined],
      ["empty", ""],
      ["too short", "a".repeat(42)],
      ["too long", "a".repeat(44)],
      ["not base64url", `${"a".repeat(42)}+`],
    ])("rejects a %s state id without touching Redis", async (_label, stateId) => {
      await expect(readSsoRecoveryIntent(stateId)).resolves.toBeNull();
      expect(mockCache.get).not.toHaveBeenCalled();
    });

    test("returns null rather than throwing when the cache is unreachable", async () => {
      mockCache.get.mockResolvedValue({ ok: false, error: { code: "RedisConnectionError" } });

      await expect(readSsoRecoveryIntent("a".repeat(43))).resolves.toBeNull();
    });

    /**
     * The check that replaces the JWT signature. A Redis value carries no integrity guarantee, so every
     * field is validated on the way out — `callbackUrl` becomes a redirect and `providerAccountId`
     * becomes the SSO identity linked to the account, and a cast would hand both straight through.
     */
    test.each([
      ["a missing userId", { ...intentInput, userId: undefined }],
      ["a missing email", { ...intentInput, email: undefined }],
      ["a missing provider", { ...intentInput, provider: undefined }],
      ["a missing providerAccountId", { ...intentInput, providerAccountId: undefined }],
      ["a missing callbackUrl", { ...intentInput, callbackUrl: undefined }],
      ["a non-string callbackUrl", { ...intentInput, callbackUrl: { href: "https://evil.example" } }],
      ["a non-string providerAccountId", { ...intentInput, providerAccountId: 42 }],
      ["an empty userId", { ...intentInput, userId: "" }],
      ["a missing createdAt", { ...intentInput }],
      ["a non-numeric createdAt", { ...intentInput, createdAt: "yesterday" }],
    ])("discards a stored record with %s", async (_label, stored) => {
      const stateId = await createSsoRecoveryIntent(intentInput);
      const [key] = [...store.keys()];
      store.set(key, { value: stored });

      await expect(readSsoRecoveryIntent(stateId)).resolves.toBeNull();
    });
  });

  describe("claim", () => {
    const claimed = (claim: TSsoRecoveryClaim) => {
      expect(claim.kind).toBe("claimed");
      return claim as Extract<TSsoRecoveryClaim, { kind: "claimed" }>;
    };

    test("one request at a time may act on a live recovery", async () => {
      const stateId = await createSsoRecoveryIntent(intentInput);

      const first = claimed(await claimSsoRecoveryIntent(stateId));
      await expect(claimSsoRecoveryIntent(stateId)).resolves.toEqual({ kind: "held" });

      await first.release();
      await expect(claimSsoRecoveryIntent(stateId)).resolves.toMatchObject({ kind: "claimed" });
    });

    test("a spent recovery cannot be claimed, and says so rather than blaming another request", async () => {
      const stateId = await createSsoRecoveryIntent(intentInput);
      await consumeSsoRecoveryIntent(stateId);

      await expect(claimSsoRecoveryIntent(stateId)).resolves.toEqual({ kind: "spent" });
    });

    test("a claim that lapsed and was retaken is not freed by its first owner", async () => {
      const stateId = await createSsoRecoveryIntent(intentInput);
      const first = claimed(await claimSsoRecoveryIntent(stateId));
      // The first claim's TTL ran out and another request claimed.
      for (const key of [...store.keys()]) if (key.endsWith(":claim")) store.delete(key);
      claimed(await claimSsoRecoveryIntent(stateId));

      await first.release();

      await expect(claimSsoRecoveryIntent(stateId)).resolves.toEqual({ kind: "held" });
    });

    test("gives back a claim that lands after the cache stopped waiting for it", async () => {
      const stateId = await createSsoRecoveryIntent(intentInput);
      const redis = await mockCache.getRedisClient();
      const realEval = vi.mocked(redis!.eval).getMockImplementation()!;
      let land!: () => void;
      // Redis takes the claim, but only after the cache's deadline has given up on it.
      vi.mocked(redis!.eval).mockImplementationOnce(
        (...args) =>
          new Promise((resolve) => {
            land = () => resolve(realEval(...args));
          })
      );
      mockCache.runRedisOperation.mockImplementationOnce(async (operation) => {
        void operation(redis!);
        throw new Error("Redis operation timed out");
      });

      await expect(claimSsoRecoveryIntent(stateId)).resolves.toEqual({ kind: "unavailable" });
      land();

      // The late claim is given back, so the recovery is claimable again rather than blocked for its TTL.
      await vi.waitFor(async () => {
        await expect(claimSsoRecoveryIntent(stateId)).resolves.toMatchObject({ kind: "claimed" });
      });
    });

    test("does not act when Redis cannot answer, and never throws", async () => {
      const stateId = await createSsoRecoveryIntent(intentInput);
      mockCache.runRedisOperation.mockRejectedValue(new Error("Redis unavailable"));

      await expect(claimSsoRecoveryIntent(stateId)).resolves.toEqual({ kind: "unavailable" });
    });

    test("expires on its own if never released", async () => {
      const stateId = await createSsoRecoveryIntent(intentInput);
      await claimSsoRecoveryIntent(stateId);

      const claim = [...store.entries()].find(([key]) => key.endsWith(":claim"));
      // Outlives the linking transaction's ceiling with room to spare, so it cannot lapse under a link.
      expect(claim?.[1].ttlMs).toBeGreaterThan(2 * MCP_GRANT_REVOKE_TRANSACTION_TIMEOUT_MS);
      expect(claim?.[1].ttlMs).toBeLessThanOrEqual(5 * 60 * 1000);
    });
  });

  describe("consume", () => {
    test("removes the record, so a completed recovery cannot be replayed", async () => {
      const stateId = await createSsoRecoveryIntent(intentInput);

      await consumeSsoRecoveryIntent(stateId);

      await expect(readSsoRecoveryIntent(stateId)).resolves.toBeNull();
    });

    test("says whether this call removed it, so a race has exactly one winner", async () => {
      const stateId = await createSsoRecoveryIntent(intentInput);

      await expect(consumeSsoRecoveryIntent(stateId)).resolves.toBe(true);
      await expect(consumeSsoRecoveryIntent(stateId)).resolves.toBe(false);
    });

    test("never throws when the delete fails — the account link has already committed", async () => {
      const stateId = await createSsoRecoveryIntent(intentInput);
      mockCache.runRedisOperation.mockRejectedValue(new Error("Redis unavailable"));

      await expect(consumeSsoRecoveryIntent(stateId)).resolves.toBe(false);
    });
  });

  /**
   * The number a resend gives to BOTH halves of the flow.
   *
   * The link and the intent are needed together, so whichever expires first ends recovery — and if the
   * link is the survivor the user is signed in and then told recovery failed. A resend is the one place
   * they can come apart, because the intent's refresh is capped against `createdAt` while a freshly
   * minted link would otherwise take a full TTL however close that ceiling is.
   */
  describe("paired ttl for a resend", () => {
    const LINK_TTL_SECONDS = LINK_TTL_MS / 1000;
    const intentAged = (ageMs: number): TSsoRecoveryIntent => ({
      ...intentInput,
      createdAt: Date.now() - ageMs,
    });

    test("gives a resend well inside the ceiling the full link TTL", () => {
      expect(getSsoRecoveryPairedTtlSeconds(intentAged(0))).toBe(LINK_TTL_SECONDS);
    });

    test("returns nothing to send once the absolute lifetime has passed", () => {
      expect(getSsoRecoveryPairedTtlSeconds(intentAged(MAX_LIFETIME_MS + 1))).toBe(0);
    });

    /**
     * The invariant, swept rather than sampled at one age: whatever the record's age, the lifetime
     * handed out can never reach past the ceiling. Asserted as an absolute instant — `createdAt + ttl`
     * against `createdAt + MAX` cancels `createdAt` and would pass for any TTL under the ceiling,
     * which is the shape that made an earlier assertion here unfalsifiable.
     */
    test.each([0, 1, 60, LINK_TTL_MS / 2, LINK_TTL_MS, LINK_TTL_MS + 1, MAX_LIFETIME_MS - 1000])(
      "aged %ims, expires no later than the ceiling and never beyond one link TTL",
      (ageMs) => {
        const intent = intentAged(ageMs);
        const now = Date.now();

        const ttlSeconds = getSsoRecoveryPairedTtlSeconds(intent);

        expect(now + ttlSeconds * 1000).toBeLessThanOrEqual(intent.createdAt + MAX_LIFETIME_MS);
        expect(ttlSeconds).toBeLessThanOrEqual(LINK_TTL_SECONDS);
      }
    );

    /**
     * Past one link TTL of age the clamp has to actually bite, or the sweep above is satisfied by a
     * function that always returns the full TTL.
     */
    test("clamps below a full link TTL once the ceiling is nearer than one", () => {
      expect(getSsoRecoveryPairedTtlSeconds(intentAged(LINK_TTL_MS + 60_000))).toBeLessThan(LINK_TTL_SECONDS);
    });
  });

  describe("refresh", () => {
    const storedIntent = (createdAt: number): TSsoRecoveryIntent => ({ ...intentInput, createdAt });
    // Refresh takes the TTL rather than the record, so drive it the way the resend action does: through
    // `getSsoRecoveryPairedTtlSeconds`. Composing them here keeps these assertions on the behaviour a
    // resend actually produces, instead of on a number no caller would pass.
    const pairedTtl = (createdAt: number): number => getSsoRecoveryPairedTtlSeconds(storedIntent(createdAt));

    test("re-pairs a resent link with a full TTL while well inside the absolute lifetime", async () => {
      const stateId = await createSsoRecoveryIntent(intentInput);
      const [key] = [...store.keys()];
      // Age it first, as a link resent later in the window would find it. Asserted against a record still
      // carrying its issued TTL, this cannot fail: a refresh that does nothing at all leaves the same
      // value, which is what made an earlier version of this test green against a no-op.
      store.set(key, { ...store.get(key)!, ttlMs: 60 * 1000 });

      await refreshSsoRecoveryIntent(stateId, pairedTtl(Date.now()));

      expect(store.get(key)!.ttlMs).toBe(LINK_TTL_MS);
    });

    /**
     * A resend and a completion can race. Refresh must extend a live record, never write one back —
     * otherwise a resend landing just after completion resurrects the intent that completion consumed,
     * and single use quietly stops holding.
     */
    test("cannot resurrect an intent that a completion already consumed", async () => {
      const stateId = await createSsoRecoveryIntent(intentInput);
      await consumeSsoRecoveryIntent(stateId);

      await refreshSsoRecoveryIntent(stateId, pairedTtl(Date.now()));

      expect(store.size).toBe(0);
      await expect(readSsoRecoveryIntent(stateId)).resolves.toBeNull();
    });

    test("does nothing when Redis is unavailable, rather than throwing at the caller", async () => {
      const stateId = await createSsoRecoveryIntent(intentInput);
      mockCache.getRedisClient.mockResolvedValue(null);

      await expect(refreshSsoRecoveryIntent(stateId, pairedTtl(Date.now()))).resolves.toBeUndefined();
    });

    /**
     * `resendVerificationEmailAction` is unauthenticated, so a plain sliding window would let anyone
     * holding a state id keep an intent alive forever. `createdAt` never moves, so the slide is capped.
     */
    test("clamps the TTL so a refresh cannot push expiry past the absolute lifetime", async () => {
      const now = Date.now();
      const stateId = await createSsoRecoveryIntent(intentInput);
      // Deep enough in that only half a link TTL of absolute lifetime is left. The record keeps the full
      // TTL it was issued with, so a refresh that does nothing also fails the expiry assertion below.
      const createdAt = now - (MAX_LIFETIME_MS - LINK_TTL_MS / 2);

      await refreshSsoRecoveryIntent(stateId, pairedTtl(createdAt));

      const { ttlMs } = [...store.values()][0];
      // The resulting EXPIRY, measured from now — not the TTL in isolation. `createdAt + ttlMs` versus
      // `createdAt + MAX_LIFETIME_MS` cancels `createdAt` and only says `ttlMs <= MAX_LIFETIME_MS`,
      // which a full link TTL always satisfies. Elapsed time cannot flake this: any drift after `now`
      // shrinks the remaining lifetime, so `ttlMs` only ever comes out smaller.
      expect(now + ttlMs!).toBeLessThanOrEqual(createdAt + MAX_LIFETIME_MS);
      expect(ttlMs).toBeLessThan(LINK_TTL_MS);
    });

    test("leaves the expiry alone once the absolute lifetime has elapsed", async () => {
      const stateId = await createSsoRecoveryIntent(intentInput);
      const [key] = [...store.keys()];
      // Age the stored TTL first. Asserted against a record still carrying its issued TTL, "unchanged"
      // is indistinguishable from "re-set to the same full TTL", and the assertion cannot fail —
      // dropping the absolute-lifetime guard left it green. A lowered TTL is a value only a real
      // refresh would overwrite.
      const agedTtlMs = 60 * 1000;
      store.set(key, { ...store.get(key)!, ttlMs: agedTtlMs });

      await refreshSsoRecoveryIntent(stateId, pairedTtl(Date.now() - MAX_LIFETIME_MS - 1));

      // The TTL, not the call order: an implementation that reaches Redis and then declines to extend
      // is equally correct, so asserting "never asked for a client" would fail a correct one.
      expect(store.get(key)!.ttlMs).toBe(agedTtlMs);
    });

    /**
     * A lifetime that is not a real number must never reach EXPIRE. `NaN` is the one that matters: it
     * fails every comparison, so a guard written as `ttlSeconds <= 0` lets it straight through — which
     * is what Sonar's S1940 suggestion would have reintroduced here.
     */
    test.each([
      ["NaN", Number.NaN],
      ["Infinity", Number.POSITIVE_INFINITY],
      ["a negative TTL", -1],
    ])("ignores %s rather than passing it to EXPIRE", async (_label, ttlSeconds) => {
      const stateId = await createSsoRecoveryIntent(intentInput);
      const [key] = [...store.keys()];
      const agedTtlMs = 60 * 1000;
      store.set(key, { ...store.get(key)!, ttlMs: agedTtlMs });

      await refreshSsoRecoveryIntent(stateId, ttlSeconds);

      expect(store.get(key)!.ttlMs).toBe(agedTtlMs);
    });

    test("leaves the stored record untouched, so a refresh cannot write back a stale copy", async () => {
      const stateId = await createSsoRecoveryIntent(intentInput);
      const issuedCreatedAt = (await readSsoRecoveryIntent(stateId))!.createdAt;
      // A createdAt a refresh would visibly overwrite if it wrote the record back at all, rather than
      // one the issued value already satisfies.
      const staleCreatedAt = issuedCreatedAt - LINK_TTL_MS;

      await refreshSsoRecoveryIntent(stateId, pairedTtl(staleCreatedAt));

      expect((await readSsoRecoveryIntent(stateId))?.createdAt).toBe(issuedCreatedAt);
    });
  });
});
