<?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('member_access_logs', function (Blueprint $table) {
            $table->id();
            $table->foreignId('member_id')->constrained('members')->cascadeOnDelete();
            $table->foreignId('scanned_by_id')->nullable()->constrained('securities')->nullOnDelete();
            $table->string('token', 128);
            $table->string('action', 20)->default('scan'); // scan
            $table->string('result', 20); // granted, denied
            $table->string('deny_reason')->nullable();
            $table->timestamp('scanned_at');
            $table->timestamps();

            $table->index(['member_id', 'scanned_at']);
            $table->index('token');
        });
    }

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