<?php

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

/**
 * Acente paneli işlem kayıtları (audit log): görüntüleme dahil tüm istekler
 * middleware üzerinden buraya yazılır. updated_at yok — kayıtlar değişmez.
 */
return new class extends Migration
{
    public function up(): void
    {
        Schema::create('activity_logs', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('agency_id');
            $table->unsignedBigInteger('user_id')->nullable();
            $table->string('action', 30)->default('view');      // view/create/update/delete/export/login/logout/other
            $table->string('module', 60)->nullable();            // route adından çıkarılan modül anahtarı (tours, sales...)
            $table->string('route_name')->nullable();
            $table->string('description', 500)->nullable();      // insan okunur Türkçe özet
            $table->string('subject_type')->nullable();          // etkilenen model (route model binding'den)
            $table->unsignedBigInteger('subject_id')->nullable();
            $table->json('properties')->nullable();              // yazma isteklerinde filtrelenmiş payload
            $table->string('method', 10)->nullable();
            $table->string('url', 1000)->nullable();
            $table->string('ip_address', 45)->nullable();
            $table->timestamp('created_at')->useCurrent();

            $table->index(['agency_id', 'created_at']);
            $table->index(['agency_id', 'user_id']);
            $table->index(['agency_id', 'module']);
            $table->index(['agency_id', 'action']);
        });
    }

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