/*
  Warnings:

  - You are about to drop the `Agent` 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 `Plugin` table. If the table is not empty, all the data it contains will be lost.
  - You are about to drop the `Tool` table. If the table is not empty, all the data it contains will be lost.
  - You are about to drop the `Toolcall` 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 "Message" DROP CONSTRAINT "Message_chat_id_fkey";

-- DropForeignKey
ALTER TABLE "Plugin" DROP CONSTRAINT "Plugin_agent_id_fkey";

-- DropForeignKey
ALTER TABLE "Tool" DROP CONSTRAINT "Tool_agent_id_fkey";

-- DropForeignKey
ALTER TABLE "Toolcall" DROP CONSTRAINT "Toolcall_tool_id_fkey";

-- DropForeignKey
ALTER TABLE "Transaction" DROP CONSTRAINT "Transaction_chat_id_fkey";

-- DropForeignKey
ALTER TABLE "Transaction" DROP CONSTRAINT "Transaction_message_id_chat_id_fkey";

-- DropTable
DROP TABLE "Agent";

-- DropTable
DROP TABLE "Chat";

-- DropTable
DROP TABLE "Message";

-- DropTable
DROP TABLE "Plugin";

-- DropTable
DROP TABLE "Tool";

-- DropTable
DROP TABLE "Toolcall";

-- DropTable
DROP TABLE "Transaction";

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

-- CreateTable
CREATE TABLE "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,
    "verified" BOOLEAN NOT NULL DEFAULT false,
    "tool_ids" TEXT[],

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

-- CreateTable
CREATE TABLE "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 DEFAULT false,

    CONSTRAINT "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,
    "agent_id" TEXT,
    "function_call" JSONB,
    "name" TEXT,

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

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

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

-- CreateTable
CREATE TABLE "transaction" (
    "account_id" TEXT NOT NULL,
    "execution_timestamp" TIMESTAMP(3),
    "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 "plugin" ADD CONSTRAINT "plugin_agent_id_fkey" FOREIGN KEY ("agent_id") REFERENCES "agent"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "tool" ADD CONSTRAINT "tool_agent_id_fkey" FOREIGN KEY ("agent_id") REFERENCES "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 "message" ADD CONSTRAINT "message_agent_id_fkey" FOREIGN KEY ("agent_id") REFERENCES "agent"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "tool_call" ADD CONSTRAINT "tool_call_tool_id_fkey" FOREIGN KEY ("tool_id") REFERENCES "tool"("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;
