from __future__ import annotations

from typing import Any

from app.domain.mappers import to_goal
from app.repositories.directus.goals import GoalRepository
from app.schemas.admin import GoalRead, goal_to_read_model


class GoalService:
    """Application service for experiment goal management."""

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

    async def list_goals(
        self,
        site_id: str | None = None,
        access_token: str | None = None,
    ) -> list[GoalRead]:
        params: dict[str, Any] | None = None
        if site_id:
            params = {"filter[site_id][_eq]": site_id}
        items = await self.repository.list(params=params, access_token=access_token)
        return [goal_to_read_model(to_goal(item)) for item in items]

    async def get_goal(
        self,
        goal_id: str,
        access_token: str | None = None,
    ) -> GoalRead | None:
        item = await self.repository.get(goal_id, access_token=access_token)
        if not item:
            return None
        return goal_to_read_model(to_goal(item))
