#!/usr/bin/env python3
import argparse
import os
import sys

from script_common import pip_env, resolve_repo_root, run


def main() -> int:
    parser = argparse.ArgumentParser(description="Cross-platform minimal pip wrapper")
    parser.add_argument("pip_args", nargs=argparse.REMAINDER)
    parser.add_argument("--repo-root", default=None)
    args = parser.parse_args()

    if not args.pip_args:
        print("Usage: python scripts/pip-min.py <pip args>", file=sys.stderr)
        return 2

    repo_root = resolve_repo_root(args.repo_root)
    pip_args = list(args.pip_args)

    # Preserve legacy behavior: for bare install, default target to repo-local deps.
    if pip_args and pip_args[0].lower() == "install":
        has_target = any(a in ("--target", "--prefix") for a in pip_args)
        if not has_target:
            pip_args = [a for a in pip_args if a != "--user"]
            target = repo_root / ".pydeps" / f"py{sys.version_info.major}{sys.version_info.minor}"
            os.makedirs(target, exist_ok=True)
            pip_args.extend(["--target", str(target)])

    return run(sys.executable, ["-m", "pip", *pip_args], repo_root, env=pip_env(repo_root))


if __name__ == "__main__":
    raise SystemExit(main())
