import asyncpg

from .config import settings


pool: asyncpg.Pool | None = None


async def connect() -> asyncpg.Pool:
    global pool
    if pool is None:
        pool = await asyncpg.create_pool(settings.database_url, min_size=1, max_size=10, command_timeout=30)
    return pool


async def disconnect() -> None:
    global pool
    if pool is not None:
        await pool.close()
        pool = None


async def get_pool() -> asyncpg.Pool:
    return pool or await connect()
