from scurrypy import (
    Addon,
    UserModel,
    EmbedAuthorPart
)

class EmbedBuilder(Addon):
    """Common Embed helpers."""
    
    @staticmethod
    def user_author(user: UserModel) -> EmbedAuthorPart:
        """Embed author builder.

        Args:
            user (UserModel): user author

        Returns:
            (EmbedAuthorPart): the EmbedAuthorPart object
        """
        if not user:
            raise ValueError("Missing user.")
        
        return EmbedAuthorPart(
            name=user.username,
            icon_url=f"https://cdn.discordapp.com/avatars/{user.id}/{user.avatar}.png"
        )
    
    @staticmethod
    def timestamp() -> str:
        """Embed timestamp builder. Adheres to ISO8601 format.

        Returns:
            (str): message timestamp
        """
        from datetime import datetime, timezone

        return datetime.now(timezone.utc).isoformat()

    @staticmethod
    def color(hex: str) -> int:
        """Converts a color hex to an integer.

        Args:
            hex (str): color hex (formatted as #{0-9, a-f})

        Raises:
            (ValueError): invalid hex

        Returns:
            (int): integer representation of color hex
        """
        try:
            return int(hex.removeprefix('#'), 16)
        except ValueError:
            raise ValueError("Format Error: Color must be formatted as #{0-9, a-f}.")
