"use strict";
// ─── Application discovery ──────────────────────────────────────────────────
Object.defineProperty(exports, "__esModule", { value: true });
exports.VOLUME_BY_STREAM = exports.COUNT_PROCESSED_SINCE = exports.COUNT_PENDING = exports.LIST_APPS = void 0;
/**
 * List all HotMesh applications from the global registry.
 * Active apps only.
 */
exports.LIST_APPS = `
  SELECT app_id, version FROM hmsh_applications
  WHERE active = TRUE
  ORDER BY app_id`;
// ─── Stream statistics ──────────────────────────────────────────────────────
//
// HotMesh v0.11.0 split the single `streams` table into `engine_streams`
// (internal engine orchestration) and `worker_streams` (worker task messages).
// All queries now UNION both tables for aggregate statistics.
/**
 * Count pending (unprocessed) stream messages across both tables.
 * expired_at IS NULL = message has not been consumed yet.
 * $1 = stream_name filter (NULL = all streams).
 */
const COUNT_PENDING = (schema) => `
  SELECT COUNT(*)::int AS count FROM (
    SELECT stream_name FROM "${schema}".engine_streams
    WHERE expired_at IS NULL
      AND ($1::text IS NULL OR stream_name = $1)
    UNION ALL
    SELECT stream_name FROM "${schema}".worker_streams
    WHERE expired_at IS NULL
      AND ($1::text IS NULL OR stream_name = $1)
  ) combined`;
exports.COUNT_PENDING = COUNT_PENDING;
/**
 * Count processed stream messages within a time interval.
 * $1 = interval, $2 = stream_name filter (NULL = all streams).
 */
const COUNT_PROCESSED_SINCE = (schema) => `
  SELECT COUNT(*)::int AS count FROM (
    SELECT stream_name FROM "${schema}".engine_streams
    WHERE expired_at IS NOT NULL
      AND expired_at > NOW() - $1::interval
      AND ($2::text IS NULL OR stream_name = $2)
    UNION ALL
    SELECT stream_name FROM "${schema}".worker_streams
    WHERE expired_at IS NOT NULL
      AND expired_at > NOW() - $1::interval
      AND ($2::text IS NULL OR stream_name = $2)
  ) combined`;
exports.COUNT_PROCESSED_SINCE = COUNT_PROCESSED_SINCE;
/**
 * Volume breakdown by stream_name within a time interval.
 * $1 = interval, $2 = stream_name filter (NULL = all streams).
 */
const VOLUME_BY_STREAM = (schema) => `
  SELECT stream_type, stream_name, COUNT(*)::int AS count FROM (
    SELECT 'engine' AS stream_type, stream_name FROM "${schema}".engine_streams
    WHERE expired_at IS NOT NULL
      AND expired_at > NOW() - $1::interval
      AND ($2::text IS NULL OR stream_name = $2)
    UNION ALL
    SELECT 'worker' AS stream_type, stream_name FROM "${schema}".worker_streams
    WHERE expired_at IS NOT NULL
      AND expired_at > NOW() - $1::interval
      AND ($2::text IS NULL OR stream_name = $2)
  ) combined
  GROUP BY stream_type, stream_name
  ORDER BY stream_type, count DESC`;
exports.VOLUME_BY_STREAM = VOLUME_BY_STREAM;
