<?php

declare(strict_types=1);

namespace App\Observers;

use App\Enums\AuditAction;
use App\Services\AuditLogger;
use Illuminate\Database\Eloquent\Model;

/**
 * Attach via Model::observe(AuditOnlyObserver::class) for models that need
 * audit logging but not the member-specific number/status-history handling.
 */
class AuditOnlyObserver
{
    public function __construct(private readonly AuditLogger $auditLogger) {}

    public function created(Model $model): void
    {
        $this->auditLogger->log($model, AuditAction::Created, [], $model->getAttributes());
    }

    public function updated(Model $model): void
    {
        $changes = $model->getChanges();
        unset($changes['updated_at']);

        if ($changes === []) {
            return;
        }

        $original = array_intersect_key($model->getOriginal(), $changes);

        $this->auditLogger->log($model, AuditAction::Updated, $original, $changes);
    }

    public function deleted(Model $model): void
    {
        $this->auditLogger->log($model, AuditAction::Deleted);
    }

    public function restored(Model $model): void
    {
        $this->auditLogger->log($model, AuditAction::Restored);
    }
}
