# retoor <retoor@molodetz.nl>

from devplacepy.services.jobs.isslop.agent.classifier import AiVerdict
from devplacepy.services.jobs.isslop.agent.reporter import _summary_payload, fallback_report
from devplacepy.services.jobs.isslop.analysis.templates import TemplateEvidence
from devplacepy.services.jobs.isslop.analysis.scoring import FileScore, aggregate
from devplacepy.services.jobs.isslop.pipeline import apply_ai_verdicts


def _file(relative, sloc, origin, quality=10.0):
    return FileScore(
        relative=relative,
        language="python",
        sloc=sloc,
        origin_score=origin,
        quality_deficit=quality,
        category="human-clean",
        criticality=1.0,
        signals=[],
    )


def _verdict(path, probability):
    return AiVerdict(
        path=path,
        origin_score=probability,
        quality_deficit=10.0,
        ai_probability=probability,
        category="uncertain",
        reasoning="",
        notable_signals=[],
    )


def test_apply_ai_verdicts_only_touches_reviewed_files():
    scores = [_file("a.py", 500, 20.0), _file("b.py", 500, 20.0)]
    adjusted = apply_ai_verdicts(scores, [_verdict("a.py", 80.0)])
    assert adjusted[0].origin_score == round(0.6 * 20.0 + 0.4 * 80.0, 1)
    assert adjusted[1].origin_score == 20.0


def test_hedging_verdicts_cannot_dominate_a_large_repo():
    scores = [_file(f"f{i}.py", 200, 15.0) for i in range(100)]
    verdicts = [_verdict(f"f{i}.py", 40.0) for i in range(12)]
    static = aggregate(scores)
    final = aggregate(apply_ai_verdicts(scores, verdicts))
    assert static.ai_percent == 0.0
    assert final.ai_percent < 5.0
    assert final.human_percent > 95.0


def test_strong_verdict_on_heavy_file_moves_the_verdict():
    scores = [_file("huge.py", 5000, 30.0)] + [_file(f"s{i}.py", 50, 15.0) for i in range(10)]
    final = aggregate(apply_ai_verdicts(scores, [_verdict("huge.py", 95.0)]))
    static = aggregate(scores)
    assert final.ai_percent > static.ai_percent + 30.0


def test_report_payload_grade_matches_final_scores():
    scores = [_file("a.py", 300, 60.0, 40.0), _file("b.py", 200, 20.0)]
    verdicts = [_verdict("a.py", 90.0)]
    adjusted = apply_ai_verdicts(scores, verdicts)
    final = aggregate(adjusted)
    payload = _summary_payload(
        "https://x.example/repo",
        "git",
        final,
        adjusted,
        verdicts,
        0,
        [],
        {},
        TemplateEvidence(),
        None,
    )
    assert payload["repo_scores"]["grade"] == final.grade
    assert payload["repo_scores"]["human_percent"] == final.human_percent
    markdown = fallback_report(payload)
    assert f"grade **{final.grade}**" in markdown
    assert f"**{final.human_percent}% human**" in markdown
