"""Exceptions for container build operations."""

from __future__ import annotations


class BuildError(Exception):
    """Base exception for build operations."""

    pass


class ContainerNotFoundError(BuildError):
    """Container not found for building."""

    def __init__(self, container_name: str) -> None:
        super().__init__(f"Container not found: {container_name}")
        self.container_name = container_name


class ContainerNotRunningError(BuildError):
    """Container must be running for certain build operations."""

    def __init__(self, container_name: str) -> None:
        super().__init__(
            f"Container '{container_name}' is not running. "
            "Some build operations require a running container."
        )
        self.container_name = container_name


class BaseImageNotFoundError(BuildError):
    """Could not determine base image for container."""

    def __init__(self, container_name: str, reason: str | None = None) -> None:
        message = f"Could not determine base image for '{container_name}'"
        if reason:
            message += f": {reason}"
        super().__init__(message)
        self.container_name = container_name
        self.reason = reason


class DockerfileGenerationError(BuildError):
    """Failed to generate Dockerfile from container."""

    def __init__(
        self,
        container_name: str,
        reason: str,
        cause: Exception | None = None,
    ) -> None:
        super().__init__(
            f"Failed to generate Dockerfile for '{container_name}': {reason}"
        )
        self.container_name = container_name
        self.reason = reason
        self.cause = cause


class ImageBuildError(BuildError):
    """Failed to build image from container."""

    def __init__(
        self,
        container_name: str,
        reason: str,
        build_log: list[str] | None = None,
        cause: Exception | None = None,
    ) -> None:
        super().__init__(f"Failed to build image for '{container_name}': {reason}")
        self.container_name = container_name
        self.reason = reason
        self.build_log = build_log or []
        self.cause = cause


class ImageNotFoundError(BuildError):
    """Image not found for tagging or pushing."""

    def __init__(self, image_ref: str) -> None:
        super().__init__(f"Image not found: {image_ref}")
        self.image_ref = image_ref


class ImageTagError(BuildError):
    """Failed to tag image."""

    def __init__(
        self,
        image_ref: str,
        tag: str,
        reason: str,
        cause: Exception | None = None,
    ) -> None:
        super().__init__(f"Failed to tag '{image_ref}' as '{tag}': {reason}")
        self.image_ref = image_ref
        self.tag = tag
        self.reason = reason
        self.cause = cause


class RegistryError(BuildError):
    """Base exception for registry operations."""

    def __init__(self, registry: str, message: str) -> None:
        super().__init__(f"Registry error ({registry}): {message}")
        self.registry = registry


class RegistryAuthenticationError(RegistryError):
    """Failed to authenticate with registry."""

    def __init__(self, registry: str, reason: str | None = None) -> None:
        message = "Authentication failed"
        if reason:
            message += f": {reason}"
        super().__init__(registry, message)
        self.reason = reason


class RegistryPushError(RegistryError):
    """Failed to push image to registry."""

    def __init__(
        self,
        registry: str,
        image_tag: str,
        reason: str,
        cause: Exception | None = None,
    ) -> None:
        super().__init__(registry, f"Failed to push '{image_tag}': {reason}")
        self.image_tag = image_tag
        self.reason = reason
        self.cause = cause


class PackageDetectionError(BuildError):
    """Failed to detect packages in container."""

    def __init__(
        self,
        container_name: str,
        reason: str,
        cause: Exception | None = None,
    ) -> None:
        super().__init__(
            f"Failed to detect packages in '{container_name}': {reason}"
        )
        self.container_name = container_name
        self.reason = reason
        self.cause = cause


class UnsupportedRuntimeError(BuildError):
    """Runtime does not support build operations."""

    def __init__(self, runtime: str, operation: str) -> None:
        super().__init__(
            f"Runtime '{runtime}' does not support {operation}"
        )
        self.runtime = runtime
        self.operation = operation
