from api_graphql.abc.db_history_model_query_abc import DbHistoryModelQueryABC
from data.schemas.public.domain import Domain
from data.schemas.public.short_url import ShortUrl
from data.schemas.public.short_url_dao import shortUrlDao


class DomainHistoryQuery(DbHistoryModelQueryABC):
    def __init__(self):
        DbHistoryModelQueryABC.__init__(self, "Domain")

        self.set_field("name", lambda x, *_: x.name)
        self.set_field("shortUrls", self._get_urls)

    @staticmethod
    async def _get_urls(domain: Domain, *_):
        return await shortUrlDao.find_by(
            [
                {ShortUrl.domain_id: domain.id},
                {ShortUrl.deleted: False},
                {"updated": {"lessOrEqual": domain.updated}},
            ]
        )
