from .user import user
from .user import user_factory
from .user import user_builder
from .users_collection import users_collection
from .app_route import app_route

class users_app(app_route):
    def __init__(self, collection: users_collection) -> None:
        self.__collection = collection

    def login(
        self, 
        nick: str,
        password: str
    ) -> dict:
        target = self.collection.login(nick, password)

        if target is None:
            return self._fail("Bad nick or password.")

        return self._success(apikey = target.apikey)

    def get(self, apikey: str) -> dict:
        target = self.collection.get(apikey)

        if target is None:
            return self._fail("User with that ApiKey not exists.")

        return self._success(
            apikey = target.apikey,
            nick = target.nick
        )

    @property
    def collection(self) -> users_collection:
        return self.__collection

