# app/exceptions/marketplace_products.py
"""
MarketplaceProduct management specific exceptions.
"""

from typing import Any

from .base import (
    BusinessLogicException,
    ConflictException,
    ResourceNotFoundException,
    ValidationException,
)


class MarketplaceProductNotFoundException(ResourceNotFoundException):
    """Raised when a product is not found."""

    def __init__(self, marketplace_product_id: str):
        super().__init__(
            resource_type="MarketplaceProduct",
            identifier=marketplace_product_id,
            message=f"MarketplaceProduct with ID '{marketplace_product_id}' not found",
            error_code="PRODUCT_NOT_FOUND",
        )


class MarketplaceProductAlreadyExistsException(ConflictException):
    """Raised when trying to create a product that already exists."""

    def __init__(self, marketplace_product_id: str):
        super().__init__(
            message=f"MarketplaceProduct with ID '{marketplace_product_id}' already exists",
            error_code="PRODUCT_ALREADY_EXISTS",
            details={"marketplace_product_id": marketplace_product_id},
        )


class InvalidMarketplaceProductDataException(ValidationException):
    """Raised when product data is invalid."""

    def __init__(
        self,
        message: str = "Invalid product data",
        field: str | None = None,
        details: dict[str, Any] | None = None,
    ):
        super().__init__(
            message=message,
            field=field,
            details=details,
        )
        self.error_code = "INVALID_PRODUCT_DATA"


class MarketplaceProductValidationException(ValidationException):
    """Raised when product validation fails."""

    def __init__(
        self,
        message: str,
        field: str | None = None,
        validation_errors: dict[str, str] | None = None,
    ):
        details = {}
        if validation_errors:
            details["validation_errors"] = validation_errors

        super().__init__(
            message=message,
            field=field,
            details=details,
        )
        self.error_code = "PRODUCT_VALIDATION_FAILED"


class InvalidGTINException(ValidationException):
    """Raised when GTIN format is invalid."""

    def __init__(self, gtin: str, message: str = "Invalid GTIN format"):
        super().__init__(
            message=f"{message}: {gtin}",
            field="gtin",
            details={"gtin": gtin},
        )
        self.error_code = "INVALID_GTIN"


class MarketplaceProductCSVImportException(BusinessLogicException):
    """Raised when product CSV import fails."""

    def __init__(
        self,
        message: str = "MarketplaceProduct CSV import failed",
        row_number: int | None = None,
        errors: dict[str, Any] | None = None,
    ):
        details = {}
        if row_number:
            details["row_number"] = row_number
        if errors:
            details["errors"] = errors

        super().__init__(
            message=message,
            error_code="PRODUCT_CSV_IMPORT_FAILED",
            details=details,
        )
