import "server-only";
import { createHash } from "node:crypto";
import { prisma } from "@formbricks/database";
import { PipelineTriggers, Prisma } from "@formbricks/database/prisma";
import {
  type JobHandler,
  type TResponsePipelineJobData,
  UnrecoverableError,
  enqueueWebhookDeliveryJob,
} from "@formbricks/jobs";
import { logger } from "@formbricks/logger";
import { type TCalBookingsByQuestionId } from "@formbricks/types/cal-booking";
import { type TSurveyBlock } from "@formbricks/types/surveys/blocks";
import { TSurveyElementTypeEnum } from "@formbricks/types/surveys/elements";
import { type TUserLocale, ZUserLocale } from "@formbricks/types/user";
import { filterAuthorizedUserIds } from "@/lib/authorization/user-list";
import { POSTHOG_KEY } from "@/lib/constants";
import { selectSurveyEmbeddedDataLinks, withInlinedEmbeddedFields } from "@/lib/embedded-data/survey-fields";
import { handleFeedbackSourcePipeline } from "@/lib/feedback-source/pipeline-handler";
import { getIntegrations } from "@/lib/integration/service";
import { isDatabasePoolExhaustionError } from "@/lib/jobs/pool-exhaustion";
import { getQuestionCodes } from "@/lib/response/data-by-question-code";
import { getResponseCountBySurveyId } from "@/lib/response/service";
import { isSurveyOutboundAllowed, surveyOutboundVisibilitySelect } from "@/lib/survey/visibility/outbound";
import { sendTelemetryEvents } from "@/lib/telemetry/usage-update";
import { isNewReleaseWorkEnabled } from "@/lib/upgrade/compatibility";
import { queueAuditEventWithoutRequest } from "@/modules/ee/audit-logs/lib/handler";
import { type TAuditStatus, UNKNOWN_DATA } from "@/modules/ee/audit-logs/types/audit-log";
import { recordResponseCreatedMeterEvent } from "@/modules/ee/billing/lib/metering";
import { dispatchWorkflowRunViaJobs } from "@/modules/ee/workflows/lib/runner/dispatch";
import { enqueueResponseCompletedWorkflowRuns } from "@/modules/ee/workflows/lib/runner/enqueue-response-completed-runs";
import { sendResponseFinishedEmail } from "@/modules/email";
import { captureSurveyResponsePostHogEvent } from "@/modules/response-pipeline/lib/posthog";
import { sendFollowUpsForResponse } from "@/modules/survey/follow-ups/lib/follow-ups";
import { FollowUpSendError } from "@/modules/survey/follow-ups/types/follow-up";
import { getElementsFromBlocks } from "@/modules/survey/lib/client-utils";
import { getFinishedResponseCountBySurveyId } from "@/modules/survey/lib/response";
import { handleIntegrations } from "./handle-integrations";
import { recordSurveyOutboundSkipped } from "./outbound-visibility-metrics";
import { processWebhookDeliveryJob } from "./process-webhook-delivery-job";

const DEFAULT_NOTIFICATION_LOCALE: TUserLocale = "en-US";

const pipelineOrganizationSelect = {
  id: true,
  displayTimeZone: true,
  billing: {
    select: {
      stripeCustomerId: true,
    },
  },
} satisfies Prisma.OrganizationSelect;

const pipelineSurveySelect = {
  id: true,
  ...surveyOutboundVisibilitySelect,
  workspaceId: true,
  name: true,
  type: true,
  status: true,
  createdAt: true,
  updatedAt: true,
  blocks: true,
  // ENG-1837: the definitions the notification email, follow-ups and integrations below resolve
  // through. Inlined by `getSurveyForPipeline` so the raw relation never reaches the handlers — and
  // (ENG-2404) the source the legacy `variables` / `hiddenFields` keys are derived from.
  embeddedDataLinks: selectSurveyEmbeddedDataLinks,
  followUps: true,
  autoComplete: true,
  languages: {
    select: {
      default: true,
      enabled: true,
      language: {
        select: {
          id: true,
          code: true,
          alias: true,
          createdAt: true,
          updatedAt: true,
          workspaceId: true,
        },
      },
    },
  },
} satisfies Prisma.SurveySelect;

type TPipelineOrganization = Prisma.OrganizationGetPayload<{ select: typeof pipelineOrganizationSelect }>;
type TPipelineSurveyRow = Prisma.SurveyGetPayload<{ select: typeof pipelineSurveySelect }>;
type TPipelineSurvey = ReturnType<typeof withInlinedEmbeddedFields<TPipelineSurveyRow>>;

const getOrganizationForPipeline = async (workspaceId: string): Promise<TPipelineOrganization | null> =>
  prisma.organization.findFirst({
    where: {
      workspaces: {
        some: {
          id: workspaceId,
        },
      },
    },
    select: pipelineOrganizationSelect,
  });

const getSurveyForPipeline = async (surveyId: string): Promise<TPipelineSurvey | null> => {
  const survey = await prisma.survey.findUnique({
    where: {
      id: surveyId,
    },
    select: pipelineSurveySelect,
  });

  return survey ? withInlinedEmbeddedFields(survey) : null;
};

const getPipelineLogContext = (
  data: TResponsePipelineJobData,
  context: Parameters<JobHandler<TResponsePipelineJobData>>[1]
) => ({
  attempt: context.attempt,
  workspaceId: data.workspaceId,
  event: data.event,
  jobId: context.jobId,
  jobName: context.jobName,
  maxAttempts: context.maxAttempts,
  queueName: context.queueName,
  responseId: data.response.id,
  surveyId: data.surveyId,
});

const toError = (error: unknown, fallbackMessage: string): Error =>
  error instanceof Error ? error : new Error(fallbackMessage);

const toUserLocale = (locale: string): TUserLocale => {
  const parsedLocale = ZUserLocale.safeParse(locale);
  return parsedLocale.success ? parsedLocale.data : DEFAULT_NOTIFICATION_LOCALE;
};

/**
 * The Standard Webhooks `webhook-id`, derived from this pipeline job's id. Unchanged from when this job
 * delivered webhooks itself, so receivers see the same ids — and because it is fixed here and carried
 * into the delivery job, it stays constant across every retry of either job.
 */
const createWebhookMessageId = ({
  event,
  jobId,
  webhookId,
}: {
  event: TResponsePipelineJobData["event"];
  jobId: string;
  webhookId: string;
}): string => createHash("sha256").update(`${jobId}:${webhookId}:${event}`).digest("hex");

/**
 * Deterministic per (pipeline job, webhook): BullMQ ignores an `add` whose jobId already exists, so a
 * pipeline retry after a partial fan-out re-enqueues only the deliveries that never made it. Keyed on the
 * pipeline job id rather than the response, because `responseUpdated` legitimately fires once per update
 * and each must be delivered.
 *
 * That dedupe holds only while BullMQ still has the completed job: delivery jobs inherit the queue's
 * `removeOnComplete` of 24 h **or 1000 jobs**, and at high throughput the count arm can trim a completed
 * delivery inside the pipeline job's own ~15 s retry window, after which a retry re-adds the id and the
 * receiver sees the event twice. The duplicate carries the same `webhook-id` (it is derived from the
 * pipeline job id), so a receiver deduping as the docs describe is unaffected — delivery is at-least-once,
 * not exactly-once. ENG-2829 tracks moving this onto BullMQ deduplication, which does not depend on
 * retention.
 */
const createWebhookDeliveryJobId = (jobId: string, webhookId: string): string => `whd-${jobId}-${webhookId}`;

type TPipelineWebhook = { id: string };

// Ids only: the delivery job re-reads url and secret at send time, so the secret never enters Redis.
const getWebhooksForPipeline = async (
  workspaceId: string,
  event: PipelineTriggers,
  surveyId: string
): Promise<TPipelineWebhook[]> => {
  return await prisma.webhook.findMany({
    where: {
      workspaceId,
      triggers: { has: event },
      OR: [{ surveyIds: { has: surveyId } }, { surveyIds: { isEmpty: true } }],
    },
    select: { id: true },
  });
};

/**
 * What the survey's Cal.com elements reported, for the webhook body's `calBookingsByQuestionId`. It is read
 * from the row rather than carried on `data.response`, because the column is omitted from every read by
 * default and the pipeline payload comes from many routes: reading it here gives every event the same
 * answer. Skipped, with no query, for a survey without a Cal element.
 */
const getCalBookingsForPipeline = async (
  responseId: string,
  blocks: readonly TSurveyBlock[]
): Promise<TCalBookingsByQuestionId | undefined> => {
  const hasCalElement = getElementsFromBlocks(blocks as TSurveyBlock[]).some(
    (element) => element.type === TSurveyElementTypeEnum.Cal
  );
  if (!hasCalElement) return undefined;

  const row = await prisma.response.findUnique({
    where: { id: responseId },
    select: { calBookings: true },
  });
  return row?.calBookings ?? undefined;
};

/**
 * Fans the event out as one `webhook-delivery.process` job per matching webhook and returns without
 * waiting on any HTTP. Each delivery then retries on its own budget, so a dead endpoint neither delays
 * the side effects below nor causes the healthy endpoints to be re-sent.
 *
 * Enqueue failures (Redis unavailable) fail this job while attempts remain — the deterministic child
 * ids make the retry idempotent — and on the final attempt are logged and skipped so the remaining
 * side effects still run, as before.
 */
const enqueueWebhookDeliveryJobs = async ({
  data,
  logContext,
  survey,
  webhooks,
}: {
  data: TResponsePipelineJobData;
  logContext: ReturnType<typeof getPipelineLogContext>;
  survey: TPipelineSurvey;
  webhooks: TPipelineWebhook[];
}): Promise<void> => {
  if (webhooks.length === 0) {
    return;
  }

  const questionCodes = getQuestionCodes(survey.blocks);
  const calBookings = await getCalBookingsForPipeline(data.response.id, survey.blocks);
  const enqueue = await isNewReleaseWorkEnabled();
  const deliver: (...args: Parameters<typeof enqueueWebhookDeliveryJob>) => Promise<unknown> = enqueue
    ? enqueueWebhookDeliveryJob
    : async (payload, options) => {
        const jobId = options?.jobId ?? createWebhookDeliveryJobId(logContext.jobId, payload.webhookId);
        try {
          await processWebhookDeliveryJob(payload, {
            attempt: logContext.attempt,
            maxAttempts: logContext.maxAttempts,
            jobId,
            jobName: "response-pipeline.process",
            queueName: "formbricks-jobs",
          });
        } catch (error) {
          // Keep the released 6.0 queue contract: transient failures retry this pipeline job before
          // its other side effects start. Old consumers cannot process a delivery-only retry payload.
          // A permanently rejected webhook must not make the entire response job unrecoverable.
          if (!(error instanceof UnrecoverableError)) throw error;
          logger.error(
            { ...logContext, err: error, webhookId: payload.webhookId },
            "Inline webhook delivery failed permanently; continuing response processing"
          );
        }
        return { jobId, jobName: "response-pipeline.process", queueName: "formbricks-jobs" };
      };
  const results = await Promise.allSettled(
    webhooks.map((webhook) =>
      deliver(
        {
          webhookId: webhook.id,
          workspaceId: data.workspaceId,
          surveyId: data.surveyId,
          event: data.event,
          webhookMessageId: createWebhookMessageId({
            event: data.event,
            jobId: logContext.jobId,
            webhookId: webhook.id,
          }),
          response: data.response,
          survey: {
            name: survey.name,
            type: survey.type,
            status: survey.status,
            createdAt: survey.createdAt,
            updatedAt: survey.updatedAt,
            questionCodes,
          },
          ...(calBookings && Object.keys(calBookings).length > 0 && { calBookings }),
        },
        { jobId: createWebhookDeliveryJobId(logContext.jobId, webhook.id) }
      )
    )
  );

  const failedResults = results.filter((result) => result.status === "rejected");
  if (failedResults.length === 0) {
    logger.debug(
      { ...logContext, webhookCount: webhooks.length },
      "Response pipeline webhook deliveries enqueued"
    );
    return;
  }

  if (logContext.attempt < logContext.maxAttempts) {
    throw toError(failedResults[0].reason, "Response pipeline webhook delivery enqueue failed");
  }

  logger.error(
    {
      ...logContext,
      // The cause only reaches the operator on the earlier attempts, via the error this throws above.
      // The final attempt is the one that drops the deliveries, so it has to carry the reason itself.
      err: failedResults[0].reason,
      failedWebhookCount: failedResults.length,
    },
    "Response pipeline webhook delivery enqueue exhausted retries; continuing with remaining side effects"
  );
};

const loadIntegrationsSafely = async ({
  logContext,
  workspaceId,
}: {
  logContext: ReturnType<typeof getPipelineLogContext>;
  workspaceId: string;
}): Promise<Awaited<ReturnType<typeof getIntegrations>>> => {
  try {
    return await getIntegrations(workspaceId);
  } catch (error) {
    logger.error(
      {
        ...logContext,
        err: error,
      },
      "Response pipeline integration lookup failed"
    );

    return [];
  }
};

/**
 * Response counts are optional side inputs: a failed lookup must never fail the job, so it
 * resolves to `null` and the consumer skips its own work instead.
 */
const loadResponseCountSafely = async ({
  count,
  failureMessage,
  logContext,
}: {
  count: () => Promise<number>;
  failureMessage: string;
  logContext: Record<string, unknown>;
}): Promise<number | null> => {
  try {
    return await count();
  } catch (error) {
    logger.error(
      {
        ...logContext,
        err: error,
      },
      failureMessage
    );

    return null;
  }
};

/**
 * ENG-3282: an alert carries the response, so it only goes to someone who may read it — for a restricted
 * survey, its owner and the organization administrators. A subscription made while the survey was
 * workspace-visible does not survive a restriction. The recipient query can include billing-only team
 * members, so every recipient still needs a response-read decision, including workspace-visible surveys.
 */
const keepRecipientsWhoMayReadResponses = async <TUser extends { id: string }>(
  users: TUser[],
  survey: TPipelineSurvey
): Promise<TUser[]> => {
  if (users.length === 0) return users;
  const allowed = await filterAuthorizedUserIds(
    users.map((user) => user.id),
    "survey.response_read",
    {
      id: survey.id,
      type: "survey",
    }
  );
  return users.filter((user) => allowed.has(user.id));
};

const getUsersWithNotifications = async ({
  data,
  logContext,
  workspaceId,
  survey,
}: {
  data: TResponsePipelineJobData;
  logContext: ReturnType<typeof getPipelineLogContext>;
  workspaceId: string;
  survey: TPipelineSurvey;
}): Promise<Array<{ email: string; locale: TUserLocale }>> => {
  try {
    const users = await prisma.user.findMany({
      where: {
        memberships: {
          some: {
            organization: {
              workspaces: {
                some: {
                  id: workspaceId,
                },
              },
            },
          },
        },
        OR: [
          {
            memberships: {
              some: {
                role: {
                  in: ["owner", "manager"],
                },
                organization: {
                  workspaces: {
                    some: {
                      id: workspaceId,
                    },
                  },
                },
              },
            },
          },
          {
            teamUsers: {
              some: {
                team: {
                  workspaceTeams: {
                    some: {
                      workspace: {
                        id: workspaceId,
                      },
                    },
                  },
                },
              },
            },
          },
        ],
        notificationSettings: {
          path: ["alert", data.surveyId],
          equals: true,
        },
        // A Dashboard viewer never reads responses (ADR-0025), so it is never a response alert's
        // recipient, whatever its team grants or a stale notification setting say.
        NOT: {
          memberships: {
            some: {
              role: "dashboard_viewer",
              organization: { workspaces: { some: { id: workspaceId } } },
            },
          },
        },
      },
      select: { email: true, id: true, locale: true },
    });

    return (await keepRecipientsWhoMayReadResponses(users, survey)).map((user) => ({
      email: user.email,
      locale: toUserLocale(user.locale),
    }));
  } catch (error) {
    logger.error(
      {
        ...logContext,
        err: error,
      },
      "Response pipeline notification recipient lookup failed"
    );

    return [];
  }
};

const handleFollowUpsSafely = async ({
  data,
  logContext,
  survey,
}: {
  data: TResponsePipelineJobData;
  logContext: ReturnType<typeof getPipelineLogContext>;
  survey: TPipelineSurvey;
}): Promise<void> => {
  if (!survey.followUps?.length) {
    return;
  }

  try {
    const followUpsResult = await sendFollowUpsForResponse(data.response.id, data.locale);
    if (!followUpsResult.ok && followUpsResult.error.code !== FollowUpSendError.FOLLOW_UP_NOT_ALLOWED) {
      logger.error(
        {
          ...logContext,
          error: followUpsResult.error,
        },
        "Response pipeline follow-up delivery failed"
      );
    }
  } catch (error) {
    logger.error(
      {
        ...logContext,
        err: error,
      },
      "Response pipeline follow-up delivery failed"
    );
  }
};

const sendNotificationEmailsSafely = async ({
  data,
  logContext,
  responseCount,
  survey,
  usersWithNotifications,
  workspaceId,
}: {
  data: TResponsePipelineJobData;
  logContext: ReturnType<typeof getPipelineLogContext>;
  responseCount: number | null;
  survey: TPipelineSurvey;
  usersWithNotifications: Array<{ email: string; locale: TUserLocale }>;
  workspaceId: string;
}): Promise<void> => {
  if (responseCount === null) {
    if (usersWithNotifications.length > 0) {
      logger.error(
        {
          ...logContext,
          notificationRecipientCount: usersWithNotifications.length,
        },
        "Response pipeline notification emails skipped because the response count could not be loaded"
      );
    }

    return;
  }

  await Promise.all(
    usersWithNotifications.map(async (user) => {
      try {
        await sendResponseFinishedEmail(
          user.email,
          user.locale,
          workspaceId,
          survey,
          data.response,
          responseCount
        );
      } catch (error) {
        logger.error(
          // No address: a capped notification lands here too, at volume, and addresses do not belong in logs.
          { ...logContext, err: error },
          "Response pipeline notification email failed"
        );
      }
    })
  );
};

/**
 * The completed-response count that closes this survey, or `null` when the response limit
 * cannot apply — no limit configured, or the survey is already closed.
 *
 * The limit is defined in terms of *completed* responses, so only finished responses count
 * towards it. Counting every response would close the survey once the number of starts
 * (partial + finished) hit the limit.
 */
const getAutoCompleteThreshold = (survey: TPipelineSurvey): number | null =>
  survey.autoComplete && survey.status !== "completed" ? survey.autoComplete : null;

const handleSurveyAutoCompleteSafely = async ({
  finishedResponseCount,
  logContext,
  organizationId,
  survey,
}: {
  finishedResponseCount: number | null;
  logContext: ReturnType<typeof getPipelineLogContext>;
  organizationId: string;
  survey: TPipelineSurvey;
}): Promise<void> => {
  const autoCompleteThreshold = getAutoCompleteThreshold(survey);

  // `finishedResponseCount` is only looked up when a threshold applies, so a `null` here means
  // the lookup failed — already logged by loadResponseCountSafely.
  if (autoCompleteThreshold === null || finishedResponseCount === null) {
    return;
  }

  if (finishedResponseCount < autoCompleteThreshold) {
    return;
  }

  let logStatus: TAuditStatus = "success";

  try {
    await prisma.survey.update({
      where: {
        id: survey.id,
      },
      data: {
        status: "completed",
      },
    });
  } catch (error) {
    logStatus = "failure";
    logger.error(
      {
        ...logContext,
        err: error,
      },
      "Response pipeline survey auto-complete update failed"
    );
  }

  try {
    await queueAuditEventWithoutRequest({
      status: logStatus,
      action: "updated",
      targetType: "survey",
      userId: UNKNOWN_DATA,
      userType: "system",
      targetId: survey.id,
      organizationId,
      ...(logStatus === "success"
        ? {
            newObject: {
              status: "completed",
            },
          }
        : {}),
    });
  } catch (error) {
    logger.error(
      {
        ...logContext,
        auditStatus: logStatus,
        err: error,
      },
      "Response pipeline survey auto-complete audit log failed"
    );
  }
};

const isLowQuality = async (responseId: string): Promise<boolean> =>
  (await prisma.response.findUnique({ where: { id: responseId }, select: { outcome: true } }))?.outcome ===
  "lowQuality";

/**
 * The responseFinished work that sends the response out of the app: integrations, the feedback-source
 * pipeline and follow-ups. ENG-3283: none of it runs for a survey that is not workspace-visible.
 */
const runOutboundResponseFinishedEffects = async ({
  data,
  displayTimeZone,
  integrations,
  logContext,
  outboundAllowed,
  survey,
  workspaceId,
}: {
  data: TResponsePipelineJobData;
  displayTimeZone: string | null;
  integrations: Awaited<ReturnType<typeof loadIntegrationsSafely>>;
  logContext: ReturnType<typeof getPipelineLogContext>;
  outboundAllowed: boolean;
  survey: TPipelineSurvey;
  workspaceId: string;
}): Promise<void> => {
  if (!outboundAllowed) {
    recordSurveyOutboundSkipped("integration");
    recordSurveyOutboundSkipped("feedback_source");
    if (survey.followUps?.length) recordSurveyOutboundSkipped("follow_up", survey.followUps.length);
    return;
  }

  if (integrations.length > 0) {
    try {
      await handleIntegrations(integrations, data, survey, displayTimeZone ?? "UTC");
    } catch (error) {
      logger.error(
        {
          ...logContext,
          err: error,
        },
        "Response pipeline integration handling failed"
      );
    }
  }

  try {
    // The job carries the response as it was when the event was queued. A partial marked Low quality and
    // then finished by the respondent still says "no outcome" there, so the outcome is read now (ENG-6785).
    // Failing to read it publishes nothing rather than risking a Low quality response's records; a restore,
    // or the historical import, brings them back.
    if (!(await isLowQuality(data.response.id))) {
      await handleFeedbackSourcePipeline(data.response, survey, workspaceId);
    }
  } catch (error) {
    logger.error(
      {
        ...logContext,
        err: error,
      },
      "Response pipeline feedbackSource handling failed"
    );
  }

  await handleFollowUpsSafely({
    data,
    logContext,
    survey,
  });
};

const runResponseFinishedSideEffects = async ({
  data,
  displayTimeZone,
  logContext,
  organizationId,
  stripeCustomerId,
  survey,
  outboundAllowed,
  workspaceId,
}: {
  data: TResponsePipelineJobData;
  displayTimeZone: string | null;
  logContext: ReturnType<typeof getPipelineLogContext>;
  organizationId: string;
  stripeCustomerId: string | null | undefined;
  survey: TPipelineSurvey;
  /** ENG-3283: false for a survey that is not workspace-visible — nothing leaves the app for it. */
  outboundAllowed: boolean;
  workspaceId: string;
}) => {
  const [integrations, usersWithNotifications] = await Promise.all([
    outboundAllowed ? loadIntegrationsSafely({ logContext, workspaceId }) : Promise.resolve([]),
    getUsersWithNotifications({
      survey,
      data,
      logContext,
      workspaceId,
    }),
  ]);

  // Neither count is consumed until the notification/auto-complete steps at the end, so start
  // them here to overlap with the integration and follow-up work below. Each is skipped
  // entirely when nothing would consume it, keeping this off the hot path for the common case.
  const autoCompleteThreshold = getAutoCompleteThreshold(survey);

  const responseCountPromise =
    usersWithNotifications.length > 0
      ? loadResponseCountSafely({
          count: () => getResponseCountBySurveyId(data.surveyId),
          failureMessage: "Response pipeline response count lookup failed",
          logContext,
        })
      : Promise.resolve(null);

  const finishedResponseCountPromise =
    autoCompleteThreshold !== null
      ? loadResponseCountSafely({
          count: () => getFinishedResponseCountBySurveyId(survey.id),
          failureMessage:
            "Response pipeline survey auto-complete skipped because the finished response count could not be loaded",
          logContext: { ...logContext, autoCompleteThreshold },
        })
      : Promise.resolve(null);

  await runOutboundResponseFinishedEffects({
    data,
    displayTimeZone,
    integrations,
    logContext,
    outboundAllowed,
    survey,
    workspaceId,
  });

  await sendNotificationEmailsSafely({
    data,
    logContext,
    responseCount: await responseCountPromise,
    survey,
    usersWithNotifications,
    workspaceId,
  });

  await handleSurveyAutoCompleteSafely({
    finishedResponseCount: await finishedResponseCountPromise,
    logContext,
    organizationId,
    survey,
  });

  if (!outboundAllowed) {
    recordSurveyOutboundSkipped("workflow");
    return;
  }

  // Workflow runner (producer): enqueue runs for matching enabled workflows. Isolated so a runner
  // failure never breaks the response pipeline job, its retries, or the other side-effects above.
  try {
    await enqueueResponseCompletedWorkflowRuns({
      response: data.response,
      workspaceId,
      organizationId,
      stripeCustomerId,
      dispatch: dispatchWorkflowRunViaJobs,
      logContext,
    });
  } catch (error) {
    // Transient DB pool exhaustion must propagate so the job retries (same contract as the outer
    // pipeline catch); otherwise a completed-response trigger is silently lost. Only non-retryable
    // failures are swallowed, so they never break the other responseFinished side-effects above.
    if (isDatabasePoolExhaustionError(error)) {
      throw error;
    }
    logger.error({ ...logContext, err: error }, "Response pipeline workflow run enqueue failed");
  }
};

const runResponseCreatedSideEffects = async ({
  data,
  logContext,
  organizationId,
  survey,
  stripeCustomerId,
}: {
  data: TResponsePipelineJobData;
  logContext: ReturnType<typeof getPipelineLogContext>;
  organizationId: string;
  survey: TPipelineSurvey;
  stripeCustomerId: string | null | undefined;
}) => {
  try {
    await recordResponseCreatedMeterEvent({
      stripeCustomerId,
      responseId: data.response.id,
      createdAt: data.response.createdAt,
    });
  } catch (error) {
    logger.error(
      {
        ...logContext,
        err: error,
      },
      "Response pipeline meter event failed"
    );
  }

  if (POSTHOG_KEY) {
    try {
      const responseCount = await getResponseCountBySurveyId(data.surveyId);
      captureSurveyResponsePostHogEvent({
        organizationId,
        surveyId: data.surveyId,
        surveyType: survey.type,
        workspaceId: data.workspaceId,
        responseCount,
      });
    } catch (error) {
      logger.error(
        {
          ...logContext,
          err: error,
        },
        "Response pipeline PostHog capture failed"
      );
    }
  }

  try {
    await sendTelemetryEvents();
  } catch (error) {
    logger.error(
      {
        ...logContext,
        err: error,
      },
      "Response pipeline telemetry dispatch failed"
    );
  }
};

export const processResponsePipelineJob: JobHandler<TResponsePipelineJobData> = async (data, context) => {
  const logContext = getPipelineLogContext(data, context);

  try {
    const [organization, survey, webhooks] = await Promise.all([
      getOrganizationForPipeline(data.workspaceId),
      getSurveyForPipeline(data.surveyId),
      getWebhooksForPipeline(data.workspaceId, data.event, data.surveyId),
    ]);

    if (!survey) {
      throw new UnrecoverableError(`Survey ${data.surveyId} not found`);
    }

    if (!organization) {
      throw new UnrecoverableError(`Organization not found for workspace ${data.workspaceId}`);
    }

    if (survey.workspaceId !== data.workspaceId) {
      throw new UnrecoverableError(
        `Survey ${data.surveyId} does not belong to workspace ${data.workspaceId}`
      );
    }

    // ENG-3283: a restricted survey's responses never leave the app — no webhook, integration, feedback
    // source, follow-up or workflow. Metering, telemetry, auto-complete and the (already filtered)
    // alert emails are internal and still run.
    const outboundAllowed = isSurveyOutboundAllowed(survey);
    if (outboundAllowed) {
      await enqueueWebhookDeliveryJobs({
        data,
        logContext,
        survey,
        webhooks,
      });
    } else {
      logger.info(
        { ...logContext, reason: "survey_not_workspace_visible" },
        "Response pipeline skipped outbound delivery for a survey that is not workspace-visible"
      );
      recordSurveyOutboundSkipped("webhook", webhooks.length);
    }

    if (data.event === "responseFinished") {
      await runResponseFinishedSideEffects({
        data,
        displayTimeZone: organization.displayTimeZone,
        logContext,
        organizationId: organization.id,
        outboundAllowed,
        stripeCustomerId: organization.billing?.stripeCustomerId,
        survey,
        workspaceId: data.workspaceId,
      });
    }

    if (data.event === "responseCreated") {
      await runResponseCreatedSideEffects({
        data,
        logContext,
        organizationId: organization.id,
        survey,
        stripeCustomerId: organization.billing?.stripeCustomerId,
      });
    }
  } catch (error) {
    if (isDatabasePoolExhaustionError(error)) {
      logger.warn(
        {
          ...logContext,
          err: error,
        },
        "Response pipeline job hit database pool exhaustion and will be retried"
      );
      throw error;
    }

    logger.error(
      {
        ...logContext,
        err: error,
      },
      "Response pipeline job failed"
    );
    throw error;
  }
};
