// slack-term relay — a Cloudflare Worker that turns Slack's Events API into a
// resumable doorbell stream for `slack stream`.
//
//   Slack ──POST /slack/events──▶ Worker ──▶ Relay (Durable Object, SQLite)
//                                                 │  {seq, channel, ts, thread_ts}
//   slack stream ◀──GET /ws (WebSocket, hibernating)┤
//   older clients ◀──GET /stream (SSE, bearer)──────┘
//
// Clients connect over a hibernatable WebSocket (hub.ts): between bells the
// object is evicted from memory and not billed, and keepalive pings are
// answered by the runtime without waking it. /stream (SSE) stays for clients
// from before the switch — every connected SSE client keeps the object awake.
//
// Privacy: the relay stores and serves ids and timestamps only — never message
// text, never sender ids (beyond marking the app's own bot posts, `self`). A client that hears a bell reads the message itself
// through the Web API with its own token, so a forged or replayed bell costs at
// most one extra scan. Bells are deleted RETENTION_SEC after they arrive; that
// window only exists so a reconnecting client can resume from its last seq.
// Nothing here logs a request body.

import { DurableObject } from "cloudflare:workers";
import { type Doorbell, doorbellOf, MAX_BODY_BYTES, plausiblySigned, readLimited, safeEqual, verifySlack } from "./slack.ts";
import * as hub from "./hub.ts";

export interface Env {
  RELAY: DurableObjectNamespace<Relay>;
  /** Slack app → Basic Information → Signing Secret. */
  SLACK_SIGNING_SECRET: string;
  /** Bearer token every stream consumer presents. */
  RELAY_TOKEN: string;
  /** Optional: drop events from any other workspace. */
  SLACK_TEAM_ID?: string;
}

/** How long a bell is kept for resume. Longer gaps are covered by the client's
 *  catch-up poll, which runs on every (re)connect anyway. */
const RETENTION_SEC = 3600;
const KEEPALIVE_MS = 25_000;
const MAX_CLIENTS = 16;

export default {
  async fetch(req: Request, env: Env): Promise<Response> {
    const url = new URL(req.url);
    const relay = env.RELAY.get(env.RELAY.idFromName("relay"));

    if (url.pathname === "/slack/events" && req.method === "POST") {
      const timestamp = req.headers.get("x-slack-request-timestamp");
      const signature = req.headers.get("x-slack-signature");
      // Reject before reading anything we would have to buffer.
      if (!plausiblySigned(timestamp, signature, Date.now() / 1000)) return new Response("bad signature", { status: 401 });
      if (Number(req.headers.get("content-length") ?? 0) > MAX_BODY_BYTES) return new Response("too large", { status: 413 });
      const body = await readLimited(req.body);
      if (body === null) return new Response("too large", { status: 413 });
      if (!await verifySlack(env.SLACK_SIGNING_SECRET, timestamp, signature, body, Date.now() / 1000)) {
        return new Response("bad signature", { status: 401 });
      }
      let payload: { type?: unknown; challenge?: unknown };
      try {
        payload = JSON.parse(body) as typeof payload;
      } catch {
        return new Response("bad json", { status: 400 });
      }
      if (payload.type === "url_verification" && typeof payload.challenge === "string") {
        return new Response(payload.challenge, { headers: { "content-type": "text/plain" } });
      }
      const bell = doorbellOf(payload, env.SLACK_TEAM_ID || undefined);
      // A single SQLite insert: well inside Slack's 3 s ack budget. Retries of
      // the same event (x-slack-retry-num) collapse on (channel, ts).
      if (bell) await relay.ring(bell);
      return new Response(null, { status: 200 });
    }

    if (url.pathname === "/ws") {
      // Checked here, before the object is touched or the socket accepted.
      // The bearer rides in a subprotocol (Node's WebSocket cannot set
      // headers); an Authorization header works too.
      const bearer = !!env.RELAY_TOKEN && safeEqual(req.headers.get("authorization") ?? "", `Bearer ${env.RELAY_TOKEN}`);
      if (!bearer && !hub.protocolAuthorized(req.headers.get("sec-websocket-protocol"), env.RELAY_TOKEN)) {
        return new Response("unauthorized", { status: 401 });
      }
      if (req.method !== "GET") return new Response("method not allowed", { status: 405 });
      if (req.headers.get("upgrade")?.toLowerCase() !== "websocket") return new Response("upgrade required", { status: 426 });
      return relay.fetch(req);
    }

    if (url.pathname === "/stream" || url.pathname === "/health") {
      if (!env.RELAY_TOKEN || !safeEqual(req.headers.get("authorization") ?? "", `Bearer ${env.RELAY_TOKEN}`)) {
        return new Response("unauthorized", { status: 401 });
      }
      if (req.method !== "GET") return new Response("method not allowed", { status: 405 });
      return relay.fetch(req);
    }

    return new Response("not found", { status: 404 });
  },
} satisfies ExportedHandler<Env>;

/** An SSE client (GET /stream, kept for clients from before /ws).
 *  `pending`: writes not yet taken by the consumer. A client that stops
 *  reading is cut off past MAX_PENDING instead of buffering without bound;
 *  it reconnects with ?after and gets the rest from storage. */
type Client = {
  writer: WritableStreamDefaultWriter<Uint8Array>;
  pending: number;
  /** Still being fed its backlog from storage; live bells reach it that way. */
  replaying: boolean;
};
const MAX_PENDING = 64;
/** Backlog rows read and written per step of a replay. */
const REPLAY_BATCH = hub.REPLAY_BATCH;
/** A replay write the reader has not taken by then frees its slot. */
const REPLAY_WRITE_MS = 30_000;
const enc = new TextEncoder();

export class Relay extends DurableObject<Env> {
  private clients = new Set<Client>();
  /** Names this storage. A client that saved a seq under another epoch is
   *  reading a reset relay, whose numbers mean something else now. */
  private readonly epoch: string;
  private keepalive: ReturnType<typeof setInterval> | undefined;
  /** An alarm is known to be set: ring() skips the getAlarm() storage read.
   *  Starts false on every (re)start of the object, so it re-checks once. */
  private alarmArmed = false;

  constructor(ctx: DurableObjectState, env: Env) {
    super(ctx, env);
    // Answered by the runtime, without waking a hibernating object.
    ctx.setWebSocketAutoResponse(new WebSocketRequestResponsePair("ping", "pong"));
    ctx.storage.sql.exec(`CREATE TABLE IF NOT EXISTS bells (
      seq INTEGER PRIMARY KEY AUTOINCREMENT,
      channel TEXT NOT NULL, ts TEXT NOT NULL, thread_ts TEXT,
      at INTEGER NOT NULL, self TEXT, kind TEXT, user TEXT, rid TEXT NOT NULL DEFAULT '',
      UNIQUE (channel, ts, rid)
    )`);
    // Added later: a relay deployed before it gets the column on its next start.
    let cols = ctx.storage.sql.exec<{ name: string }>("PRAGMA table_info(bells)").toArray().map((c) => c.name);
    if (!cols.includes("self")) ctx.storage.sql.exec("ALTER TABLE bells ADD COLUMN self TEXT");
    // Reaction bells (11-05): UNIQUE (channel, ts) swallowed every reaction on a message that had already rung (an
    // ask's pills), and kind/user were not stored. SQLite cannot change a UNIQUE constraint: rebuild, keeping seq.
    cols = ctx.storage.sql.exec<{ name: string }>("PRAGMA table_info(bells)").toArray().map((c) => c.name);
    if (!cols.includes("rid")) {
      ctx.storage.sql.exec(`CREATE TABLE bells_v2 (
        seq INTEGER PRIMARY KEY AUTOINCREMENT,
        channel TEXT NOT NULL, ts TEXT NOT NULL, thread_ts TEXT,
        at INTEGER NOT NULL, self TEXT, kind TEXT, user TEXT, rid TEXT NOT NULL DEFAULT '',
        UNIQUE (channel, ts, rid)
      )`);
      ctx.storage.sql.exec("INSERT INTO bells_v2 (seq, channel, ts, thread_ts, at, self) SELECT seq, channel, ts, thread_ts, at, self FROM bells");
      ctx.storage.sql.exec("DROP TABLE bells");
      ctx.storage.sql.exec("ALTER TABLE bells_v2 RENAME TO bells");
    }
    ctx.storage.sql.exec("CREATE TABLE IF NOT EXISTS meta (k TEXT PRIMARY KEY, v TEXT NOT NULL)");
    ctx.storage.sql.exec("INSERT OR IGNORE INTO meta (k, v) VALUES ('epoch', ?)", crypto.randomUUID());
    this.epoch = ctx.storage.sql.exec<{ v: string }>("SELECT v FROM meta WHERE k = 'epoch'").one().v;
  }

  /** Store a bell (once per channel+ts) and push it to every live client. */
  async ring(bell: Doorbell): Promise<void> {
    const now = Date.now();
    const rows = this.ctx.storage.sql.exec<{ seq: number }>(
      "INSERT OR IGNORE INTO bells (channel, ts, thread_ts, self, kind, user, rid, at) VALUES (?, ?, ?, ?, ?, ?, ?, ?) RETURNING seq",
      bell.channel, bell.ts, bell.thread_ts ?? null, bell.self ?? null, bell.kind ?? null, bell.user ?? null,
      bell.kind === "reaction" ? `r:${bell.rid ?? now}` : "", now,
    ).toArray();
    const seq = rows[0]?.seq;
    if (seq === undefined) return; // a retry of a bell we already have
    if (!this.alarmArmed) {
      if ((await this.ctx.storage.getAlarm()) === null) await this.ctx.storage.setAlarm(now + RETENTION_SEC * 1000);
      this.alarmArmed = true;
    }
    this.broadcast(frame(seq, bell));
    hub.deliver(this.sockets(), seq, bell, now);
  }

  /** TTL sweep: drop expired bells, re-arm while any remain. */
  override async alarm(): Promise<void> {
    const cutoff = Date.now() - RETENTION_SEC * 1000;
    this.ctx.storage.sql.exec("DELETE FROM bells WHERE at <= ?", cutoff);
    const next = this.ctx.storage.sql.exec<{ at: number | null }>("SELECT MIN(at) AS at FROM bells").one().at;
    if (next !== null) await this.ctx.storage.setAlarm(next + RETENTION_SEC * 1000);
    this.alarmArmed = next !== null;
    hub.sweep(this.sockets(), Date.now());
  }

  override webSocketMessage(ws: WebSocket, data: string | ArrayBuffer): void {
    hub.message(this.sock(ws), data, this.store, Date.now());
  }

  // Socket state lives on the socket: nothing to clean up but the socket.
  override webSocketClose(ws: WebSocket, code: number): void {
    try { ws.close(code === 1005 || code === 1006 ? 1000 : code, "bye"); } catch { /* already closed */ }
  }

  override webSocketError(ws: WebSocket): void {
    try { ws.close(1011, "error"); } catch { /* already closed */ }
  }

  private readonly store: hub.Store = {
    rowsAfter: (seq, limit) => this.ctx.storage.sql.exec<hub.Row>(
      "SELECT seq, channel, ts, thread_ts, self, kind, user, rid FROM bells WHERE seq > ? ORDER BY seq LIMIT ?", seq, limit,
    ).toArray(),
  };

  private sock(ws: WebSocket): hub.Sock {
    return {
      send: (d) => ws.send(d),
      close: (code, reason) => { try { ws.close(code, reason); } catch { /* already closed */ } },
      attachment: () => ws.deserializeAttachment() as hub.Attachment | null,
      setAttachment: (a) => ws.serializeAttachment(a),
      lastPing: () => this.ctx.getWebSocketAutoResponseTimestamp(ws)?.getTime() ?? null,
    };
  }

  private sockets(): hub.Sock[] {
    return this.ctx.getWebSockets().filter((ws) => ws.readyState === WebSocket.OPEN).map((ws) => this.sock(ws));
  }

  override async fetch(req: Request): Promise<Response> {
    const url = new URL(req.url);
    const { oldest, latest } = this.bounds();
    const now = Date.now();
    const sockets = hub.sweep(this.sockets(), now);
    if (url.pathname === "/health") {
      return Response.json({
        ok: true, epoch: this.epoch, latest, oldest, clients: this.clients.size + sockets.length,
        websocket_clients: sockets.length, sse_clients: this.clients.size, retention_sec: RETENTION_SEC,
      });
    }

    if (this.clients.size + sockets.length >= MAX_CLIENTS) return new Response("too many clients", { status: 503 });
    // Resume point: ?after=<seq> or the SSE Last-Event-ID header (hub.resumePoint).
    const { from, gap } = hub.resumePoint(
      url.searchParams.get("after") ?? req.headers.get("last-event-id"), url.searchParams.get("epoch"), this.epoch, oldest, latest,
    );
    const hello = { seq: from, gap, retention_sec: RETENTION_SEC, epoch: this.epoch };

    if (url.pathname === "/ws") {
      const [client, server] = Object.values(new WebSocketPair()) as [WebSocket, WebSocket];
      this.ctx.acceptWebSocket(server);
      hub.open(this.sock(server), from, hello, this.store, now);
      const offered = (req.headers.get("sec-websocket-protocol") ?? "").split(",").some((p) => p.trim() === hub.PROTOCOL);
      return new Response(null, { status: 101, webSocket: client, headers: offered ? { "sec-websocket-protocol": hub.PROTOCOL } : {} });
    }

    const { readable, writable } = new TransformStream<Uint8Array, Uint8Array>();
    const client: Client = { writer: writable.getWriter(), pending: 0, replaying: true };
    this.clients.add(client);
    this.keepalive ??= setInterval(() => this.broadcast(": ka\n\n"), KEEPALIVE_MS);
    void this.replay(client, from, `event: hello\ndata: ${JSON.stringify(hello)}\n\n`);

    return new Response(readable, {
      headers: { "content-type": "text/event-stream", "cache-control": "no-store", "x-accel-buffering": "no" },
    });
  }

  private bounds(): { oldest: number | null; latest: number } {
    const b = this.ctx.storage.sql.exec<{ oldest: number | null }>("SELECT MIN(seq) AS oldest FROM bells").one();
    // sqlite_sequence remembers the high-water mark even after every row expired.
    const hw = this.ctx.storage.sql.exec<{ seq: number }>("SELECT seq FROM sqlite_sequence WHERE name = 'bells'").toArray();
    return { oldest: b.oldest, latest: hw[0]?.seq ?? 0 };
  }

  /** Feed a client its backlog in batches, each write awaited — a reader
   *  that stalls stalls only its own replay, holding one batch at most. A
   *  bell rung meanwhile is already stored, so the next batch picks it up;
   *  the switch to live happens right after a read that came back empty,
   *  with no await in between, so nothing falls between the two. */
  private async replay(c: Client, from: number, hello: string): Promise<void> {
    let chunk = hello;
    let cursor = from;
    for (;;) {
      const rows = this.store.rowsAfter(cursor, REPLAY_BATCH);
      for (const r of rows) {
        chunk += frame(r.seq, hub.bellOf(r));
        cursor = r.seq;
      }
      if (!rows.length) c.replaying = false;
      if (chunk) {
        let timer: ReturnType<typeof setTimeout> | undefined;
        try {
          await Promise.race([
            c.writer.write(enc.encode(chunk)),
            new Promise((_, reject) => { timer = setTimeout(() => reject(new Error("stalled")), REPLAY_WRITE_MS); }),
          ]);
        } catch {
          return this.drop(c);
        } finally {
          if (timer !== undefined) clearTimeout(timer);
        }
      }
      if (!c.replaying || !this.clients.has(c)) return;
      chunk = "";
    }
  }

  private broadcast(chunk: string): void {
    for (const c of this.clients) if (!c.replaying) void this.send(c, chunk);
  }

  private async send(c: Client, chunk: string): Promise<void> {
    if (c.pending >= MAX_PENDING) return this.drop(c);
    c.pending++;
    try {
      await c.writer.write(enc.encode(chunk));
      c.pending--;
    } catch {
      this.drop(c);
    }
  }

  private drop(c: Client): void {
    if (!this.clients.delete(c)) return;
    c.writer.abort().catch(() => {});
    if (this.clients.size === 0 && this.keepalive !== undefined) {
      clearInterval(this.keepalive);
      this.keepalive = undefined;
    }
  }
}

function frame(seq: number, bell: Doorbell): string {
  return `id: ${seq}\nevent: bell\ndata: ${JSON.stringify(bell)}\n\n`;
}
