from typing import Optional

from boss.interactor.authenticate_nfc.dto import AuthenticateNfcRequest
from boss.model import User
from boss.model.repository import UserRepository
from boss.util import generate_hmac_md5_hash
from .._BaseInteractor import BaseInteractor


class AuthenticateNfcInteractor(BaseInteractor):

    def __init__(
        self,
        user_repository: UserRepository,
    ) -> None:

        self.user_repository = user_repository

    def execute(
        self,
        request: AuthenticateNfcRequest,
    ) -> Optional[User]:

        nfc_uuid_hash = self.get_nfc_uuid_hash(nfc_uuid=request.nfc_uuid)
        return self.user_repository.get_user_by_nfc_uuid(nfc_uuid=nfc_uuid_hash)

    @staticmethod
    def get_nfc_uuid_hash(
        nfc_uuid: str,
    ) -> str:

        key = str(ord(str(nfc_uuid)[5]) + 0x18122008)
        return generate_hmac_md5_hash(
            message=str(nfc_uuid),
            key=key,
        )
