from __future__ import annotations

from typing import Any, Callable, Dict, List, Optional, Tuple, Union

import json
import sqlite3

def meta_value(conn: sqlite3.Connection, key: str, default: Union[str, int, float]) -> Union[str, int, float]:
    row = conn.execute("SELECT value_json FROM store_meta WHERE key=?", (key,)).fetchone()
    if not row:
        return default
    try:
        val = json.loads(row["value_json"] or "null")
    except json.JSONDecodeError:
        return default
    return val

def meta_int(conn: sqlite3.Connection, key: str, default: int) -> int:
    v = meta_value(conn, key, default)
    return int(v)

def meta_float(conn: sqlite3.Connection, key: str, default: float) -> float:
    v = meta_value(conn, key, default)
    return float(v)
