<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

/**
 * System-level (host) metrik geçmişi. Per-account resource_history'den ayrı tutulur
 * çünkü buradaki kayıtların FK ilişkisi yok — host'un anlık /proc snapshot'ı.
 *
 * Kaynak: SystemMetrics::snapshotSystemLevel() — her 5dk cron'la yazılır.
 * Tüketici: Admin/Health (24h grafik), Admin/Monitoring (live overview).
 */
return new class extends Migration
{
    public function up(): void
    {
        Schema::create('system_metrics_history', function (Blueprint $table) {
            $table->id();
            $table->decimal('cpu_percent', 5, 2)->default(0);
            $table->unsignedBigInteger('memory_bytes')->default(0);
            $table->decimal('memory_percent', 5, 2)->default(0);
            $table->decimal('load_1m', 6, 2)->default(0);
            $table->decimal('load_5m', 6, 2)->default(0);
            $table->decimal('load_15m', 6, 2)->default(0);
            $table->unsignedBigInteger('network_rx_bytes')->default(0)->comment('Cumulative since boot');
            $table->unsignedBigInteger('network_tx_bytes')->default(0)->comment('Cumulative since boot');
            $table->timestamp('recorded_at')->useCurrent();
            $table->timestamp('created_at')->useCurrent();

            $table->index('recorded_at');
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('system_metrics_history');
    }
};
