<?php

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

/**
 * Backs the "Live Server Status", "Online Players" and "Discord Logs" admin
 * pages. Populated by routes/api.php (ServerHeartbeatController,
 * DiscordRoleSyncController) — see Phase 7.
 */
return new class extends Migration
{
    public function up(): void
    {
        Schema::create('server_heartbeats', function (Blueprint $table) {
            $table->id();
            $table->foreignId('fivem_server_id')->constrained()->cascadeOnDelete();
            $table->unsignedInteger('player_count');
            $table->unsignedInteger('max_players');
            $table->json('resources')->nullable();
            $table->timestamp('recorded_at');
        });

        Schema::create('online_players', function (Blueprint $table) {
            $table->id();
            $table->foreignId('fivem_server_id')->constrained()->cascadeOnDelete();
            $table->string('citizen_id')->nullable();
            $table->string('name');
            $table->unsignedInteger('source_id');
            $table->timestamp('updated_at');

            $table->unique(['fivem_server_id', 'source_id']);
        });

        Schema::create('discord_logs', function (Blueprint $table) {
            $table->id();
            $table->string('type');
            $table->text('message');
            $table->json('payload')->nullable();
            $table->timestamp('created_at')->useCurrent();
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('server_heartbeats');
        Schema::dropIfExists('online_players');
        Schema::dropIfExists('discord_logs');
    }
};
