"""Exception classes for 1Password CLI integration."""


class OnePasswordException(Exception):
    """Base exception for all 1Password-related errors."""

    def __init__(self, message: str) -> None:
        """Initialize the exception with a message.

        Args:
            message: Human-readable error description
        """
        self.message = message
        super().__init__(message)

    def __str__(self) -> str:
        """Return string representation of the exception."""
        return self.message

    def __repr__(self) -> str:
        """Return detailed representation of the exception."""
        return f"{self.__class__.__name__}({self.message!r})"


class OnePasswordCLIError(OnePasswordException):
    """Exception raised when there's an issue with the 1Password CLI itself."""

    pass


class OnePasswordNotInstalledError(OnePasswordCLIError):
    """Exception raised when the 1Password CLI is not installed or not found in PATH."""

    def __init__(self) -> None:
        """Initialize the exception with a default message."""
        super().__init__(
            "1Password CLI not found. Please install it from https://developer.1password.com/docs/cli/get-started/"
        )


class OnePasswordNotAuthenticatedError(OnePasswordCLIError):
    """Exception raised when the user is not authenticated with 1Password CLI."""

    def __init__(self) -> None:
        """Initialize the exception with a default message."""
        super().__init__("Not authenticated with 1Password CLI. Please run 'op signin' to authenticate.")


class SecretInjectionError(OnePasswordException):
    """Exception raised when secret injection fails."""

    def __init__(self, message: str, reference: str | None = None) -> None:
        """Initialize the exception with a message and optional reference.

        Args:
            message: Human-readable error description
            reference: The 1Password secret reference that failed (e.g., "op://vault/item/field")
        """
        self.reference = reference
        full_message = f"{message} (reference: {reference})" if reference else message
        super().__init__(full_message)

    def __repr__(self) -> str:
        """Return detailed representation of the exception."""
        if self.reference:
            return f"{self.__class__.__name__}({self.message!r}, reference={self.reference!r})"
        return f"{self.__class__.__name__}({self.message!r})"
