<?php
namespace Bot\Services;

use Bot\Database;
use PDO;

class SettingService
{
    private PDO $pdo;

    public function __construct()
    {
        $this->pdo = Database::connection();
    }

    public function get(string $key, $default = null)
    {
        $stmt = $this->pdo->prepare('SELECT value FROM settings WHERE `key` = ?');
        $stmt->execute([$key]);
        $value = $stmt->fetchColumn();
        if ($value === false) {
            return $default;
        }
        return $value;
    }

    public function set(string $key, $value): void
    {
        $stmt = $this->pdo->prepare(
            'INSERT INTO settings (`key`, `value`, updated_at) VALUES (:key, :value, NOW())
             ON DUPLICATE KEY UPDATE `value` = VALUES(`value`), updated_at = NOW()'
        );
        $stmt->execute([
            ':key'   => $key,
            ':value' => (string) $value,
        ]);
    }
}
