from __future__ import annotations

from app.domain.mappers import to_site
from app.repositories.directus.sites import SiteRepository
from app.schemas.admin import SiteRead, site_to_read_model


class SiteService:
    """Application service for site administration.

    Site is a good example of a content-like resource:
    FastAPI owns orchestration, while Directus remains the audited store.
    """

    def __init__(self, repository: SiteRepository | None = None) -> None:
        self.repository = repository or SiteRepository()

    async def list_sites(self, access_token: str | None = None) -> list[SiteRead]:
        items = await self.repository.list(access_token=access_token)
        return [site_to_read_model(to_site(item)) for item in items]

    async def get_site(
        self,
        site_id: str,
        access_token: str | None = None,
    ) -> SiteRead | None:
        item = await self.repository.get(site_id, access_token=access_token)
        if not item:
            return None
        return site_to_read_model(to_site(item))
