<?php

namespace App\Models;

use App\Core\Model;
use PDO;

class Setting extends Model {
    public function getAll() {
        $stmt = $this->db->query("SELECT * FROM settings");
        $results = $stmt->fetchAll();
        $settings = [];
        foreach ($results as $row) {
            $settings[$row['key_name']] = $row['key_value'];
        }
        return $settings;
    }

    public function update($key, $value) {
        $stmt = $this->db->prepare("INSERT INTO settings (key_name, key_value) VALUES (:key, :value) 
                                    ON DUPLICATE KEY UPDATE key_value = :value_update");
        return $stmt->execute([
            'key' => $key,
            'value' => $value,
            'value_update' => $value
        ]);
    }

    public function get($key, $default = null) {
        $stmt = $this->db->prepare("SELECT key_value FROM settings WHERE key_name = :key LIMIT 1");
        $stmt->execute(['key' => $key]);
        $row = $stmt->fetch();
        return $row ? $row['key_value'] : $default;
    }
}
