<?php
namespace EIS\Component\EIS\Administrator\Helper;

\defined('_JEXEC') or die;

use Joomla\CMS\Factory;

final class SettingsHelper
{
    public static function getSetting(string $param, $default = '')
    {
        $db = Factory::getDbo();
        $q  = $db->getQuery(true)
            ->select($db->quoteName('value'))
            ->from($db->quoteName('#__eis_settings'))
            ->where($db->quoteName('param') . ' = ' . $db->quote($param));
        $db->setQuery($q);
        $val = $db->loadResult();

        return ($val !== null && $val !== '') ? $val : $default;
    }

    public static function setSetting(string $param, $value): void
    {
        $db = Factory::getDbo();

        // Basis-INSERT per Query-Builder zusammenstellen …
        $qb = $db->getQuery(true)
            ->insert($db->quoteName('#__eis_settings'))
            ->columns([$db->quoteName('param'), $db->quoteName('value'), $db->quoteName('modified')])
            ->values(
                $db->quote($param) . ', ' .
                $db->quote((string) $value) . ', ' .
                $db->quote(date('Y-m-d H:i:s'))
            );

        // … und dann als String um "ON DUPLICATE KEY UPDATE" erweitern.
        $sql = (string) $qb
            . ' ON DUPLICATE KEY UPDATE '
            . $db->quoteName('value')    . ' = VALUES(' . $db->quoteName('value') . '), '
            . $db->quoteName('modified') . ' = VALUES(' . $db->quoteName('modified') . ')';

        $db->setQuery($sql)->execute();
    }
}
