from __future__ import annotations

import argparse
import os
from pathlib import Path

import uvicorn


def parse_args() -> argparse.Namespace:
    parser = argparse.ArgumentParser(description="Run Finder Commander remote agent HTTP API")
    parser.add_argument(
        "--config",
        required=True,
        help="Path to remote agent config JSON",
    )
    parser.add_argument("--host", default="0.0.0.0", help="Listen host")
    parser.add_argument("--port", type=int, default=8765, help="Listen port")
    return parser.parse_args()


def main() -> int:
    args = parse_args()
    config_path = Path(args.config).expanduser().resolve(strict=False)
    if not config_path.is_file():
        raise SystemExit(f"Config file not found: {config_path}")

    os.environ["FINDER_COMMANDER_REMOTE_AGENT_CONFIG"] = str(config_path)
    print(f"Using config: {config_path}", flush=True)
    uvicorn.run("app.main:app", host=args.host, port=args.port)
    return 0


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