<?php

if (!defined('ABSPATH')) {
    exit;
}

/**
 * Class that handles creation of webhook consumers to receive order
 * webhook event sent by Checkout Solution.
 *
 * @package  WooCommerce
 * @category Payment Gateways
 * @author   fiserv
 * @since    1.0.0
 */
final class WC_Fiserv_Logger
{
    public const SOURCE = 'fiserv-checkout-for-woocommerce';

    /**
     * Log some message to WC admin page as info log
     *
     * @param string   $message Message to log
     * @param WC_Order $order   Order that failed
     */
    public static function log(WC_Order $order, string $message): void
    {
        if (!WC_Fiserv_Payment_Settings::isLoggingEnabled()) {
            return;
        }

        wc_get_logger()->notice($message, self::create_log_context($order));
    }

    /**
     * Generic log without order reference
     *
     * @param  string $message
     * @return void
     */
    public static function generic_log(object|string $message): void
    {
        if (!WC_Fiserv_Payment_Settings::isLoggingEnabled()) {
            return;
        }
        if (is_object($message)) {
            $message = json_encode($message, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
        }
        wc_get_logger()->notice($message, ['source' => self::SOURCE]);
    }

    /**
     * Log some message to WC admin page as error log
     *
     * @param string   $message Message to log
     * @param WC_Order $order   Order that failed
     */
    public static function error(WC_Order $order, string $message): void
    {
        wc_get_logger()->error($message, self::create_log_context($order));
    }

    /**
     * Create generic message template containing log context from order
     *
     * @param  WC_Order $order Order that failed
     * @return array<string, mixed>
     */
    private static function create_log_context(WC_Order $order): array
    {
        return [
            'source' => self::SOURCE,
            'wc_order_id' => $order->get_id(),
            'wc_order_key' => $order->get_order_key(),
            'fiserv_link' => $order->get_meta('_fiserv_plugin_checkout_link'),
            'fiserv_checkout_id' => $order->get_meta('_fiserv_plugin_checkout_id'),
            'fiserv_trace_id' => $order->get_meta('_fiserv_plugin_trace_id'),
        ];
    }
}
