from mongoengine import StringField, ReferenceField, DateTimeField
 
from app.models import Base

class Blacklist(Base):
    """
    Model for blacklisting tokens or entities.

    Attributes:
        token (str): The token or entity to be blacklisted (required).
        token_type (str): The type of token (e.g., 'access' or 'refresh').
        user (User): The user associated with the token.
        jti (str): The unique JWT ID (if applicable).
        reason (str): The reason for blacklisting (e.g., 'expired' or 'revoked').
        blacklisted_at (datetime): The date and time when the token was blacklisted.

    Methods:
        __str__(): Return the token as a string representation.
        is_token_valid(): Check if the token is still valid and not blacklisted.
        blacklist(reason: str): Blacklist the token with a given reason.
    """

    token      = StringField(help_text="The token or entity to be blacklisted.")
    token_type = StringField(help_text="The type of token (e.g., 'access' or 'refresh').")
    user       = ReferenceField('User', help_text="The user associated with the token.")
    jti        = StringField(required=True, help_text="The unique JWT ID (if applicable).")
    reason     = StringField(help_text="The reason for blacklisting.")
    revoked_at = DateTimeField(help_text="The date and time when the token was blacklisted.")

    def __str__(self) -> str:
        """
        Return the token as a string representation.

        Returns:
            str: The token or entity being blacklisted.
        """
        return self.token