"""Robot Framework helper for LSP config fields integration tests."""

from __future__ import annotations

import importlib
import sys
from pathlib import Path

_SRC = str(Path(__file__).resolve().parents[1] / "src")
if _SRC not in sys.path:
    sys.path.insert(0, _SRC)

import cleveragents  # noqa: E402

importlib.reload(cleveragents)

from cleveragents.lsp.models import (  # noqa: E402
    LspCapability,
    LspServerConfig,
    LspTransport,
)
from cleveragents.lsp.registry import LspRegistry  # noqa: E402


def _run_transport_enum() -> None:
    """Verify LspTransport enum values."""
    assert LspTransport.STDIO.value == "stdio"
    assert LspTransport.TCP.value == "tcp"
    # PIPE is not in the spec -- only stdio and tcp
    assert len(LspTransport) == 2, f"Expected 2 members, got {len(LspTransport)}"
    print("transport-enum-ok")


def _run_config_defaults() -> None:
    """Verify new field defaults."""
    cfg = LspServerConfig(name="local/test", command="echo", languages=["python"])
    assert cfg.description == ""
    assert cfg.transport == LspTransport.STDIO
    assert cfg.initialization == {}
    assert cfg.workspace_settings == {}
    print("config-defaults-ok")


def _run_config_all_fields() -> None:
    """Verify config with all fields populated."""
    cfg = LspServerConfig(
        name="local/pyright",
        description="Pyright type checker",
        command="pyright-langserver",
        args=["--stdio"],
        transport=LspTransport.STDIO,
        languages=["python"],
        capabilities=[LspCapability.DIAGNOSTICS],
        initialization={"python": {"pythonPath": "/usr/bin/python3"}},
        workspace_settings={"editor.formatOnSave": True},
    )
    assert cfg.description == "Pyright type checker"
    assert cfg.transport == LspTransport.STDIO
    assert "python" in cfg.initialization
    assert "editor.formatOnSave" in cfg.workspace_settings
    print("config-all-fields-ok")


def _run_config_roundtrip() -> None:
    """Verify serialization round-trip."""
    cfg = LspServerConfig(
        name="local/test",
        description="Test server",
        command="echo",
        transport=LspTransport.TCP,
        languages=["python"],
        initialization={"key": "value"},
        workspace_settings={"ws": True},
    )
    data = cfg.model_dump(mode="json")
    cfg2 = LspServerConfig.model_validate(data)
    assert cfg.description == cfg2.description
    assert cfg.transport == cfg2.transport
    assert cfg.initialization == cfg2.initialization
    assert cfg.workspace_settings == cfg2.workspace_settings
    print("config-roundtrip-ok")


def _run_registry_preserves() -> None:
    """Verify registry preserves new fields."""
    reg = LspRegistry()
    cfg = LspServerConfig(
        name="local/test",
        description="Test",
        command="echo",
        transport=LspTransport.TCP,
        languages=["python"],
        initialization={"k": "v"},
        workspace_settings={"w": True},
    )
    reg.register(cfg)
    retrieved = reg.get("local/test")
    assert retrieved is not None
    assert retrieved.description == "Test"
    assert retrieved.transport == LspTransport.TCP
    assert retrieved.initialization == {"k": "v"}
    assert retrieved.workspace_settings == {"w": True}
    print("registry-preserves-ok")


if __name__ == "__main__":
    cmd = sys.argv[1] if len(sys.argv) > 1 else "all"
    dispatch = {
        "transport-enum": _run_transport_enum,
        "config-defaults": _run_config_defaults,
        "config-all-fields": _run_config_all_fields,
        "config-roundtrip": _run_config_roundtrip,
        "registry-preserves": _run_registry_preserves,
    }
    if cmd == "all":
        for fn in dispatch.values():
            fn()
    elif cmd in dispatch:
        dispatch[cmd]()
    else:
        print(f"Unknown command: {cmd}")
        sys.exit(1)
