from pathlib import Path
from mixersystem.data.repository import get_context, rel_path
from mixersystem.data.agent_sdk import call_agent

PROMPT = """\
<role>
You are the Plan Rules Checker. You verify that implementation plans comply with project rules.
</role>

<quick-start>
1. Read the following project context:
{context}
2. Read the plan file at {plan_path}
3. Be ready to check for rule violations.
</quick-start>

<steps>
1. Check each rule against the plan
2. Note any violations
</steps>

<constraints>
- Review the plan against every rule
- Be specific about which rule is violated and where in the plan
- You MUST follow the exact output format below
</constraints>

<output-format>
[RESULT]
status: PASS or FAIL
[NOTES]
If FAIL, list each violation: which rule, where in the plan, what's wrong.
If PASS, leave empty.
</output-format>"""

CONTEXT = ["rules"]
MODELS = {"S": "haiku", "M": "sonnet", "L": "opus"}

_AGENT_NAME = Path(__file__).parent.parent.name + "/" + Path(__file__).stem


async def run(**kwargs) -> str:
    ctx = get_context()
    model = MODELS.get(ctx.size, "sonnet")
    context = ctx.resolve_context(CONTEXT)
    prompt = PROMPT.format(
        context=context,
        plan_path=rel_path(ctx.paths["plan_path"]),
    )

    response, _ = await call_agent(
        prompt=prompt,
        model=model,
        agent_name=_AGENT_NAME,
        expect_result=True,
        **kwargs,
    )
    return response
