# retoor <retoor@molodetz.nl>

import json

from devplacepy.services.jobs.isslop.analysis.scoring import RepoScores, adjust_for_template, aggregate
from devplacepy.services.jobs.isslop.analysis.templates import detect_template


def _scores(ai=2.0, quality=20.0):
    return RepoScores(
        origin_score=20.0,
        quality_deficit=quality,
        slop_score=7.8,
        grade="A",
        category="human-clean",
        human_percent=100.0 - ai,
        ai_percent=ai,
        confidence="medium",
        strong_signal_count=0,
        medium_signal_count=0,
        files_scored=50,
    )


def test_untouched_boilerplate_is_detected(tmp_path):
    (tmp_path / "package.json").write_text(json.dumps({
        "name": "next-js-boilerplate",
        "author": "Ixartz (https://github.com/ixartz)",
    }))
    (tmp_path / "README.md").write_text(
        "# Boilerplate and Starter for Next.js\n\nSponsors welcome. Demo at https://demo.nextjs-boilerplate.com\n"
    )
    for artifact in (".storybook", ".husky", ".devcontainer"):
        (tmp_path / artifact).mkdir()
    for artifact in ("commitlint.config.ts", "playwright.config.ts", "vitest.config.ts", "drizzle.config.ts", "crowdin.yml", "CHANGELOG.md"):
        (tmp_path / artifact).write_text("")
    evidence = detect_template(tmp_path)
    assert evidence.score >= 70.0
    assert any("known template" in marker for marker in evidence.markers)
    adjusted = adjust_for_template(_scores(), evidence.score)
    assert adjusted.grade in ("C", "D", "F")
    assert adjusted.ai_percent >= 55.0
    assert adjusted.category == "ai-slop"


def test_t3_scaffold_metadata_is_detected(tmp_path):
    (tmp_path / "package.json").write_text(json.dumps({
        "name": "my-cool-product",
        "ct3aMetadata": {"initVersion": "7.39.0"},
    }))
    evidence = detect_template(tmp_path)
    assert any("create-t3-app" in marker for marker in evidence.markers)


def test_genuine_project_produces_no_evidence(tmp_path):
    (tmp_path / "package.json").write_text(json.dumps({
        "name": "acme-billing-portal",
        "description": "Internal billing portal for Acme",
    }))
    (tmp_path / "README.md").write_text("# Acme billing portal\n\nInternal tool, see the wiki for onboarding.\n")
    (tmp_path / "Dockerfile").write_text("")
    evidence = detect_template(tmp_path)
    assert evidence.score < 35.0
    assert adjust_for_template(_scores(), evidence.score) == _scores()


def test_weak_evidence_below_gate_never_adjusts():
    base = _scores()
    assert adjust_for_template(base, 34.9) == base
    adjusted = adjust_for_template(base, 90.0)
    assert adjusted.ai_percent == round(0.85 * 90.0, 1)
    assert adjusted.human_percent == round(100.0 - adjusted.ai_percent, 1)
    assert adjusted.category == "ai-slop"


def test_confident_but_partial_evidence_caps_category_at_uncertain():
    adjusted = adjust_for_template(_scores(), 55.0)
    assert adjusted.category == "uncertain"
    assert adjusted.grade != "A"
