"""Compress the whole exercise package for the shared drive.

The package carries the author side of the work: both editions, the fused
draft, every program, the frozen data, the figures, the handoff packages and
the site source. It holds answer keys throughout, so it goes to a co-author or
an instructor and never to a class. The student facing product is the student
edition and the public half of the companion website.

Two things stay out. The built site under site/_site duplicates material the
package already carries in source form, and site/geoai_site_upload.zip
duplicates that build again in compressed form.

Usage:
    python make_package_zip.py
"""

import zipfile
from datetime import datetime
from pathlib import Path

ROOT = Path(__file__).resolve().parents[1]
DEST = ROOT.parent
SKIP_DIRS = {"__pycache__", "_site", "_superseded"}
SKIP_FILES = {"geoai_site_upload.zip"}   # plus geoai_site_*.zip by pattern


def keep(p: Path) -> bool:
    rel = p.relative_to(ROOT)
    if any(part in SKIP_DIRS for part in rel.parts):
        return False
    if p.name.startswith("geoai_site_") and p.suffix == ".zip":
        return False
    return p.name not in SKIP_FILES and p.suffix != ".pyc"


def main() -> int:
    stamp = datetime.now().strftime("%Y-%m-%d_%H%M")
    out = DEST / f"GeoAIRisks_Exercise_Package_{stamp}_TJB.zip"
    files = sorted(p for p in ROOT.rglob("*") if p.is_file() and keep(p))
    with zipfile.ZipFile(out, "w", zipfile.ZIP_DEFLATED, compresslevel=9) as z:
        for p in files:
            z.write(p, Path("Exercises", p.relative_to(ROOT)).as_posix())
    print(f"wrote {out}")
    print(f"  entries : {len(files)}")
    print(f"  size    : {out.stat().st_size / 1_048_576:.1f} MB")
    return 0


if __name__ == "__main__":
    raise SystemExit(main())
