/*
  Warnings:

  - You are about to drop the `ai_agent` table. If the table is not empty, all the data it contains will be lost.
  - You are about to drop the `ai_plugin` table. If the table is not empty, all the data it contains will be lost.
  - You are about to drop the `ai_tool` table. If the table is not empty, all the data it contains will be lost.
  - You are about to drop the `chat` table. If the table is not empty, all the data it contains will be lost.
  - You are about to drop the `message` table. If the table is not empty, all the data it contains will be lost.
  - You are about to drop the `tool_calls` table. If the table is not empty, all the data it contains will be lost.
  - You are about to drop the `transaction` table. If the table is not empty, all the data it contains will be lost.

*/
-- DropForeignKey
ALTER TABLE "ai_plugin" DROP CONSTRAINT "ai_plugin_agent_id_fkey";

-- DropForeignKey
ALTER TABLE "ai_tool" DROP CONSTRAINT "ai_tool_agent_id_fkey";

-- DropForeignKey
ALTER TABLE "message" DROP CONSTRAINT "message_chat_id_fkey";

-- DropForeignKey
ALTER TABLE "tool_calls" DROP CONSTRAINT "tool_calls_tool_id_fkey";

-- DropForeignKey
ALTER TABLE "transaction" DROP CONSTRAINT "transaction_chat_id_fkey";

-- DropForeignKey
ALTER TABLE "transaction" DROP CONSTRAINT "transaction_message_id_fkey";

-- DropTable
DROP TABLE "ai_agent";

-- DropTable
DROP TABLE "ai_plugin";

-- DropTable
DROP TABLE "ai_tool";

-- DropTable
DROP TABLE "chat";

-- DropTable
DROP TABLE "message";

-- DropTable
DROP TABLE "tool_calls";

-- DropTable
DROP TABLE "transaction";

-- CreateTable
CREATE TABLE "AiPlugin" (
    "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 "AiPlugin_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "AiAgent" (
    "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 "AiAgent_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "AiTool" (
    "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 "AiTool_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 "ToolCall" (
    "id" SERIAL NOT NULL,
    "tool_id" TEXT NOT NULL,
    "call_timestamp" TIMESTAMP(3) NOT NULL,

    CONSTRAINT "ToolCall_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")
);

-- AddForeignKey
ALTER TABLE "AiPlugin" ADD CONSTRAINT "AiPlugin_agent_id_fkey" FOREIGN KEY ("agent_id") REFERENCES "AiAgent"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "AiTool" ADD CONSTRAINT "AiTool_agent_id_fkey" FOREIGN KEY ("agent_id") REFERENCES "AiAgent"("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 "ToolCall" ADD CONSTRAINT "ToolCall_tool_id_fkey" FOREIGN KEY ("tool_id") REFERENCES "AiTool"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "Transaction" ADD CONSTRAINT "Transaction_message_id_chat_id_fkey" FOREIGN KEY ("message_id", "chat_id") REFERENCES "Message"("message_id", "chat_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;
