from __future__ import annotations

from src.services.agent_market_discovery_review import (
    run_agent_market_discovery_review,
)


def test_discovery_review_classifies_known_and_unknown_repositories():
    report = run_agent_market_discovery_review(
        watch_report=_watch_report(),
        candidate_registry={
            "schema_version": "agent_replacement_candidates_v1",
            "candidates": [
                {
                    "candidate_id": "microsoft_agent_framework",
                    "official_url": "https://learn.microsoft.com/en-us/agent-framework/overview/",
                }
            ],
        },
        source_registry={
            "schema_version": "agent_market_watch_sources_v1",
            "candidates": [
                {
                    "candidate_id": "microsoft_agent_framework",
                    "sources": [
                        {
                            "source_id": "microsoft_agent_framework_github_release",
                            "url": "https://api.github.com/repos/microsoft/agent-framework/releases/latest",
                        }
                    ],
                }
            ],
        },
        generated_at="2026-06-03T00:00:00+00:00",
    )

    assert report["policy"]["auto_registry_addition_approved"] is False
    assert report["summary"]["unique_repositories"] == 2
    assert report["summary"]["already_watched_or_registered"] == 1
    assert report["summary"]["manual_classification_required"] == 1
    assert report["summary"]["new_manual_classification_required"] == 1

    drafts = {draft["repository_full_name"]: draft for draft in report["candidate_drafts"]}
    assert drafts["microsoft/agent-framework"]["status"] == "already_watched_or_registered"
    assert drafts["pydantic/pydantic-ai"]["status"] == "needs_primary_source_classification"
    assert drafts["pydantic/pydantic-ai"]["recommended_next_gate"] == (
        "classify_official_sources_then_update_watch_registry"
    )
    assert drafts["pydantic/pydantic-ai"]["approval_boundary"][
        "approved_for_registry_addition"
    ] is False


def test_discovery_review_previous_review_suppresses_new_repeat_signal():
    previous = run_agent_market_discovery_review(
        watch_report=_watch_report(),
        candidate_registry={"schema_version": "agent_replacement_candidates_v1", "candidates": []},
        source_registry={"schema_version": "agent_market_watch_sources_v1", "candidates": []},
        generated_at="2026-06-02T00:00:00+00:00",
    )

    report = run_agent_market_discovery_review(
        watch_report=_watch_report(),
        candidate_registry={"schema_version": "agent_replacement_candidates_v1", "candidates": []},
        source_registry={"schema_version": "agent_market_watch_sources_v1", "candidates": []},
        previous_review=previous,
        generated_at="2026-06-03T00:00:00+00:00",
    )

    assert report["summary"]["manual_classification_required"] == 2
    assert report["summary"]["new_manual_classification_required"] == 0
    assert all(not draft["new_since_previous_review"] for draft in report["candidate_drafts"])


def _watch_report() -> dict:
    return {
        "schema_version": "agent_market_watch_report_v1",
        "generated_at": "2026-06-03T00:00:00+00:00",
        "mode": "live",
        "new_candidate_discovery": [
            {
                "source_id": "github_agent_framework_topic",
                "status": "ok",
                "http_status": 200,
                "items": [
                    {
                        "full_name": "pydantic/pydantic-ai",
                        "html_url": "https://github.com/pydantic/pydantic-ai",
                        "stargazers_count": 17451,
                        "updated_at": "2026-06-02T03:35:50Z",
                    },
                    {
                        "full_name": "microsoft/agent-framework",
                        "html_url": "https://github.com/microsoft/agent-framework",
                        "stargazers_count": 10954,
                        "updated_at": "2026-06-02T02:55:57Z",
                    },
                    {
                        "full_name": "pydantic/pydantic-ai",
                        "html_url": "https://github.com/pydantic/pydantic-ai",
                        "stargazers_count": 17499,
                        "updated_at": "2026-06-02T04:00:00Z",
                    },
                ],
            }
        ],
    }
