<?php
namespace App\Traits;

use App\Models\SystemLog;
use Illuminate\Support\Facades\Auth;

trait Loggable
{
    public static function bootLoggable()
    {
        // Log de creación
        static::created(function ($model) {
            SystemLog::createLog([
                'action_type' => 'create',
                'model_type' => get_class($model),
                'model_id' => $model->id,
                'description' => "Nuevo registro creado en " . class_basename($model),
                'severity' => 'info'
            ]);
        });

        // Log de actualización
        static::updated(function ($model) {
            $changes = $model->getChanges();

            SystemLog::createLog([
                'action_type' => 'update',
                'model_type' => get_class($model),
                'model_id' => $model->id,
                'description' => "Registro actualizado en " . class_basename($model),
                'severity' => 'info'
            ]);
        });

        // Log de eliminación
        static::deleted(function ($model) {
            SystemLog::createLog([
                'action_type' => 'delete',
                'model_type' => get_class($model),
                'model_id' => $model->id,
                'description' => "Registro eliminado en " . class_basename($model),
                'severity' => 'warning'
            ]);
        });
    }
}
