#!/usr/bin/env python3
"""
Extract presenter notes from a Keynote .key file.

Usage:
  python extract_keynote_notes.py path/to/deck.key --out notes.json

Notes:
- A .key file is a package (zip). The presenter notes live in an XML-ish file
  typically called index.apxl inside the package.
- This script tries to be robust across minor format changes by searching for
  likely note fields.
"""
import json
import os
import subprocess
import argparse
import json
import os
import re
import shutil
import tempfile
import zipfile
from pathlib import Path

from gnommo.parser import _read_json


def write_manuscript(data: Path, out_path: Path):
    data = _read_json(data.read_text(encoding="utf-8"))
    lines = []
    i = 0
    for item in data:
        print(f"Writing notes for slide {i} to file")
        idx = item.get("slide_index")
        notes = (item.get("notes") or "").rstrip()

        lines.append(f"[S{idx}]")
        lines.append(notes)
        lines.append("")  # blank line between slides
        i += 1

    out_path.write_text("\n".join(lines).rstrip() + "\n", encoding="utf-8")
    print(f"Wrote {out_path}")


def main():
    keynote_file = Path("video1/video1.key").expanduser().resolve()
    if not keynote_file.exists():
        raise FileNotFoundError(f"Keynote file not found: {keynote_file}")

    script_file = Path("gnommo/extract_keynote_notes.js").expanduser().resolve()
    if not script_file.exists():
        raise FileNotFoundError(f"Extractor script not found: {script_file}")

    presenter_notes_json_file = Path("video1/manuscript.json").expanduser().resolve()

    # Run JXA extractor
    proc = subprocess.run(
        [
            "osascript",
            "-l",
            "JavaScript",
            str(script_file),
            str(keynote_file),
        ],
        capture_output=True,
        text=True,
    )

    if proc.returncode != 0:
        raise RuntimeError(
            "Failed to extract presenter notes:\n"
            f"STDERR:\n{proc.stderr}\n"
            f"STDOUT:\n{proc.stdout}"
        )

    # Write JSON output
    presenter_notes_json_file.write_text(proc.stdout, encoding="utf-8")

    if not presenter_notes_json_file.exists():
        raise FileNotFoundError(
            f"Failed to extract presenter notes to {presenter_notes_json_file}"
        )

    # Convert JSON → manuscript.txt
    write_manuscript(
        presenter_notes_json_file, out_path=keynote_file.parent / "manuscript.txt"
    )


if __name__ == "__main__":
    main()
