import json
from typing import Dict
from datetime import datetime

class User:
    '''
    This class represents a user in the SMART system
    '''

    def __init__(self, username : str, password : str, role : str ="u"):
        '''
        Initializes the new user object with the given data

        @params:
            username    - Required : unique identifier for the user i.e. an E-Mail address
            password    - Required : raw, unhashed password used to authenticate the user later
            role        - Optional : indicates the privileges of the user "u": standard user
        '''
        self.username = username
        self.password = password
        self.created_at = str(datetime.now())
        self.last_login = str(datetime.now())
        self.role = role

    def to_serializable_dict(self) -> Dict:
        return {
            "username": self.username,
            "password": self.password,
            "role": self.role,
            "created_at": self.created_at,
            "last_login": self.last_login,
        }

    @staticmethod
    def from_serializable_dict(user_dict: Dict):
        result = User(user_dict["username"], user_dict["password"], user_dict["role"])
        result.created_at = user_dict["created_at"]
        result.last_login = user_dict["last_login"]
        return result

    def __repr__(self):
        return json.dumps(self.to_serializable_dict())

    def __str__(self):
        return f"User({self.__repr__()})"
