"""Package the accompanying resource-free document with Python 3, without MCP.

Usage: python3 create-package.py [document.json] [presentation.tryslides]
This creates the container, not a replacement for TrySlides Core validation.
"""

import hashlib
import json
from pathlib import Path
import sys
import zipfile


def package_document(source, destination):
    document_bytes = source.read_bytes()
    document = json.loads(document_bytes)
    if document.get("schemaVersion") != 16:
        raise ValueError("Use the current schema-version-16 example document.")
    for slide in document["slides"]:
        if slide.get("steps") or any(
            element.get("type") != "shape" for element in slide["elements"]
        ):
            raise ValueError("This example packages Base-only shapes without resources.")
    manifest = {
        "format": "tryslides",
        "formatVersion": 1,
        "metadata": {
            "title": document["title"],
            "createdAt": document["createdAt"],
        },
        "document": {
            "path": "document.json",
            "size": len(document_bytes),
            "sha256": hashlib.sha256(document_bytes).hexdigest(),
        },
        "assets": [],
        "fonts": [],
    }
    if destination.suffix != ".tryslides":
        raise ValueError("The output filename must end in .tryslides.")
    # Exclusive creation protects existing presentations from accidental replacement.
    with zipfile.ZipFile(destination, "x", compression=zipfile.ZIP_DEFLATED) as archive:
        for name, data in (
            ("manifest.json", json.dumps(manifest).encode("utf-8")),
            ("document.json", document_bytes),
        ):
            entry = zipfile.ZipInfo(name, date_time=(2026, 10, 14, 0, 0, 0))
            entry.compress_type = zipfile.ZIP_DEFLATED
            archive.writestr(entry, data)
    print(f"Created {destination}. Import it into TrySlides to validate the document.")


if __name__ == "__main__":
    source_path = Path(sys.argv[1]) if len(sys.argv) > 1 else Path(__file__).with_name("document.json")
    output_path = Path(sys.argv[2]) if len(sys.argv) > 2 else Path.cwd() / "presentation.tryslides"
    package_document(source_path, output_path)
