-- CreateSchema
CREATE SCHEMA IF NOT EXISTS "public";

-- CreateEnum
CREATE TYPE "Role" AS ENUM ('SUPER_ADMIN', 'TENANT_ADMIN', 'OPERATOR', 'VIEWER');

-- CreateEnum
CREATE TYPE "TenantStatus" AS ENUM ('ACTIVE', 'SUSPENDED', 'TRIAL', 'CANCELLED');

-- CreateEnum
CREATE TYPE "VmStatus" AS ENUM ('RUNNING', 'STOPPED', 'PAUSED', 'MIGRATING', 'ERROR');

-- CreateEnum
CREATE TYPE "VmType" AS ENUM ('QEMU', 'LXC');

-- CreateEnum
CREATE TYPE "NodeStatus" AS ENUM ('ONLINE', 'OFFLINE', 'MAINTENANCE');

-- CreateEnum
CREATE TYPE "Currency" AS ENUM ('NGN', 'USD', 'GHS', 'KES', 'ZAR');

-- CreateEnum
CREATE TYPE "PaymentProvider" AS ENUM ('PAYSTACK', 'FLUTTERWAVE', 'MANUAL');

-- CreateEnum
CREATE TYPE "InvoiceStatus" AS ENUM ('DRAFT', 'PENDING', 'PAID', 'OVERDUE', 'CANCELLED', 'REFUNDED');

-- CreateEnum
CREATE TYPE "BackupStatus" AS ENUM ('PENDING', 'RUNNING', 'COMPLETED', 'FAILED', 'EXPIRED');

-- CreateEnum
CREATE TYPE "BackupType" AS ENUM ('FULL', 'INCREMENTAL', 'SNAPSHOT');

-- CreateEnum
CREATE TYPE "BackupSchedule" AS ENUM ('MANUAL', 'DAILY', 'WEEKLY', 'MONTHLY');

-- CreateEnum
CREATE TYPE "BackupSource" AS ENUM ('LOCAL', 'PBS', 'REMOTE');

-- CreateEnum
CREATE TYPE "BackupRestoreMode" AS ENUM ('FULL_VM', 'FILES', 'SINGLE_FILE');

-- CreateEnum
CREATE TYPE "BackupRestoreStatus" AS ENUM ('PENDING', 'RUNNING', 'COMPLETED', 'FAILED');

-- CreateEnum
CREATE TYPE "SnapshotFrequency" AS ENUM ('HOURLY', 'DAILY', 'WEEKLY');

-- CreateEnum
CREATE TYPE "Severity" AS ENUM ('INFO', 'WARNING', 'ERROR', 'CRITICAL');

-- CreateEnum
CREATE TYPE "SecurityStatus" AS ENUM ('OPEN', 'INVESTIGATING', 'RESOLVED', 'FALSE_POSITIVE');

-- CreateEnum
CREATE TYPE "Direction" AS ENUM ('INBOUND', 'OUTBOUND', 'BOTH');

-- CreateEnum
CREATE TYPE "FirewallAction" AS ENUM ('ALLOW', 'DENY', 'RATE_LIMIT', 'LOG');

-- CreateEnum
CREATE TYPE "Protocol" AS ENUM ('TCP', 'UDP', 'ICMP', 'ANY');

-- CreateEnum
CREATE TYPE "AppliesTo" AS ENUM ('ALL_NODES', 'ALL_VMS', 'SPECIFIC_NODE', 'SPECIFIC_VM');

-- CreateEnum
CREATE TYPE "ResourceType" AS ENUM ('VM', 'TENANT', 'USER', 'BACKUP', 'INVOICE', 'NODE', 'NETWORK', 'SYSTEM', 'SECURITY', 'BILLING');

-- CreateEnum
CREATE TYPE "SettingType" AS ENUM ('PROXMOX', 'PAYMENT', 'EMAIL', 'SECURITY', 'NETWORK', 'GENERAL');

-- CreateEnum
CREATE TYPE "OperationTaskStatus" AS ENUM ('QUEUED', 'RUNNING', 'SUCCESS', 'FAILED', 'RETRYING', 'CANCELED');

-- CreateEnum
CREATE TYPE "OperationTaskType" AS ENUM ('VM_POWER', 'VM_MIGRATION', 'VM_REINSTALL', 'VM_NETWORK', 'VM_CONFIG', 'VM_BACKUP', 'VM_SNAPSHOT', 'VM_CREATE', 'VM_DELETE', 'SYSTEM_SYNC');

-- CreateEnum
CREATE TYPE "PowerScheduleAction" AS ENUM ('START', 'STOP', 'RESTART', 'SHUTDOWN');

-- CreateEnum
CREATE TYPE "TemplateType" AS ENUM ('APPLICATION', 'KVM_TEMPLATE', 'LXC_TEMPLATE', 'ISO_IMAGE', 'ARCHIVE');

-- CreateEnum
CREATE TYPE "ProductType" AS ENUM ('VPS', 'CLOUD');

-- CreateEnum
CREATE TYPE "ServiceLifecycleStatus" AS ENUM ('ACTIVE', 'SUSPENDED', 'TERMINATED');

-- CreateEnum
CREATE TYPE "IpVersion" AS ENUM ('IPV4', 'IPV6');

-- CreateEnum
CREATE TYPE "IpScope" AS ENUM ('PUBLIC', 'PRIVATE');

-- CreateEnum
CREATE TYPE "IpAddressStatus" AS ENUM ('AVAILABLE', 'ASSIGNED', 'RESERVED', 'RETIRED');

-- CreateEnum
CREATE TYPE "IpAssignmentType" AS ENUM ('PRIMARY', 'ADDITIONAL', 'FLOATING');

-- CreateEnum
CREATE TYPE "PrivateNetworkType" AS ENUM ('BRIDGE', 'VLAN', 'SDN_ZONE', 'VNET');

-- CreateEnum
CREATE TYPE "PrivateNetworkAttachmentStatus" AS ENUM ('ATTACHED', 'DETACHED');

-- CreateEnum
CREATE TYPE "IpAllocationStrategy" AS ENUM ('FIRST_AVAILABLE', 'BEST_FIT');

-- CreateEnum
CREATE TYPE "HealthCheckTargetType" AS ENUM ('NODE', 'VM', 'CLUSTER');

-- CreateEnum
CREATE TYPE "HealthCheckType" AS ENUM ('CONNECTIVITY', 'RESOURCE_THRESHOLD', 'SERVICE_PORT');

-- CreateEnum
CREATE TYPE "HealthCheckStatus" AS ENUM ('PASS', 'WARNING', 'FAIL');

-- CreateEnum
CREATE TYPE "MonitoringAlertStatus" AS ENUM ('OPEN', 'ACKNOWLEDGED', 'RESOLVED');

-- CreateEnum
CREATE TYPE "AlertChannel" AS ENUM ('EMAIL', 'WEBHOOK', 'IN_APP');

-- CreateEnum
CREATE TYPE "AlertDispatchStatus" AS ENUM ('QUEUED', 'SENT', 'FAILED', 'SKIPPED');

-- CreateTable
CREATE TABLE "User" (
    "id" TEXT NOT NULL,
    "email" TEXT NOT NULL,
    "password_hash" TEXT NOT NULL,
    "full_name" TEXT,
    "role" "Role" NOT NULL DEFAULT 'VIEWER',
    "tenant_id" TEXT,
    "is_active" BOOLEAN NOT NULL DEFAULT true,
    "last_login_at" TIMESTAMP(3),
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "Tenant" (
    "id" TEXT NOT NULL,
    "name" TEXT NOT NULL,
    "slug" TEXT NOT NULL,
    "status" "TenantStatus" NOT NULL DEFAULT 'ACTIVE',
    "plan" TEXT NOT NULL DEFAULT 'starter',
    "owner_email" TEXT NOT NULL,
    "member_emails" JSONB NOT NULL DEFAULT '[]',
    "vm_limit" INTEGER NOT NULL DEFAULT 5,
    "cpu_limit" INTEGER NOT NULL DEFAULT 16,
    "ram_limit_mb" INTEGER NOT NULL DEFAULT 32768,
    "disk_limit_gb" INTEGER NOT NULL DEFAULT 500,
    "balance" DECIMAL(14,2) NOT NULL DEFAULT 0,
    "currency" "Currency" NOT NULL DEFAULT 'NGN',
    "payment_provider" "PaymentProvider" NOT NULL DEFAULT 'PAYSTACK',
    "notes" TEXT,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "ProxmoxNode" (
    "id" TEXT NOT NULL,
    "name" TEXT NOT NULL,
    "hostname" TEXT NOT NULL,
    "status" "NodeStatus" NOT NULL DEFAULT 'OFFLINE',
    "cpu_cores" INTEGER NOT NULL DEFAULT 8,
    "cpu_usage" DOUBLE PRECISION NOT NULL DEFAULT 0,
    "ram_total_mb" INTEGER NOT NULL DEFAULT 32768,
    "ram_used_mb" INTEGER NOT NULL DEFAULT 0,
    "disk_total_gb" INTEGER NOT NULL DEFAULT 500,
    "disk_used_gb" INTEGER NOT NULL DEFAULT 0,
    "vm_count" INTEGER NOT NULL DEFAULT 0,
    "uptime_seconds" INTEGER NOT NULL DEFAULT 0,
    "pve_version" TEXT,
    "is_connected" BOOLEAN NOT NULL DEFAULT false,
    "last_sync_at" TIMESTAMP(3),
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "BillingPlan" (
    "id" TEXT NOT NULL,
    "name" TEXT NOT NULL,
    "slug" TEXT NOT NULL,
    "description" TEXT,
    "price_monthly" DECIMAL(12,2) NOT NULL,
    "price_hourly" DECIMAL(12,4) NOT NULL,
    "currency" "Currency" NOT NULL DEFAULT 'NGN',
    "cpu_cores" INTEGER NOT NULL,
    "ram_mb" INTEGER NOT NULL,
    "disk_gb" INTEGER NOT NULL,
    "bandwidth_gb" INTEGER,
    "is_active" BOOLEAN NOT NULL DEFAULT true,
    "features" JSONB NOT NULL DEFAULT '[]',
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "VirtualMachine" (
    "id" TEXT NOT NULL,
    "name" TEXT NOT NULL,
    "vmid" INTEGER NOT NULL,
    "status" "VmStatus" NOT NULL DEFAULT 'STOPPED',
    "type" "VmType" NOT NULL DEFAULT 'QEMU',
    "node" TEXT NOT NULL,
    "node_id" TEXT,
    "tenant_id" TEXT NOT NULL,
    "billing_plan_id" TEXT,
    "os_template" TEXT,
    "cpu_cores" INTEGER NOT NULL DEFAULT 2,
    "ram_mb" INTEGER NOT NULL DEFAULT 2048,
    "disk_gb" INTEGER NOT NULL DEFAULT 40,
    "ip_address" TEXT,
    "cpu_usage" DOUBLE PRECISION NOT NULL DEFAULT 0,
    "ram_usage" DOUBLE PRECISION NOT NULL DEFAULT 0,
    "disk_usage" DOUBLE PRECISION NOT NULL DEFAULT 0,
    "network_in" DOUBLE PRECISION NOT NULL DEFAULT 0,
    "network_out" DOUBLE PRECISION NOT NULL DEFAULT 0,
    "uptime_seconds" INTEGER NOT NULL DEFAULT 0,
    "started_at" TIMESTAMP(3),
    "last_backup_at" TIMESTAMP(3),
    "proxmox_upid" TEXT,
    "last_sync_at" TIMESTAMP(3),
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "ServerHealthCheck" (
    "id" TEXT NOT NULL,
    "name" TEXT NOT NULL,
    "description" TEXT,
    "target_type" "HealthCheckTargetType" NOT NULL,
    "check_type" "HealthCheckType" NOT NULL DEFAULT 'RESOURCE_THRESHOLD',
    "tenant_id" TEXT,
    "vm_id" TEXT,
    "node_id" TEXT,
    "cpu_warn_pct" DOUBLE PRECISION,
    "cpu_critical_pct" DOUBLE PRECISION,
    "ram_warn_pct" DOUBLE PRECISION,
    "ram_critical_pct" DOUBLE PRECISION,
    "disk_warn_pct" DOUBLE PRECISION,
    "disk_critical_pct" DOUBLE PRECISION,
    "disk_io_read_warn" DOUBLE PRECISION,
    "disk_io_read_critical" DOUBLE PRECISION,
    "disk_io_write_warn" DOUBLE PRECISION,
    "disk_io_write_critical" DOUBLE PRECISION,
    "network_in_warn" DOUBLE PRECISION,
    "network_in_critical" DOUBLE PRECISION,
    "network_out_warn" DOUBLE PRECISION,
    "network_out_critical" DOUBLE PRECISION,
    "latency_warn_ms" INTEGER,
    "latency_critical_ms" INTEGER,
    "schedule_minutes" INTEGER NOT NULL DEFAULT 5,
    "enabled" BOOLEAN NOT NULL DEFAULT true,
    "last_run_at" TIMESTAMP(3),
    "next_run_at" TIMESTAMP(3),
    "created_by" TEXT,
    "metadata" JSONB NOT NULL DEFAULT '{}',
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "ServerHealthCheckResult" (
    "id" TEXT NOT NULL,
    "check_id" TEXT NOT NULL,
    "status" "HealthCheckStatus" NOT NULL,
    "severity" "Severity" NOT NULL DEFAULT 'INFO',
    "message" TEXT,
    "latency_ms" INTEGER,
    "cpu_usage" DOUBLE PRECISION,
    "ram_usage" DOUBLE PRECISION,
    "disk_usage" DOUBLE PRECISION,
    "disk_io_read" DOUBLE PRECISION,
    "disk_io_write" DOUBLE PRECISION,
    "network_in" DOUBLE PRECISION,
    "network_out" DOUBLE PRECISION,
    "metadata" JSONB NOT NULL DEFAULT '{}',
    "checked_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "MonitoringAlertRule" (
    "id" TEXT NOT NULL,
    "name" TEXT NOT NULL,
    "description" TEXT,
    "tenant_id" TEXT,
    "vm_id" TEXT,
    "node_id" TEXT,
    "cpu_threshold_pct" DOUBLE PRECISION,
    "ram_threshold_pct" DOUBLE PRECISION,
    "disk_threshold_pct" DOUBLE PRECISION,
    "disk_io_read_threshold" DOUBLE PRECISION,
    "disk_io_write_threshold" DOUBLE PRECISION,
    "network_in_threshold" DOUBLE PRECISION,
    "network_out_threshold" DOUBLE PRECISION,
    "consecutive_breaches" INTEGER NOT NULL DEFAULT 1,
    "evaluation_window_minutes" INTEGER NOT NULL DEFAULT 15,
    "severity" "Severity" NOT NULL DEFAULT 'WARNING',
    "channels" JSONB NOT NULL DEFAULT '["IN_APP"]',
    "enabled" BOOLEAN NOT NULL DEFAULT true,
    "last_evaluated_at" TIMESTAMP(3),
    "created_by" TEXT,
    "metadata" JSONB NOT NULL DEFAULT '{}',
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "MonitoringAlertEvent" (
    "id" TEXT NOT NULL,
    "rule_id" TEXT NOT NULL,
    "tenant_id" TEXT,
    "vm_id" TEXT,
    "node_id" TEXT,
    "status" "MonitoringAlertStatus" NOT NULL DEFAULT 'OPEN',
    "severity" "Severity" NOT NULL DEFAULT 'WARNING',
    "title" TEXT NOT NULL,
    "message" TEXT,
    "metric_key" TEXT,
    "trigger_value" DOUBLE PRECISION,
    "threshold_value" DOUBLE PRECISION,
    "breach_count" INTEGER NOT NULL DEFAULT 1,
    "resolved_at" TIMESTAMP(3),
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "MonitoringAlertNotification" (
    "id" TEXT NOT NULL,
    "alert_event_id" TEXT NOT NULL,
    "channel" "AlertChannel" NOT NULL,
    "destination" TEXT,
    "status" "AlertDispatchStatus" NOT NULL DEFAULT 'QUEUED',
    "provider_message" TEXT,
    "sent_at" TIMESTAMP(3),
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "IpAddressPool" (
    "id" TEXT NOT NULL,
    "address" TEXT NOT NULL,
    "cidr" INTEGER NOT NULL,
    "version" "IpVersion" NOT NULL,
    "scope" "IpScope" NOT NULL DEFAULT 'PUBLIC',
    "status" "IpAddressStatus" NOT NULL DEFAULT 'AVAILABLE',
    "gateway" TEXT,
    "subnet" TEXT,
    "server" TEXT,
    "node_id" TEXT,
    "node_hostname" TEXT,
    "bridge" TEXT,
    "vlan_tag" INTEGER,
    "sdn_zone" TEXT,
    "tags" JSONB NOT NULL DEFAULT '[]',
    "metadata" JSONB NOT NULL DEFAULT '{}',
    "assigned_vm_id" TEXT,
    "assigned_tenant_id" TEXT,
    "imported_by" TEXT,
    "imported_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "assigned_at" TIMESTAMP(3),
    "returned_at" TIMESTAMP(3),
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "TenantIpQuota" (
    "id" TEXT NOT NULL,
    "tenant_id" TEXT NOT NULL,
    "ipv4_limit" INTEGER,
    "ipv6_limit" INTEGER,
    "reserved_ipv4" INTEGER NOT NULL DEFAULT 0,
    "reserved_ipv6" INTEGER NOT NULL DEFAULT 0,
    "burst_allowed" BOOLEAN NOT NULL DEFAULT false,
    "burst_ipv4_limit" INTEGER,
    "burst_ipv6_limit" INTEGER,
    "is_active" BOOLEAN NOT NULL DEFAULT true,
    "metadata" JSONB NOT NULL DEFAULT '{}',
    "created_by" TEXT,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "IpReservedRange" (
    "id" TEXT NOT NULL,
    "name" TEXT NOT NULL,
    "cidr" TEXT NOT NULL,
    "version" "IpVersion" NOT NULL,
    "scope" "IpScope" NOT NULL DEFAULT 'PUBLIC',
    "tenant_id" TEXT,
    "reason" TEXT,
    "node_hostname" TEXT,
    "bridge" TEXT,
    "vlan_tag" INTEGER,
    "sdn_zone" TEXT,
    "is_active" BOOLEAN NOT NULL DEFAULT true,
    "metadata" JSONB NOT NULL DEFAULT '{}',
    "created_by" TEXT,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "IpPoolPolicy" (
    "id" TEXT NOT NULL,
    "name" TEXT NOT NULL,
    "tenant_id" TEXT,
    "scope" "IpScope",
    "version" "IpVersion",
    "node_hostname" TEXT,
    "bridge" TEXT,
    "vlan_tag" INTEGER,
    "sdn_zone" TEXT,
    "allocation_strategy" "IpAllocationStrategy" NOT NULL DEFAULT 'BEST_FIT',
    "enforce_quota" BOOLEAN NOT NULL DEFAULT true,
    "disallow_reserved_use" BOOLEAN NOT NULL DEFAULT true,
    "is_active" BOOLEAN NOT NULL DEFAULT true,
    "priority" INTEGER NOT NULL DEFAULT 100,
    "metadata" JSONB NOT NULL DEFAULT '{}',
    "created_by" TEXT,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "IpAssignment" (
    "id" TEXT NOT NULL,
    "ip_address_id" TEXT NOT NULL,
    "vm_id" TEXT NOT NULL,
    "tenant_id" TEXT,
    "assignment_type" "IpAssignmentType" NOT NULL DEFAULT 'ADDITIONAL',
    "interface_name" TEXT,
    "notes" TEXT,
    "metadata" JSONB NOT NULL DEFAULT '{}',
    "assigned_by" TEXT,
    "assigned_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "released_at" TIMESTAMP(3),
    "is_active" BOOLEAN NOT NULL DEFAULT true,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "PrivateNetwork" (
    "id" TEXT NOT NULL,
    "name" TEXT NOT NULL,
    "slug" TEXT NOT NULL,
    "network_type" "PrivateNetworkType" NOT NULL DEFAULT 'VLAN',
    "cidr" TEXT NOT NULL,
    "gateway" TEXT,
    "bridge" TEXT,
    "vlan_tag" INTEGER,
    "sdn_zone" TEXT,
    "server" TEXT,
    "node_hostname" TEXT,
    "is_private" BOOLEAN NOT NULL DEFAULT true,
    "metadata" JSONB NOT NULL DEFAULT '{}',
    "created_by" TEXT,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "PrivateNetworkAttachment" (
    "id" TEXT NOT NULL,
    "network_id" TEXT NOT NULL,
    "vm_id" TEXT NOT NULL,
    "tenant_id" TEXT,
    "interface_name" TEXT,
    "requested_ip" TEXT,
    "status" "PrivateNetworkAttachmentStatus" NOT NULL DEFAULT 'ATTACHED',
    "metadata" JSONB NOT NULL DEFAULT '{}',
    "attached_by" TEXT,
    "attached_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "detached_at" TIMESTAMP(3),
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "AppTemplate" (
    "id" TEXT NOT NULL,
    "name" TEXT NOT NULL,
    "slug" TEXT NOT NULL,
    "template_type" "TemplateType" NOT NULL,
    "virtualization_type" "VmType",
    "source" TEXT,
    "description" TEXT,
    "default_cloud_init" TEXT,
    "metadata" JSONB NOT NULL DEFAULT '{}',
    "is_active" BOOLEAN NOT NULL DEFAULT true,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "ApplicationGroup" (
    "id" TEXT NOT NULL,
    "name" TEXT NOT NULL,
    "slug" TEXT NOT NULL,
    "description" TEXT,
    "is_active" BOOLEAN NOT NULL DEFAULT true,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "ApplicationGroupTemplate" (
    "id" TEXT NOT NULL,
    "group_id" TEXT NOT NULL,
    "template_id" TEXT NOT NULL,
    "priority" INTEGER NOT NULL DEFAULT 100,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "NodePlacementPolicy" (
    "id" TEXT NOT NULL,
    "group_id" TEXT,
    "node_id" TEXT,
    "product_type" "ProductType",
    "cpu_weight" INTEGER NOT NULL DEFAULT 40,
    "ram_weight" INTEGER NOT NULL DEFAULT 30,
    "disk_weight" INTEGER NOT NULL DEFAULT 20,
    "vm_count_weight" INTEGER NOT NULL DEFAULT 10,
    "max_vms" INTEGER,
    "min_free_ram_mb" INTEGER,
    "min_free_disk_gb" INTEGER,
    "is_active" BOOLEAN NOT NULL DEFAULT true,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "VmIdRange" (
    "id" TEXT NOT NULL,
    "node_id" TEXT,
    "node_hostname" TEXT NOT NULL,
    "application_group_id" TEXT,
    "range_start" INTEGER NOT NULL,
    "range_end" INTEGER NOT NULL,
    "next_vmid" INTEGER NOT NULL,
    "is_active" BOOLEAN NOT NULL DEFAULT true,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "OperationTask" (
    "id" TEXT NOT NULL,
    "task_type" "OperationTaskType" NOT NULL,
    "status" "OperationTaskStatus" NOT NULL DEFAULT 'QUEUED',
    "vm_id" TEXT,
    "vm_name" TEXT,
    "node" TEXT,
    "requested_by" TEXT,
    "payload" JSONB,
    "result" JSONB,
    "error_message" TEXT,
    "proxmox_upid" TEXT,
    "scheduled_for" TIMESTAMP(3),
    "started_at" TIMESTAMP(3),
    "completed_at" TIMESTAMP(3),
    "retry_count" INTEGER NOT NULL DEFAULT 0,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "PowerSchedule" (
    "id" TEXT NOT NULL,
    "vm_id" TEXT NOT NULL,
    "action" "PowerScheduleAction" NOT NULL,
    "cron_expression" TEXT NOT NULL,
    "timezone" TEXT NOT NULL DEFAULT 'UTC',
    "enabled" BOOLEAN NOT NULL DEFAULT true,
    "next_run_at" TIMESTAMP(3),
    "last_run_at" TIMESTAMP(3),
    "created_by" TEXT,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "ProvisionedService" (
    "id" TEXT NOT NULL,
    "service_group_id" TEXT,
    "vm_id" TEXT NOT NULL,
    "tenant_id" TEXT NOT NULL,
    "product_type" "ProductType" NOT NULL,
    "lifecycle_status" "ServiceLifecycleStatus" NOT NULL DEFAULT 'ACTIVE',
    "application_group_id" TEXT,
    "template_id" TEXT,
    "package_options" JSONB NOT NULL DEFAULT '{}',
    "suspended_reason" TEXT,
    "terminated_at" TIMESTAMP(3),
    "created_by" TEXT,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "Invoice" (
    "id" TEXT NOT NULL,
    "invoice_number" TEXT NOT NULL,
    "tenant_id" TEXT NOT NULL,
    "tenant_name" TEXT,
    "status" "InvoiceStatus" NOT NULL DEFAULT 'PENDING',
    "amount" DECIMAL(14,2) NOT NULL,
    "currency" "Currency" NOT NULL DEFAULT 'NGN',
    "due_date" TIMESTAMP(3) NOT NULL,
    "paid_date" TIMESTAMP(3),
    "payment_provider" "PaymentProvider" NOT NULL DEFAULT 'MANUAL',
    "payment_reference" TEXT,
    "payment_url" TEXT,
    "line_items" JSONB NOT NULL DEFAULT '[]',
    "notes" TEXT,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "UsageRecord" (
    "id" TEXT NOT NULL,
    "vm_id" TEXT NOT NULL,
    "vm_name" TEXT NOT NULL,
    "tenant_id" TEXT NOT NULL,
    "tenant_name" TEXT NOT NULL,
    "billing_plan_id" TEXT,
    "plan_name" TEXT,
    "hours_used" DECIMAL(8,2) NOT NULL DEFAULT 1,
    "price_per_hour" DECIMAL(12,4) NOT NULL,
    "currency" "Currency" NOT NULL DEFAULT 'NGN',
    "total_cost" DECIMAL(14,4) NOT NULL,
    "period_start" TIMESTAMP(3) NOT NULL,
    "period_end" TIMESTAMP(3) NOT NULL,
    "billed" BOOLEAN NOT NULL DEFAULT false,
    "invoice_id" TEXT,
    "cpu_hours" DECIMAL(10,4),
    "ram_gb_hours" DECIMAL(10,4),
    "disk_gb_hours" DECIMAL(10,4),
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "Backup" (
    "id" TEXT NOT NULL,
    "vm_id" TEXT NOT NULL,
    "vm_name" TEXT NOT NULL,
    "tenant_id" TEXT,
    "node" TEXT,
    "status" "BackupStatus" NOT NULL DEFAULT 'PENDING',
    "type" "BackupType" NOT NULL DEFAULT 'FULL',
    "source" "BackupSource" NOT NULL DEFAULT 'LOCAL',
    "size_mb" DOUBLE PRECISION,
    "storage" TEXT,
    "backup_path" TEXT,
    "pbs_snapshot_id" TEXT,
    "route_key" TEXT,
    "is_protected" BOOLEAN NOT NULL DEFAULT false,
    "restore_enabled" BOOLEAN NOT NULL DEFAULT true,
    "total_files" INTEGER,
    "schedule" "BackupSchedule" NOT NULL DEFAULT 'MANUAL',
    "retention_days" INTEGER NOT NULL DEFAULT 7,
    "snapshot_job_id" TEXT,
    "started_at" TIMESTAMP(3),
    "completed_at" TIMESTAMP(3),
    "next_run_at" TIMESTAMP(3),
    "expires_at" TIMESTAMP(3),
    "notes" TEXT,
    "proxmox_upid" TEXT,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "BackupPolicy" (
    "id" TEXT NOT NULL,
    "tenant_id" TEXT,
    "billing_plan_id" TEXT,
    "max_files" INTEGER NOT NULL DEFAULT 10,
    "max_total_size_mb" DOUBLE PRECISION NOT NULL DEFAULT 51200,
    "max_protected_files" INTEGER NOT NULL DEFAULT 3,
    "allow_file_restore" BOOLEAN NOT NULL DEFAULT true,
    "allow_cross_vm_restore" BOOLEAN NOT NULL DEFAULT true,
    "allow_pbs_restore" BOOLEAN NOT NULL DEFAULT true,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "BackupRestoreTask" (
    "id" TEXT NOT NULL,
    "backup_id" TEXT NOT NULL,
    "source_vm_id" TEXT NOT NULL,
    "target_vm_id" TEXT NOT NULL,
    "mode" "BackupRestoreMode" NOT NULL,
    "requested_files" JSONB NOT NULL DEFAULT '[]',
    "pbs_enabled" BOOLEAN NOT NULL DEFAULT false,
    "status" "BackupRestoreStatus" NOT NULL DEFAULT 'PENDING',
    "result_message" TEXT,
    "started_at" TIMESTAMP(3),
    "completed_at" TIMESTAMP(3),
    "created_by" TEXT,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "SnapshotJob" (
    "id" TEXT NOT NULL,
    "vm_id" TEXT NOT NULL,
    "name" TEXT NOT NULL,
    "frequency" "SnapshotFrequency" NOT NULL DEFAULT 'DAILY',
    "interval" INTEGER NOT NULL DEFAULT 1,
    "day_of_week" INTEGER,
    "hour_utc" INTEGER NOT NULL DEFAULT 2,
    "minute_utc" INTEGER NOT NULL DEFAULT 0,
    "retention" INTEGER NOT NULL DEFAULT 7,
    "enabled" BOOLEAN NOT NULL DEFAULT true,
    "next_run_at" TIMESTAMP(3),
    "last_run_at" TIMESTAMP(3),
    "created_by" TEXT,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "AuditLog" (
    "id" TEXT NOT NULL,
    "action" TEXT NOT NULL,
    "resource_type" "ResourceType" NOT NULL,
    "resource_id" TEXT,
    "resource_name" TEXT,
    "actor_email" TEXT NOT NULL,
    "actor_role" TEXT,
    "severity" "Severity" NOT NULL DEFAULT 'INFO',
    "details" JSONB,
    "ip_address" TEXT,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "SecurityEvent" (
    "id" TEXT NOT NULL,
    "event_type" TEXT NOT NULL,
    "severity" "Severity" NOT NULL DEFAULT 'WARNING',
    "status" "SecurityStatus" NOT NULL DEFAULT 'OPEN',
    "source_ip" TEXT,
    "source_country" TEXT,
    "target_vm_id" TEXT,
    "node" TEXT,
    "description" TEXT,
    "details" JSONB,
    "resolved_at" TIMESTAMP(3),
    "resolved_by" TEXT,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "FirewallRule" (
    "id" TEXT NOT NULL,
    "name" TEXT NOT NULL,
    "direction" "Direction" NOT NULL DEFAULT 'INBOUND',
    "action" "FirewallAction" NOT NULL DEFAULT 'DENY',
    "protocol" "Protocol" NOT NULL DEFAULT 'TCP',
    "source_ip" TEXT,
    "destination_ip" TEXT,
    "port_range" TEXT,
    "priority" INTEGER NOT NULL DEFAULT 100,
    "enabled" BOOLEAN NOT NULL DEFAULT true,
    "applies_to" "AppliesTo" NOT NULL DEFAULT 'ALL_VMS',
    "target_id" TEXT,
    "hit_count" INTEGER NOT NULL DEFAULT 0,
    "description" TEXT,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "Setting" (
    "id" TEXT NOT NULL,
    "key" TEXT NOT NULL,
    "type" "SettingType" NOT NULL DEFAULT 'GENERAL',
    "value" JSONB NOT NULL,
    "is_encrypted" BOOLEAN NOT NULL DEFAULT false,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,

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

-- CreateIndex
CREATE UNIQUE INDEX "User_email_key" ON "User"("email");

-- CreateIndex
CREATE INDEX "User_tenant_id_idx" ON "User"("tenant_id");

-- CreateIndex
CREATE UNIQUE INDEX "Tenant_slug_key" ON "Tenant"("slug");

-- CreateIndex
CREATE INDEX "Tenant_status_idx" ON "Tenant"("status");

-- CreateIndex
CREATE INDEX "Tenant_owner_email_idx" ON "Tenant"("owner_email");

-- CreateIndex
CREATE UNIQUE INDEX "ProxmoxNode_hostname_key" ON "ProxmoxNode"("hostname");

-- CreateIndex
CREATE INDEX "ProxmoxNode_status_idx" ON "ProxmoxNode"("status");

-- CreateIndex
CREATE UNIQUE INDEX "BillingPlan_slug_key" ON "BillingPlan"("slug");

-- CreateIndex
CREATE INDEX "BillingPlan_is_active_idx" ON "BillingPlan"("is_active");

-- CreateIndex
CREATE INDEX "VirtualMachine_tenant_id_idx" ON "VirtualMachine"("tenant_id");

-- CreateIndex
CREATE INDEX "VirtualMachine_node_idx" ON "VirtualMachine"("node");

-- CreateIndex
CREATE INDEX "VirtualMachine_status_idx" ON "VirtualMachine"("status");

-- CreateIndex
CREATE UNIQUE INDEX "VirtualMachine_vmid_node_key" ON "VirtualMachine"("vmid", "node");

-- CreateIndex
CREATE INDEX "ServerHealthCheck_enabled_next_run_at_idx" ON "ServerHealthCheck"("enabled", "next_run_at");

-- CreateIndex
CREATE INDEX "ServerHealthCheck_tenant_id_enabled_idx" ON "ServerHealthCheck"("tenant_id", "enabled");

-- CreateIndex
CREATE INDEX "ServerHealthCheck_vm_id_enabled_idx" ON "ServerHealthCheck"("vm_id", "enabled");

-- CreateIndex
CREATE INDEX "ServerHealthCheck_node_id_enabled_idx" ON "ServerHealthCheck"("node_id", "enabled");

-- CreateIndex
CREATE INDEX "ServerHealthCheckResult_check_id_checked_at_idx" ON "ServerHealthCheckResult"("check_id", "checked_at");

-- CreateIndex
CREATE INDEX "ServerHealthCheckResult_status_checked_at_idx" ON "ServerHealthCheckResult"("status", "checked_at");

-- CreateIndex
CREATE INDEX "MonitoringAlertRule_enabled_severity_idx" ON "MonitoringAlertRule"("enabled", "severity");

-- CreateIndex
CREATE INDEX "MonitoringAlertRule_tenant_id_enabled_idx" ON "MonitoringAlertRule"("tenant_id", "enabled");

-- CreateIndex
CREATE INDEX "MonitoringAlertRule_vm_id_enabled_idx" ON "MonitoringAlertRule"("vm_id", "enabled");

-- CreateIndex
CREATE INDEX "MonitoringAlertRule_node_id_enabled_idx" ON "MonitoringAlertRule"("node_id", "enabled");

-- CreateIndex
CREATE INDEX "MonitoringAlertEvent_rule_id_status_idx" ON "MonitoringAlertEvent"("rule_id", "status");

-- CreateIndex
CREATE INDEX "MonitoringAlertEvent_status_severity_created_at_idx" ON "MonitoringAlertEvent"("status", "severity", "created_at");

-- CreateIndex
CREATE INDEX "MonitoringAlertEvent_tenant_id_status_idx" ON "MonitoringAlertEvent"("tenant_id", "status");

-- CreateIndex
CREATE INDEX "MonitoringAlertEvent_vm_id_status_idx" ON "MonitoringAlertEvent"("vm_id", "status");

-- CreateIndex
CREATE INDEX "MonitoringAlertEvent_node_id_status_idx" ON "MonitoringAlertEvent"("node_id", "status");

-- CreateIndex
CREATE INDEX "MonitoringAlertNotification_alert_event_id_status_idx" ON "MonitoringAlertNotification"("alert_event_id", "status");

-- CreateIndex
CREATE INDEX "MonitoringAlertNotification_status_created_at_idx" ON "MonitoringAlertNotification"("status", "created_at");

-- CreateIndex
CREATE INDEX "IpAddressPool_status_scope_version_idx" ON "IpAddressPool"("status", "scope", "version");

-- CreateIndex
CREATE INDEX "IpAddressPool_node_hostname_bridge_vlan_tag_idx" ON "IpAddressPool"("node_hostname", "bridge", "vlan_tag");

-- CreateIndex
CREATE INDEX "IpAddressPool_assigned_vm_id_status_idx" ON "IpAddressPool"("assigned_vm_id", "status");

-- CreateIndex
CREATE INDEX "IpAddressPool_assigned_tenant_id_status_idx" ON "IpAddressPool"("assigned_tenant_id", "status");

-- CreateIndex
CREATE UNIQUE INDEX "IpAddressPool_address_cidr_key" ON "IpAddressPool"("address", "cidr");

-- CreateIndex
CREATE UNIQUE INDEX "TenantIpQuota_tenant_id_key" ON "TenantIpQuota"("tenant_id");

-- CreateIndex
CREATE INDEX "TenantIpQuota_is_active_idx" ON "TenantIpQuota"("is_active");

-- CreateIndex
CREATE INDEX "IpReservedRange_scope_version_is_active_idx" ON "IpReservedRange"("scope", "version", "is_active");

-- CreateIndex
CREATE INDEX "IpReservedRange_tenant_id_is_active_idx" ON "IpReservedRange"("tenant_id", "is_active");

-- CreateIndex
CREATE INDEX "IpReservedRange_node_hostname_bridge_vlan_tag_is_active_idx" ON "IpReservedRange"("node_hostname", "bridge", "vlan_tag", "is_active");

-- CreateIndex
CREATE INDEX "IpPoolPolicy_tenant_id_is_active_priority_idx" ON "IpPoolPolicy"("tenant_id", "is_active", "priority");

-- CreateIndex
CREATE INDEX "IpPoolPolicy_scope_version_is_active_idx" ON "IpPoolPolicy"("scope", "version", "is_active");

-- CreateIndex
CREATE INDEX "IpPoolPolicy_node_hostname_bridge_vlan_tag_is_active_idx" ON "IpPoolPolicy"("node_hostname", "bridge", "vlan_tag", "is_active");

-- CreateIndex
CREATE INDEX "IpAssignment_ip_address_id_is_active_idx" ON "IpAssignment"("ip_address_id", "is_active");

-- CreateIndex
CREATE INDEX "IpAssignment_vm_id_is_active_idx" ON "IpAssignment"("vm_id", "is_active");

-- CreateIndex
CREATE INDEX "IpAssignment_tenant_id_is_active_idx" ON "IpAssignment"("tenant_id", "is_active");

-- CreateIndex
CREATE UNIQUE INDEX "PrivateNetwork_slug_key" ON "PrivateNetwork"("slug");

-- CreateIndex
CREATE INDEX "PrivateNetwork_network_type_idx" ON "PrivateNetwork"("network_type");

-- CreateIndex
CREATE INDEX "PrivateNetwork_node_hostname_bridge_vlan_tag_idx" ON "PrivateNetwork"("node_hostname", "bridge", "vlan_tag");

-- CreateIndex
CREATE INDEX "PrivateNetworkAttachment_vm_id_status_idx" ON "PrivateNetworkAttachment"("vm_id", "status");

-- CreateIndex
CREATE INDEX "PrivateNetworkAttachment_tenant_id_status_idx" ON "PrivateNetworkAttachment"("tenant_id", "status");

-- CreateIndex
CREATE INDEX "PrivateNetworkAttachment_network_id_status_idx" ON "PrivateNetworkAttachment"("network_id", "status");

-- CreateIndex
CREATE UNIQUE INDEX "PrivateNetworkAttachment_network_id_vm_id_interface_name_key" ON "PrivateNetworkAttachment"("network_id", "vm_id", "interface_name");

-- CreateIndex
CREATE UNIQUE INDEX "AppTemplate_slug_key" ON "AppTemplate"("slug");

-- CreateIndex
CREATE INDEX "AppTemplate_template_type_is_active_idx" ON "AppTemplate"("template_type", "is_active");

-- CreateIndex
CREATE UNIQUE INDEX "ApplicationGroup_slug_key" ON "ApplicationGroup"("slug");

-- CreateIndex
CREATE INDEX "ApplicationGroup_is_active_idx" ON "ApplicationGroup"("is_active");

-- CreateIndex
CREATE INDEX "ApplicationGroupTemplate_priority_idx" ON "ApplicationGroupTemplate"("priority");

-- CreateIndex
CREATE UNIQUE INDEX "ApplicationGroupTemplate_group_id_template_id_key" ON "ApplicationGroupTemplate"("group_id", "template_id");

-- CreateIndex
CREATE INDEX "NodePlacementPolicy_group_id_is_active_idx" ON "NodePlacementPolicy"("group_id", "is_active");

-- CreateIndex
CREATE INDEX "NodePlacementPolicy_node_id_is_active_idx" ON "NodePlacementPolicy"("node_id", "is_active");

-- CreateIndex
CREATE INDEX "NodePlacementPolicy_product_type_is_active_idx" ON "NodePlacementPolicy"("product_type", "is_active");

-- CreateIndex
CREATE INDEX "VmIdRange_node_hostname_is_active_idx" ON "VmIdRange"("node_hostname", "is_active");

-- CreateIndex
CREATE INDEX "VmIdRange_application_group_id_is_active_idx" ON "VmIdRange"("application_group_id", "is_active");

-- CreateIndex
CREATE UNIQUE INDEX "VmIdRange_node_hostname_range_start_range_end_key" ON "VmIdRange"("node_hostname", "range_start", "range_end");

-- CreateIndex
CREATE INDEX "OperationTask_status_idx" ON "OperationTask"("status");

-- CreateIndex
CREATE INDEX "OperationTask_task_type_idx" ON "OperationTask"("task_type");

-- CreateIndex
CREATE INDEX "OperationTask_vm_id_idx" ON "OperationTask"("vm_id");

-- CreateIndex
CREATE INDEX "OperationTask_created_at_idx" ON "OperationTask"("created_at");

-- CreateIndex
CREATE INDEX "PowerSchedule_vm_id_idx" ON "PowerSchedule"("vm_id");

-- CreateIndex
CREATE INDEX "PowerSchedule_enabled_next_run_at_idx" ON "PowerSchedule"("enabled", "next_run_at");

-- CreateIndex
CREATE UNIQUE INDEX "ProvisionedService_vm_id_key" ON "ProvisionedService"("vm_id");

-- CreateIndex
CREATE INDEX "ProvisionedService_tenant_id_lifecycle_status_idx" ON "ProvisionedService"("tenant_id", "lifecycle_status");

-- CreateIndex
CREATE INDEX "ProvisionedService_service_group_id_idx" ON "ProvisionedService"("service_group_id");

-- CreateIndex
CREATE INDEX "ProvisionedService_application_group_id_idx" ON "ProvisionedService"("application_group_id");

-- CreateIndex
CREATE UNIQUE INDEX "Invoice_invoice_number_key" ON "Invoice"("invoice_number");

-- CreateIndex
CREATE INDEX "Invoice_tenant_id_idx" ON "Invoice"("tenant_id");

-- CreateIndex
CREATE INDEX "Invoice_status_idx" ON "Invoice"("status");

-- CreateIndex
CREATE INDEX "Invoice_due_date_idx" ON "Invoice"("due_date");

-- CreateIndex
CREATE INDEX "UsageRecord_vm_id_idx" ON "UsageRecord"("vm_id");

-- CreateIndex
CREATE INDEX "UsageRecord_tenant_id_idx" ON "UsageRecord"("tenant_id");

-- CreateIndex
CREATE INDEX "UsageRecord_period_start_idx" ON "UsageRecord"("period_start");

-- CreateIndex
CREATE INDEX "UsageRecord_billed_idx" ON "UsageRecord"("billed");

-- CreateIndex
CREATE UNIQUE INDEX "UsageRecord_vm_id_period_start_period_end_key" ON "UsageRecord"("vm_id", "period_start", "period_end");

-- CreateIndex
CREATE INDEX "Backup_vm_id_idx" ON "Backup"("vm_id");

-- CreateIndex
CREATE INDEX "Backup_tenant_id_idx" ON "Backup"("tenant_id");

-- CreateIndex
CREATE INDEX "Backup_status_idx" ON "Backup"("status");

-- CreateIndex
CREATE INDEX "Backup_next_run_at_idx" ON "Backup"("next_run_at");

-- CreateIndex
CREATE INDEX "Backup_snapshot_job_id_idx" ON "Backup"("snapshot_job_id");

-- CreateIndex
CREATE INDEX "BackupPolicy_tenant_id_idx" ON "BackupPolicy"("tenant_id");

-- CreateIndex
CREATE INDEX "BackupPolicy_billing_plan_id_idx" ON "BackupPolicy"("billing_plan_id");

-- CreateIndex
CREATE INDEX "BackupRestoreTask_backup_id_idx" ON "BackupRestoreTask"("backup_id");

-- CreateIndex
CREATE INDEX "BackupRestoreTask_source_vm_id_idx" ON "BackupRestoreTask"("source_vm_id");

-- CreateIndex
CREATE INDEX "BackupRestoreTask_target_vm_id_idx" ON "BackupRestoreTask"("target_vm_id");

-- CreateIndex
CREATE INDEX "BackupRestoreTask_status_idx" ON "BackupRestoreTask"("status");

-- CreateIndex
CREATE INDEX "SnapshotJob_vm_id_idx" ON "SnapshotJob"("vm_id");

-- CreateIndex
CREATE INDEX "SnapshotJob_enabled_next_run_at_idx" ON "SnapshotJob"("enabled", "next_run_at");

-- CreateIndex
CREATE INDEX "AuditLog_resource_type_idx" ON "AuditLog"("resource_type");

-- CreateIndex
CREATE INDEX "AuditLog_severity_idx" ON "AuditLog"("severity");

-- CreateIndex
CREATE INDEX "AuditLog_created_at_idx" ON "AuditLog"("created_at");

-- CreateIndex
CREATE INDEX "SecurityEvent_status_idx" ON "SecurityEvent"("status");

-- CreateIndex
CREATE INDEX "SecurityEvent_severity_idx" ON "SecurityEvent"("severity");

-- CreateIndex
CREATE INDEX "SecurityEvent_created_at_idx" ON "SecurityEvent"("created_at");

-- CreateIndex
CREATE INDEX "FirewallRule_enabled_idx" ON "FirewallRule"("enabled");

-- CreateIndex
CREATE INDEX "FirewallRule_priority_idx" ON "FirewallRule"("priority");

-- CreateIndex
CREATE UNIQUE INDEX "Setting_key_key" ON "Setting"("key");

-- AddForeignKey
ALTER TABLE "User" ADD CONSTRAINT "User_tenant_id_fkey" FOREIGN KEY ("tenant_id") REFERENCES "Tenant"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "VirtualMachine" ADD CONSTRAINT "VirtualMachine_tenant_id_fkey" FOREIGN KEY ("tenant_id") REFERENCES "Tenant"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "VirtualMachine" ADD CONSTRAINT "VirtualMachine_node_id_fkey" FOREIGN KEY ("node_id") REFERENCES "ProxmoxNode"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "VirtualMachine" ADD CONSTRAINT "VirtualMachine_billing_plan_id_fkey" FOREIGN KEY ("billing_plan_id") REFERENCES "BillingPlan"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "ServerHealthCheck" ADD CONSTRAINT "ServerHealthCheck_tenant_id_fkey" FOREIGN KEY ("tenant_id") REFERENCES "Tenant"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "ServerHealthCheck" ADD CONSTRAINT "ServerHealthCheck_vm_id_fkey" FOREIGN KEY ("vm_id") REFERENCES "VirtualMachine"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "ServerHealthCheck" ADD CONSTRAINT "ServerHealthCheck_node_id_fkey" FOREIGN KEY ("node_id") REFERENCES "ProxmoxNode"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "ServerHealthCheckResult" ADD CONSTRAINT "ServerHealthCheckResult_check_id_fkey" FOREIGN KEY ("check_id") REFERENCES "ServerHealthCheck"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "MonitoringAlertRule" ADD CONSTRAINT "MonitoringAlertRule_tenant_id_fkey" FOREIGN KEY ("tenant_id") REFERENCES "Tenant"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "MonitoringAlertRule" ADD CONSTRAINT "MonitoringAlertRule_vm_id_fkey" FOREIGN KEY ("vm_id") REFERENCES "VirtualMachine"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "MonitoringAlertRule" ADD CONSTRAINT "MonitoringAlertRule_node_id_fkey" FOREIGN KEY ("node_id") REFERENCES "ProxmoxNode"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "MonitoringAlertEvent" ADD CONSTRAINT "MonitoringAlertEvent_rule_id_fkey" FOREIGN KEY ("rule_id") REFERENCES "MonitoringAlertRule"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "MonitoringAlertEvent" ADD CONSTRAINT "MonitoringAlertEvent_tenant_id_fkey" FOREIGN KEY ("tenant_id") REFERENCES "Tenant"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "MonitoringAlertEvent" ADD CONSTRAINT "MonitoringAlertEvent_vm_id_fkey" FOREIGN KEY ("vm_id") REFERENCES "VirtualMachine"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "MonitoringAlertEvent" ADD CONSTRAINT "MonitoringAlertEvent_node_id_fkey" FOREIGN KEY ("node_id") REFERENCES "ProxmoxNode"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "MonitoringAlertNotification" ADD CONSTRAINT "MonitoringAlertNotification_alert_event_id_fkey" FOREIGN KEY ("alert_event_id") REFERENCES "MonitoringAlertEvent"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "IpAddressPool" ADD CONSTRAINT "IpAddressPool_assigned_vm_id_fkey" FOREIGN KEY ("assigned_vm_id") REFERENCES "VirtualMachine"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "IpAddressPool" ADD CONSTRAINT "IpAddressPool_assigned_tenant_id_fkey" FOREIGN KEY ("assigned_tenant_id") REFERENCES "Tenant"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "TenantIpQuota" ADD CONSTRAINT "TenantIpQuota_tenant_id_fkey" FOREIGN KEY ("tenant_id") REFERENCES "Tenant"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "IpReservedRange" ADD CONSTRAINT "IpReservedRange_tenant_id_fkey" FOREIGN KEY ("tenant_id") REFERENCES "Tenant"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "IpPoolPolicy" ADD CONSTRAINT "IpPoolPolicy_tenant_id_fkey" FOREIGN KEY ("tenant_id") REFERENCES "Tenant"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "IpAssignment" ADD CONSTRAINT "IpAssignment_ip_address_id_fkey" FOREIGN KEY ("ip_address_id") REFERENCES "IpAddressPool"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "IpAssignment" ADD CONSTRAINT "IpAssignment_vm_id_fkey" FOREIGN KEY ("vm_id") REFERENCES "VirtualMachine"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "IpAssignment" ADD CONSTRAINT "IpAssignment_tenant_id_fkey" FOREIGN KEY ("tenant_id") REFERENCES "Tenant"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "PrivateNetworkAttachment" ADD CONSTRAINT "PrivateNetworkAttachment_network_id_fkey" FOREIGN KEY ("network_id") REFERENCES "PrivateNetwork"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "PrivateNetworkAttachment" ADD CONSTRAINT "PrivateNetworkAttachment_vm_id_fkey" FOREIGN KEY ("vm_id") REFERENCES "VirtualMachine"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "PrivateNetworkAttachment" ADD CONSTRAINT "PrivateNetworkAttachment_tenant_id_fkey" FOREIGN KEY ("tenant_id") REFERENCES "Tenant"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "ApplicationGroupTemplate" ADD CONSTRAINT "ApplicationGroupTemplate_group_id_fkey" FOREIGN KEY ("group_id") REFERENCES "ApplicationGroup"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "ApplicationGroupTemplate" ADD CONSTRAINT "ApplicationGroupTemplate_template_id_fkey" FOREIGN KEY ("template_id") REFERENCES "AppTemplate"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "NodePlacementPolicy" ADD CONSTRAINT "NodePlacementPolicy_group_id_fkey" FOREIGN KEY ("group_id") REFERENCES "ApplicationGroup"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "NodePlacementPolicy" ADD CONSTRAINT "NodePlacementPolicy_node_id_fkey" FOREIGN KEY ("node_id") REFERENCES "ProxmoxNode"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "VmIdRange" ADD CONSTRAINT "VmIdRange_node_id_fkey" FOREIGN KEY ("node_id") REFERENCES "ProxmoxNode"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "VmIdRange" ADD CONSTRAINT "VmIdRange_application_group_id_fkey" FOREIGN KEY ("application_group_id") REFERENCES "ApplicationGroup"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "OperationTask" ADD CONSTRAINT "OperationTask_vm_id_fkey" FOREIGN KEY ("vm_id") REFERENCES "VirtualMachine"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "PowerSchedule" ADD CONSTRAINT "PowerSchedule_vm_id_fkey" FOREIGN KEY ("vm_id") REFERENCES "VirtualMachine"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "ProvisionedService" ADD CONSTRAINT "ProvisionedService_vm_id_fkey" FOREIGN KEY ("vm_id") REFERENCES "VirtualMachine"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "ProvisionedService" ADD CONSTRAINT "ProvisionedService_tenant_id_fkey" FOREIGN KEY ("tenant_id") REFERENCES "Tenant"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "ProvisionedService" ADD CONSTRAINT "ProvisionedService_application_group_id_fkey" FOREIGN KEY ("application_group_id") REFERENCES "ApplicationGroup"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "ProvisionedService" ADD CONSTRAINT "ProvisionedService_template_id_fkey" FOREIGN KEY ("template_id") REFERENCES "AppTemplate"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "Invoice" ADD CONSTRAINT "Invoice_tenant_id_fkey" FOREIGN KEY ("tenant_id") REFERENCES "Tenant"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "UsageRecord" ADD CONSTRAINT "UsageRecord_vm_id_fkey" FOREIGN KEY ("vm_id") REFERENCES "VirtualMachine"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "UsageRecord" ADD CONSTRAINT "UsageRecord_tenant_id_fkey" FOREIGN KEY ("tenant_id") REFERENCES "Tenant"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "UsageRecord" ADD CONSTRAINT "UsageRecord_billing_plan_id_fkey" FOREIGN KEY ("billing_plan_id") REFERENCES "BillingPlan"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "UsageRecord" ADD CONSTRAINT "UsageRecord_invoice_id_fkey" FOREIGN KEY ("invoice_id") REFERENCES "Invoice"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "Backup" ADD CONSTRAINT "Backup_vm_id_fkey" FOREIGN KEY ("vm_id") REFERENCES "VirtualMachine"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "Backup" ADD CONSTRAINT "Backup_tenant_id_fkey" FOREIGN KEY ("tenant_id") REFERENCES "Tenant"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "Backup" ADD CONSTRAINT "Backup_snapshot_job_id_fkey" FOREIGN KEY ("snapshot_job_id") REFERENCES "SnapshotJob"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "BackupPolicy" ADD CONSTRAINT "BackupPolicy_tenant_id_fkey" FOREIGN KEY ("tenant_id") REFERENCES "Tenant"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "BackupPolicy" ADD CONSTRAINT "BackupPolicy_billing_plan_id_fkey" FOREIGN KEY ("billing_plan_id") REFERENCES "BillingPlan"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "BackupRestoreTask" ADD CONSTRAINT "BackupRestoreTask_backup_id_fkey" FOREIGN KEY ("backup_id") REFERENCES "Backup"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "BackupRestoreTask" ADD CONSTRAINT "BackupRestoreTask_source_vm_id_fkey" FOREIGN KEY ("source_vm_id") REFERENCES "VirtualMachine"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "BackupRestoreTask" ADD CONSTRAINT "BackupRestoreTask_target_vm_id_fkey" FOREIGN KEY ("target_vm_id") REFERENCES "VirtualMachine"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "SnapshotJob" ADD CONSTRAINT "SnapshotJob_vm_id_fkey" FOREIGN KEY ("vm_id") REFERENCES "VirtualMachine"("id") ON DELETE CASCADE ON UPDATE CASCADE;

