<?php

namespace TbiBank\FusionPay\Observer;

use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\Event\Observer;
use Magento\Checkout\Model\Session as CheckoutSession;

class AddSuccessMessage implements ObserverInterface
{
    /**
     * @var CheckoutSession
     */
    protected $checkoutSession;

    /**
     * Constructor.
     *
     * @param CheckoutSession $checkoutSession
     */
    public function __construct(CheckoutSession $checkoutSession)
    {
        $this->checkoutSession = $checkoutSession;
    }

    /**
     * Execute method triggered when the observer is called.
     *
     * @param Observer $observer
     */
    public function execute(Observer $observer)
    {
        // Get the order IDs from the event
        $orderIds = $observer->getEvent()->getOrderIds();

        // Check if order IDs are empty or not an array
        if (empty($orderIds) || !is_array($orderIds)) {
            return;
        }

        // Get the last real order from the checkout session
        $order = $this->checkoutSession->getLastRealOrder();

        // Check if payment method instance code is 'fusionpay'
        // Adjust 'fusionpay' to your actual method code
        if ($order->getPayment()->getMethodInstance()->getCode() == 'fusionpay') {
            // Set custom message flag in checkout session
            $this->checkoutSession->setFusionPayCustomMessage(true);
        }
    }
}
