"""Exception classes for Ghostty Settings TUI."""


class GhosttyTUIError(Exception):
    """Base exception for all Ghostty TUI errors."""

    def __init__(self, message: str, details: str | None = None):
        self.message = message
        self.details = details
        super().__init__(message)

    def __str__(self) -> str:
        if self.details:
            return f"{self.message}\n{self.details}"
        return self.message


class GhosttyNotFoundError(GhosttyTUIError):
    """Raised when Ghostty executable is not found."""

    def __init__(self):
        super().__init__(
            "Ghostty executable not found",
            "Please ensure Ghostty is installed and available in your PATH.\n"
            "Visit https://ghostty.org for installation instructions."
        )


class SchemaParseError(GhosttyTUIError):
    """Raised when schema parsing fails."""

    def __init__(self, message: str, line: int | None = None):
        details = f"Error at line {line}" if line else None
        super().__init__(f"Failed to parse schema: {message}", details)
        self.line = line


class ConfigReadError(GhosttyTUIError):
    """Raised when configuration file cannot be read."""

    def __init__(self, path: str, reason: str):
        super().__init__(
            f"Cannot read configuration file: {path}",
            reason
        )
        self.path = path


class ConfigWriteError(GhosttyTUIError):
    """Raised when configuration file cannot be written."""

    def __init__(self, path: str, reason: str):
        super().__init__(
            f"Cannot write configuration file: {path}",
            reason
        )
        self.path = path


class ConfigValidationError(GhosttyTUIError):
    """Raised when a configuration value is invalid."""

    def __init__(self, option_name: str, value: str, expected: str):
        super().__init__(
            f"Invalid value for '{option_name}': {value}",
            f"Expected: {expected}"
        )
        self.option_name = option_name
        self.value = value
        self.expected = expected


class TerminalSizeError(GhosttyTUIError):
    """Raised when terminal is too small."""

    def __init__(self, current: tuple[int, int], minimum: tuple[int, int]):
        super().__init__(
            f"Terminal too small: {current[0]}x{current[1]}",
            f"Minimum required: {minimum[0]}x{minimum[1]}"
        )
        self.current = current
        self.minimum = minimum


def handle_error(error: Exception) -> str:
    """Convert an exception to a user-friendly message.

    Args:
        error: The exception to handle

    Returns:
        User-friendly error message
    """
    if isinstance(error, GhosttyTUIError):
        return str(error)

    if isinstance(error, FileNotFoundError):
        return f"File not found: {error.filename}"

    if isinstance(error, PermissionError):
        return f"Permission denied: {error.filename}"

    if isinstance(error, OSError):
        return f"System error: {error.strerror}"

    return f"Unexpected error: {error}"
