"""Helper utilities for ProjectContextPolicy Robot tests."""

from __future__ import annotations

import json
import sys

from cleveragents.domain.models.core.context_policy import (
    ContextView,
    ProjectContextPolicy,
)


def _serialize_test() -> None:
    """Serialize a policy and validate structure."""
    policy = ProjectContextPolicy(
        default_view=ContextView(
            include_resources=["db-*"],
            exclude_resources=["db-test"],
            include_paths=["src/**/*.py"],
            exclude_paths=["*.pyc"],
            max_file_size=1048576,
            max_total_size=10485760,
        ),
        strategize_view=ContextView(
            include_resources=["db-*", "cache-*"],
        ),
    )

    data = json.loads(policy.model_dump_json())

    # Validate top-level keys
    assert "default_view" in data, "missing default_view"
    assert "strategize_view" in data, "missing strategize_view"
    assert "execute_view" in data, "missing execute_view"
    assert "apply_view" in data, "missing apply_view"

    # Validate default_view structure
    dv = data["default_view"]
    assert dv["include_resources"] == ["db-*"]
    assert dv["exclude_resources"] == ["db-test"]
    assert dv["include_paths"] == ["src/**/*.py"]
    assert dv["exclude_paths"] == ["*.pyc"]
    assert dv["max_file_size"] == 1048576
    assert dv["max_total_size"] == 10485760

    # Validate strategize_view
    sv = data["strategize_view"]
    assert sv["include_resources"] == ["db-*", "cache-*"]

    # Validate None views are null
    assert data["execute_view"] is None
    assert data["apply_view"] is None

    print("context-policy-serialize-ok")


def _resolve_test() -> None:
    """Test resolve_view inheritance."""
    policy = ProjectContextPolicy(
        default_view=ContextView(
            include_resources=["default-*"],
        ),
        strategize_view=ContextView(
            include_resources=["strategize-*"],
        ),
    )

    # execute should inherit from strategize
    view = policy.resolve_view("execute")
    assert view.include_resources == ["strategize-*"]

    # default should return default
    view = policy.resolve_view("default")
    assert view.include_resources == ["default-*"]

    print("context-policy-resolve-ok")


def _empty_test() -> None:
    """Test empty policy defaults."""
    policy = ProjectContextPolicy()
    view = policy.resolve_view("apply")
    assert view.include_resources == []
    assert view.exclude_resources == []
    assert view.include_paths == []
    assert view.exclude_paths == []
    assert view.max_file_size is None
    assert view.max_total_size is None
    print("context-policy-empty-ok")


def main() -> None:
    if len(sys.argv) < 2:
        raise SystemExit("Expected command argument")
    command = sys.argv[1]
    if command == "serialize":
        _serialize_test()
    elif command == "resolve":
        _resolve_test()
    elif command == "empty":
        _empty_test()
    else:
        raise SystemExit(f"Unknown helper command: {command}")


if __name__ == "__main__":
    main()
