<?php
/**
 * @package     pkg_rca_jea_basic
 *
 * @author      ReCreatingAnew (by GinA)
 * @copyright   Copyright (C) 2008 - 2026 ReCreatingAnew (by GinA), Sidney, Maine, USA. All rights reserved.
 * @license     GNU General Public License version 2 or later; see LICENSE.txt
 */

\defined('_JEXEC') || die;

use Joomla\CMS\Factory;
use Joomla\CMS\Installer\InstallerAdapter;
use Joomla\CMS\Installer\InstallerScriptInterface;
use Joomla\Database\DatabaseInterface;

return new class implements InstallerScriptInterface
{
    public function preflight(string $type, InstallerAdapter $adapter): bool
    {
        return true;
    }

    public function install(InstallerAdapter $adapter): bool
    {
        $this->enablePlugins();

        return true;
    }

    public function update(InstallerAdapter $adapter): bool
    {
        $this->enablePlugins();

        return true;
    }

    public function uninstall(InstallerAdapter $adapter): bool
    {
        return true;
    }

    public function postflight(string $type, InstallerAdapter $adapter): bool
    {
        return true;
    }

    private function enablePlugins(): void
    {
        $db = Factory::getContainer()->get(DatabaseInterface::class);

        $plugins = [
            ['element' => 'rca_jea',     'folder' => 'finder'],
            ['element' => 'rca_jea',     'folder' => 'system'],
            ['element' => 'social',      'folder' => 'system'],
            ['element' => 'geolocalize', 'folder' => 'system'],
        ];

        foreach ($plugins as $plugin) {
            $db->setQuery(
                $db->getQuery(true)
                    ->update($db->quoteName('#__extensions'))
                    ->set($db->quoteName('enabled') . ' = 1')
                    ->where($db->quoteName('type') . ' = ' . $db->quote('plugin'))
                    ->where($db->quoteName('element') . ' = ' . $db->quote($plugin['element']))
                    ->where($db->quoteName('folder') . ' = ' . $db->quote($plugin['folder']))
            );
            $db->execute();
        }
    }
};
