from pathlib import Path


ROOT = Path(__file__).resolve().parents[1]


def test_ai_automation_debt_scan_is_read_only_and_prioritized():
    from services.ai_automation_debt_service import build_ai_automation_debt_report

    report = build_ai_automation_debt_report(max_findings=40, per_file_limit=6)

    assert report["policy"] == "read_only_ai_automation_debt_scan"
    assert report["success"] is True
    assert report["result"] == "PRODUCT_SURFACE_CLEAR"
    assert report["summary"]["product_surface_blocker_count"] == 0
    assert report["summary"]["primary_human_gate_count"] == 0
    assert report["summary"]["ai_controlled_apply_ready"] is True
    assert report["summary"]["market_intel_ai_controlled_alias_count"] >= 10
    assert report["summary"]["controlled_apply_candidate_count"] == 0
    assert report["summary"]["category_counts"].get("market_intel_ai_controlled_apply_candidate", 0) == 0
    assert report["summary"]["category_counts"].get("automation_debt", 0) == 0
    assert report["summary"]["category_counts"].get("governance_doc_debt", 0) == 0
    assert report["summary"]["category_counts"].get("ea_legacy_callback_debt", 0) == 0
    assert report["safety"] == {
        "read_only": True,
        "writes_database": False,
        "executes_network": False,
        "uses_llm": False,
        "scans_raw_sessions": False,
        "github_used": False,
    }
    assert all(item["priority"] != "P0" for item in report["findings"])
    assert report["next_work_order"][0]["lane"] == "product_surface"
    assert report["next_work_order"][0]["status"] == "clear"
    assert report["next_work_order"][1]["lane"] == "market_intel_controlled_apply"
    assert report["next_work_order"][1]["status"] == "review_report_alias_layer_complete"
    assert report["next_work_order"][1]["alias_count"] >= 90
    assert report["next_work_order"][1]["target_count"] == 0
    assert report["next_work_order"][2]["target_count"] == 0
    assert report["next_work_order"][3]["lane"] == "legacy_compatibility_aliases"
    assert report["next_work_order"][3]["target_count"] == report["summary"]["legacy_compatibility_field_count"]


def test_ai_automation_debt_api_route_is_registered():
    route_source = (ROOT / "routes" / "ai_routes.py").read_text(encoding="utf-8")

    assert "@ai_bp.route('/api/ai/automation-debt')" in route_source
    assert "build_ai_automation_debt_report" in route_source
    assert '"source_endpoint"] = "/api/ai/automation-debt"' in route_source
