<?php
/**
 * Configuration Changed Event
 *
 * Dispatched when a configuration value is modified. Provides details
 * about the changed key, old and new values, and the user who made
 * the change.
 *
 * @package     WPUltimateSecurity\UltimateSecurity\Events
 * @since       2.0.0
 */

declare(strict_types=1);

namespace WPUltimateSecurity\UltimateSecurity\Events;

/**
 * Event dispatched when a configuration value changes.
 *
 * This event carries information about configuration modifications,
 * enabling subscribers to react to settings changes, log audit trails,
 * or trigger dependent updates.
 *
 * @since 2.0.0
 */
class ConfigChangedEvent extends AbstractEvent
{
    /**
     * The event name constant.
     *
     * @since 2.0.0
     *
     * @var string
     */
    public const NAME = 'config.changed';

    /**
     * Create a new ConfigChangedEvent instance.
     *
     * @since 2.0.0
     *
     * @param string      $key       The configuration key that was changed.
     * @param mixed       $oldValue  The previous value before the change.
     * @param mixed       $newValue  The new value after the change.
     * @param string|null $changedBy Optional. The identifier of who made the change
     *                               (e.g., user ID, 'system', 'api').
     */
    public function __construct(
        string $key,
        mixed $oldValue,
        mixed $newValue,
        ?string $changedBy = null
    ) {
        parent::__construct(self::NAME, [
            'key'       => $key,
            'oldValue'  => $oldValue,
            'newValue'  => $newValue,
            'changedBy' => $changedBy,
        ]);
    }

    /**
     * Get the configuration key that was changed.
     *
     * @since 2.0.0
     *
     * @return string The configuration key.
     */
    public function getKey(): string
    {
        return $this->payload['key'];
    }

    /**
     * Get the previous value before the change.
     *
     * @since 2.0.0
     *
     * @return mixed The old configuration value.
     */
    public function getOldValue(): mixed
    {
        return $this->payload['oldValue'];
    }

    /**
     * Get the new value after the change.
     *
     * @since 2.0.0
     *
     * @return mixed The new configuration value.
     */
    public function getNewValue(): mixed
    {
        return $this->payload['newValue'];
    }

    /**
     * Get the identifier of who made the change.
     *
     * @since 2.0.0
     *
     * @return string|null The change author identifier, or null if not specified.
     */
    public function getChangedBy(): ?string
    {
        return $this->payload['changedBy'];
    }
}
