-- Dashboard metrics & content health
--
-- Adds two tables that back the redesigned admin Dashboard:
--   * actuate_api_request_metrics  — per-minute pre-aggregated request
--     counters with a fixed log-scale latency histogram, sized to keep
--     a week of traffic in O(minutes × routes × status-buckets) rows.
--   * actuate_content_issues       — persisted content-quality issues
--     produced by the nightly content-health scanner. Upserted by
--     (documentId, type) so the scanner is idempotent.
--
-- Both tables are safe to add to a populated database: they have no
-- backfill dependency and no constraints that touch existing rows.

-- ---------------------------------------------------------------------------
-- ContentIssueType enum
-- ---------------------------------------------------------------------------
CREATE TYPE "ContentIssueType" AS ENUM (
  'MISSING_META_DESCRIPTION',
  'MISSING_ALT_TEXT',
  'BROKEN_INTERNAL_LINK',
  'OUTDATED_CONTENT'
);

-- ---------------------------------------------------------------------------
-- actuate_api_request_metrics
-- ---------------------------------------------------------------------------
CREATE TABLE "actuate_api_request_metrics" (
  "id"                TEXT      NOT NULL,
  "bucketStart"       TIMESTAMP(3) NOT NULL,
  "route"             TEXT      NOT NULL,
  "statusBucket"      TEXT      NOT NULL,
  "count"             INTEGER   NOT NULL DEFAULT 0,
  "sumLatencyMs"      DOUBLE PRECISION NOT NULL DEFAULT 0,
  "maxLatencyMs"      DOUBLE PRECISION NOT NULL DEFAULT 0,
  "latencyHistogram"  JSONB     NOT NULL,
  "updatedAt"         TIMESTAMP(3) NOT NULL,

  CONSTRAINT "actuate_api_request_metrics_pkey" PRIMARY KEY ("id")
);

CREATE UNIQUE INDEX "actuate_api_request_metrics_bucketStart_route_statusBucket_key"
  ON "actuate_api_request_metrics" ("bucketStart", "route", "statusBucket");

CREATE INDEX "actuate_api_request_metrics_bucketStart_idx"
  ON "actuate_api_request_metrics" ("bucketStart");

-- ---------------------------------------------------------------------------
-- actuate_content_issues
-- ---------------------------------------------------------------------------
CREATE TABLE "actuate_content_issues" (
  "id"          TEXT             NOT NULL,
  "documentId"  TEXT             NOT NULL,
  "type"        "ContentIssueType" NOT NULL,
  "count"       INTEGER          NOT NULL DEFAULT 1,
  "details"     JSONB,
  "firstSeenAt" TIMESTAMP(3)     NOT NULL DEFAULT CURRENT_TIMESTAMP,
  "lastSeenAt"  TIMESTAMP(3)     NOT NULL DEFAULT CURRENT_TIMESTAMP,

  CONSTRAINT "actuate_content_issues_pkey" PRIMARY KEY ("id"),
  CONSTRAINT "actuate_content_issues_documentId_fkey"
    FOREIGN KEY ("documentId")
    REFERENCES "actuate_documents"("id")
    ON DELETE CASCADE
    ON UPDATE CASCADE
);

CREATE UNIQUE INDEX "actuate_content_issues_documentId_type_key"
  ON "actuate_content_issues" ("documentId", "type");

CREATE INDEX "actuate_content_issues_type_idx"
  ON "actuate_content_issues" ("type");

CREATE INDEX "actuate_content_issues_documentId_idx"
  ON "actuate_content_issues" ("documentId");
