<?php

namespace TbiBank\FusionPay\Setup;

use Magento\Framework\Setup\UninstallInterface;
use Magento\Framework\Setup\SchemaSetupInterface;
use Magento\Framework\Setup\ModuleContextInterface;

use TbiBank\FusionPay\Helper\Data as FusionPayHelper;

/**
 * Class Uninstall
 * This class defines the uninstall script to remove the 'tbi_status' column from the 'sales_order' table.
 */
class Uninstall implements UninstallInterface
{

    protected $fusionPayHelper;
    protected $tbiEndpointStatus;


    /**
     * Invoked when uninstalling the module to remove the 'tbi_status' column from the 'sales_order' table.
     *
     * @param SchemaSetupInterface $setup The setup schema interface.
     * @param ModuleContextInterface $context The module context interface.
     */
    public function uninstall(SchemaSetupInterface $setup, ModuleContextInterface $context)
    {
        $setup->startSetup();

        // Check if the 'tbi_status' column exists and drop it from the 'sales_order' table
        $connection = $setup->getConnection();
        $tableName = $setup->getTable('sales_order');

        if ($connection->tableColumnExists($tableName, 'tbi_status')) {
            $connection->dropColumn($tableName, 'tbi_status');
        }

        if ($connection->tableColumnExists($tableName, 'tbi_contract_id')) {
            $connection->dropColumn($tableName, 'tbi_contract_id');
        }

        // Also drop the denormalized copy from 'sales_order_grid'
        $gridTableName = $setup->getTable('sales_order_grid');

        if ($connection->tableColumnExists($gridTableName, 'tbi_status')) {
            $connection->dropColumn($gridTableName, 'tbi_status');
        }

        if ($connection->tableColumnExists($gridTableName, 'tbi_contract_id')) {
            $connection->dropColumn($gridTableName, 'tbi_contract_id');
        }


        // Send notification when plugin is uninstalled
        $this->sendNotification();
        

        $setup->endSetup();
    }



    /**
     * Sends a notification to the TBI endpoint about the status of the plugin.
     *
     * This function is used to send a notification to the TBI endpoint about the
     * status of the plugin.
     *
     * @return void
    */
    protected function sendNotification() {

        $this->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'        => 'uninstall',
            'date'          => date('Y-m-d H:i:s')
        ];

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

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

    }




}
