<?php
/**
 * Logger class for Stripe Subscription Manager
 * Handles logging of events and errors
 */

// Exit if accessed directly
if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

class STRP_SUB_Logger {

    /**
     * Check if a message should be logged based on log level setting
     */
    private static function should_log( $level ) {
        if ( ! class_exists( 'STRP_SUB_Settings' ) ) {
            return $level === 'error'; // Always log errors during bootstrap
        }
        $log_level = STRP_SUB_Settings::get( 'log_level', 'info' );

        $levels = array( 'error' => 1, 'warning' => 2, 'info' => 3, 'debug' => 4 );
        $current_level = isset( $levels[ $log_level ] ) ? $levels[ $log_level ] : 3;
        $message_level = isset( $levels[ $level ] ) ? $levels[ $level ] : 3;

        return $message_level <= $current_level;
    }

    /**
     * Log a message
     */
    public static function log( $message, $level = 'info' ) {
        if ( ! self::should_log( $level ) ) {
            return;
        }

        if ( function_exists( 'wc_get_logger' ) ) {
            $logger = wc_get_logger();
            $context = array( 'source' => 'subscription-manager-for-stripe' );

            switch ( $level ) {
                case 'error':
                    $logger->error( $message, $context );
                    break;
                case 'warning':
                    $logger->warning( $message, $context );
                    break;
                case 'debug':
                    $logger->debug( $message, $context );
                    break;
                default:
                    $logger->info( $message, $context );
                    break;
            }
        } elseif ( in_array( $level, array( 'error', 'warning' ), true ) && defined( 'WP_DEBUG_LOG' ) && WP_DEBUG_LOG ) {
            error_log( '[stripe-sub-manager] ' . $level . ': ' . $message ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log
        }
    }

    /**
     * Log an error
     */
    public static function error( $message ) {
        self::log( $message, 'error' );
    }

    /**
     * Log a warning
     */
    public static function warning( $message ) {
        self::log( $message, 'warning' );
    }

    /**
     * Log debug information
     */
    public static function debug( $message ) {
        self::log( $message, 'debug' );
    }
}
