"""Swahili weekday/month names, for date displays that need to follow the
site's own session-based language toggle — Django's built-in `date`
template filter (`{{ d|date:'l' }}`) always renders in English here since
this app's bilingual system is a custom session flag, not Django's locale
framework, so it never picks up Kiswahili on its own."""

import datetime

WEEKDAY_NAMES_SW = {
    0: 'Jumatatu', 1: 'Jumanne', 2: 'Jumatano', 3: 'Alhamisi',
    4: 'Ijumaa', 5: 'Jumamosi', 6: 'Jumapili',
}
MONTH_NAMES_SW = {
    1: 'Januari', 2: 'Februari', 3: 'Machi', 4: 'Aprili', 5: 'Mei', 6: 'Juni',
    7: 'Julai', 8: 'Agosti', 9: 'Septemba', 10: 'Oktoba', 11: 'Novemba', 12: 'Desemba',
}


def format_weekday(d, lang):
    """Full weekday name, e.g. 'Jumatano' / 'Wednesday'."""
    if lang == 'sw':
        return WEEKDAY_NAMES_SW[d.weekday()]
    return d.strftime('%A')


def format_month_day(d, lang):
    """'Julai 15' / 'Jul 15' — full Swahili month name (no standard short
    form in common use), matching the app's existing abbreviated English
    style for the English side. Built without '%-d'/'%e' (not portable to
    Windows' strftime) so the day number never gets a stray leading zero."""
    if lang == 'sw':
        return f'{MONTH_NAMES_SW[d.month]} {d.day}'
    return f'{d.strftime("%b")} {d.day}'


def format_delivery_days_note(weekdays, lang):
    """'Deliveries run Monday, Wednesday, and Saturday only.' /
    'Utoaji unafanyika Jumatatu, Jumatano, na Jumamosi tu.' — built from
    whichever weekdays (0=Monday..6=Sunday) an admin has actually
    configured (see site_settings.get_delivery_weekdays()), so the note
    never drifts out of sync with the real schedule."""
    ordered = sorted(weekdays)
    if lang == 'sw':
        names = [WEEKDAY_NAMES_SW[d] for d in ordered]
        joiner, template = 'na', 'Utoaji unafanyika {days} tu.'
    else:
        names = [datetime.date(2001, 1, 1 + d).strftime('%A') for d in ordered]
        joiner, template = 'and', 'Deliveries run {days} only.'

    if len(names) == 1:
        days = names[0]
    elif len(names) == 2:
        days = f'{names[0]} {joiner} {names[1]}'
    else:
        days = ', '.join(names[:-1]) + f', {joiner} {names[-1]}'
    return template.format(days=days)
