#!/usr/bin/env python3
"""
Classify market discovery repositories using primary GitHub metadata.

The command is read-only. It does not add watch-registry entries, install SDKs,
call LLMs, approve paid provider use, enter replay, or change production.
"""

from __future__ import annotations

import argparse
import importlib.util
import json
import sys
from pathlib import Path
from typing import Any
from urllib.request import Request, urlopen


ROOT = Path(__file__).resolve().parents[2]
SERVICE_PATH = ROOT / "apps" / "api" / "src" / "services" / "agent_market_discovery_classifier.py"


def main() -> int:
    parser = argparse.ArgumentParser(description="Classify AWOOOI Agent discovery candidates.")
    parser.add_argument("--discovery-review", required=True, help="agent_market_discovery_review_v1 JSON")
    parser.add_argument("--metadata", help="optional repository metadata JSON keyed by repository_full_name")
    parser.add_argument("--output", help="classification output JSON")
    parser.add_argument("--timeout-seconds", type=int, default=12)
    args = parser.parse_args()

    discovery_review = _read_json(Path(args.discovery_review))
    metadata = (
        _read_json(Path(args.metadata))
        if args.metadata
        else _fetch_repository_metadata(discovery_review, args.timeout_seconds)
    )
    service = _load_service()
    report = service.run_agent_market_discovery_classification(
        discovery_review=discovery_review,
        repository_metadata=metadata,
    )
    rendered = json.dumps(report, ensure_ascii=False, indent=2, sort_keys=True)
    if args.output:
        Path(args.output).write_text(rendered + "\n", encoding="utf-8")
    else:
        print(rendered)
    print(json.dumps(report["summary"], ensure_ascii=False, sort_keys=True))
    return 0


def _fetch_repository_metadata(
    discovery_review: dict[str, Any],
    timeout_seconds: int,
) -> dict[str, dict[str, Any]]:
    metadata: dict[str, dict[str, Any]] = {}
    for draft in discovery_review.get("candidate_drafts") or []:
        if draft.get("status") != "needs_primary_source_classification":
            continue
        repo = str(draft.get("repository_full_name", ""))
        if not repo:
            continue
        try:
            metadata[repo] = _fetch_one_repository(repo, timeout_seconds)
        except Exception as exc:  # noqa: BLE001
            metadata[repo] = {
                "full_name": repo,
                "html_url": draft.get("html_url"),
                "description": None,
                "topics": [],
                "stargazers_count": draft.get("stargazers_count_max"),
                "error": str(exc),
            }
    return metadata


def _fetch_one_repository(repo: str, timeout_seconds: int) -> dict[str, Any]:
    request = Request(
        f"https://api.github.com/repos/{repo}",
        headers={
            "User-Agent": "awoooi-agent-market-discovery-classifier/1.0",
            "Accept": "application/vnd.github+json",
        },
    )
    with urlopen(request, timeout=timeout_seconds) as response:  # noqa: S310
        payload = json.loads(response.read().decode("utf-8"))
    return {
        "full_name": str(payload.get("full_name") or repo),
        "html_url": payload.get("html_url"),
        "description": payload.get("description"),
        "homepage": payload.get("homepage"),
        "topics": list(payload.get("topics") or []),
        "language": payload.get("language"),
        "stargazers_count": payload.get("stargazers_count"),
        "pushed_at": payload.get("pushed_at"),
        "archived": bool(payload.get("archived", False)),
    }


def _read_json(path: Path) -> dict[str, Any]:
    with path.open(encoding="utf-8") as handle:
        payload = json.load(handle)
    if not isinstance(payload, dict):
        raise SystemExit(f"{path}: expected JSON object")
    return payload


def _load_service() -> Any:
    module_name = "awoooi_agent_market_discovery_classifier_service"
    spec = importlib.util.spec_from_file_location(module_name, SERVICE_PATH)
    if spec is None or spec.loader is None:
        raise SystemExit(f"cannot load discovery classifier service from {SERVICE_PATH}")
    module = importlib.util.module_from_spec(spec)
    sys.modules[module_name] = module
    spec.loader.exec_module(module)
    return module


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