<?php
namespace CloudXM\SDK\Support;

/**
 * HasLogger trait - provides unified logging access across all CloudXM plugins
 *
 * This trait provides a standardized way for classes to access the central
 * CloudXM Logger with automatic client detection and correlation ID management.
 */
trait HasLogger
{
    /**
     * Get the logger instance with automatic client context
     *
     * @return \Psr\Log\LoggerInterface
     */
    protected function log(): \Psr\Log\LoggerInterface
    {
        return \CloudXM\SDK\Utilities\LoggerFactory::get(static::class);
    }

    /**
     * Get correlation ID for current request
     *
     * @return string|null
     */
    protected function getCorrelationId(): ?string
    {
        return \CloudXM\SDK\Logger::getCorrelationId();
    }

    /**
     * Log with automatic correlation ID
     *
     * @param string $level
     * @param string $message
     * @param array $context
     * @return void
     */
    protected function logEvent(string $level, string $message, array $context = []): void
    {
        $context['correlation_id'] = $this->getCorrelationId();
        $this->log()->log($level, $message, $context);
    }

    /**
     * Log success event
     *
     * @param string $message
     * @param array $context
     * @return void
     */
    protected function logSuccess(string $message, array $context = []): void
    {
        $this->logEvent('info', $message, array_merge($context, ['status' => 'success']));
    }

    /**
     * Log error event
     *
     * @param string $message
     * @param array $context
     * @return void
     */
    protected function logError(string $message, array $context = []): void
    {
        $this->logEvent('error', $message, array_merge($context, ['status' => 'error']));
    }

    /**
     * Log warning event
     *
     * @param string $message
     * @param array $context
     * @return void
     */
    protected function logWarning(string $message, array $context = []): void
    {
        $this->logEvent('warning', $message, array_merge($context, ['status' => 'warning']));
    }
}