"""
Mizan IR (KDL) export CLI for FastAPI backends.

Usage:
    python -m mizan_fastapi.ir <module>

Imports the named module (whose import side effects must register every
@client function with `mizan_core.registry`), then writes the canonical
Mizan IR as KDL to stdout. The Rust codegen binary consumes this
directly.
"""

from __future__ import annotations

import importlib
import sys

from mizan_core.ir import build_ir


def main(argv: list[str] | None = None) -> int:
    args = list(sys.argv[1:] if argv is None else argv)
    if len(args) != 1:
        print("usage: python -m mizan_fastapi.ir <module>", file=sys.stderr)
        return 2

    module_name = args[0]
    try:
        importlib.import_module(module_name)
    except Exception as e:
        print(f"failed to import {module_name!r}: {e}", file=sys.stderr)
        return 1

    sys.stdout.write(build_ir())
    return 0


if __name__ == "__main__":
    sys.exit(main())
