import "server-only";
import { MessageChannel, type MessagePort, Worker, receiveMessageOnPort } from "node:worker_threads";
import { logger } from "@formbricks/logger";
import type { SamlDecryption, SamlIdpVerification } from "@/modules/ee/auth/saml/lib/saml-parse-worker";
import type { SamlDecryptedRefusal } from "@/modules/ee/auth/saml/lib/saml-response-bounds";

/**
 * Longest a SAMLResponse may take to parse before jackson sees it (ENG-3973).
 *
 * `checkSamlResponseBounds` is the cheap first gate, but it models xmldom from the outside, and a shape it misreads
 * is a way back to a slow parse. This gate does not model anything: a worker thread parses the response the way
 * `@boxyhq/saml20` does, runs saml20's `parseIssuer` as jackson does first, and is terminated if it runs past the
 * budget. A response that passes is then processed again by jackson on this thread, so what this bounds is jackson's
 * cost per response: no response slower than the budget reaches jackson, whatever its shape.
 *
 * The worker also runs saml20's signature check (ENG-4108), with the configured connection's IdP certificates, as
 * jackson will: saml20 tries them one by one, the whole check each time, so a connection with several (a rollover)
 * costs several times as much when none matches. That check is most of the time measured: canonicalizing and
 * digesting the signed document costs far more than parsing it. The largest real response the byte bounds admit (862
 * verbose Okta groups, 255 KiB) takes ~77 ms with one certificate and ~140 ms with two (Node 26.4, arm64 laptop,
 * warm); a slower host takes longer. The budget has to let that through, and it is one figure for every connection,
 * however many certificates it lists, so one response holds jackson's thread for at most ~1.4 times it (see
 * `JACKSON_COST_PER_WORKER_MS`) whatever the configuration.
 *
 * An encrypted assertion is ciphertext to the byte bounds, and jackson decrypts it and parses the plaintext before it
 * checks any signature (ENG-4051). Given the SP key, the worker does the same within the same budget: the outer parse,
 * the decryption and the plaintext's parse all count. A plaintext of nested namespace scopes that would block jackson
 * for ~1-3 s is cut off here, and a real one (500 groups) takes ~5-20 ms. The worker then holds the decrypted document
 * to the same byte bounds as an unencrypted response (`checkDecryptedSamlResponseBounds`): a flood of cheap elements
 * parses well within the budget, but jackson's later passes over it cost several times as much. An encrypted assertion
 * that will not decrypt goes on to jackson, which fails it the same way (see `takeMessage`).
 *
 * The worker opens an encrypted assertion before jackson has checked the issuer, the connection or the SP session, so
 * how its job ends after that can depend on the plaintext. Those ends are `refused_encrypted`, which the route answers
 * with jackson's own verdict on the response without its assertion (ENG-6646).
 */
export const SAML_PARSE_TIME_BUDGET_MS = 250;

/**
 * Jobs that may wait for the worker. One parse runs at a time, so a flood that fills the queue is refused at once
 * rather than queued behind itself. The per-IP limit in front of this keeps one caller from filling it, but only
 * behind a trusted proxy: without one every caller shares an identity and the limit is skipped, so the queue bound is
 * what remains.
 */
export const SAML_PARSE_QUEUE_LIMIT = 16;

/**
 * Heap for the worker. A response within the markup budget builds a DOM of a few MB; a parse that tries to grow past
 * this ends the worker, not the server.
 */
const WORKER_RESOURCE_LIMITS = { maxOldGenerationSizeMb: 64, maxYoungGenerationSizeMb: 16 };

/** How long a new worker may take to load xmldom, warm up and report ready before it is treated as failed. */
const WORKER_STARTUP_TIMEOUT_MS = 5_000;

/**
 * Main-thread parse time the SAML callback may spend per second, per process. A response that passes the budget is
 * then processed by jackson on this thread (its issuer parse, decryption, two xml2js passes and the signature check),
 * so each one is charged its worker-measured time times `JACKSON_COST_PER_WORKER_MS`. Past the allowance the callback
 * answers 503 rather than let many sources together keep the event loop busy with SAML. It is checked when a response
 * is queued and again when its parse finishes, so a burst can overdraw it by one response at most. A real sign-in
 * costs ~12 ms in the worker with 150 groups and ~77 ms at the largest the bounds admit, so this is about a dozen
 * sign-ins a second per process, or two of the largest. The debt is floored at one response's largest charge
 * (`spendAllowance`), so however many queued jobs settle after it runs out, sign-ins are answered again within about
 * two seconds.
 */
export const SAML_MAIN_THREAD_PARSE_ALLOWANCE_MS = 300;

/**
 * jackson's main-thread time per millisecond the worker measured. The worker runs saml20's signature check as well
 * (ENG-4108), the dearest of jackson's passes, so its time tracks jackson's: measured on Node 26.4, jackson's XML work
 * (`parseIssuer` then `validate`) took 1.0-1.4 times the worker's time on real signed responses (150-500 groups,
 * plain and encrypted) and on the costliest shapes the bounds let through (element floods, wide signed references).
 * Before the worker ran the signature check that was 8-18 times. 2 leaves room for jackson's work around the XML.
 */
export const JACKSON_COST_PER_WORKER_MS = 2;

/** What the worker read from the response: identifiers only, for logging. All of it is unauthenticated. */
export interface SamlResponseClaims {
  issuer?: string;
  id?: string;
  inResponseTo?: string;
}

export type SamlParseProbe =
  | { outcome: "parsed"; ms: number; claims: SamlResponseClaims }
  /** It ran past the time budget or the worker's heap limit. */
  | { outcome: "too_costly"; reason: "time" | "memory" }
  /**
   * Refused once the worker had begun decrypting its encrypted assertion: the decrypted document is past the byte
   * bounds jackson's later passes are sized for, or the job then ran past the budget or the heap limit, or the worker
   * died. Any answer of its own would say something about the plaintext before jackson has checked who sent it (the
   * signal attacks on XML Encryption rely on, W3C XML Encryption 1.1 section 6.1), so the route asks jackson about the
   * response without its assertion instead. `issuer` is as jackson's `parseIssuer` reads it, untruncated; the worker
   * decrypts nothing without one, as jackson does not.
   */
  | {
      outcome: "refused_encrypted";
      issuer: string;
      cause: SamlDecryptedRefusal | { reason: "time" | "memory" | "failed" };
    }
  /**
   * saml20 could not parse it at all (not XML, empty, several roots, a parse error), which jackson throws on at its
   * first step, with no status to answer it by (ENG-7054). It does not reach jackson, so it costs no allowance.
   */
  | { outcome: "unreadable" }
  /**
   * The queue was full, or this process has spent its parse allowance for now, either before the job was queued or
   * by the time the worker answered; the response does not reach jackson.
   */
  | { outcome: "busy" }
  /** The worker could not start or died (out of memory, a crash); nothing is known about the response. */
  | { outcome: "failed" };

interface Job {
  id: number;
  xml: string;
  /** The SP private key, so the worker can decrypt an encrypted assertion as jackson will; `null` when jackson won't. */
  decryptionKey: string | null;
  /** What jackson checks the signature with, so the worker can time that check as jackson will run it. */
  idpVerification: SamlIdpVerification | null;
  resolve: (probe: SamlParseProbe) => void;
}

interface WorkerHandle {
  worker: Worker;
  /** Answers come over this port rather than the worker's own, so a stalled thread can still read them on time. */
  port: MessagePort;
}

/** The worker's word that it is about to decrypt the job's encrypted assertion, with the issuer as jackson reads it. */
interface DecryptingNotice {
  id: number;
  decrypting: true;
  issuer: string;
}

interface WorkerResult {
  id: number;
  /** How long the parse took, measured in the worker, so a busy main thread does not count against the response. */
  ms: number;
  /** Whether saml20 could parse it (see `ParsedSamlResponse`). */
  readable: boolean;
  claims: SamlResponseClaims;
  /** How its encrypted assertion fared, when it holds one jackson would decrypt. */
  decryption?: SamlDecryption;
}

interface Pool {
  handle: WorkerHandle | null;
  ready: Promise<WorkerHandle> | null;
  /**
   * The job holding the worker. Claimed before the worker is ready, so only one job is ever dispatched. `decrypting`
   * is set once the worker has said it is decrypting the job's encrypted assertion.
   */
  running: (Job & { dispatched?: boolean; timer?: NodeJS.Timeout; decrypting?: { issuer: string } }) | null;
  queue: Job[];
  nextId: number;
  /** Main-thread parse time left in the current allowance, and when it was last topped up. */
  allowanceMs: number;
  allowanceAt: number;
}

declare global {
  // Kept on the global object so a `next dev` reload of this module reuses the pool instead of leaking a worker.
  var samlParsePool: Pool | undefined;
}

globalThis.samlParsePool ??= {
  handle: null,
  ready: null,
  running: null,
  queue: [],
  nextId: 0,
  allowanceMs: SAML_MAIN_THREAD_PARSE_ALLOWANCE_MS,
  allowanceAt: 0,
};
const pool = globalThis.samlParsePool;

/** Tops up the main-thread parse allowance for the time passed, up to one second's worth. */
const refillAllowance = (now: number) => {
  const elapsed = now - pool.allowanceAt;
  pool.allowanceAt = now;
  pool.allowanceMs = Math.min(
    SAML_MAIN_THREAD_PARSE_ALLOWANCE_MS,
    pool.allowanceMs + (elapsed * SAML_MAIN_THREAD_PARSE_ALLOWANCE_MS) / 1000
  );
};

/**
 * Spends `ms` of the allowance. Every job queued while it was positive still settles after it runs out, and each may
 * charge up to a whole budget, so unfloored debt would keep sign-ins at 503 long after a burst. The floor is one
 * response's largest charge, so it never forgives part of the response that crossed zero, and recovery takes at most
 * about two seconds.
 */
const ALLOWANCE_DEBT_FLOOR_MS = -(SAML_PARSE_TIME_BUDGET_MS * JACKSON_COST_PER_WORKER_MS);

const spendAllowance = (ms: number) => {
  pool.allowanceMs = Math.max(ALLOWANCE_DEBT_FLOOR_MS, pool.allowanceMs - ms);
};

/**
 * How a job that had begun decrypting ends. Parsed and busy stand as they are (`takeMessage` checks the allowance before
 * it answers parsed). Any other end may have come of the plaintext, so it is `refused_encrypted`, but only while the
 * allowance lasts: once it is spent the job answers busy, as a parsed one would, so that whether a response gets busy
 * never depends on how its plaintext fared.
 */
const afterDecrypting = (probe: SamlParseProbe, issuer: string): SamlParseProbe => {
  if (probe.outcome === "parsed" || probe.outcome === "busy") return probe;
  refillAllowance(performance.now());
  if (pool.allowanceMs <= 0) return { outcome: "busy" };
  switch (probe.outcome) {
    case "too_costly":
      return { outcome: "refused_encrypted", issuer, cause: { reason: probe.reason } };
    case "failed":
      return { outcome: "refused_encrypted", issuer, cause: { reason: "failed" } };
    default:
      return probe;
  }
};

const settleRunning = (probe: SamlParseProbe) => {
  const job = pool.running;
  if (!job) return;
  if (job.timer) clearTimeout(job.timer);
  pool.running = null;
  job.resolve(job.decrypting ? afterDecrypting(probe, job.decrypting.issuer) : probe);
  pump();
};

/**
 * Settle the running job with `message` if it is that job's answer, and return whether it did; a stale message from an
 * older job is ignored. A decrypting notice only marks the job. The allowance is checked again here, not only when the
 * job was queued: every job in a full queue was accepted while the allowance was still positive, so a burst would
 * otherwise all reach jackson at once. Here only the job that spends the allowance does, and the rest answer busy.
 */
const takeMessage = (message: WorkerResult | DecryptingNotice): boolean => {
  const job = pool.running;
  if (!job?.dispatched || message.id !== job.id) return false;
  if ("decrypting" in message) {
    job.decrypting = { issuer: message.issuer };
    return false;
  }
  const result = message;
  if (result.ms > SAML_PARSE_TIME_BUDGET_MS) {
    settleRunning({ outcome: "too_costly", reason: "time" });
    return true;
  }
  // jackson would throw on it before anything else and spend no more time on it, so it is answered before the
  // allowance is checked and charges none. Only an explicit `false`: a worker left running by `next dev` from before
  // the field existed leaves it out, and its answers go on to jackson as they did.
  if (result.readable === false) {
    // It names no issuer, so the worker never began decrypting it. Were it to have, an answer of our own could depend on
    // the plaintext, so the job fails, as for an unannounced refusal below.
    if (job.decrypting) {
      logger.error("SAML parse worker found a response unreadable after it began decrypting");
      settleRunning({ outcome: "failed" });
      return true;
    }
    settleRunning({ outcome: "unreadable" });
    return true;
  }
  // An encrypted assertion that did not decrypt is not refused here. jackson decrypts with the same saml20 and the same
  // key (read from the x509 copy it initialised), so it fails the response the same way, and the failed attempt it
  // repeats is what the worker just timed and is charged for below. Refusing it with an answer of its own would tell
  // an anonymous sender which ciphertexts decrypt, the signal attacks on XML Encryption rely on; failed decryptions
  // and failed signature checks have to look alike from outside.
  if (result.decryption?.decrypted && result.decryption.refusal) {
    // The worker says it is decrypting before it does, on this same port, so the notice is always here first. Were it
    // not, nothing could be answered without telling the sender about the plaintext, so the job fails.
    if (!job.decrypting) {
      logger.error("SAML parse worker refused a decrypted assertion it had not announced");
      settleRunning({ outcome: "failed" });
      return true;
    }
    settleRunning({
      outcome: "refused_encrypted",
      issuer: job.decrypting.issuer,
      cause: result.decryption.refusal,
    });
    return true;
  }
  refillAllowance(performance.now());
  if (pool.allowanceMs <= 0) {
    settleRunning({ outcome: "busy" });
    return true;
  }
  spendAllowance(result.ms * JACKSON_COST_PER_WORKER_MS);
  settleRunning({ outcome: "parsed", ms: result.ms, claims: result.claims });
  return true;
};

const discardWorker = (handle: WorkerHandle) => {
  if (pool.handle !== handle) return;
  pool.handle = null;
  pool.ready = null;
  handle.port.close();
  void handle.worker.terminate();
};

/**
 * A new worker thread, or `null` when it could not be created at all (ERR_WORKER_INIT_FAILED under thread or memory
 * pressure), which is logged here.
 */
const createWorker = (): WorkerHandle | null => {
  try {
    const { port1, port2 } = new MessageChannel();
    const worker = new Worker(new URL("./saml-parse-worker.ts", import.meta.url), {
      resourceLimits: WORKER_RESOURCE_LIMITS,
      workerData: { port: port2 },
      transferList: [port2],
    });
    // A background thread must never keep the server process alive on its own.
    worker.unref();
    port1.unref();
    return { worker, port: port1 };
  } catch (error) {
    logger.error(
      { error: error instanceof Error ? error.message : String(error) },
      "SAML parse worker could not start"
    );
    return null;
  }
};

/** Resolves once `handle`'s worker has warmed up and reported ready; rejects if it fails first. */
const whenReady = (handle: WorkerHandle): Promise<WorkerHandle> => {
  const { worker, port } = handle;
  return new Promise<WorkerHandle>((resolve, reject) => {
    let ready = false;
    const onFailure = (error: Error | number) => {
      clearTimeout(startup);
      if (pool.handle !== handle) return;
      // Node reads what is left on a worker's own port before it reports the worker gone, but not this channel, so a
      // decrypting notice the main thread had not yet got to would be lost with it, and the job judged as if it had
      // never begun decrypting. Only notices are read here: the job ends by this failure either way.
      for (let pending = receiveMessageOnPort(port); pending; pending = receiveMessageOnPort(port)) {
        const message = pending.message as WorkerResult | DecryptingNotice;
        if ("decrypting" in message) takeMessage(message);
      }
      discardWorker(handle);
      // Running out of heap mid-job is the response's cost, not a fault of the server.
      if (
        error instanceof Error &&
        "code" in error &&
        error.code === "ERR_WORKER_OUT_OF_MEMORY" &&
        pool.running?.dispatched
      ) {
        reject(error);
        settleRunning({ outcome: "too_costly", reason: "memory" });
        return;
      }
      logger.error(
        { error: error instanceof Error ? error.message : `exit ${error}` },
        "SAML parse worker stopped"
      );
      reject(error instanceof Error ? error : new Error(`SAML parse worker exited with ${error}`));
      // A worker that never became ready fails everything waiting for it, rather than each job starting another
      // worker and waiting out the same startup in turn.
      const waiting = ready ? [] : pool.queue.splice(0);
      settleRunning({ outcome: "failed" });
      for (const queued of waiting) queued.resolve({ outcome: "failed" });
    };
    const startup = setTimeout(
      () => onFailure(new Error("SAML parse worker did not start")),
      WORKER_STARTUP_TIMEOUT_MS
    );

    port.on("message", (message: { ready: true } | WorkerResult | DecryptingNotice) => {
      if ("ready" in message) {
        clearTimeout(startup);
        ready = true;
        resolve(handle);
        return;
      }
      takeMessage(message);
    });
    worker.on("error", onFailure);
    worker.on("exit", onFailure);
  });
};

/** Runs the next queued job, if the worker is free. Never throws: every failure settles the job as `failed`. */
function pump(): void {
  if (pool.running) return;
  const job = pool.queue.shift();
  if (!job) return;
  pool.running = job;

  if (!pool.ready) {
    const handle = createWorker();
    if (!handle) {
      settleRunning({ outcome: "failed" });
      return;
    }
    pool.handle = handle;
    pool.ready = whenReady(handle);
  }

  pool.ready.then(
    (handle) => {
      if (pool.running !== job) return;
      const timer = setTimeout(() => {
        // The answer may already be waiting behind a busy main thread: read it before blaming the response.
        for (
          let pending = receiveMessageOnPort(handle.port);
          pending;
          pending = receiveMessageOnPort(handle.port)
        ) {
          if (takeMessage(pending.message as WorkerResult | DecryptingNotice)) return;
        }
        // Terminating stops xmldom mid-parse: it is plain JavaScript, which V8 interrupts at once.
        discardWorker(handle);
        settleRunning({ outcome: "too_costly", reason: "time" });
      }, SAML_PARSE_TIME_BUDGET_MS);
      pool.running = { ...job, dispatched: true, timer };
      handle.port.postMessage({
        id: job.id,
        xml: job.xml,
        decryptionKey: job.decryptionKey,
        idpVerification: job.idpVerification,
      });
    },
    () => {
      // Settled by the worker's failure handler already; this only guards a job left waiting on a failed start.
      if (pool.running === job) settleRunning({ outcome: "failed" });
    }
  );
}

/**
 * Charges main-thread SAML time spent outside a probe to the allowance: the route's call to jackson in place of a
 * response refused once its assertion was opened (ENG-6646). Measured on this thread, so it is charged as it is.
 */
export const chargeMainThreadParse = (ms: number): void => {
  refillAllowance(performance.now());
  spendAllowance(ms);
};

/**
 * Parse `samlResponseBase64` in the worker under `SAML_PARSE_TIME_BUDGET_MS`, decoded as jackson decodes it, and with
 * `decryptionKey` (`null` for a response jackson would not decrypt) decrypt its encrypted assertion within the same
 * budget and hold the result to the byte bounds, then run jackson's signature check with `idpVerification` (`null`
 * when no connection is configured). Never rejects: every way it can end is an outcome.
 */
export const probeSamlResponseParse = (
  samlResponseBase64: string,
  decryptionKey: string | null,
  idpVerification: SamlIdpVerification | null
): Promise<SamlParseProbe> => {
  refillAllowance(performance.now());
  if (pool.queue.length >= SAML_PARSE_QUEUE_LIMIT || pool.allowanceMs <= 0) {
    return Promise.resolve({ outcome: "busy" });
  }

  return new Promise<SamlParseProbe>((resolve) => {
    pool.queue.push({
      id: pool.nextId++,
      xml: Buffer.from(samlResponseBase64, "base64").toString(),
      decryptionKey,
      idpVerification,
      resolve,
    });
    pump();
  });
};
