-- CreateEnum
CREATE TYPE "FederationConnectionStatus" AS ENUM ('PENDING', 'ACTIVE', 'SUSPENDED', 'DISCONNECTED');

-- CreateEnum
CREATE TYPE "FederationMessageType" AS ENUM ('QUERY', 'COMMAND', 'EVENT');

-- CreateEnum
CREATE TYPE "FederationMessageStatus" AS ENUM ('PENDING', 'DELIVERED', 'FAILED', 'TIMEOUT');

-- CreateTable
CREATE TABLE "federation_connections" (
    "id" UUID NOT NULL,
    "workspace_id" UUID NOT NULL,
    "remote_instance_id" TEXT NOT NULL,
    "remote_url" TEXT NOT NULL,
    "remote_public_key" TEXT NOT NULL,
    "remote_capabilities" JSONB NOT NULL DEFAULT '{}',
    "status" "FederationConnectionStatus" NOT NULL DEFAULT 'PENDING',
    "metadata" JSONB NOT NULL DEFAULT '{}',
    "created_at" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMPTZ NOT NULL,
    "connected_at" TIMESTAMPTZ,
    "disconnected_at" TIMESTAMPTZ,

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

-- CreateTable
CREATE TABLE "federated_identities" (
    "id" UUID NOT NULL,
    "local_user_id" UUID NOT NULL,
    "remote_user_id" TEXT NOT NULL,
    "remote_instance_id" TEXT NOT NULL,
    "oidc_subject" TEXT NOT NULL,
    "email" TEXT NOT NULL,
    "metadata" JSONB NOT NULL DEFAULT '{}',
    "created_at" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMPTZ NOT NULL,

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

-- CreateTable
CREATE TABLE "federation_messages" (
    "id" UUID NOT NULL,
    "workspace_id" UUID NOT NULL,
    "connection_id" UUID NOT NULL,
    "message_type" "FederationMessageType" NOT NULL,
    "message_id" TEXT NOT NULL,
    "correlation_id" TEXT,
    "query" TEXT,
    "command_type" TEXT,
    "event_type" TEXT,
    "payload" JSONB DEFAULT '{}',
    "response" JSONB DEFAULT '{}',
    "status" "FederationMessageStatus" NOT NULL DEFAULT 'PENDING',
    "error" TEXT,
    "signature" TEXT NOT NULL,
    "created_at" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMPTZ NOT NULL,
    "delivered_at" TIMESTAMPTZ,

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

-- CreateIndex
CREATE UNIQUE INDEX "federation_connections_workspace_id_remote_instance_id_key" ON "federation_connections"("workspace_id", "remote_instance_id");

-- CreateIndex
CREATE INDEX "federation_connections_workspace_id_idx" ON "federation_connections"("workspace_id");

-- CreateIndex
CREATE INDEX "federation_connections_workspace_id_status_idx" ON "federation_connections"("workspace_id", "status");

-- CreateIndex
CREATE INDEX "federation_connections_remote_instance_id_idx" ON "federation_connections"("remote_instance_id");

-- CreateIndex
CREATE UNIQUE INDEX "federated_identities_local_user_id_remote_instance_id_key" ON "federated_identities"("local_user_id", "remote_instance_id");

-- CreateIndex
CREATE INDEX "federated_identities_local_user_id_idx" ON "federated_identities"("local_user_id");

-- CreateIndex
CREATE INDEX "federated_identities_remote_instance_id_idx" ON "federated_identities"("remote_instance_id");

-- CreateIndex
CREATE INDEX "federated_identities_oidc_subject_idx" ON "federated_identities"("oidc_subject");

-- CreateIndex
CREATE UNIQUE INDEX "federation_messages_message_id_key" ON "federation_messages"("message_id");

-- CreateIndex
CREATE INDEX "federation_messages_workspace_id_idx" ON "federation_messages"("workspace_id");

-- CreateIndex
CREATE INDEX "federation_messages_connection_id_idx" ON "federation_messages"("connection_id");

-- CreateIndex
CREATE INDEX "federation_messages_message_id_idx" ON "federation_messages"("message_id");

-- CreateIndex
CREATE INDEX "federation_messages_correlation_id_idx" ON "federation_messages"("correlation_id");

-- CreateIndex
CREATE INDEX "federation_messages_event_type_idx" ON "federation_messages"("event_type");

-- AddForeignKey
ALTER TABLE "federation_connections" ADD CONSTRAINT "federation_connections_workspace_id_fkey" FOREIGN KEY ("workspace_id") REFERENCES "workspaces"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "federated_identities" ADD CONSTRAINT "federated_identities_local_user_id_fkey" FOREIGN KEY ("local_user_id") REFERENCES "users"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "federation_messages" ADD CONSTRAINT "federation_messages_connection_id_fkey" FOREIGN KEY ("connection_id") REFERENCES "federation_connections"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "federation_messages" ADD CONSTRAINT "federation_messages_workspace_id_fkey" FOREIGN KEY ("workspace_id") REFERENCES "workspaces"("id") ON DELETE CASCADE ON UPDATE CASCADE;
