-- CreateEnum
CREATE TYPE "ContainerEventType" AS ENUM ('CRASH', 'OOM_KILLED', 'RESTART', 'STOPPED');

-- CreateTable
CREATE TABLE "log_scan_positions" (
    "id" TEXT NOT NULL,
    "server_id" TEXT NOT NULL,
    "container_id" TEXT NOT NULL,
    "last_line_count" INTEGER NOT NULL DEFAULT 0,
    "last_log_hash" TEXT,
    "last_scanned_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "container_state_snapshots" (
    "id" TEXT NOT NULL,
    "server_id" TEXT NOT NULL,
    "container_id" TEXT NOT NULL,
    "container_name" TEXT NOT NULL,
    "state" TEXT NOT NULL,
    "exit_code" INTEGER,
    "captured_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "container_events" (
    "id" TEXT NOT NULL,
    "server_id" TEXT NOT NULL,
    "container_id" TEXT NOT NULL,
    "container_name" TEXT NOT NULL,
    "event_type" "ContainerEventType" NOT NULL,
    "exit_code" INTEGER,
    "details" TEXT,
    "acknowledged_at" TIMESTAMP(3),
    "acknowledged_by" TEXT,
    "timestamp" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "notification_settings" (
    "id" TEXT NOT NULL,
    "client_id" TEXT NOT NULL,
    "enabled" BOOLEAN NOT NULL DEFAULT false,
    "critical_errors_only" BOOLEAN NOT NULL DEFAULT true,
    "container_crashes" BOOLEAN NOT NULL DEFAULT true,
    "recipients" TEXT[] DEFAULT ARRAY[]::TEXT[],
    "cooldown_minutes" INTEGER NOT NULL DEFAULT 30,
    "last_notified_at" TIMESTAMP(3),

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

-- CreateIndex
CREATE UNIQUE INDEX "log_scan_positions_server_id_container_id_key" ON "log_scan_positions"("server_id", "container_id");

-- CreateIndex
CREATE INDEX "container_state_snapshots_server_id_container_id_captured_a_idx" ON "container_state_snapshots"("server_id", "container_id", "captured_at");

-- CreateIndex
CREATE INDEX "container_events_server_id_timestamp_idx" ON "container_events"("server_id", "timestamp");

-- CreateIndex
CREATE INDEX "container_events_event_type_acknowledged_at_idx" ON "container_events"("event_type", "acknowledged_at");

-- CreateIndex
CREATE UNIQUE INDEX "notification_settings_client_id_key" ON "notification_settings"("client_id");

-- AddForeignKey
ALTER TABLE "log_scan_positions" ADD CONSTRAINT "log_scan_positions_server_id_fkey" FOREIGN KEY ("server_id") REFERENCES "enterprise_servers"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "container_state_snapshots" ADD CONSTRAINT "container_state_snapshots_server_id_fkey" FOREIGN KEY ("server_id") REFERENCES "enterprise_servers"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "container_events" ADD CONSTRAINT "container_events_server_id_fkey" FOREIGN KEY ("server_id") REFERENCES "enterprise_servers"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "notification_settings" ADD CONSTRAINT "notification_settings_client_id_fkey" FOREIGN KEY ("client_id") REFERENCES "enterprise_clients"("id") ON DELETE CASCADE ON UPDATE CASCADE;
