<?php
declare(strict_types=1);

require_once __DIR__ . '/../config/database.php';

class SiteSetting
{
    public function getMany(array $keys): array
    {
        if (!$keys) {
            return [];
        }

        $placeholders = implode(',', array_fill(0, count($keys), '?'));
        $stmt = db()->prepare("SELECT setting_key, setting_value FROM site_settings WHERE setting_key IN ($placeholders)");
        $stmt->execute(array_values($keys));
        $rows = $stmt->fetchAll();

        $result = [];
        foreach ($rows as $row) {
            $result[(string)$row['setting_key']] = (string)$row['setting_value'];
        }
        return $result;
    }

    public function setMany(array $pairs): void
    {
        if (!$pairs) {
            return;
        }

        $stmt = db()->prepare(
            'INSERT INTO site_settings (setting_key, setting_value) VALUES (:setting_key, :setting_value)
             ON DUPLICATE KEY UPDATE setting_value = VALUES(setting_value)'
        );
        foreach ($pairs as $key => $value) {
            $stmt->execute([
                'setting_key' => (string)$key,
                'setting_value' => (string)$value,
            ]);
        }
    }
}
