<?php
/**
 * 2026 Paysera
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Academic Free License (AFL 3.0)
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://opensource.org/licenses/afl-3.0.php
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@prestashop.com so we can send you a copy immediately.
 *
 * @author Paysera <plugins@paysera.com>
 * @copyright 2026 Paysera
 * @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0)
 * International Registered Trademark & Property of Paysera
 */

declare(strict_types=1);

namespace Paysera\PrestashopPayments\Installer;

use Paysera\PrestashopPayments\Enum\ConfigurationKey;
use Paysera\PrestashopPayments\Helper\TabHelper;
use Paysera\PrestashopPayments\Service\TranslationFileRefresher;
use Psr\Log\LoggerInterface;

if (!defined('_PS_VERSION_')) {
    exit;
}

class ModuleInstaller
{
    public function __construct(
        private OrderStatusInstaller $orderStatusInstaller,
        private ConfigurationInstaller $configurationInstaller,
        private DatabaseInstaller $databaseInstaller,
        private TabHelper $tabHelper,
        private TranslationFileRefresher $translationFileRefresher,
        private LoggerInterface $logger
    ) {
    }

    public function uninstall(): void
    {
        $this->logger->info('Starting module uninstallation');

        $this->tabHelper->uninstallTabs();
        $this->logger->debug('Tabs uninstalled');

        $this->databaseInstaller->uninstall();
        $this->logger->debug('Database tables dropped');

        $this->configurationInstaller->uninstall();
        $this->logger->debug('Configuration cleared');

        $this->logger->info('Module uninstallation completed successfully');
    }

    public function installTabs(): void
    {
        $this->logger->debug('Starting tab installation');

        $this->tabHelper->installTabs();

        $this->logger->debug('Tabs installed successfully');
    }

    public function performPostInstallSetup(): void
    {
        $this->logger->debug('Starting post-install setup');

        try {
            $this->databaseInstaller->install();
            $this->logger->debug('Database tables installed');

            $this->orderStatusInstaller->install();
            $this->logger->debug('Order statuses installed');

            $this->configurationInstaller->install();
            $this->logger->debug('Default settings installed');

            $this->installTabs();
            $this->translationFileRefresher->generateTranslationFiles();

            \Configuration::updateValue(ConfigurationKey::POST_INSTALL_SETUP_COMPLETED, true);
            $this->logger->info('Post-install setup completed successfully');
        } catch (\Throwable $exception) {
            $this->logger->error('Post-install setup failed', [
                'message' => $exception->getMessage(),
                'file' => $exception->getFile(),
                'line' => $exception->getLine(),
                'trace' => $exception->getTraceAsString(),
            ]);
            throw $exception;
        }
    }
}
