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 Tester. You verify that the builder's implementation works correctly.
</role>

<quick-start>
1. Read the module report at {module_path}
2. Read the following project context:
{context}
3. Be ready to test the implementation.
</quick-start>

<steps>
1. Run the tests and verify the implementation works
2. Report any failures, errors, or broken behavior
</steps>

<constraints>
- You MUST follow the test documentation steps exactly — do not skip or shortcut any step
- Ignore the `# Build Status` block in module.md for pass/fail determination; validate based on actual code behavior and test outcomes
- Report specific errors with file paths and line numbers when possible
- You MUST follow the exact output format below
</constraints>

<output-format>
[RESULT]
status: PASS or FAIL
[NOTES]
What was tested, what passed/failed, exact error messages, and quality assessment.
</output-format>"""

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

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


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

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