<?php

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

/**
 * AUD-01 from the system analysis: the only change-history anywhere in the
 * app was the booking-specific status history — drivers, vehicles, users,
 * companies and fare rules recorded no who/when/what-changed at all. This
 * is a generic, cross-entity log any controller can write to.
 */
return new class extends Migration
{
    public function up(): void
    {
        Schema::create('activity_logs', function (Blueprint $table) {
            $table->id();
            $table->foreignId('company_id')->nullable()->constrained()->nullOnDelete();
            $table->nullableMorphs('causer');
            $table->morphs('subject');
            $table->string('action');
            $table->json('changes')->nullable();
            $table->string('description');
            $table->timestamp('created_at')->nullable();

            $table->index(['subject_type', 'subject_id', 'created_at']);
        });
    }

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