import typing as t
from uuid import uuid4

import flask_login

from server.config import get_app_config

cnf = get_app_config()
POOL: list["User"] = []


class User(flask_login.UserMixin):
    def __init__(self):
        super().__init__()
        self.user_name = cnf.admin_login
        self._password = cnf.admin_pass
        self.alternative_id = None
        self.id = self.user_name

    @property
    def password(self):
        return self._password

    def check_password(self, password: str | None) -> bool:
        if password is not None:
            if self._password == password:
                self.alternative_id = str(uuid4())
                POOL.append(self)
                return True
        return False

    def get_id(self):
        """
        Override flask_login.UserMixin.get_id to return alternative_id for security.
        """
        return self.alternative_id

    def __repr__(self):
        return self.user_name

    @staticmethod
    def get(data, field) -> t.Optional["User"]:
        if field != "username":
            return None
        if data != cnf.admin_login:
            return None
        return User()
