<?php

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

return new class extends Migration
{
    public function up()
    {
        if (!Schema::hasTable('ss_access_logs')) {
            Schema::create('ss_access_logs', function (Blueprint $table) {
                $table->bigIncrements('id');
                $table->unsignedInteger('business_id')->nullable()->index();
                $table->unsignedBigInteger('file_id')->nullable()->index();
                $table->string('logical_path', 500)->nullable();
                $table->string('action', 20)->index();   // read|write|delete|denied
                $table->boolean('granted')->default(true);
                $table->string('reason', 191)->nullable();

                $table->unsignedInteger('user_id')->nullable()->index();
                $table->string('device_fingerprint', 191)->nullable();
                $table->string('ip', 64)->nullable()->index();
                $table->string('user_agent', 255)->nullable();

                $table->string('geo_country', 100)->nullable();
                $table->string('geo_city', 100)->nullable();
                $table->decimal('geo_lat', 10, 6)->nullable();
                $table->decimal('geo_lng', 10, 6)->nullable();

                $table->unsignedBigInteger('otp_session_id')->nullable();
                $table->timestamp('created_at')->nullable()->index();
            });
        }
    }

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