"""
Availability Checker Module

Handles checking property availability for booking dates.
"""

from datetime import datetime


class AvailabilityChecker:
    """
    Check property availability for booking dates.
    
    Validates:
    - Date overlaps with existing bookings
    - Booking date validity
    """
    
    def __init__(self):
        """Initialize AvailabilityChecker"""
        pass
    
    def is_available(self, check_in, check_out, existing_bookings):
        """
        Check if property is available for the requested dates.
        
        Args:
            check_in (str or datetime): Requested check-in date (YYYY-MM-DD if string)
            check_out (str or datetime): Requested check-out date (YYYY-MM-DD if string)
            existing_bookings (list): List of existing bookings, each containing:
                - check_in (str or datetime): Existing booking check-in
                - check_out (str or datetime): Existing booking check-out
        
        Returns:
            bool: True if available, False if dates conflict with existing bookings
        
        Raises:
            ValueError: If check_out is before or same as check_in
        
        Example:
            >>> checker = AvailabilityChecker()
            >>> existing = [
            ...     {'check_in': '2025-11-15', 'check_out': '2025-11-20'},
            ...     {'check_in': '2025-12-10', 'check_out': '2025-12-15'}
            ... ]
            >>> checker.is_available('2025-12-01', '2025-12-05', existing)
            True
            >>> checker.is_available('2025-11-18', '2025-11-22', existing)
            False  # Overlaps with first booking
        """
        # Convert strings to datetime if needed
        if isinstance(check_in, str):
            requested_check_in = datetime.strptime(check_in, '%Y-%m-%d')
        else:
            requested_check_in = check_in
        
        if isinstance(check_out, str):
            requested_check_out = datetime.strptime(check_out, '%Y-%m-%d')
        else:
            requested_check_out = check_out
        
        # Validate dates
        if requested_check_out <= requested_check_in:
            raise ValueError("Check-out date must be after check-in date")
        
        # Check each existing booking for overlap
        for booking in existing_bookings:
            # Convert booking dates to datetime if needed
            if isinstance(booking['check_in'], str):
                booking_check_in = datetime.strptime(booking['check_in'], '%Y-%m-%d')
            else:
                booking_check_in = booking['check_in']
            
            if isinstance(booking['check_out'], str):
                booking_check_out = datetime.strptime(booking['check_out'], '%Y-%m-%d')
            else:
                booking_check_out = booking['check_out']
            
            # Check for date overlap
            # Bookings overlap if:
            # - Requested check-in is before existing check-out AND
            # - Requested check-out is after existing check-in
            if requested_check_in < booking_check_out and requested_check_out > booking_check_in:
                # Dates overlap - not available
                return False
        
        # No overlaps found - available
        return True
    
    def check_date_overlap(self, start1, end1, start2, end2):
        """
        Check if two date ranges overlap.
        
        Args:
            start1 (str or datetime): First range start date
            end1 (str or datetime): First range end date
            start2 (str or datetime): Second range start date
            end2 (str or datetime): Second range end date
        
        Returns:
            bool: True if ranges overlap, False otherwise
        
        Example:
            >>> checker = AvailabilityChecker()
            >>> checker.check_date_overlap('2025-11-15', '2025-11-20', '2025-11-18', '2025-11-22')
            True
            >>> checker.check_date_overlap('2025-11-15', '2025-11-20', '2025-11-25', '2025-11-30')
            False
        """
        # Convert to datetime if needed
        if isinstance(start1, str):
            start1 = datetime.strptime(start1, '%Y-%m-%d')
        if isinstance(end1, str):
            end1 = datetime.strptime(end1, '%Y-%m-%d')
        if isinstance(start2, str):
            start2 = datetime.strptime(start2, '%Y-%m-%d')
        if isinstance(end2, str):
            end2 = datetime.strptime(end2, '%Y-%m-%d')
        
        # Check overlap: start1 < end2 AND end1 > start2
        return start1 < end2 and end1 > start2