-- CreateEnum
CREATE TYPE "IiosAiJobType" AS ENUM ('CLASSIFY', 'SUMMARIZE', 'EXTRACT');

-- CreateEnum
CREATE TYPE "IiosAiJobStatus" AS ENUM ('PENDING', 'RUNNING', 'DONE', 'FAILED', 'ABSTAINED');

-- CreateEnum
CREATE TYPE "IiosAiArtifactType" AS ENUM ('CLASSIFICATION', 'SUMMARY', 'TRANSCRIPT', 'DIGEST', 'EXTRACTION');

-- CreateEnum
CREATE TYPE "IiosAiArtifactStatus" AS ENUM ('PROPOSED', 'ACCEPTED', 'REJECTED', 'SUPERSEDED');

-- CreateEnum
CREATE TYPE "IiosAiClaimType" AS ENUM ('MODERATION_FLAG', 'EVENT', 'FAQ', 'INTENT', 'PRIORITY');

-- CreateEnum
CREATE TYPE "IiosAiClaimValidation" AS ENUM ('UNVALIDATED', 'ACCEPTED', 'REJECTED');

-- CreateTable
CREATE TABLE "IiosAiJob" (
    "id" TEXT NOT NULL,
    "scopeId" TEXT NOT NULL,
    "jobType" "IiosAiJobType" NOT NULL,
    "interactionId" TEXT,
    "inputWindowRef" TEXT NOT NULL,
    "purpose" TEXT NOT NULL,
    "status" "IiosAiJobStatus" NOT NULL DEFAULT 'PENDING',
    "modelPolicyRef" TEXT NOT NULL DEFAULT 'golden-v1',
    "policyDecisionRef" TEXT,
    "inputScopeHash" TEXT NOT NULL,
    "replayVersion" TEXT NOT NULL DEFAULT 'v1',
    "abstentionReason" TEXT,
    "traceId" TEXT,
    "idempotencyKey" TEXT NOT NULL,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "IiosAiModelRun" (
    "id" TEXT NOT NULL,
    "scopeId" TEXT NOT NULL,
    "provider" TEXT NOT NULL DEFAULT 'fake',
    "model" TEXT NOT NULL,
    "modelVersion" TEXT NOT NULL,
    "promptVersion" TEXT NOT NULL,
    "toolPolicyRef" TEXT,
    "tokensIn" INTEGER NOT NULL DEFAULT 0,
    "tokensOut" INTEGER NOT NULL DEFAULT 0,
    "costUnits" INTEGER NOT NULL DEFAULT 0,
    "latencyMs" INTEGER NOT NULL DEFAULT 0,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "IiosAiArtifact" (
    "id" TEXT NOT NULL,
    "jobId" TEXT NOT NULL,
    "modelRunId" TEXT NOT NULL,
    "artifactType" "IiosAiArtifactType" NOT NULL,
    "status" "IiosAiArtifactStatus" NOT NULL DEFAULT 'PROPOSED',
    "confidence" DOUBLE PRECISION,
    "contentRef" JSONB NOT NULL,
    "explanationRef" TEXT,
    "abstentionReason" TEXT,
    "acceptedByActorId" TEXT,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "IiosAiClaim" (
    "id" TEXT NOT NULL,
    "artifactId" TEXT NOT NULL,
    "claimType" "IiosAiClaimType" NOT NULL,
    "claimJson" JSONB NOT NULL,
    "confidence" DOUBLE PRECISION,
    "validationStatus" "IiosAiClaimValidation" NOT NULL DEFAULT 'UNVALIDATED',
    "acceptedByRef" TEXT,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "IiosAiEvidenceLink" (
    "id" TEXT NOT NULL,
    "artifactId" TEXT NOT NULL,
    "sourceType" TEXT NOT NULL,
    "sourceId" TEXT NOT NULL,
    "spanRef" TEXT,
    "relevanceScore" DOUBLE PRECISION,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "IiosAiToolCall" (
    "id" TEXT NOT NULL,
    "modelRunId" TEXT NOT NULL,
    "toolName" TEXT NOT NULL,
    "toolInputHash" TEXT NOT NULL,
    "toolOutputRef" TEXT,
    "policyRefId" TEXT,
    "status" TEXT NOT NULL DEFAULT 'PENDING',
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "IiosEmbeddingRef" (
    "id" TEXT NOT NULL,
    "sourceType" TEXT NOT NULL,
    "sourceId" TEXT NOT NULL,
    "embeddingModel" TEXT NOT NULL,
    "vectorStoreRef" TEXT NOT NULL,
    "embeddingHash" TEXT NOT NULL,
    "version" TEXT NOT NULL,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateIndex
CREATE UNIQUE INDEX "IiosAiJob_idempotencyKey_key" ON "IiosAiJob"("idempotencyKey");

-- CreateIndex
CREATE INDEX "IiosAiJob_scopeId_jobType_idx" ON "IiosAiJob"("scopeId", "jobType");

-- CreateIndex
CREATE INDEX "IiosAiJob_interactionId_idx" ON "IiosAiJob"("interactionId");

-- CreateIndex
CREATE INDEX "IiosAiModelRun_scopeId_idx" ON "IiosAiModelRun"("scopeId");

-- CreateIndex
CREATE INDEX "IiosAiArtifact_jobId_idx" ON "IiosAiArtifact"("jobId");

-- CreateIndex
CREATE INDEX "IiosAiArtifact_status_idx" ON "IiosAiArtifact"("status");

-- CreateIndex
CREATE INDEX "IiosAiClaim_artifactId_idx" ON "IiosAiClaim"("artifactId");

-- CreateIndex
CREATE INDEX "IiosAiEvidenceLink_artifactId_idx" ON "IiosAiEvidenceLink"("artifactId");

-- CreateIndex
CREATE INDEX "IiosAiToolCall_modelRunId_idx" ON "IiosAiToolCall"("modelRunId");

-- CreateIndex
CREATE UNIQUE INDEX "IiosEmbeddingRef_sourceType_sourceId_version_key" ON "IiosEmbeddingRef"("sourceType", "sourceId", "version");

-- AddForeignKey
ALTER TABLE "IiosAiArtifact" ADD CONSTRAINT "IiosAiArtifact_jobId_fkey" FOREIGN KEY ("jobId") REFERENCES "IiosAiJob"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "IiosAiArtifact" ADD CONSTRAINT "IiosAiArtifact_modelRunId_fkey" FOREIGN KEY ("modelRunId") REFERENCES "IiosAiModelRun"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "IiosAiClaim" ADD CONSTRAINT "IiosAiClaim_artifactId_fkey" FOREIGN KEY ("artifactId") REFERENCES "IiosAiArtifact"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "IiosAiEvidenceLink" ADD CONSTRAINT "IiosAiEvidenceLink_artifactId_fkey" FOREIGN KEY ("artifactId") REFERENCES "IiosAiArtifact"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "IiosAiToolCall" ADD CONSTRAINT "IiosAiToolCall_modelRunId_fkey" FOREIGN KEY ("modelRunId") REFERENCES "IiosAiModelRun"("id") ON DELETE CASCADE ON UPDATE CASCADE;
