"""Helper script for Robot Framework automation profile smoke tests."""

from __future__ import annotations

import sys
from pathlib import Path
from typing import Any

# Ensure src is importable when run from workspace root
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "src"))

from cleveragents.domain.models.core.automation_profile import (
    BUILTIN_PROFILES,
    AutomationProfile,
    get_builtin_profile,
)

_PROFILE_NAMES = [
    "manual",
    "review",
    "supervised",
    "cautious",
    "trusted",
    "auto",
    "ci",
    "full-auto",
]


def _test_profile(name: str) -> None:
    """Load a built-in profile and verify it."""
    profile = get_builtin_profile(name)
    assert isinstance(profile, AutomationProfile)
    assert profile.name == name
    assert 0.0 <= profile.decompose_task <= 1.0
    assert 0.0 <= profile.create_tool <= 1.0
    assert 0.0 <= profile.select_tool <= 1.0
    print(
        f"profile-{name}-ok "
        f"(sandbox={profile.safety.require_sandbox}, "
        f"checkpoints={profile.safety.require_checkpoints}, "
        f"unsafe={profile.safety.allow_unsafe_tools})"
    )


def _test_summary() -> None:
    """Print summary of all built-in profiles."""
    assert len(BUILTIN_PROFILES) == 8
    for name in _PROFILE_NAMES:
        profile = get_builtin_profile(name)
        thresholds = [
            profile.decompose_task,
            profile.create_tool,
            profile.select_tool,
            profile.edit_code,
            profile.execute_command,
            profile.create_file,
            profile.delete_content,
            profile.access_network,
            profile.install_dependency,
            profile.modify_config,
            profile.approve_plan,
        ]
        avg = sum(thresholds) / len(thresholds)
        print(
            f"  {name:>12}: avg_threshold={avg:.2f} "
            f"sandbox={profile.safety.require_sandbox} "
            f"checkpoints={profile.safety.require_checkpoints} "
            f"unsafe={profile.safety.allow_unsafe_tools}"
        )
    print("summary-ok")


if __name__ == "__main__":
    cmd = sys.argv[1] if len(sys.argv) > 1 else "summary"
    dispatch: dict[str, Any] = {
        "summary": _test_summary,
    }
    # Add individual profile tests
    for pname in _PROFILE_NAMES:
        dispatch[pname] = lambda n=pname: _test_profile(n)

    fn = dispatch.get(cmd)
    if fn:
        fn()
    else:
        print(f"Unknown command: {cmd}", file=sys.stderr)
        sys.exit(1)
