<?php
/**
 * @package     MefisAssetBridge
 * @subpackage  Joomla_Administrator_Component
 *
 * @copyright   Copyright (C) 2025 Goran Usljebrka. All rights reserved.
 * @license     Proprietary — No resale or incorporation into other projects without explicit written permission. see LICENSE.txt
 */

defined('_JEXEC') or die;

use Joomla\CMS\Factory;
use Joomla\CMS\Installer\InstallerAdapter;
use Joomla\CMS\Language\Text;
use Joomla\CMS\Log\Log;
use Joomla\Database\DatabaseInterface;

/**
 * Script file for MefisAssetBridge component
 *
 * @since  1.0.0
 */
class Com_MefisAssetBridgeInstallerScript
{
    protected $minPhp = '7.4.0';
    protected $minYootheme = '4.0.0';
    protected $minJoomla = '4.0.0';

    /**
     * Function called before extension installation/update/removal procedure commences
     *
     * @param   string            $type    The type of change (install, update, discover_install, extension_site)
     * @param   InstallerAdapter  $parent  The class calling this method
     *
     * @return  boolean  True on success
     *
     * @since   1.0.0
     */
    public function preflight($type, $parent)
    {
        // verify joomls
        if(!$this->verifyJoomla()) {
            return false;
        }
        // verify yootheme
        if (!$this->verifyYooTheme()) {
            return false;
        }
        // verify PHP
        if (!$this->verifyPHP()) {
            return false;
        }
        // verify cURL
        if (!$this->verifyCurl()) {
            return false;
        }

        return true;
    }

    /**
     * Function called after extension installation/update/removal procedure commences
     *
     * @param   string            $type    The type of change (install, update, discover_install)
     * @param   InstallerAdapter  $parent  The class calling this method
     *
     * @return  boolean  True on success
     *
     * @since   1.0.0
     */
    public function postflight($type, $parent)
    {
        return true;
    }

    /**
     * Method to install the component
     *
     * @param   InstallerAdapter  $parent  The class calling this method
     *
     * @return  boolean  True on success
     *
     * @since   1.0.0
     */
    public function install($parent)
    {
        return true;
    }

    /**
     * Method to uninstall the component
     *
     * @param   InstallerAdapter  $parent  The class calling this method
     *
     * @return  boolean  True on success
     *
     * @since   1.0.0
     */
    public function uninstall($parent)
    {
        // Clear any stored API keys or session data
        $app = Factory::getApplication();
        $app->getSession()->clear('mefisAssetBridge.authenticated');
        $app->getSession()->clear('mefisAssetBridge.token');
        $app->getSession()->clear('mefisAssetBridge.token_expires');
        $app->getSession()->clear('mefisAssetBridge.refresh_token');
        $app->getSession()->clear('mefisAssetBridge.refresh_token_expires');
        $app->getSession()->clear('mefisAssetBridge.user_email');
        $app->getSession()->clear('mefisAssetBridge.user_name');

        // $app->enqueueMessage(Text::_('COM_MEFISASSETBRIDGE_UNINSTALL_SUCCESS'), 'info');

        return true;
    }

    /**
     * Method to update the component
     *
     * @param   InstallerAdapter  $parent  The class calling this method
     *
     * @return  boolean  True on success
     *
     * @since   1.0.0
     */
    public function update($parent)
    {
        return true;
    }

    protected function verifyYooTheme()
    {
        $message = "<p>This application requires Yootheme Pro, version {$this->minYootheme} or higher!</p>";

        $yoothemeXml = JPATH_ROOT . '/plugins/system/yootheme/yootheme.xml';

        if (! is_file($yoothemeXml)) {
            Factory::getApplication()->enqueueMessage($message, 'warning');
            return false;
        }

        $manifest = simplexml_load_file($yoothemeXml);
        if (! $manifest || $manifest->getName() !== 'extension' || empty($manifest->version)) {
            Factory::getApplication()->enqueueMessage($message, 'warning');
            return false;
        }

        if (version_compare((string) $manifest->version, $this->minYootheme, '<')) {
            Factory::getApplication()->enqueueMessage($message, 'warning');
            return false;
        }

        return true;
    }

    protected function verifyJoomla()
    {
        if (version_compare(JVERSION, $this->minJoomla, '>=')) {
            return true;
        }

        $message = "This application requires Joomla version {$this->minJoomla} or later.";

        Factory::getApplication()->enqueueMessage("<p>".$message."</p>",'warning');

        return false;
    }

    protected function verifyPHP()
    {
        if (version_compare(PHP_VERSION, $this->minPhp, '>=')) {
            return true;
        }

        Factory::getApplication()->enqueueMessage("<p>This application requires PHP version {$this->minPhp} or later.</p>",'warning');

        return false;
    }

    protected function verifyCurl()
    {
        if (extension_loaded('curl') && function_exists('curl_init')) {
            return true;
        }

        $message = "<p>This application requires the PHP cURL extension to be enabled.</p>";

        Factory::getApplication()->enqueueMessage($message, 'error');

        return false;
    }
}