-- CreateEnum
CREATE TYPE "IiosActorKind" AS ENUM ('HUMAN', 'AI', 'SERVICE', 'BOT', 'UNKNOWN');

-- CreateEnum
CREATE TYPE "IiosHandleKind" AS ENUM ('PORTAL_USER', 'EMAIL', 'PHONE', 'WHATSAPP', 'SLACK', 'MATTERMOST', 'TELEGRAM', 'EXTERNAL_VISITOR');

-- CreateEnum
CREATE TYPE "IiosInteractionKind" AS ENUM ('MESSAGE', 'EMAIL', 'SYSTEM_NOTICE', 'INBOX_WORK', 'SUPPORT_CASE', 'MEETING_REQUEST', 'DIGEST', 'SUMMARY', 'NOTIFICATION');

-- CreateEnum
CREATE TYPE "IiosMessagePartKind" AS ENUM ('TEXT', 'HTML', 'MARKDOWN', 'MEDIA_REF', 'FILE_REF', 'VOICE_REF', 'LOCATION', 'STRUCTURED_JSON');

-- CreateEnum
CREATE TYPE "IiosDeliveryState" AS ENUM ('RECEIVED', 'NORMALIZED', 'POLICY_HELD', 'STORED', 'DELIVERED', 'READ', 'FAILED', 'DELETED_LOCAL', 'REDACTED');

-- CreateTable
CREATE TABLE "IiosScope" (
    "id" TEXT NOT NULL,
    "orgId" TEXT NOT NULL,
    "appId" TEXT NOT NULL,
    "buId" TEXT,
    "tenantId" TEXT,
    "workspaceId" TEXT,
    "projectId" TEXT,
    "userScopeId" TEXT,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "IiosSourceHandle" (
    "id" TEXT NOT NULL,
    "scopeId" TEXT NOT NULL,
    "kind" "IiosHandleKind" NOT NULL,
    "externalId" TEXT NOT NULL,
    "displayName" TEXT,
    "canonicalEntityId" TEXT,
    "confidence" DOUBLE PRECISION NOT NULL DEFAULT 0,
    "verificationState" TEXT NOT NULL DEFAULT 'UNVERIFIED',
    "firstSeenAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "lastSeenAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "metadata" JSONB,

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

-- CreateTable
CREATE TABLE "IiosActorRef" (
    "id" TEXT NOT NULL,
    "kind" "IiosActorKind" NOT NULL,
    "sourceHandleId" TEXT,
    "platformPrincipalRef" TEXT,
    "canonicalEntityId" TEXT,
    "displayName" TEXT,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "IiosChannel" (
    "id" TEXT NOT NULL,
    "scopeId" TEXT NOT NULL,
    "channelType" TEXT NOT NULL,
    "channelName" TEXT NOT NULL,
    "direction" TEXT NOT NULL DEFAULT 'BOTH',
    "capabilityContract" JSONB,
    "externalRef" TEXT,
    "enabled" BOOLEAN NOT NULL DEFAULT true,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "IiosThread" (
    "id" TEXT NOT NULL,
    "scopeId" TEXT NOT NULL,
    "channelId" TEXT,
    "threadKind" TEXT NOT NULL DEFAULT 'CONVERSATION',
    "subject" TEXT,
    "externalThreadRef" TEXT,
    "status" TEXT NOT NULL DEFAULT 'OPEN',
    "visibilityClass" TEXT NOT NULL DEFAULT 'PRIVATE',
    "createdByActorId" TEXT,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,
    "metadata" JSONB,

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

-- CreateTable
CREATE TABLE "IiosThreadParticipant" (
    "threadId" TEXT NOT NULL,
    "actorId" TEXT NOT NULL,
    "participantRole" TEXT NOT NULL DEFAULT 'MEMBER',
    "joinedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "leftAt" TIMESTAMP(3),

    CONSTRAINT "IiosThreadParticipant_pkey" PRIMARY KEY ("threadId","actorId")
);

-- CreateTable
CREATE TABLE "IiosInteraction" (
    "id" TEXT NOT NULL,
    "scopeId" TEXT NOT NULL,
    "kind" "IiosInteractionKind" NOT NULL,
    "threadId" TEXT,
    "channelId" TEXT,
    "actorId" TEXT,
    "parentInteractionId" TEXT,
    "externalId" TEXT,
    "idempotencyKey" TEXT NOT NULL,
    "occurredAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "receivedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "status" "IiosDeliveryState" NOT NULL DEFAULT 'RECEIVED',
    "policyDecisionRef" TEXT,
    "consentReceiptRef" TEXT,
    "crreBundleRef" TEXT,
    "traceId" TEXT,
    "metadata" JSONB,

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

-- CreateTable
CREATE TABLE "IiosMessagePart" (
    "id" TEXT NOT NULL,
    "interactionId" TEXT NOT NULL,
    "partIndex" INTEGER NOT NULL,
    "kind" "IiosMessagePartKind" NOT NULL,
    "bodyText" TEXT,
    "contentRef" TEXT,
    "mimeType" TEXT,
    "sizeBytes" BIGINT,
    "checksumSha256" TEXT,
    "metadata" JSONB,

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

-- CreateTable
CREATE TABLE "IiosOutboxEvent" (
    "eventId" TEXT NOT NULL,
    "aggregateType" TEXT NOT NULL,
    "aggregateId" TEXT NOT NULL,
    "eventType" TEXT NOT NULL,
    "cloudEvent" JSONB NOT NULL,
    "partitionKey" TEXT NOT NULL,
    "status" TEXT NOT NULL DEFAULT 'PENDING',
    "attempts" INTEGER NOT NULL DEFAULT 0,
    "nextAttemptAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "publishedAt" TIMESTAMP(3),

    CONSTRAINT "IiosOutboxEvent_pkey" PRIMARY KEY ("eventId")
);

-- CreateTable
CREATE TABLE "IiosProcessedEvent" (
    "consumerName" TEXT NOT NULL,
    "eventId" TEXT NOT NULL,
    "processedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "resultHash" TEXT,

    CONSTRAINT "IiosProcessedEvent_pkey" PRIMARY KEY ("consumerName","eventId")
);

-- CreateIndex
CREATE INDEX "IiosScope_orgId_appId_tenantId_idx" ON "IiosScope"("orgId", "appId", "tenantId");

-- CreateIndex
CREATE INDEX "IiosSourceHandle_canonicalEntityId_idx" ON "IiosSourceHandle"("canonicalEntityId");

-- CreateIndex
CREATE UNIQUE INDEX "IiosSourceHandle_scopeId_kind_externalId_key" ON "IiosSourceHandle"("scopeId", "kind", "externalId");

-- CreateIndex
CREATE INDEX "IiosChannel_scopeId_channelType_idx" ON "IiosChannel"("scopeId", "channelType");

-- CreateIndex
CREATE INDEX "IiosThread_scopeId_status_idx" ON "IiosThread"("scopeId", "status");

-- CreateIndex
CREATE INDEX "IiosInteraction_threadId_occurredAt_idx" ON "IiosInteraction"("threadId", "occurredAt");

-- CreateIndex
CREATE UNIQUE INDEX "IiosInteraction_scopeId_idempotencyKey_key" ON "IiosInteraction"("scopeId", "idempotencyKey");

-- CreateIndex
CREATE UNIQUE INDEX "IiosMessagePart_interactionId_partIndex_key" ON "IiosMessagePart"("interactionId", "partIndex");

-- CreateIndex
CREATE INDEX "IiosOutboxEvent_status_nextAttemptAt_createdAt_idx" ON "IiosOutboxEvent"("status", "nextAttemptAt", "createdAt");

-- AddForeignKey
ALTER TABLE "IiosSourceHandle" ADD CONSTRAINT "IiosSourceHandle_scopeId_fkey" FOREIGN KEY ("scopeId") REFERENCES "IiosScope"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "IiosActorRef" ADD CONSTRAINT "IiosActorRef_sourceHandleId_fkey" FOREIGN KEY ("sourceHandleId") REFERENCES "IiosSourceHandle"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "IiosChannel" ADD CONSTRAINT "IiosChannel_scopeId_fkey" FOREIGN KEY ("scopeId") REFERENCES "IiosScope"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "IiosThread" ADD CONSTRAINT "IiosThread_scopeId_fkey" FOREIGN KEY ("scopeId") REFERENCES "IiosScope"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "IiosThread" ADD CONSTRAINT "IiosThread_channelId_fkey" FOREIGN KEY ("channelId") REFERENCES "IiosChannel"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "IiosThread" ADD CONSTRAINT "IiosThread_createdByActorId_fkey" FOREIGN KEY ("createdByActorId") REFERENCES "IiosActorRef"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "IiosThreadParticipant" ADD CONSTRAINT "IiosThreadParticipant_threadId_fkey" FOREIGN KEY ("threadId") REFERENCES "IiosThread"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "IiosThreadParticipant" ADD CONSTRAINT "IiosThreadParticipant_actorId_fkey" FOREIGN KEY ("actorId") REFERENCES "IiosActorRef"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "IiosInteraction" ADD CONSTRAINT "IiosInteraction_scopeId_fkey" FOREIGN KEY ("scopeId") REFERENCES "IiosScope"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "IiosInteraction" ADD CONSTRAINT "IiosInteraction_threadId_fkey" FOREIGN KEY ("threadId") REFERENCES "IiosThread"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "IiosInteraction" ADD CONSTRAINT "IiosInteraction_channelId_fkey" FOREIGN KEY ("channelId") REFERENCES "IiosChannel"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "IiosInteraction" ADD CONSTRAINT "IiosInteraction_actorId_fkey" FOREIGN KEY ("actorId") REFERENCES "IiosActorRef"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "IiosInteraction" ADD CONSTRAINT "IiosInteraction_parentInteractionId_fkey" FOREIGN KEY ("parentInteractionId") REFERENCES "IiosInteraction"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "IiosMessagePart" ADD CONSTRAINT "IiosMessagePart_interactionId_fkey" FOREIGN KEY ("interactionId") REFERENCES "IiosInteraction"("id") ON DELETE CASCADE ON UPDATE CASCADE;
