<?php
declare(strict_types=1);

namespace CloudXM\SDK\Events;

use Monolog\LogRecord;

/**
 * Event dispatched when a log is being captured, allowing plugins to enrich
 * the log record with additional metadata before shipping.
 */
final class LogCaptureEvent
{
    private LogRecord $record;

    public function __construct(LogRecord $record)
    {
        $this->record = $record;
    }

    public function getRecord(): LogRecord
    {
        return $this->record;
    }

    /**
     * Add contextual data to the log record
     */
    public function addContext(string $key, mixed $value): void
    {
        $existingContext = $this->record->context;
        $existingContext[$key] = $value;

        // Create a new LogRecord with updated context
        $this->record = new LogRecord(
            $this->record->datetime,
            $this->record->channel,
            $this->record->level,
            $this->record->message,
            $existingContext,
            $this->record->extra,
            $this->record->formatted
        );
    }

    /**
     * Add extra data to the log record
     */
    public function addExtra(string $key, mixed $value): void
    {
        $existingExtra = $this->record->extra;
        $existingExtra[$key] = $value;

        // Create a new LogRecord with updated extra
        $this->record = new LogRecord(
            $this->record->datetime,
            $this->record->channel,
            $this->record->level,
            $this->record->message,
            $this->record->context,
            $existingExtra,
            $this->record->formatted
        );
    }
}