"""
Custom exceptions for the application.
"""
from fastapi import HTTPException, status


class NotFoundException(HTTPException):
    """Resource not found exception."""
    def __init__(self, detail: str = "Resource not found"):
        super().__init__(status_code=status.HTTP_404_NOT_FOUND, detail=detail)


class BadRequestException(HTTPException):
    """Bad request exception."""
    def __init__(self, detail: str = "Bad request"):
        super().__init__(status_code=status.HTTP_400_BAD_REQUEST, detail=detail)


class UnauthorizedException(HTTPException):
    """Unauthorized exception."""
    def __init__(self, detail: str = "Unauthorized"):
        super().__init__(
            status_code=status.HTTP_401_UNAUTHORIZED,
            detail=detail,
            headers={"WWW-Authenticate": "Bearer"}
        )


class ForbiddenException(HTTPException):
    """Forbidden exception."""
    def __init__(self, detail: str = "Forbidden"):
        super().__init__(status_code=status.HTTP_403_FORBIDDEN, detail=detail)


class ConflictException(HTTPException):
    """Conflict exception (e.g., duplicate resource)."""
    def __init__(self, detail: str = "Resource conflict"):
        super().__init__(status_code=status.HTTP_409_CONFLICT, detail=detail)


class RoomNotAvailableException(BadRequestException):
    """Room is not available for the requested dates."""
    def __init__(self, room_id: int = None):
        detail = "Room is not available for the requested dates"
        if room_id:
            detail = f"Room {room_id} is not available for the requested dates"
        super().__init__(detail=detail)


class ReservationConflictException(ConflictException):
    """Reservation dates conflict with existing reservation."""
    def __init__(self, detail: str = "Reservation dates conflict with existing booking"):
        super().__init__(detail=detail)


class InvalidStatusTransitionException(BadRequestException):
    """Invalid status transition for a resource."""
    def __init__(self, current_status: str, target_status: str):
        detail = f"Cannot transition from '{current_status}' to '{target_status}'"
        super().__init__(detail=detail)


class InsufficientPaymentException(BadRequestException):
    """Insufficient payment for checkout."""
    def __init__(self, amount_due: float):
        detail = f"Insufficient payment. Amount due: {amount_due:.2f}"
        super().__init__(detail=detail)


class FolioNotClosedException(BadRequestException):
    """Folio must be closed before checkout."""
    def __init__(self):
        super().__init__(detail="Folio must be settled before checkout")
