"""Settings repository.

Provides CRUD operations for the ``settings`` key-value table in the
application SQLite database.  All methods are plain async functions that
accept a :class:`aiosqlite.Connection` — no ORM, no HTTP exceptions.
"""

from __future__ import annotations

from typing import TYPE_CHECKING

if TYPE_CHECKING:
    import aiosqlite


async def get_setting(db: aiosqlite.Connection, key: str) -> str | None:
    """Return the value for *key*, or ``None`` if it does not exist.

    Args:
        db: Active aiosqlite connection.
        key: The setting key to look up.

    Returns:
        The stored value string, or ``None`` if the key is absent.
    """
    async with db.execute(
        "SELECT value FROM settings WHERE key = ?",
        (key,),
    ) as cursor:
        row = await cursor.fetchone()
        return str(row[0]) if row is not None else None


async def set_setting(db: aiosqlite.Connection, key: str, value: str) -> None:
    """Insert or replace the setting identified by *key*.

    Args:
        db: Active aiosqlite connection.
        key: The setting key.
        value: The value to store.
    """
    await db.execute(
        "INSERT OR REPLACE INTO settings (key, value) VALUES (?, ?)",
        (key, value),
    )
    await db.commit()


async def delete_setting(db: aiosqlite.Connection, key: str) -> None:
    """Delete the setting identified by *key* if it exists.

    Args:
        db: Active aiosqlite connection.
        key: The setting key to remove.
    """
    await db.execute("DELETE FROM settings WHERE key = ?", (key,))
    await db.commit()


async def get_all_settings(db: aiosqlite.Connection) -> dict[str, str]:
    """Return all settings as a plain ``dict``.

    Args:
        db: Active aiosqlite connection.

    Returns:
        A dictionary mapping every stored key to its value.
    """
    async with db.execute("SELECT key, value FROM settings") as cursor:
        rows = await cursor.fetchall()
        return {str(row[0]): str(row[1]) for row in rows}
