<?php

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

class CreateAuditsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('audits', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('user_id')->nullable(); // Usuario que realizó la acción
            $table->foreign('user_id')->references('id')->on('users')->onDelete('set null');
            $table->string('model'); // Nombre del modelo
            $table->string('model_id')->nullable(); // ID del registro modificado
            $table->string('action'); // create, update, delete
            $table->json('old_values')->nullable(); // Valores anteriores
            $table->json('new_values')->nullable(); // Valores nuevos
            $table->string('url')->nullable(); // URL donde se realizó la acción
            $table->string('method')->nullable(); // Método HTTP
            $table->string('ip')->nullable(); // Dirección IP
            $table->text('user_agent')->nullable(); // Navegador/dispositivo
            $table->timestamps();
        });
    }

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