<?php

namespace TbiBank\FusionPay\Block\Frontend;

// Import necessary Magento classes
use Magento\Framework\View\Element\Template;
use Magento\Checkout\Model\Session as CheckoutSession;

/**
 * Custom Message Block for FusionPay
 *
 * This block class is responsible for handling custom messages related to FusionPay
 * in the checkout process.
 */
class SuccessMessage extends Template
{
    /**
     * Checkout session instance
     *
     * @var CheckoutSession
     */
    protected $checkoutSession;

    /**
     * Constructor
     *
     * @param Template\Context $context Magento context object, containing environment data
     * @param CheckoutSession $checkoutSession Session object to manage checkout data
     * @param array $data Additional data array
     */
    public function __construct(
        Template\Context $context,
        CheckoutSession $checkoutSession,
        array $data = []
    ) {
        parent::__construct($context, $data); // Call parent constructor
        $this->checkoutSession = $checkoutSession; // Initialize checkout session
    }

    /**
     * Retrieve and clear the custom message for FusionPay from the checkout session
     *
     * This method retrieves the FusionPay custom message stored in the session,
     * then clears it to ensure it's only shown once.
     *
     * @return mixed The custom message if it exists, null otherwise
     */
    public function showFusionPayMessage()
    {
        // Retrieve the custom message from the session
        $show = $this->checkoutSession->getFusionPayCustomMessage();
        
        // Clear the message from the session to avoid showing it again
        $this->checkoutSession->unsFusionPayCustomMessage();
        
        return $show; // Return the retrieved message
    }
}
