-- =============================================
-- FIX: Create prompt_templates and prompt_versions tables if they don't exist
-- Background: These tables were originally created via `prisma db push` 
-- (bypassing migrations), causing shadow database validation failures.
-- This fix ensures migrations can replay cleanly from scratch.
-- =============================================

-- Create PromptStatus enum if not exists
DO $$ BEGIN
    CREATE TYPE "capability_schema"."PromptStatus" AS ENUM ('DRAFT', 'ACTIVE', 'ARCHIVED');
EXCEPTION
    WHEN duplicate_object THEN null;
END $$;

-- CreateTable: prompt_templates (if not exists)
CREATE TABLE IF NOT EXISTS "capability_schema"."prompt_templates" (
    "id" SERIAL NOT NULL,
    "code" TEXT NOT NULL,
    "name" TEXT NOT NULL,
    "module" TEXT NOT NULL,
    "description" TEXT,
    "variables" JSONB,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,

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

-- CreateIndex (if not exists)
CREATE UNIQUE INDEX IF NOT EXISTS "prompt_templates_code_key" ON "capability_schema"."prompt_templates"("code");
CREATE INDEX IF NOT EXISTS "idx_prompt_templates_module" ON "capability_schema"."prompt_templates"("module");

-- CreateTable: prompt_versions (if not exists)
CREATE TABLE IF NOT EXISTS "capability_schema"."prompt_versions" (
    "id" SERIAL NOT NULL,
    "template_id" INTEGER NOT NULL,
    "version" INTEGER NOT NULL,
    "content" TEXT NOT NULL,
    "model_config" JSONB,
    "status" "capability_schema"."PromptStatus" NOT NULL DEFAULT 'DRAFT',
    "changelog" TEXT,
    "created_by" TEXT,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateIndex (if not exists)
CREATE INDEX IF NOT EXISTS "idx_prompt_versions_template_status" ON "capability_schema"."prompt_versions"("template_id", "status");
CREATE INDEX IF NOT EXISTS "idx_prompt_versions_status" ON "capability_schema"."prompt_versions"("status");

-- AddForeignKey (if not exists)
DO $$ BEGIN
    ALTER TABLE "capability_schema"."prompt_versions" 
    ADD CONSTRAINT "prompt_versions_template_id_fkey" 
    FOREIGN KEY ("template_id") REFERENCES "capability_schema"."prompt_templates"("id") 
    ON DELETE RESTRICT ON UPDATE CASCADE;
EXCEPTION
    WHEN duplicate_object THEN null;
END $$;

-- =============================================
-- Original migration content starts here
-- =============================================

-- Add knowledge_config field to prompt_templates (if not exists)
DO $$ BEGIN
    ALTER TABLE "capability_schema"."prompt_templates" ADD COLUMN "knowledge_config" JSONB;
EXCEPTION
    WHEN duplicate_column THEN null;
END $$;

-- CreateTable: system_knowledge_bases
CREATE TABLE IF NOT EXISTS "capability_schema"."system_knowledge_bases" (
    "id" TEXT NOT NULL,
    "code" VARCHAR(50) NOT NULL,
    "name" VARCHAR(100) NOT NULL,
    "description" TEXT,
    "category" VARCHAR(50),
    "document_count" INTEGER NOT NULL DEFAULT 0,
    "total_tokens" INTEGER NOT NULL DEFAULT 0,
    "status" VARCHAR(20) NOT NULL DEFAULT 'active',
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,

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

-- CreateTable: system_kb_documents
CREATE TABLE IF NOT EXISTS "capability_schema"."system_kb_documents" (
    "id" TEXT NOT NULL,
    "kb_id" TEXT NOT NULL,
    "filename" VARCHAR(255) NOT NULL,
    "file_path" VARCHAR(500),
    "file_size" INTEGER,
    "file_type" VARCHAR(50),
    "content" TEXT,
    "token_count" INTEGER NOT NULL DEFAULT 0,
    "status" VARCHAR(20) NOT NULL DEFAULT 'pending',
    "error_message" TEXT,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,

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

-- CreateIndex (if not exists)
CREATE UNIQUE INDEX IF NOT EXISTS "system_knowledge_bases_code_key" ON "capability_schema"."system_knowledge_bases"("code");
CREATE INDEX IF NOT EXISTS "idx_system_kb_category" ON "capability_schema"."system_knowledge_bases"("category");
CREATE INDEX IF NOT EXISTS "idx_system_kb_status" ON "capability_schema"."system_knowledge_bases"("status");
CREATE INDEX IF NOT EXISTS "idx_system_kb_docs_kb_id" ON "capability_schema"."system_kb_documents"("kb_id");
CREATE INDEX IF NOT EXISTS "idx_system_kb_docs_status" ON "capability_schema"."system_kb_documents"("status");

-- AddForeignKey (if not exists)
DO $$ BEGIN
    ALTER TABLE "capability_schema"."system_kb_documents" 
    ADD CONSTRAINT "system_kb_documents_kb_id_fkey" 
    FOREIGN KEY ("kb_id") REFERENCES "capability_schema"."system_knowledge_bases"("id") 
    ON DELETE CASCADE ON UPDATE CASCADE;
EXCEPTION
    WHEN duplicate_object THEN null;
END $$;
