<?php

namespace App\Traits;

use App\Services\Audit\AuditLogService;

trait Auditable
{
    protected static function bootAuditable()
    {
        static::created(function ($model) {

            AuditLogService::log(
                module: strtolower(class_basename($model)),
                action: 'create',
                tableName: $model->getTable(),
                recordId: $model->id,
                newValues: $model->toArray()
            );
        });

        static::updated(function ($model) {

            AuditLogService::log(
                module: strtolower(class_basename($model)),
                action: 'update',
                tableName: $model->getTable(),
                recordId: $model->id,
                oldValues: $model->getOriginal(),
                newValues: $model->getAttributes()
            );
        });

        static::deleted(function ($model) {

            AuditLogService::log(
                module: strtolower(class_basename($model)),
                action: 'delete',
                tableName: $model->getTable(),
                recordId: $model->id,
                oldValues: $model->toArray()
            );
        });
    }
}