"""albus config -- Configuration commands."""

from __future__ import annotations

from server.transport.cli._models import ConfigArgs


def cmd_config(args: ConfigArgs) -> int:
    """Configuration commands."""
    if args.action == "validate":
        return _config_validate(args)
    if args.action == "models":
        return _config_models()
    print(f"Unknown action: {args.action}")
    return 2


def _config_validate(args: ConfigArgs) -> int:
    """Validate deployment config."""
    from server.config import DeploymentConfig

    config_path = (args.config or "").strip() or None
    cfg = DeploymentConfig.load(config_path)
    errs = cfg.validate_config()

    if errs:
        print(f"\u274c Invalid config ({cfg.source}):")
        for e in errs:
            print(f"  - {e}")
        return 1

    print(f"\u2713 Valid: {cfg.source}")
    return 0


def _config_models() -> int:
    """Show model configuration."""
    from server.config import DeploymentConfig
    from stdlib.llm import list_available_providers

    config = DeploymentConfig.load()

    print(f"\n\U0001f9e0 Model Configuration ({config.source})")
    print("=" * 50)

    if config.models and config.models.routing:
        for cap, model in sorted(config.models.routing.items()):
            print(f"  {cap:20} \u2192 {model}")
    else:
        print("  (no routing configured)")

    print("\n\U0001f4e1 Providers:")
    for p in list_available_providers():
        print(f"  \u2713 {p}")

    return 0
