-- AlterTable
ALTER TABLE "documents" ADD COLUMN     "char_count" INTEGER,
ADD COLUMN     "extracted_text" TEXT,
ADD COLUMN     "extraction_method" TEXT,
ADD COLUMN     "extraction_quality" DOUBLE PRECISION,
ADD COLUMN     "language" TEXT;

-- CreateTable
CREATE TABLE "batch_tasks" (
    "id" TEXT NOT NULL,
    "user_id" TEXT NOT NULL,
    "kb_id" TEXT NOT NULL,
    "name" TEXT NOT NULL,
    "template_type" TEXT NOT NULL,
    "template_id" TEXT,
    "prompt" TEXT NOT NULL,
    "status" TEXT NOT NULL,
    "total_documents" INTEGER NOT NULL,
    "completed_count" INTEGER NOT NULL DEFAULT 0,
    "failed_count" INTEGER NOT NULL DEFAULT 0,
    "model_type" TEXT NOT NULL,
    "concurrency" INTEGER NOT NULL DEFAULT 3,
    "started_at" TIMESTAMP(3),
    "completed_at" TIMESTAMP(3),
    "duration_seconds" INTEGER,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "batch_results" (
    "id" TEXT NOT NULL,
    "task_id" TEXT NOT NULL,
    "document_id" TEXT NOT NULL,
    "status" TEXT NOT NULL,
    "data" JSONB,
    "rawOutput" TEXT,
    "error_message" TEXT,
    "processing_time_ms" INTEGER,
    "tokens_used" INTEGER,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "task_templates" (
    "id" TEXT NOT NULL,
    "user_id" TEXT NOT NULL,
    "name" TEXT NOT NULL,
    "description" TEXT,
    "prompt" TEXT NOT NULL,
    "outputFields" JSONB NOT NULL,
    "is_public" BOOLEAN NOT NULL DEFAULT false,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "general_conversations" (
    "id" TEXT NOT NULL,
    "user_id" TEXT NOT NULL,
    "title" TEXT NOT NULL,
    "model_name" TEXT,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "deleted_at" TIMESTAMP(3),

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

-- CreateTable
CREATE TABLE "general_messages" (
    "id" TEXT NOT NULL,
    "conversation_id" TEXT NOT NULL,
    "role" TEXT NOT NULL,
    "content" TEXT NOT NULL,
    "model" TEXT,
    "metadata" JSONB,
    "tokens" INTEGER,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateIndex
CREATE INDEX "batch_tasks_user_id_idx" ON "batch_tasks"("user_id");

-- CreateIndex
CREATE INDEX "batch_tasks_kb_id_idx" ON "batch_tasks"("kb_id");

-- CreateIndex
CREATE INDEX "batch_tasks_status_idx" ON "batch_tasks"("status");

-- CreateIndex
CREATE INDEX "batch_tasks_created_at_idx" ON "batch_tasks"("created_at");

-- CreateIndex
CREATE INDEX "batch_results_task_id_idx" ON "batch_results"("task_id");

-- CreateIndex
CREATE INDEX "batch_results_document_id_idx" ON "batch_results"("document_id");

-- CreateIndex
CREATE INDEX "batch_results_status_idx" ON "batch_results"("status");

-- CreateIndex
CREATE INDEX "task_templates_user_id_idx" ON "task_templates"("user_id");

-- CreateIndex
CREATE INDEX "general_conversations_user_id_idx" ON "general_conversations"("user_id");

-- CreateIndex
CREATE INDEX "general_conversations_created_at_idx" ON "general_conversations"("created_at");

-- CreateIndex
CREATE INDEX "general_conversations_updated_at_idx" ON "general_conversations"("updated_at");

-- CreateIndex
CREATE INDEX "general_messages_conversation_id_idx" ON "general_messages"("conversation_id");

-- CreateIndex
CREATE INDEX "general_messages_created_at_idx" ON "general_messages"("created_at");

-- CreateIndex
CREATE INDEX "documents_extraction_method_idx" ON "documents"("extraction_method");

-- AddForeignKey
ALTER TABLE "batch_tasks" ADD CONSTRAINT "batch_tasks_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "users"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "batch_tasks" ADD CONSTRAINT "batch_tasks_kb_id_fkey" FOREIGN KEY ("kb_id") REFERENCES "knowledge_bases"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "batch_results" ADD CONSTRAINT "batch_results_task_id_fkey" FOREIGN KEY ("task_id") REFERENCES "batch_tasks"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "batch_results" ADD CONSTRAINT "batch_results_document_id_fkey" FOREIGN KEY ("document_id") REFERENCES "documents"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "task_templates" ADD CONSTRAINT "task_templates_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "users"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "general_conversations" ADD CONSTRAINT "general_conversations_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "users"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "general_messages" ADD CONSTRAINT "general_messages_conversation_id_fkey" FOREIGN KEY ("conversation_id") REFERENCES "general_conversations"("id") ON DELETE CASCADE ON UPDATE CASCADE;
