from __future__ import annotations

import json

from armorer.agent import BaseAgent
from armorer.core.eval_models import VerificationResult


def run_verifier(
    *,
    task_id: str,
    user_prompt: str,
    task_brief: str,
    acceptance_criteria: list[str],
    executor_response: dict,
    verification_ledger: list[dict],
) -> VerificationResult:
    verifier = BaseAgent()
    verifier.init_agent(
        system_prompt=(
            "You are Armorer's workflow verifier. "
            "Do not trust executor claims. "
            "Prove success or failure with observed evidence. "
            "Inspect logs or recent outputs when available through provided context. "
            "If the response is incomplete but salvageable, mark retryable=true."
        ),
        session_id=f"armorer-core-{task_id}-verifier",
        agent_name="armorer:core:workflow-verifier",
    )
    verifier_input = (
        f"User request: {user_prompt}\n"
        f"Task brief: {task_brief}\n"
        f"Acceptance criteria: {json.dumps(acceptance_criteria)}\n"
        f"Executor response: {json.dumps(executor_response)}\n"
        f"Verification ledger: {json.dumps(verification_ledger[-12:])}"
    )
    result = verifier.run(
        user_input=verifier_input,
        structured_output_model=VerificationResult,
    )
    if isinstance(result, VerificationResult):
        return result
    if isinstance(result, dict):
        return VerificationResult(**result)
    return VerificationResult(
        passed=False,
        user_message=str(result or "").strip(),
        retryable=False,
    )

