<?php
/**
 * Centralized Logging Utility
 *
 * @package PackshotPhotoAI
 */

namespace PackshotPhotoAI\Core;

defined('ABSPATH') || exit;

/**
 * Handles logging for the plugin with different levels and optional activation
 */
class Logger {

    /**
     * Log a warning message
     *
     * @param string $message
     * @param array  $context Additional data to log
     */
    public static function warning(string $message, array $context = []): void {
        self::log('WARNING', $message, $context);
    }

    /**
     * Log an error message
     *
     * @param string $message
     * @param array  $context Additional data to log
     */
    public static function error(string $message, array $context = []): void {
        self::log('ERROR', $message, $context);
    }

    /**
     * Log an info message
     *
     * @param string $message
     * @param array  $context Additional data to log
     */
    public static function info(string $message, array $context = []): void {
        self::log('INFO', $message, $context);
    }

    /**
     * Log a debug message (only if \WP_DEBUG is on or packshotphoto_debug_logging is enabled)
     *
     * @param string $message
     * @param array  $context Additional data to log
     */
    public static function debug(string $message, array $context = []): void {
        if (!defined('\WP_DEBUG') || !\WP_DEBUG) {
            $enabled = get_option('packshotphoto_debug_logging', 'no');
            if ('yes' !== $enabled) {
                return;
            }
        }
        self::log('DEBUG', $message, $context);
    }

    /**
     * Core logging function
     *
     * @param string $level   ERROR|INFO|DEBUG
     * @param string $message
     * @param array  $context
     */
    private static function log(string $level, string $message, array $context): void {
        $formatted_context = !empty($context) ? ' | Context: ' . \wp_json_encode($context) : '';
        $log_entry = sprintf(
            '[PMM][%s] %s%s',
            $level,
            $message,
            $formatted_context
        );

        $upload_dir = \wp_upload_dir();
        $log_dir = $upload_dir['basedir'] . '/packshotphoto-logs';
        $log_file = $log_dir . '/packshotphoto-debug-' . \wp_hash('packshotphoto_log_salt') . '.log';
        
        if (!is_dir($log_dir)) {
            \wp_mkdir_p($log_dir);
        }

        // Always ensure direct HTTP access to log files is blocked
        $htaccess = $log_dir . '/.htaccess';
        if (!file_exists($htaccess)) {
            @file_put_contents($htaccess, "Deny from all\n");
        }
        $index = $log_dir . '/index.php';
        if (!file_exists($index)) {
            @file_put_contents($index, "<?php // Silence is golden.\n");
        }
        
        $timestamp = current_time('Y-m-d H:i:s');
        $entry = "\n[{$timestamp}] [{$level}] {$message}{$formatted_context}\n";
        
        @file_put_contents($log_file, $entry, FILE_APPEND);
        
        // Also use error_log for server logs if WP_DEBUG is enabled
        if (defined('WP_DEBUG') && WP_DEBUG) {
            // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log
            error_log($log_entry);
        }
    }
}
