from core.logger import DBLogger
from data.schemas.public.short_url import ShortUrl
from data.schemas.public.short_url_visit import ShortUrlVisit

logger = DBLogger(__name__)

from core.database.abc.db_model_dao_abc import DbModelDaoABC


class ShortUrlVisitDao(DbModelDaoABC[ShortUrlVisit]):
    def __init__(self):
        DbModelDaoABC.__init__(self, __name__, ShortUrl, "public.short_url_visits")
        self.attribute(ShortUrlVisit.short_url_id, int)
        self.attribute(ShortUrlVisit.client, str)

    async def count_by_id(self, fid: int) -> int:
        result = await self._db.select_map(
            f"""SELECT COUNT(*) FROM {self._table_name}
            WHERE shortUrlId = {fid}
            AND deleted = FALSE
            AND client NOT LIKE '%wheregoes%'"""
        )
        if result is None or len(result) == 0:
            return 0
        return result[0]["count"]


shortUrlVisitDao = ShortUrlVisitDao()
