import asyncio
from datetime import datetime
from typing import List, Optional, TYPE_CHECKING

if TYPE_CHECKING:
    from ...domain.entities.consultor import Consultor


class RankingCache:
    def __init__(self, ttl_seconds: int = 300):
        self.ttl = ttl_seconds
        self._cache: List["Consultor"] = []
        self._last_update: Optional[datetime] = None
        self._loading = False
        self._lock = asyncio.Lock()

    def is_valid(self) -> bool:
        if not self._cache or not self._last_update:
            return False
        return (datetime.now() - self._last_update).total_seconds() < self.ttl

    def get(self) -> List["Consultor"]:
        return self._cache

    def set(self, consultores: List["Consultor"]) -> None:
        self._cache = consultores
        self._last_update = datetime.now()

    def invalidate(self) -> None:
        self._cache = []
        self._last_update = None


ranking_cache = RankingCache(ttl_seconds=300)
