import pydantic
from sqlalchemy.ext.asyncio import (
    async_sessionmaker as sqlalchemy_async_sessionmaker,
    AsyncEngine as SQLAlchemyAsyncEngine,
    AsyncSession as SQLAlchemyAsyncSession,
    create_async_engine as create_sqlalchemy_async_engine,
)
from sqlalchemy.pool import Pool as SQLAlchemyPool, QueuePool as SQLAlchemyQueuePool
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.pool import NullPool  # Pool adecuado para asyncio

#from src.config.manager import settings
from app.core import config


class AsyncDatabase:
    def __init__(self):
        \
        self.postgres_uri: pydantic.PostgresDsn = pydantic.PostgresDsn(
            url=f"{config.DB_POSTGRES_SCHEMA}://{config.DB_USER}:{config.DB_PASSWORD}@{config.DB_HOST}:{config.DB_PORT}/{config.DB_NAME}"
           # ,scheme=config.DB_POSTGRES_SCHEMA
        )
        self.async_engine: SQLAlchemyAsyncEngine = create_sqlalchemy_async_engine(
            #url=self.postgres_uri,
            url =f"{config.DB_POSTGRES_SCHEMA}://{config.DB_USER}:{config.DB_PASSWORD}@{config.DB_HOST}:{config.DB_PORT}/{config.DB_NAME}",
            echo=config.IS_DB_ECHO_LOG,
            pool_size=config.DB_POOL_SIZE,
            max_overflow=config.DB_POOL_OVERFLOW,
            #poolclass=SQLAlchemyQueuePool,
        )
        
      #  self.async_session: SQLAlchemyAsyncSession = SQLAlchemyAsyncSession(bind=self.async_engine)
       # self.pool: SQLAlchemyPool = self.async_engine.pool

    @property
    def set_async_db_uri(self) -> str | pydantic.PostgresDsn:
        """
        Set the synchronous database driver into asynchronous version by utilizing AsyncPG:

            `postgresql://` => `postgresql+asyncpg://`
        """
        return self.postgres_uri
    """
        return (
            #self.postgres_uri.replace("postgresql://", "postgresql+asyncpg://")
            if self.postgres_uri
            else self.postgres_uri
"""

async_db: AsyncDatabase = AsyncDatabase()

#Base = declarative_base()

