"""Export the live OpenAPI schema to a file.

    python -m app.commands.export_openapi
    python -m app.commands.export_openapi --out docs/openapi.json

Reads directly from `app.main.app`'s generated schema — the same one
served at /openapi.json — so it can never drift from what the running
API actually returns. Regenerate this after any route/schema change
that should be reflected in the committed copy (a Flutter/codegen
consumer works off the committed file, not a live server)."""

import argparse
import json
from pathlib import Path

from app.main import app


def main() -> None:
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument(
        "--out",
        default="openapi.json",
        help="Output path (default: openapi.json at the repo root)",
    )
    args = parser.parse_args()

    schema = app.openapi()
    out_path = Path(args.out)
    out_path.write_text(json.dumps(schema, indent=2, sort_keys=True) + "\n")
    print(f"Wrote {out_path} ({len(schema.get('paths', {}))} paths)")


if __name__ == "__main__":
    main()
