import random
from functools import lru_cache

from loguru import logger
from pydantic import BaseModel, Field


class HelloGeneratorSvc(BaseModel):
    hellos: list[str] = [
        "Hola",
        "Ayo",
        "Hi",
        "Bon día",
        "Hola mi tremendo elemento",
        "HOLA",
        "hiii",
    ]

    greeted_users: dict[str, str] = Field(default_factory=dict)

    def run(self, username: str) -> str | None:
        if username in self.greeted_users:
            logger.info(f"User {username} already greeted")
            return None

        greeting = f"{random.choice(self.hellos)} @{username}"  # noqa: S311

        self.greeted_users[username] = greeting

        return greeting


@lru_cache(maxsize=1)
def get_hello_generator_svc() -> HelloGeneratorSvc:
    return HelloGeneratorSvc()  # pragma: no cover
