-- CreateTable
CREATE TABLE "ai_plugin" (
    "id" TEXT NOT NULL,
    "openapi" TEXT NOT NULL,
    "info" JSONB NOT NULL DEFAULT '{}',
    "servers" TEXT[],
    "paths" JSONB NOT NULL DEFAULT '{}',
    "agent_id" TEXT NOT NULL,

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

-- CreateTable
CREATE TABLE "ai_agent" (
    "id" TEXT NOT NULL,
    "description" TEXT NOT NULL,
    "instructions" TEXT NOT NULL,
    "name" TEXT NOT NULL,
    "url" TEXT NOT NULL,
    "key_id" TEXT NOT NULL,
    "categories" TEXT[],
    "image" TEXT,
    "account_id" TEXT,
    "repo" TEXT,
    "email" TEXT,
    "tool_ids" TEXT[],

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

-- CreateTable
CREATE TABLE "ai_tool" (
    "id" TEXT NOT NULL,
    "agent_id" TEXT NOT NULL,
    "execution" JSONB NOT NULL,
    "function" JSONB NOT NULL,
    "type" TEXT NOT NULL,
    "verified" BOOLEAN NOT NULL,

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

-- CreateTable
CREATE TABLE "chat" (
    "id" TEXT NOT NULL,
    "title" TEXT NOT NULL,
    "path" TEXT NOT NULL,
    "share_path" TEXT,
    "user_id" TEXT,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "message" (
    "message_id" INTEGER NOT NULL,
    "chat_id" TEXT NOT NULL,
    "role" TEXT NOT NULL,
    "content" TEXT NOT NULL,
    "message_timestamp" TIMESTAMP(3) NOT NULL,
    "function_call" JSONB,
    "name" TEXT,

    CONSTRAINT "message_pkey" PRIMARY KEY ("chat_id","message_id")
);

-- CreateTable
CREATE TABLE "tool_calls" (
    "id" SERIAL NOT NULL,
    "tool_id" TEXT NOT NULL,
    "call_timestamp" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "transaction" (
    "account_id" TEXT NOT NULL,
    "execution_timestamp" TIMESTAMP(3) NOT NULL,
    "chain_type" TEXT NOT NULL,
    "chain_id" TEXT NOT NULL,
    "message_id" INTEGER NOT NULL,
    "chat_id" TEXT NOT NULL,
    "body" JSONB NOT NULL,
    "success" JSONB,
    "failure" JSONB,

    CONSTRAINT "transaction_pkey" PRIMARY KEY ("chat_id","message_id")
);

-- CreateIndex
CREATE UNIQUE INDEX "message_message_id_key" ON "message"("message_id");

-- AddForeignKey
ALTER TABLE "ai_plugin" ADD CONSTRAINT "ai_plugin_agent_id_fkey" FOREIGN KEY ("agent_id") REFERENCES "ai_agent"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "ai_tool" ADD CONSTRAINT "ai_tool_agent_id_fkey" FOREIGN KEY ("agent_id") REFERENCES "ai_agent"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "message" ADD CONSTRAINT "message_chat_id_fkey" FOREIGN KEY ("chat_id") REFERENCES "chat"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "tool_calls" ADD CONSTRAINT "tool_calls_tool_id_fkey" FOREIGN KEY ("tool_id") REFERENCES "ai_tool"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "transaction" ADD CONSTRAINT "transaction_message_id_fkey" FOREIGN KEY ("message_id") REFERENCES "message"("message_id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "transaction" ADD CONSTRAINT "transaction_chat_id_fkey" FOREIGN KEY ("chat_id") REFERENCES "chat"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
