"""Bundled Kimi host strategy."""
from __future__ import annotations

import shutil
from collections.abc import Callable
from pathlib import Path

from okstra_ctl.adapters.accounting import UnavailableUsageAccountingPort
from okstra_ctl.adapters.hosts.capability_adapter import (
    INTERACTION_FUNCTIONS,
    PENDING_HOST_PORT,
    CapabilityHostAdapter,
    no_automatic_claim,
    numbered_interaction_port,
)
from okstra_ctl.domain.host import HostDescriptor
from okstra_ctl.ports.host_model import NativeExecutionValueHostModelBindingPort
from okstra_ctl.registry.provider_registry import ProviderRegistry


DESCRIPTOR = HostDescriptor(
    id="kimi",
    aliases=("kimi",),
    native_provider_id="kimi",
    required_executables=("kimi",),
    launch_mode="lead",
    install_targets=frozenset({"agents"}),
    agent_id="kimi",
    agent_label="Kimi CLI",
    role="Kimi lead",
    dispatch_mode="render-only",
    session_accounting="artifact-only",
    has_claude_session=False,
    relay_contract=str(Path(__file__).with_name("relay.md").resolve()),
)


def create_adapter(
    *,
    executable_finder: Callable[[str], str | None] = shutil.which,
    interaction_port=None,
    lead_session_port=PENDING_HOST_PORT,
    worker_dispatch_port=PENDING_HOST_PORT,
    usage_accounting_port=UnavailableUsageAccountingPort(
        "Kimi host usage accounting is unavailable because no session "
        "transcript or CLI usage artifact contract is registered."
    ),
    provider_registry: ProviderRegistry | None = None,
    host_model_port=None,
) -> CapabilityHostAdapter:
    return CapabilityHostAdapter(
        DESCRIPTOR,
        executable_finder=executable_finder,
        interaction_port=(
            interaction_port
            if interaction_port is not None
            else numbered_interaction_port()
        ),
        lead_session_port=lead_session_port,
        worker_dispatch_port=worker_dispatch_port,
        usage_accounting_port=usage_accounting_port,
        supported_functions=INTERACTION_FUNCTIONS,
        detector=no_automatic_claim,
        provider_registry=provider_registry,
        host_model_port=host_model_port or NativeExecutionValueHostModelBindingPort(
            host_runtime=DESCRIPTOR.id,
            native_provider=DESCRIPTOR.native_provider_id,
        ),
    )
