from django import template

register = template.Library()


@register.filter
def natural_duration(value):
    if not value:
        return
    value = int(value)
    days = int(value / 86400)
    remainder = value % 86400
    hours = int(remainder / 3600)
    minutes = int((remainder % 3600) / 60)
    seconds = remainder % 60
    parts = []
    if days:
        parts.append(f"{days} d")
    if hours:
        parts.append(f"{hours} hr")
    if minutes:
        parts.append(f"{minutes} min")
    if seconds or not parts:
        parts.append(f"{seconds} sec")
    return ", ".join(parts)
