from __future__ import annotations

from src.services.agent_market_watch_promotion_review import (
    run_agent_market_watch_promotion_review,
)


def test_watch_promotion_review_allows_only_scorecard_prescreen_readiness():
    report = run_agent_market_watch_promotion_review(
        watch_report=_watch_report(),
        integration_review=_integration_review(),
        discovery_classification=_classification(),
        candidate_registry=_registry(),
        generated_at="2026-06-04T00:00:00+00:00",
    )

    assert report["policy"]["priority_upgrade_approved"] is False
    assert report["policy"]["replay_candidate_approved"] is False
    assert report["summary"]["watch_only_candidates_reviewed"] == 1
    assert report["summary"]["eligible_for_market_scorecard_prescreen"] == 1
    review = report["reviews"][0]
    assert review["candidate_id"] == "hermes_agent_personal_platform"
    assert review["eligible_for_market_scorecard_prescreen"] is True
    assert review["approved_for_replay"] is False
    assert review["required_next_gate"] == (
        "operator_priority_upgrade_then_market_scorecard_prescreen"
    )


def test_watch_promotion_review_blocks_incomplete_watch_evidence():
    watch_report = _watch_report()
    watch_report["candidates"][0]["sources"] = [
        {
            "source_id": "homepage",
            "type": "docs",
            "url": "https://example.com",
            "status": "ok",
            "http_status": 200,
            "version": None,
            "error": None,
        }
    ]

    report = run_agent_market_watch_promotion_review(
        watch_report=watch_report,
        integration_review=_integration_review(),
        discovery_classification=_classification(),
        candidate_registry=_registry(),
        generated_at="2026-06-04T00:00:00+00:00",
    )

    review = report["reviews"][0]
    assert review["eligible_for_market_scorecard_prescreen"] is False
    assert review["approved_for_replay"] is False
    assert "needs_at_least_two_primary_sources" in review["blockers"]
    assert "needs_versioned_release_source" in review["blockers"]


def test_watch_promotion_review_matches_classification_by_source_repository():
    registry = _registry()
    registry["candidates"][0]["official_url"] = "https://docs.example.com/hermes"
    registry["candidates"][0]["source_repository"] = "nousresearch/hermes-agent"

    report = run_agent_market_watch_promotion_review(
        watch_report=_watch_report(),
        integration_review=_integration_review(),
        discovery_classification=_classification(),
        candidate_registry=registry,
        generated_at="2026-06-04T00:00:00+00:00",
    )

    review = report["reviews"][0]
    assert review["classification"]["repository_full_name"] == "nousresearch/hermes-agent"
    assert review["eligible_for_market_scorecard_prescreen"] is True


def _registry() -> dict:
    return {
        "schema_version": "agent_replacement_candidates_v1",
        "candidates": [
            {
                "candidate_id": "hermes_agent_personal_platform",
                "display_name": "NousResearch Hermes Agent",
                "official_url": "https://hermes-agent.nousresearch.com",
                "role": "personal_agent_platform_candidate",
                "evaluation_priority": "watch_only",
                "required_stage": "watch_only_primary_source_monitoring",
            }
        ],
    }


def _watch_report() -> dict:
    return {
        "schema_version": "agent_market_watch_report_v1",
        "generated_at": "2026-06-04T00:00:00+00:00",
        "candidates": [
            {
                "candidate_id": "hermes_agent_personal_platform",
                "sources": [
                    {
                        "source_id": "homepage",
                        "type": "docs",
                        "url": "https://hermes-agent.nousresearch.com",
                        "status": "ok",
                        "http_status": 200,
                        "version": None,
                        "error": None,
                    },
                    {
                        "source_id": "release",
                        "type": "github_release",
                        "url": "https://api.github.com/repos/NousResearch/hermes-agent/releases/latest",
                        "status": "ok",
                        "http_status": 200,
                        "version": "v2026.5.29.2",
                        "error": None,
                    },
                ],
            }
        ],
    }


def _integration_review() -> dict:
    return {
        "schema_version": "agent_market_integration_review_v1",
        "generated_at": "2026-06-04T00:00:00+00:00",
        "reviews": [
            {
                "candidate_id": "hermes_agent_personal_platform",
                "readiness": {"stage": "watch_only_primary_source_monitoring"},
            }
        ],
    }


def _classification() -> dict:
    return {
        "schema_version": "agent_market_discovery_classification_v1",
        "generated_at": "2026-06-04T00:00:00+00:00",
        "candidates": [
            {
                "repository_full_name": "nousresearch/hermes-agent",
                "html_url": "https://github.com/NousResearch/hermes-agent",
                "homepage": "https://hermes-agent.nousresearch.com",
                "classification": "personal_agent_platform_candidate",
                "recommendation": "add_to_watch_registry_after_manual_source_review",
                "watch_addition_recommended": True,
                "risk_flags": ["requires_dependency_boundary_review"],
            }
        ],
    }
