#!/usr/bin/env python3

from pathlib import Path
from datetime import date, timedelta

BACKUP_ROOT = Path("/BACKUP")
STALE_DAYS = 2

cutoff = (date.today() - timedelta(days=STALE_DAYS)).strftime("%Y%m%d")

for directory in sorted(BACKUP_ROOT.iterdir()):
    if not directory.is_dir():
        continue

    if directory.name == "default":
        continue

    latest_file = None
    latest_date = "00000000"

    for zip_file in directory.rglob("*.zip"):
        name = zip_file.name

        # expected format: YYYYMMDD_HHMM.zip
        try:
            date_part = name.split("_")[0]

            if len(date_part) != 8 or not date_part.isdigit():
                continue

        except Exception:
            continue

        if date_part > latest_date:
            latest_date = date_part
            latest_file = zip_file

    if latest_file is None:
        print(f"{directory.name} -> NO BACKUPS")
        continue

    status = "STALE" if latest_date < cutoff else "OK"

    print(
        f"{directory.name} -> "
        f"{latest_file.name} -> "
        f"{latest_date} -> "
        f"{status}"
    )
