<?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\Diagnostic\Service;

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

class PlatformInfoService
{
    public function getPrestaShopVersion(): ?string
    {
        return defined('_PS_VERSION_') ? (string) _PS_VERSION_ : null;
    }

    public function getWebServerSoftware(): string
    {
        return isset($_SERVER['SERVER_SOFTWARE']) ? (string) $_SERVER['SERVER_SOFTWARE'] : '';
    }

    public function getDatabaseVersion(): string
    {
        return (string) \Db::getInstance()->getValue('SELECT VERSION()');
    }

    public function getActiveThemeName(): ?string
    {
        $context = \Context::getContext();
        $shop = $context !== null ? $context->shop : null;

        if ($shop === null) {
            return null;
        }

        return $shop->theme_name ?? null;
    }

    /**
     * @return list<array{name: string, version: string, active: bool}>
     */
    public function getInstalledModules(): array
    {
        $modules = \Module::getModulesInstalled();

        if (!is_array($modules) || $modules === []) {
            return [];
        }

        $list = [];
        foreach ($modules as $module) {
            $list[] = [
                'name' => isset($module['name']) ? (string) $module['name'] : '',
                'version' => isset($module['version']) ? (string) $module['version'] : '',
                'active' => isset($module['active']) && (int) $module['active'] === 1,
            ];
        }

        return $list;
    }

    /**
     * @return list<string>
     */
    public function getInstalledLocales(): array
    {
        $locales = [];
        foreach (\Language::getLanguages(false) as $language) {
            if (isset($language['locale'])) {
                $locales[] = (string) $language['locale'];
            }
        }

        return $locales;
    }

    public function getConfiguration(string $key): string
    {
        $value = \Configuration::get($key);

        return $value === false ? '' : (string) $value;
    }

    public function isPathWritable(string $path): bool
    {
        return is_dir($path) && is_writable($path);
    }

    public function tableExists(string $tableName): bool
    {
        $db = \Db::getInstance();
        $escapedName = pSQL(_DB_PREFIX_ . $tableName);
        $query = 'SELECT 1 FROM information_schema.tables'
            . " WHERE table_schema = DATABASE() AND table_name = '" . $escapedName . "'";
        $result = $db->getValue($query);

        return $result !== false && $result !== '';
    }

    public function orderStateExists(int $orderStateId): bool
    {
        if ($orderStateId <= 0) {
            return false;
        }

        return \Validate::isLoadedObject(new \OrderState($orderStateId));
    }
}
