#!/usr/bin/env python3
"""Statusline plugin: write session metadata to STATUSLINE_SESSION_DIR/metadata.json.

Standalone executable AND importable. No imports from the statusline package —
this file is self-contained when copied to statusline.d/.
"""
import json
import os
import sys
from pathlib import Path


def run(session_data, session_dir):
    """Write metadata.json from session_data dict into session_dir.

    Importable entry point for testing.
    """
    out = Path(session_dir)
    out.mkdir(parents=True, exist_ok=True)

    with open(out / "metadata.json", "w") as f:
        json.dump({
            "session_id": session_data.get("session_id"),
            "transcript_path": session_data.get("transcript_path"),
        }, f)


def main():
    session_dir = os.environ.get("STATUSLINE_SESSION_DIR")
    if not session_dir:
        return

    try:
        session_data = json.load(sys.stdin)
    except Exception:
        return

    run(session_data, session_dir)


if __name__ == "__main__":
    main()
