"""Compress the complete GeoAI Risks package for the shared drive.

The complete package gathers everything an adopting instructor or a co-author
needs in one file: the review punch list, the Canvas course package, the whole
Exercises tree including the built site and its upload zip, the lecture decks,
and the newest exercise package zip. The composition matches the first
complete package of 2026-09-21. It holds answer keys throughout, so it never
goes to a class.

Build order matters, because this package nests the others:
    1. site/build_site.py, then site/make_upload_zip.py, if the site changed
    2. toolkit/make_package_zip.py
    3. toolkit/make_complete_package.py

Each run writes a fresh directive 3.11 stamp. Delete the previous complete
package and the previous exercise package once the new ones check out.

Usage:
    python make_complete_package.py
"""

import zipfile
from datetime import datetime
from pathlib import Path

ROOT = Path(__file__).resolve().parents[1]      # the Exercises folder
DEST = ROOT.parent                              # the GeoAI project folder
FOLDERS = ("Canvas Package", "Exercises", "Slide Decks")
SKIP_DIRS = {"__pycache__"}


def keep(p: Path) -> bool:
    rel = p.relative_to(DEST)
    if any(part in SKIP_DIRS for part in rel.parts):
        return False
    return p.suffix != ".pyc" and not p.name.startswith("~$")


def main() -> int:
    stamp = datetime.now().strftime("%Y-%m-%d_%H%M")
    out = DEST / f"GeoAIRisks_Complete_Package_{stamp}_TJB.zip"
    punch = sorted(DEST.glob("GeoAIRisks_Review_Punch_List_*.docx"))
    packages = sorted(DEST.glob("GeoAIRisks_Exercise_Package_*.zip"))
    if not punch or not packages:
        print("punch list or exercise package missing; run make_package_zip.py first")
        return 1
    tops = [punch[-1], packages[-1]]
    n = 0
    with zipfile.ZipFile(out, "w", zipfile.ZIP_DEFLATED, compresslevel=9) as z:
        for name in FOLDERS:
            base = DEST / name
            for p in [base] + sorted(base.rglob("*")):
                if not keep(p):
                    continue
                arc = p.relative_to(DEST).as_posix()
                if p.is_dir():
                    # Directory entries stored, as in the 2026-09-21 original.
                    z.writestr(zipfile.ZipInfo(arc + "/"), b"",
                               compress_type=zipfile.ZIP_STORED)
                else:
                    z.write(p, arc)
                    n += 1
        for p in tops:
            z.write(p, p.name)
            n += 1
    print(f"wrote {out}")
    print(f"  files   : {n}")
    print(f"  size    : {out.stat().st_size / 1_048_576:.1f} MB")
    print(f"  nested  : {tops[1].name}")
    return 0


if __name__ == "__main__":
    raise SystemExit(main())
