<?php

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

return new class extends Migration
{
    public function up(): void
    {
        Schema::create('access_logs', function (Blueprint $table) {
            $table->id();
            $table->foreignId('user_id')->constrained()->cascadeOnDelete();
            $table->foreignId('server_resource_id')->constrained()->cascadeOnDelete();
            $table->foreignId('bastion_account_id')->nullable()->constrained()->nullOnDelete();
            $table->string('protocol', 16);
            $table->string('action', 64);
            $table->timestamp('requested_at');
            $table->json('metadata')->nullable();
            $table->timestamps();

            $table->index(['user_id', 'requested_at']);
            $table->index(['server_resource_id', 'requested_at']);
        });
    }

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