<?php
declare(strict_types=1);

namespace App\Models;

use App\Core\Model;

final class AppMetaModel extends Model
{
    public function get(string $key, ?string $default = null): ?string
    {
        $stmt = $this->db->prepare("
            SELECT meta_value
            FROM app_meta
            WHERE meta_key = :meta_key
            LIMIT 1
        ");
        $stmt->execute([':meta_key' => $key]);
        $val = $stmt->fetchColumn();
        return $val !== false ? (string) $val : $default;
    }

    public function set(string $key, string $value): void
    {
        $stmt = $this->db->prepare("
            INSERT INTO app_meta (meta_key, meta_value)
            VALUES (:meta_key, :meta_value)
            ON DUPLICATE KEY UPDATE meta_value = VALUES(meta_value)
        ");
        $stmt->execute([
            ':meta_key' => $key,
            ':meta_value' => $value,
        ]);
    }
}
