<?php
/**
 * Helper for writing structured logs to the Action Scheduler logger.
 *
 * @package MOMCS_Sync
 */

declare(strict_types=1);

namespace MOMCS_Sync\Includes\Common\Utils;

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

/**
 * Class AS_Log_Helper
 */
class AS_Log_Helper {
	/**
	 * Write a JSON-encoded structured entry to Action Scheduler.
	 *
	 * @param int    $log_action_id Action Scheduler action/job ID.
	 * @param string $severity      info|success|warning|error.
	 * @param int    $product_id    WooCommerce product ID (0 if N/A).
	 * @param array  $payload       Free-form associative array added to envelope.
	 * @param string $stage         Logical stage (catalog_search_request, etc.).
	 * @return void
	 */
	public static function log( int $log_action_id, string $severity, int $product_id, array $payload, string $stage = '' ): void {
		if ( ! $log_action_id ) {
			return;
		}

		$entry = array(
			'severity'   => strtolower( $severity ),
			'product_id' => $product_id,
			'stage'      => $stage,
		);

		$entry  = array_merge( $entry, $payload );
		$json   = wp_json_encode( $entry );
		$prefix = '[' . strtoupper( $severity ) . '][' . $product_id . '] ';
		\ActionScheduler::logger()->log( $log_action_id, $prefix . $json );
	}
}
