<?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('activity_logs', function (Blueprint $table) {
            $table->uuid('id')->primary();

            // ── Timestamps ──────────────────────────────────────────────
            $table->timestamp('occurred_at')->index();     // when the event happened (from client)
            $table->timestamps();                          // created_at/updated_at (server receipt time)

            // ── User context ────────────────────────────────────────────
            $table->unsignedBigInteger('user_id')->nullable()->index();
            $table->string('user_email', 191)->nullable()->index();
            $table->string('user_name', 191)->nullable();
            $table->string('user_role', 64)->nullable()->index();

            // ── Tenant context ──────────────────────────────────────────
            $table->unsignedBigInteger('tenant_id')->nullable()->index();
            $table->string('tenant_name', 191)->nullable();

            // ── Session ─────────────────────────────────────────────────
            $table->string('session_id', 64)->nullable()->index();

            // ── Event classification ────────────────────────────────────
            $table->string('category', 64)->index();      // booking, crm, driver, auth …
            $table->string('action', 64)->index();        // create, view, delete …
            $table->string('label', 500);                 // human-readable description
            $table->string('priority', 16)->default('normal'); // low, normal, high, critical
            $table->string('severity', 16)->default('info');   // info, warning, error, debug

            // ── Module / page ────────────────────────────────────────────
            $table->string('module', 128)->nullable()->index();
            $table->string('route_path', 500)->nullable();
            $table->string('page_name', 191)->nullable();

            // ── Target entity ────────────────────────────────────────────
            $table->string('entity_type', 128)->nullable()->index();
            $table->string('entity_id', 128)->nullable()->index();
            $table->string('entity_name', 191)->nullable();

            // ── Outcome ──────────────────────────────────────────────────
            $table->boolean('success')->nullable();
            $table->text('error_message')->nullable();
            $table->unsignedInteger('duration_ms')->nullable(); // execution time

            // ── Extra payload ─────────────────────────────────────────────
            $table->json('metadata')->nullable();

            // ── Client info ───────────────────────────────────────────────
            $table->text('user_agent')->nullable();

            // ── Composite indexes for common queries ──────────────────────
            $table->index(['category', 'action']);
            $table->index(['tenant_id', 'category']);
            $table->index(['user_id', 'occurred_at']);
            $table->index(['entity_type', 'entity_id']);
        });
    }

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