<?php

namespace WPSN\Core;

defined('ABSPATH') || exit;

final class Logger
{
    private bool $debug;

    public function __construct(bool $debug)
    {
        $this->debug = $debug;
    }

    public function isDebug(): bool
    {
        return $this->debug;
    }

    /**
     * @param array<string, mixed> $context
     */
    public function debug(string $message, array $context = []): void
    {
        if (!$this->debug) {
            return;
        }
        $this->log('DEBUG', $message, $context);
    }

    /**
     * @param array<string, mixed> $context
     */
    public function info(string $message, array $context = []): void
    {
        if (!$this->debug) {
            return;
        }
        $this->log('INFO', $message, $context);
    }

    /**
     * @param array<string, mixed> $context
     */
    public function warning(string $message, array $context = []): void
    {
        $this->log('WARN', $message, $context);
    }

    /**
     * @param array<string, mixed> $context
     */
    public function error(string $message, array $context = []): void
    {
        $this->log('ERROR', $message, $context);
    }

    /**
     * @param array<string, mixed> $context
     */
    private function log(string $level, string $message, array $context = []): void
    {
        $line = sprintf('[WPSN][%s] %s', $level, $message);
        if (!empty($context)) {
            $line .= ' ' . wp_json_encode($context);
        }
        // Use WP debug log when enabled; otherwise this is still safe.
        error_log($line);
    }
}
