from psycopg import Connection
from src.__types.index import GroupBy, QuotaUsage, Tenant
from src.domain.entities.quota_allocation import QuotaAllocationStatus
from src.infrastructure.repositories.quota_usage_repository import QuotaUsageRepository
from src.tracer import tracer


class QuotaUsageUseCase:
    def __init__(self, repository: QuotaUsageRepository) -> None:
        self.repository = repository

    @classmethod
    def create(cls, tenant: Tenant, connection: Connection) -> "QuotaUsageUseCase":
        return QuotaUsageUseCase(repository=QuotaUsageRepository(tenant=tenant, connection=connection))

    @tracer.capture_method
    def query(
        self,
        group_by: list[GroupBy],
        quota_allocation_status: list[QuotaAllocationStatus],
        year: int | None,
    ) -> list[QuotaUsage]:
        """クォータ使用実績を集計する"""
        return self.repository.aggregate(
            group_by=group_by,
            quota_allocation_status=quota_allocation_status,
            year=year,
        )
