from datetime import date, timedelta


def count_working_days(
    from_date: date,
    to_date: date,
    *,
    work_days: list[int] | None = None,
    holiday_dates: set[date] | None = None,
) -> int:
    if work_days is None:
        work_days = [0, 1, 2, 3, 4]
    if holiday_dates is None:
        holiday_dates = set()
    if from_date > to_date:
        return 0

    count = 0
    current = from_date
    while current <= to_date:
        if current.weekday() in work_days and current not in holiday_dates:
            count += 1
        current += timedelta(days=1)
    return count


def _get_tenant_work_days(tenant_id: int) -> list[int]:
    try:
        from simorgh.apps.app_settings.services import resolve
        value = resolve("hr.leave.work_days", tenant_id=tenant_id)
        if isinstance(value, list):
            return value
    except Exception:
        pass
    return [0, 1, 2, 3, 4]


def _get_tenant_holidays(tenant_id: int, from_date: date, to_date: date) -> set[date]:
    from simorgh.apps.hr_core.models import PublicHoliday
    holidays = PublicHoliday.objects.filter(
        tenant_id=tenant_id,
        date__gte=from_date,
        date__lte=to_date,
    ).values_list("date", flat=True)
    return set(holidays)
