<?php

declare(strict_types=1);

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

return new class extends Migration
{
    public function up(): void
    {
        // ─── 1. packages tablosuna cgroup limit kolonları ──────────────────
        Schema::table('packages', function (Blueprint $table) {
            if (! Schema::hasColumn('packages', 'cpu_limit_percent')) {
                $table->integer('cpu_limit_percent')->default(100)->after('sort_order'); // % of one CPU core
            }
            if (! Schema::hasColumn('packages', 'memory_limit_mb')) {
                $table->integer('memory_limit_mb')->default(1024)->after('cpu_limit_percent');
            }
            if (! Schema::hasColumn('packages', 'io_read_mbps')) {
                $table->integer('io_read_mbps')->default(50)->after('memory_limit_mb');
            }
            if (! Schema::hasColumn('packages', 'io_write_mbps')) {
                $table->integer('io_write_mbps')->default(30)->after('io_read_mbps');
            }
            if (! Schema::hasColumn('packages', 'entry_processes_limit')) {
                $table->integer('entry_processes_limit')->default(20)->after('io_write_mbps'); // max concurrent entry processes
            }
            if (! Schema::hasColumn('packages', 'nproc_limit')) {
                $table->integer('nproc_limit')->default(100)->after('entry_processes_limit'); // total process count
            }
        });

        // ─── 2. Per-account custom overrides ──────────────────────────────
        Schema::create('account_resource_overrides', function (Blueprint $table) {
            $table->id();
            $table->foreignId('account_id')->constrained('accounts')->cascadeOnDelete()->unique();
            $table->integer('cpu_limit_percent')->nullable();
            $table->integer('memory_limit_mb')->nullable();
            $table->integer('io_read_mbps')->nullable();
            $table->integer('io_write_mbps')->nullable();
            $table->integer('entry_processes_limit')->nullable();
            $table->integer('nproc_limit')->nullable();
            $table->string('reason', 500)->nullable();
            $table->foreignId('set_by')->nullable()->constrained('users')->nullOnDelete();
            $table->timestamp('expires_at')->nullable();
            $table->timestamps();
        });

        // ─── 3. Resource fault log — append-only ──────────────────────────
        Schema::create('account_resource_faults', function (Blueprint $table) {
            $table->id();
            $table->foreignId('account_id')->constrained('accounts')->cascadeOnDelete();
            $table->enum('fault_type', [
                'cpu_limit', 'memory_oom', 'io_limit', 'pmem_limit', 'ep_limit', 'nproc_limit',
            ]);
            $table->integer('limit_value');
            $table->integer('actual_value')->nullable();
            $table->json('context')->nullable();
            $table->timestamp('occurred_at');
            $table->timestamps();

            $table->index(['account_id', 'occurred_at']);
            $table->index(['fault_type', 'occurred_at']);
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('account_resource_faults');
        Schema::dropIfExists('account_resource_overrides');

        Schema::table('packages', function (Blueprint $table) {
            foreach (['cpu_limit_percent', 'memory_limit_mb', 'io_read_mbps', 'io_write_mbps', 'entry_processes_limit', 'nproc_limit'] as $col) {
                if (Schema::hasColumn('packages', $col)) {
                    $table->dropColumn($col);
                }
            }
        });
    }
};
