from __future__ import annotations

import json
import unittest
from pathlib import Path

from mais_humana.catalog import get_platform
from mais_humana.matrix import build_matrix, build_platform_report
from mais_humana.playbooks import build_playbooks, playbooks_markdown
from mais_humana.quality import evaluate_platform_quality, quality_to_markdown
from mais_humana.questions import questions_for_report, unanswered_questions
from mais_humana.scanner import scan_platform
from tests.helpers import make_tmp


class QualityQuestionsPlaybookTests(unittest.TestCase):
    def make_minimal_report(self, root: Path):
        repo = root / "tudo-para-ia-docs-plataform"
        repo.mkdir()
        (repo / "README.md").write_text("docs contract evidence help runbook", encoding="utf-8")
        (repo / "package.json").write_text(json.dumps({"scripts": {"test": "node --test"}}), encoding="utf-8")
        scan = scan_platform(root, get_platform("docs"))
        cells = build_matrix((scan,))
        return build_platform_report(scan, cells)

    def test_quality_report_flags_known_blocker_and_missing_git(self) -> None:
        tmp = make_tmp()
        report = self.make_minimal_report(tmp)
        quality = evaluate_platform_quality(report)
        self.assertGreaterEqual(quality.blocker_count, 1)
        markdown = quality_to_markdown((quality,))
        self.assertIn("known_blockers", markdown)
        self.assertIn("git_ready", markdown)

    def test_questions_are_generated_for_profiles(self) -> None:
        tmp = make_tmp()
        report = self.make_minimal_report(tmp)
        questions = questions_for_report(report, max_per_profile=1)
        self.assertGreaterEqual(len(questions), 12)
        weak = unanswered_questions(questions)
        self.assertLessEqual(len(weak), len(questions))
        self.assertTrue(all(question.answer for question in questions))

    def test_playbooks_turn_recommendations_into_steps(self) -> None:
        tmp = make_tmp()
        report = self.make_minimal_report(tmp)
        playbooks = build_playbooks((report,))
        self.assertGreaterEqual(len(playbooks), 12)
        text = playbooks_markdown(playbooks)
        self.assertIn("Playbooks humanos", text)
        self.assertIn("Playbook humano", text)


if __name__ == "__main__":
    unittest.main()
