import { pruneIntegrationLeftovers } from "./retention";
import { env } from "cloudflare:workers";
import { all, chunks, first, placeholders, run } from "../db";
import { repoDb } from "../scoped-db";
import { sha256Hex } from "../crypto";
import { loadRepoById } from "../repos";
import { badRequest, notFoundError } from "../errors";
import { publicUrl } from "../events/emit";
import { deliverToIntegration } from "./deliver";
import type { DeliverInput } from "./deliver";
import { isPermanentFailure, isSuccess } from "./outbound";
import { integrationOwner, pruneUsageKeys, recordOutboundDelivery } from "./metering";
import { failImportJob, runImportPage } from "./github-import";
import type { ImportJobMessage } from "./github-import";
import { integrationsForRepo, loadIntegration, parseConfig, parseEvents, recordDelivery } from "./store";
import type { IntegrationRow } from "./store";
import { passesFilters, summarizeEvent } from "./summarize";
import type { EventSummary, RepoContext } from "./summarize";
import { notifyIntegrationDead } from "../notify/integrations";
import type { RepoRow } from "~/lib/types";
export const INTEGRATIONS_QUEUE = "gitfor-integrations";
export const INTEGRATIONS_DLQ = "gitfor-integrations-dlq";
const BASE_RETRY_SECONDS = 15;
const MAX_RETRY_SECONDS = 3600;
const MAX_QUEUED_PAYLOAD = 96 * 1024;
const MAX_PAYLOAD_COMMITS = 20;
const DIGEST_BATCH = 25;
const DIGEST_ITEMS = 80;
const MAX_DIGEST_BACKLOG = 500;
const MIRROR_BATCH = 10;
const ID_LENGTH = 20;
const DELIVERY_LEASE_MS = 2 * 60_000;
const MS_PER_SECOND = 1000;
/** wrangler.jsonc `max_retries: 6` for gitfor-integrations, i.e. 7 attempts. */
const MAX_ATTEMPTS = 7;
const MIRROR_LEASE_MS = 20 * 60_000;
const MAX_BATCH_BYTES = 200 * 1024;
const MAX_BATCH_MESSAGES = 50;