<?php

namespace App\Listeners;

use App\Events\ActionPerformed;
use App\Services\Log\LogSaver;
use Exception;

class LogActionListener
{
    public function __construct(private LogSaver $logSaver) {}

    public function handle(ActionPerformed $event): void
    {
        try {
            $this->logSaver->log($event->level, $event->message, context: $event->context);
        } catch (Exception $e) {
            error_log('Failed to log action to database: '.$e->getMessage());
        }
    }
}
