<?php

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

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('activity_logs', function (Blueprint $table) {
            $table->id();
            $table->string('action'); // 'created', 'updated', 'deleted', 'stock_out'
            $table->string('module'); // 'inventory', 'stock_out', 'warehouse', etc.
            $table->unsignedBigInteger('module_id')->nullable(); // ID of the affected record
            $table->string('description')->nullable(); // Human-readable description
            $table->json('old_data')->nullable(); // Old values before update/delete
            $table->json('new_data')->nullable(); // New values after create/update
            $table->unsignedBigInteger('user_id'); // User who performed the action
            $table->string('ip_address')->nullable();
            $table->string('user_agent')->nullable();
            $table->timestamps();
            
            $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
            $table->index(['module', 'module_id']);
            $table->index('user_id');
            $table->index('created_at');
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('activity_logs');
    }
};
