import { env } from "cloudflare:workers";
import { all, first, run } from "./db";
import { captureServerEvent } from "./analytics";
import { hmacSha256Hex, randomId } from "./crypto";
import { badRequest, notFoundError } from "./errors";
import { cookieSecret, publishMeta, SCRATCH_LIMITS } from "./sites/config";
import { loadSiteById } from "./sites/deploy";
import { resolveAbuseTarget } from "./abuse-targets";
import type { AbuseTarget } from "./abuse-targets";
import { recordAdminAction } from "./admin-audit";
import { isHideable, setModerationHidden } from "./moderation";
import { notifyAdmins } from "./account-disable-actions";
import { ABUSE_TARGET_LABELS } from "~/lib/abuse";
import type { AbuseAction, AbuseCategory, AbuseStatus, AbuseTargetKind } from "~/lib/abuse";
export { siteIdForUrl } from "./abuse-targets";
export type AbuseSource = "web" | "email" | "site";
/**
* How much one reporter counts toward auto-pause. Signed-in users and mail whose sender domain passed DMARC are
* accountable; an IP is cheap to rotate; unauthenticated mail can forge any sender, so it never pauses anything.
*/
export type ReporterKind = "user" | "ip" | "email-verified" | "email-unverified";
const REPORTER_WEIGHT: Record<ReporterKind, number> = { user: 2, "email-verified": 2, ip: 1, "email-unverified": 0 };
/** Phishing and malware hurt visitors immediately, so fewer distinct reporters are enough to hold the site. */
const SEVERE: ReadonlySet<AbuseCategory> = new Set(["phishing", "malware"]);
/**
* Weighted reporter totals that pause a site (an IP weighs 1, an accountable reporter 2). Scratch sites are
* anonymous and short-lived, so a few reports hold them. Owned sites count accountable reporters only and need
* many of them; below that an admin decides.
*/
const PAUSE_THRESHOLDS = {
scratch: { severe: 2, normal: SCRATCH_LIMITS.reportsToDisable, minWeight: 1 },
owned: { severe: 8, normal: 20, minWeight: 2 },
} as const;
const PAUSE_WINDOW_MS = 7 * 24 * 60 * 60 * 1000;
export const PAUSE_REASON = "Paused pending abuse review";
const MAX_DETAILS = 4000;