import asyncio

from mixersystem.data.repository import parse_result
from mixersystem.workflows.plan.agents import rules_checker
from mixersystem.workflows.plan.agents import completeness_checker


async def run() -> str:
    """Review a plan. Runs all checkers concurrently and aggregates results."""
    rules_result, completeness_result = await asyncio.gather(
        rules_checker.run(),
        completeness_checker.run(),
    )

    checks = [
        ("rules", rules_result),
        ("completeness", completeness_result),
    ]

    failures = []
    for name, raw in checks:
        result = parse_result(raw)
        if result.passed:
            continue
        detail = (result.notes or raw).strip() or "No details provided."
        failures.append(f"[{name}] {detail}")

    if failures:
        notes = "\n\n".join(failures)
        return f"[RESULT]\nstatus: FAIL\n[NOTES]\n{notes}"

    return "[RESULT]\nstatus: PASS\n[NOTES]\n"


if __name__ == "__main__":
    from mixersystem.data.repository import run_cli
    run_cli(run)
