-- CreateEnum
CREATE TYPE "NotificationType" AS ENUM ('TELEGRAM');

-- CreateTable
CREATE TABLE "users" (
    "id" TEXT NOT NULL,
    "username" TEXT NOT NULL,
    "name" TEXT NOT NULL,
    "email" TEXT NOT NULL,
    "password" TEXT NOT NULL,
    "lastLogin" TIMESTAMP(3),
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "sites" (
    "id" SERIAL NOT NULL,
    "name" TEXT NOT NULL,
    "description" TEXT,

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

-- CreateTable
CREATE TABLE "networks" (
    "id" SERIAL NOT NULL,
    "siteId" INTEGER NOT NULL,
    "name" TEXT NOT NULL,
    "ipv4Subnet" TEXT,
    "ipv6Subnet" TEXT,
    "gateway" TEXT,

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

-- CreateTable
CREATE TABLE "servers" (
    "id" SERIAL NOT NULL,
    "networkId" INTEGER NOT NULL,
    "name" TEXT NOT NULL,
    "description" TEXT,
    "icon" TEXT,
    "ipv4Address" TEXT,
    "osDetails" TEXT,
    "cpuDetails" TEXT,
    "gpuDetails" TEXT,
    "memoryDetails" TEXT,
    "storageDetails" TEXT,
    "managementUrl" TEXT,
    "monitoring" BOOLEAN NOT NULL DEFAULT false,
    "monitoringUrl" TEXT,

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

-- CreateTable
CREATE TABLE "server_monitoring" (
    "id" SERIAL NOT NULL,
    "serverId" INTEGER NOT NULL,
    "cpuUsage" DOUBLE PRECISION NOT NULL,
    "gpuUsage" DOUBLE PRECISION NOT NULL,
    "memoryUsage" DOUBLE PRECISION NOT NULL,
    "diskUsage" DOUBLE PRECISION NOT NULL,
    "temperature" DOUBLE PRECISION NOT NULL,
    "online" BOOLEAN NOT NULL,
    "uptimeSeconds" INTEGER NOT NULL,
    "timestamp" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "applications" (
    "id" SERIAL NOT NULL,
    "serverId" INTEGER NOT NULL,
    "name" TEXT NOT NULL,
    "description" TEXT,
    "icon" TEXT,
    "url" TEXT,
    "monitoring" BOOLEAN NOT NULL DEFAULT false,

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

-- CreateTable
CREATE TABLE "application_monitoring" (
    "id" SERIAL NOT NULL,
    "applicationId" INTEGER NOT NULL,
    "online" BOOLEAN NOT NULL,
    "latency" DOUBLE PRECISION NOT NULL,
    "timestamp" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "server_monitoring_notifications" (
    "id" SERIAL NOT NULL,
    "statusChange" BOOLEAN NOT NULL,
    "cpuLimit" DOUBLE PRECISION NOT NULL,
    "gpuLimit" DOUBLE PRECISION NOT NULL,
    "memoryLimit" DOUBLE PRECISION NOT NULL,
    "diskLimit" DOUBLE PRECISION NOT NULL,
    "temperatureLimit" DOUBLE PRECISION NOT NULL,

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

-- CreateTable
CREATE TABLE "application_monitoring_notifications" (
    "id" SERIAL NOT NULL,
    "statusChange" BOOLEAN NOT NULL,
    "latencyLimit" DOUBLE PRECISION NOT NULL,

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

-- CreateTable
CREATE TABLE "notification_providers" (
    "id" SERIAL NOT NULL,
    "name" TEXT NOT NULL,
    "type" "NotificationType" NOT NULL,
    "config" JSONB NOT NULL,

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

-- CreateIndex
CREATE UNIQUE INDEX "users_username_key" ON "users"("username");

-- CreateIndex
CREATE UNIQUE INDEX "users_email_key" ON "users"("email");

-- CreateIndex
CREATE INDEX "server_monitoring_serverId_timestamp_idx" ON "server_monitoring"("serverId", "timestamp");

-- CreateIndex
CREATE INDEX "server_monitoring_online_idx" ON "server_monitoring"("online");

-- CreateIndex
CREATE INDEX "application_monitoring_applicationId_timestamp_idx" ON "application_monitoring"("applicationId", "timestamp");

-- CreateIndex
CREATE INDEX "application_monitoring_online_idx" ON "application_monitoring"("online");

-- AddForeignKey
ALTER TABLE "networks" ADD CONSTRAINT "networks_siteId_fkey" FOREIGN KEY ("siteId") REFERENCES "sites"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "servers" ADD CONSTRAINT "servers_networkId_fkey" FOREIGN KEY ("networkId") REFERENCES "networks"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "server_monitoring" ADD CONSTRAINT "server_monitoring_serverId_fkey" FOREIGN KEY ("serverId") REFERENCES "servers"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "applications" ADD CONSTRAINT "applications_serverId_fkey" FOREIGN KEY ("serverId") REFERENCES "servers"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "application_monitoring" ADD CONSTRAINT "application_monitoring_applicationId_fkey" FOREIGN KEY ("applicationId") REFERENCES "applications"("id") ON DELETE CASCADE ON UPDATE CASCADE;
