# Path: dozer/db.py
# DOZER_LOGGER = logging.getLogger(__name__)
# async def db_init(db_url):
# async def db_migrate():
#     async def initial_create(cls):
#     async def initial_migrate(cls):
#     def nullify():
#     async def update_or_add(self):
#     def __repr__(self):
#     async def get_by(cls, **filters):
#     async def delete(cls, **filters):
#     async def set_initial_version(cls):
#     def __init__(self, table):
#     def _hash_dict(dic):
#     async def query_one(self, **kwargs):
#     async def query_all(self, **kwargs):
#     def invalidate_entry(self, **kwargs):
# class DatabaseTable:
# class ConfigCache:


import asyncio
import inspect
import logging
import typing
import discord
from collections.abc import Mapping
from discord.ext import commands
from dozer import db

class PrefixHandler:
    """Handles dynamic prefixes"""

    def __init__(self, default_prefix):
        self.default_prefix = default_prefix
        self.prefix_cache = {}

    def handler(self, bot, message):
        """Process the dynamic prefix for each message"""
        dynamic = self.prefix_cache.get(message.guild.id) if message.guild else self.default_prefix
        # <@!> is a nickname mention which discord.py doesn't make by default
        return [f"<@!{bot.user.id}> ", bot.user.mention, dynamic if dynamic else self.default_prefix]

    async def refresh(self):
        """Refreshes the prefix cache"""
        prefixes = await DynamicPrefixEntry.get_by()  # no filters, get all
        for prefix in prefixes:
            self.prefix_cache[prefix.guild_id] = prefix.prefix
        DOZER_LOGGER.info(f"{len(prefixes)} prefixes loaded from database")


class DynamicPrefixEntry(db.DatabaseTable):
    """Holds the custom prefixes for guilds"""
    __tablename__ = 'dynamic_prefixes'
    __uniques__ = 'guild_id'

    @classmethod
    async def initial_create(cls):
        """Create the table in the database"""

# Path: dozer/db.py
# DOZER_LOGGER = logging.getLogger(__name__)
# async def db_init(db_url):
# async def db_migrate():
#     async def initial_create(cls):
#     async def initial_migrate(cls):
#     def nullify():
#     async def update_or_add(self):
#     def __repr__(self):
#     async def get_by(cls, **filters):
#     async def delete(cls, **filters):
#     async def set_initial_version(cls):
#     def __init__(self, table):
#     def _hash_dict(dic):
#     async def query_one(self, **kwargs):
#     async def query_all(self, **kwargs):
#     def invalidate_entry(self, **kwargs):
# class DatabaseTable:
# class ConfigCache:


# Path: dozer/cogs/_utils.py
import asyncio
import inspect
import logging
import typing
import discord
from collections.abc import Mapping
from discord.ext import commands
from dozer import db


class PrefixHandler:
    """Handles dynamic prefixes"""

    def __init__(self, default_prefix):
        self.default_prefix = default_prefix
        self.prefix_cache = {}

    def handler(self, bot, message):
        """Process the dynamic prefix for each message"""
        dynamic = self.prefix_cache.get(message.guild.id) if message.guild else self.default_prefix
        # <@!> is a nickname mention which discord.py doesn't make by default
        return [f"<@!{bot.user.id}> ", bot.user.mention, dynamic if dynamic else self.default_prefix]

    async def refresh(self):
        """Refreshes the prefix cache"""
        prefixes = await DynamicPrefixEntry.get_by()  # no filters, get all
        for prefix in prefixes:
            self.prefix_cache[prefix.guild_id] = prefix.prefix
        DOZER_LOGGER.info(f"{len(prefixes)} prefixes loaded from database")


class DynamicPrefixEntry(db.DatabaseTable):
    """Holds the custom prefixes for guilds"""
    __tablename__ = 'dynamic_prefixes'
    __uniques__ = 'guild_id'

    @classmethod
    async def initial_create(cls):
        """Create the table in the database"""

        async with db.Pool.acquire() as conn: