from typing import Any

from pydantic import field_validator
from typing_extensions import override

from lion_core.exceptions import LionValueError
from lion_core.types import Field, LnID

from .base_mail import BaseMail
from .package import Package, PackageCategory


class Mail(BaseMail):
    """
    A mail component with sender, recipient, and package.

    Attributes:
        sender (str): The ID of the sender node.
        recipient (str): The ID of the recipient node.
        package (Package): The package to be delivered.
    """

    sender: LnID = Field(
        ...,
        title="Sender",
        description="The ID of the sender node, or a role",
    )

    recipient: LnID = Field(
        ...,
        title="Recipient",
        description="The ID of the recipient node, or a role",
    )

    package: Package = Field(
        ...,
        title="Package",
        description="The package to be delivered.",
    )

    @property
    def category(self) -> PackageCategory:
        """
        Return the category of the package.

        Returns:
            PackageCategory: The category of the package.
        """
        return self.package.category

    @override
    @field_validator("sender", "recipient", mode="before")
    @classmethod
    def _validate_sender_recipient(cls, value: Any) -> LnID:
        """Validate the sender and recipient fields."""
        value = super()._validate_sender_recipient(value)
        if value == "N/A":
            raise LionValueError("Invalid sender or recipient for Mail")
        return value


# File: lion_core/communication/mail.py
