-- CreateEnum
CREATE TYPE "AgentRegistrationStatus" AS ENUM ('PENDING', 'APPROVED', 'REJECTED', 'EXPIRED');

-- AlterEnum
-- This migration adds more than one value to an enum.
-- With PostgreSQL versions 11 and earlier, this is not possible
-- in a single migration. This can be worked around by creating
-- multiple migrations, each migration adding only one value to
-- the enum.


ALTER TYPE "AuditAction" ADD VALUE 'AGENT_CONNECT';
ALTER TYPE "AuditAction" ADD VALUE 'AGENT_REGISTER';
ALTER TYPE "AuditAction" ADD VALUE 'AGENT_APPROVE';
ALTER TYPE "AuditAction" ADD VALUE 'AGENT_REJECT';
ALTER TYPE "AuditAction" ADD VALUE 'INVITE_CREATE';
ALTER TYPE "AuditAction" ADD VALUE 'INVITE_REVOKE';
ALTER TYPE "AuditAction" ADD VALUE 'CERT_ISSUE';
ALTER TYPE "AuditAction" ADD VALUE 'CERT_REVOKE';

-- AlterTable
ALTER TABLE "instances" ADD COLUMN     "agent_fingerprint" TEXT,
ADD COLUMN     "agent_last_seen" TIMESTAMP(3),
ADD COLUMN     "agent_url" TEXT,
ADD COLUMN     "agent_version" TEXT,
ADD COLUMN     "is_remote" BOOLEAN NOT NULL DEFAULT false;

-- CreateTable
CREATE TABLE "ccp_certificate_authority" (
    "id" TEXT NOT NULL,
    "common_name" TEXT NOT NULL,
    "encrypted_key" TEXT NOT NULL,
    "cert_pem" TEXT NOT NULL,
    "fingerprint" TEXT NOT NULL,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "expires_at" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "issued_agent_certs" (
    "id" TEXT NOT NULL,
    "ca_id" TEXT NOT NULL,
    "instance_id" TEXT NOT NULL,
    "common_name" TEXT NOT NULL,
    "encrypted_key" TEXT NOT NULL,
    "cert_pem" TEXT NOT NULL,
    "fingerprint" TEXT NOT NULL,
    "issued_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "expires_at" TIMESTAMP(3) NOT NULL,
    "revoked_at" TIMESTAMP(3),

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

-- CreateTable
CREATE TABLE "agent_invite_codes" (
    "id" TEXT NOT NULL,
    "code" TEXT NOT NULL,
    "created_by_id" TEXT NOT NULL,
    "used_by_id" TEXT,
    "expires_at" TIMESTAMP(3) NOT NULL,
    "used_at" TIMESTAMP(3),
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "agent_registrations" (
    "id" TEXT NOT NULL,
    "invite_code_id" TEXT NOT NULL,
    "slug" TEXT NOT NULL,
    "name" TEXT NOT NULL,
    "domain" TEXT NOT NULL,
    "agent_url" TEXT NOT NULL,
    "base_path" TEXT NOT NULL,
    "compose_project" TEXT NOT NULL,
    "metadata" JSONB,
    "status" "AgentRegistrationStatus" NOT NULL DEFAULT 'PENDING',
    "instance_id" TEXT,
    "approved_by_id" TEXT,
    "approved_at" TIMESTAMP(3),
    "rejected_at" TIMESTAMP(3),
    "cert_bundle" JSONB,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateIndex
CREATE UNIQUE INDEX "issued_agent_certs_instance_id_key" ON "issued_agent_certs"("instance_id");

-- CreateIndex
CREATE INDEX "issued_agent_certs_instance_id_idx" ON "issued_agent_certs"("instance_id");

-- CreateIndex
CREATE UNIQUE INDEX "agent_invite_codes_code_key" ON "agent_invite_codes"("code");

-- CreateIndex
CREATE INDEX "agent_registrations_status_idx" ON "agent_registrations"("status");

-- AddForeignKey
ALTER TABLE "issued_agent_certs" ADD CONSTRAINT "issued_agent_certs_ca_id_fkey" FOREIGN KEY ("ca_id") REFERENCES "ccp_certificate_authority"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "issued_agent_certs" ADD CONSTRAINT "issued_agent_certs_instance_id_fkey" FOREIGN KEY ("instance_id") REFERENCES "instances"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "agent_invite_codes" ADD CONSTRAINT "agent_invite_codes_created_by_id_fkey" FOREIGN KEY ("created_by_id") REFERENCES "ccp_users"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
