"""Robot Framework helper for actor YAML schema validation.

Provides a CLI-style interface for Robot to invoke schema validation
on actor YAML files. Exit code 0 = success, 1 = failure.

Usage:
    python robot/helper_actor_schema.py validate <yaml_file>
    python robot/helper_actor_schema.py validate-invalid <yaml_file>
"""

from __future__ import annotations

import io
import logging
import sys
from pathlib import Path

# Ensure the src directory is on the import path.
_SRC = str(Path(__file__).resolve().parents[1] / "src")
if _SRC not in sys.path:
    sys.path.insert(0, _SRC)

from cleveragents.actor.schema import (  # noqa: E402
    ActorConfigSchema,
    actor_role_warnings,
)


def main() -> int:
    """Entry point called by Robot Framework ``Run Process``."""
    if len(sys.argv) < 3:
        print(
            "Usage: helper_actor_schema.py "
            "<validate|validate-invalid|role-warning-model|role-warning-payload|"
            "role-warning-uppercase|role-warning-invalid-role-log> <file-or-dummy>"
        )
        return 1

    command = sys.argv[1]
    yaml_path = sys.argv[2]

    if command == "validate":
        try:
            config = ActorConfigSchema.from_yaml_file(yaml_path)
            print(f"actor-schema-ok: {config.name}")
            return 0
        except Exception as exc:
            print(f"actor-schema-fail: {exc}")
            return 1

    if command == "validate-invalid":
        try:
            ActorConfigSchema.from_yaml_file(yaml_path)
            print("actor-schema-unexpected-success")
            return 1
        except Exception as exc:
            print(f"actor-schema-expected-fail: {exc}")
            return 0

    if command == "role-warning-model":
        actor = ActorConfigSchema(
            name="local/robot-model-warning",
            type="llm",
            description="Robot model warning check",
            model="gpt-4",
            role_hint="estimation",
            context_view="strategist",
        )
        warnings = actor_role_warnings(actor)
        if any("response_format" in warning for warning in warnings):
            print("actor-role-warning-model-ok")
            return 0
        print(f"actor-role-warning-model-fail: {warnings}")
        return 1

    if command == "role-warning-payload":
        payload = {
            "name": "local/robot-payload-warning",
            "type": "llm",
            "model": "gpt-4",
            "role_hint": "estimation",
            "context_view": "plannerish",
            "response_format": {"type": "object"},
        }
        warnings = actor_role_warnings(payload)
        if any("context_view" in warning for warning in warnings):
            print("actor-role-warning-payload-ok")
            return 0
        print(f"actor-role-warning-payload-fail: {warnings}")
        return 1

    if command == "role-warning-uppercase":
        payload = {
            "name": "local/robot-uppercase-warning",
            "type": "llm",
            "model": "gpt-4",
            "role_hint": "ESTIMATION",
            "context_view": "strategist",
        }
        warnings = actor_role_warnings(payload)
        if any("response_format" in warning for warning in warnings):
            print("actor-role-warning-uppercase-ok")
            return 0
        print(f"actor-role-warning-uppercase-fail: {warnings}")
        return 1

    if command == "role-warning-invalid-role-log":
        stream = io.StringIO()
        handler = logging.StreamHandler(stream)
        schema_logger = logging.getLogger("cleveragents.actor.role_validation")
        schema_logger.addHandler(handler)
        schema_logger.setLevel(logging.WARNING)
        try:
            payload = {
                "name": "local/robot-invalid-role-warning",
                "type": "llm",
                "model": "gpt-4",
                "role_hint": "estmation",
            }
            warnings = actor_role_warnings(payload)
        finally:
            schema_logger.removeHandler(handler)

        logs = stream.getvalue()
        if "Unrecognized role_hint value" in logs and warnings == []:
            print("actor-role-warning-invalid-role-log-ok")
            return 0
        print(
            f"actor-role-warning-invalid-role-log-fail: warnings={warnings} logs={logs}"
        )
        return 1

    print(f"Unknown command: {command}")
    return 1


if __name__ == "__main__":
    sys.exit(main())
