from datetime import timedelta, datetime
from models import LeaveBalance, LeaveType,Employee
from extensions import db

def calculate_sandwich_days(start_date, end_date, holidays, leave_type, settings):
    """
    Returns total leave days including weekends/holidays if sandwich logic is applicable.
    """
    if not leave_type.sandwich_applicable or not settings.sandwich_policy_enabled:
        return (end_date - start_date).days + 1

    sandwich_days = 0
    curr = start_date
    while curr <= end_date:
        if curr.weekday() in (5, 6) or curr in holidays:  # Sat=5, Sun=6
            sandwich_days += 1
        curr += timedelta(days=1)

    total_days = (end_date - start_date).days + 1
    return total_days  # include all — sandwich days are part of range


