"""تنظیمات گروه‌ها (chat_settings)"""
from __future__ import annotations

from bot.db.core import _exec, _fetchone, _fetchall


async def get_setting(chat_id: int, key: str, default: str = "") -> str:
    row = await _fetchone(
        "SELECT value FROM chat_settings WHERE chat_id=? AND key=?",
        (chat_id, key),
    )
    if row is None:
        return default
    return row["value"] or default


async def set_setting(chat_id: int, key: str, value: str) -> None:
    await _exec(
        "INSERT OR REPLACE INTO chat_settings (chat_id, key, value) VALUES (?, ?, ?)",
        (chat_id, key, value),
    )


async def del_setting(chat_id: int, key: str) -> None:
    await _exec(
        "DELETE FROM chat_settings WHERE chat_id=? AND key=?",
        (chat_id, key),
    )


async def get_all_settings(chat_id: int) -> dict[str, str]:
    rows = await _fetchall(
        "SELECT key, value FROM chat_settings WHERE chat_id=?",
        (chat_id,),
    )
    return {row["key"]: row["value"] or "" for row in rows}
