"""Chat-scoped persistence helpers."""

from __future__ import annotations

from bot.infra.db.manager import db_manager


class ChatRepository:
    async def export_chat_data(self, chat_id: int) -> dict:
        settings = await db_manager.fetchall(
            "SELECT key, value FROM chat_settings WHERE chat_id=?",
            (chat_id,),
        )

        notes = await db_manager.fetchall(
            "SELECT name, content, file_id, file_type, buttons FROM notes WHERE chat_id=?",
            (chat_id,),
        )

        filters = await db_manager.fetchall(
            "SELECT keyword, response, file_id, file_type FROM filters WHERE chat_id=?",
            (chat_id,),
        )

        custom_commands = await db_manager.fetchall(
            "SELECT command, response FROM custom_commands WHERE chat_id=?",
            (chat_id,),
        )

        locks = await db_manager.fetchall(
            "SELECT lock_type FROM locks WHERE chat_id=?",
            (chat_id,),
        )

        warns = await db_manager.fetchall(
            "SELECT user_id, reason, count FROM warns WHERE chat_id=?",
            (chat_id,),
        )

        learned = await db_manager.fetchall(
            "SELECT keyword, response, weight FROM learned_responses WHERE chat_id=?",
            (chat_id,),
        )

        return {
            "settings": {row["key"]: row["value"] for row in settings},
            "notes": [dict(row) for row in notes],
            "filters": [dict(row) for row in filters],
            "custom_commands": [dict(row) for row in custom_commands],
            "locks": [row["lock_type"] for row in locks],
            "warns": [dict(row) for row in warns],
            "learned": [dict(row) for row in learned],
        }


chat_repository = ChatRepository()
