
    j              
           d Z ddlmZmZmZ ddlmZ ddlmZm	Z	 ddl
mZ ddlmZmZ dee   defd	Zd
ededededef
dZdedede	e   fdZdefdZddedededefdZddededefdZddee   dedefdZy)z
Dashboard utility functions for Aimantis hotel management platform.

Provides reusable helper functions for:
- Occupancy night calculations
- Channel name normalization
- Date utilities
- Query optimization helpers
    )date	timedeltatimezone)Decimal)OptionalList)r   )CHANNEL_DISPLAY_NAMESDEFAULT_CHANNEL_NAMEplatformreturnc                     | st         S | j                         j                         }d|v ry|t        v r	t        |   S t        j                         D ]  \  }}||v s|c S  t         S )a  
    Normalize platform/channel name to user-friendly display name.
    
    Args:
        platform: Raw platform string from booking (e.g., "airbnb", "Booking")
        
    Returns:
        Normalized display name (e.g., "Airbnb", "Booking.com", "Direct")
    experiaExpedia)r
   lowerstripr	   items)r   platform_lowerkeydisplay_names       $/backend/dashboard/services/utils.pynormalize_channel_namer      su     ##^^%++-N N" ..$^44 388:\.  ;      check_in	check_outwindow_start
window_endc                 h    t        | |      }t        ||      }||z
  j                  }t        d|      S )a  
    Calculate number of nights a booking overlaps with a given date window.
    
    This is the core occupancy calculation function used across all
    occupancy metrics.
    
    Args:
        check_in: Booking check-in date
        check_out: Booking check-out date
        window_start: Start of the analysis window (inclusive)
        window_end: End of the analysis window (exclusive)
        
    Returns:
        Number of overlapping nights (0 if no overlap)
    r   )maxmindays)r   r   r   r   effective_starteffective_endoverlap_dayss          r   calculate_overlapping_nightsr$   3   s9    , (L1O	:.M!O399Lq,r   startendc                 d    g }| }||k  r&|j                  |       |t        d      z  }||k  r&|S )z
    Generate a list of dates from start to end (exclusive).
    
    Args:
        start: Start date (inclusive)
        end: End date (exclusive)
        
    Returns:
        List of date objects
       )r    )appendr   )r%   r&   datescurrents       r   get_date_ranger,   Q   s@     EG
C-W9!$$ C- Lr   c                  *    t        j                         S )z{
    Get today's date using Django's timezone utilities.
    
    Returns:
        Current date in the active timezone
    )django_timezone	localdate r   r   	get_todayr1   d   s     $$&&r   	numeratordenominatordefaultc                     |dk(  r|S | |z  S )a  
    Safely divide two numbers, returning default if denominator is zero.
    
    Args:
        numerator: The dividend
        denominator: The divisor
        default: Value to return if denominator is zero
        
    Returns:
        Division result or default value
    r   r0   )r2   r3   r4   s      r   safe_divider6   n   s     a{""r   valuedecimalsc                     t        | |      S )z
    Round occupancy percentage to specified decimal places.
    
    Args:
        value: Occupancy percentage
        decimals: Number of decimal places (default: 0 for integer)
        
    Returns:
        Rounded occupancy percentage
    )roundr7   r8   s     r   round_occupancyr<      s     !!r   c                 2    | yt        t        | |            S )z
    Safely convert Decimal to float with rounding.
    
    Args:
        value: Decimal value
        decimals: Number of decimal places
        
    Returns:
        Rounded float value
            )floatr:   r;   s     r   decimal_to_floatr@      s     }uh'((r   N)r>   )r   )   )__doc__datetimer   r   r   decimalr   typingr   r   django.utilsr.   dashboard.constantsr	   r
   strr   intr$   r,   r1   r?   r6   r<   r@   r0   r   r   <module>rJ      s    / .  ! 4 K Xc]  s  >      	 
 	 <$ T d4j &'4 '#5 #u #u #u #""5 "C " ")HW- ) )U )r   