"""Nova Contract Resolver CLI — command-line entry point.

Extracted from core/contract_resolver.py (P12, REQ-176).

G-113 import direction: this module imports core.contract_resolver (the
re-export shim) for the resolve function. The shim imports the split
modules. Nothing imports this CLI module except direct invocation.
"""
from __future__ import annotations

import json
import sys

from core.contract_resolver import resolve
from core import env


def main(argv=None):
    """CLI: resolve a contract YAML to a Target Stack JSON."""
    argv = argv if argv is not None else sys.argv[1:]
    if len(argv) < 2:
        print("usage: contract_resolver.py <contract.yml> <out.json> [--environment <name>", file=sys.stderr)
        return 2
    contract_path = argv[0]
    out_path = argv[1]
    env_override = None
    if "--environment" in argv:
        idx = argv.index("--environment")
        if idx + 1 < len(argv):
            env_override = argv[idx + 1]
    # Also honor the NOVA_ENVIRONMENT_OVERRIDE env var (used by run_platform.sh).
    if env_override is None and env.get_env("ENVIRONMENT_OVERRIDE"):
        env_override = env.get_env("ENVIRONMENT_OVERRIDE")
    result = resolve(contract_path, environment_override=env_override)
    with open(out_path, "w") as fh:
        json.dump(result, fh, indent=2)
    return 0


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