-- ADR-0027: org-scoped capability connections for external document/media
-- transforms (PDF→MD, audio/video→text, …). Mirrors the Feishu Application
-- Connection shape: reuses the ADR-0024 envelope machinery with its own
-- payload schema and readiness probe, distinct from the model-provider
-- connection.

CREATE TABLE "OrganizationCapabilityConnection" (
    "id"                    TEXT                   NOT NULL,
    "organizationId"        TEXT                   NOT NULL,
    "capabilityId"          TEXT                   NOT NULL,
    "status"                "OrganizationConnectionStatus" NOT NULL DEFAULT 'DRAFT',
    "activeSecretVersionId" TEXT,
    "activatedAt"           TIMESTAMP(3),
    "disabledAt"            TIMESTAMP(3),
    "createdAt"             TIMESTAMP(3)           NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt"             TIMESTAMP(3)           NOT NULL,
    CONSTRAINT "OrganizationCapabilityConnection_pkey" PRIMARY KEY ("id")
);

CREATE UNIQUE INDEX "OrganizationCapabilityConnection_organizationId_capabilityId_key"
  ON "OrganizationCapabilityConnection"("organizationId", "capabilityId");
CREATE UNIQUE INDEX "OrganizationCapabilityConnection_activeSecretVersionId_key"
  ON "OrganizationCapabilityConnection"("activeSecretVersionId");
CREATE INDEX "OrganizationCapabilityConnection_organizationId_status_idx"
  ON "OrganizationCapabilityConnection"("organizationId", "status");
CREATE INDEX "OrganizationCapabilityConnection_capabilityId_status_idx"
  ON "OrganizationCapabilityConnection"("capabilityId", "status");

CREATE TABLE "CapabilityCredentialVersion" (
    "id"              TEXT                   NOT NULL,
    "connectionId"    TEXT                   NOT NULL,
    "version"         INTEGER                NOT NULL,
    "envelopeVersion" INTEGER                NOT NULL DEFAULT 1,
    "keyId"           TEXT                   NOT NULL,
    "envelope"        JSONB                  NOT NULL,
    "createdByUserId" TEXT,
    "createdAt"       TIMESTAMP(3)           NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "retiredAt"       TIMESTAMP(3),
    CONSTRAINT "CapabilityCredentialVersion_pkey" PRIMARY KEY ("id")
);

CREATE UNIQUE INDEX "CapabilityCredentialVersion_connectionId_version_key"
  ON "CapabilityCredentialVersion"("connectionId", "version");
CREATE INDEX "CapabilityCredentialVersion_connectionId_retiredAt_idx"
  ON "CapabilityCredentialVersion"("connectionId", "retiredAt");
CREATE INDEX "CapabilityCredentialVersion_keyId_idx"
  ON "CapabilityCredentialVersion"("keyId");
CREATE INDEX "CapabilityCredentialVersion_createdByUserId_idx"
  ON "CapabilityCredentialVersion"("createdByUserId");

ALTER TABLE "OrganizationCapabilityConnection"
  ADD CONSTRAINT "OrganizationCapabilityConnection_organizationId_fkey"
  FOREIGN KEY ("organizationId") REFERENCES "Organization"("id")
  ON DELETE CASCADE ON UPDATE CASCADE;

ALTER TABLE "OrganizationCapabilityConnection"
  ADD CONSTRAINT "OrganizationCapabilityConnection_activeSecretVersionId_fkey"
  FOREIGN KEY ("activeSecretVersionId") REFERENCES "CapabilityCredentialVersion"("id")
  ON DELETE RESTRICT ON UPDATE CASCADE;

ALTER TABLE "CapabilityCredentialVersion"
  ADD CONSTRAINT "CapabilityCredentialVersion_connectionId_fkey"
  FOREIGN KEY ("connectionId") REFERENCES "OrganizationCapabilityConnection"("id")
  ON DELETE CASCADE ON UPDATE CASCADE;

ALTER TABLE "CapabilityCredentialVersion"
  ADD CONSTRAINT "CapabilityCredentialVersion_createdByUserId_fkey"
  FOREIGN KEY ("createdByUserId") REFERENCES "User"("id")
  ON DELETE SET NULL ON UPDATE CASCADE;
