<?php

namespace TbiBank\FusionPay\Observer;

use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\Event\Observer;
use Magento\Framework\Module\Manager as ModuleManager;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\HTTP\Client\Curl;

use TbiBank\FusionPay\Helper\Data as FusionPayHelper;
use TbiBank\FusionPay\Logger\FusionPayLogger;

class ModuleDisableObserver implements ObserverInterface
{
    protected $moduleManager;
    protected $scopeConfig;

    /**
     * @var Curl
    */
    protected $curl;

    /**
     * @var FusionPayHelper
    */
    protected $fusionPayHelper;

    /**
     * @var FusionPayLogger
    */
    protected $logger; // Custom Logger

    public function __construct(
        ModuleManager $moduleManager,
        ScopeConfigInterface $scopeConfig,
        Curl $curl,
        FusionPayHelper $fusionPayHelper,
        FusionPayLogger $logger, // Inject the custom Logger
    ) {
        $this->moduleManager = $moduleManager;
        $this->scopeConfig = $scopeConfig;
        $this->curl = $curl;
        $this->fusionPayHelper = $fusionPayHelper;
        $this->logger = $logger; // Initialize the custom Logger
        //$this->logger->info('TbiBank_FusionPay module is disabled CONSTRUCTOR');

    }

    public function execute(Observer $observer)
    {
        // Check if the module is disabled
        if ($this->moduleManager->isEnabled('TbiBank_FusionPay')) {
        }else{
            $this->sendNotification();
        }
    }

    protected function sendNotification() {
        $moduleName = 'TbiBank_FusionPay';

        $tbiEndpointStatus = $this->fusionPayHelper->getConstValue('TBI_PLATFORM_URL') . '/api/PluginsSettings/status';

        $apiData = [
            'reseller_code' => $this->fusionPayHelper->getConfigValue('tbi_settings/general_settings/reseller_code'),
            'reseller_key'  => $this->fusionPayHelper->getConfigValue('tbi_settings/general_settings/reseller_key'),
            'plugin'        => 'Magento2',
            'version'       => '1.5',
            'status'        => 'inactive',
            'date'          => date('Y-m-d H:i:s')
        ];

        // Send data to the external API
        $jsonData = json_encode($apiData);

        $this->curl->post($tbiEndpointStatus, $jsonData);

    }

}
