"""Traefik parameter handling for the special agent"""

from collections.abc import Iterable

from pydantic import BaseModel

from cmk.server_side_calls.v1 import (
    HostConfig,
    Secret,
    SpecialAgentCommand,
    SpecialAgentConfig,
)


class TraefikArgs(BaseModel):
    """defines all needed parameters for the special agent"""

    hostname: str
    username: str
    password: Secret
    auth_type: str
    no_http_check: bool = False
    no_tcp_check: bool = False
    no_udp_check: bool = False
    no_cert_check: bool = False
    no_https: bool = False
    port: int | None = None


def agent_traefik_arguments(
    params: TraefikArgs, _host_config: HostConfig
) -> Iterable[SpecialAgentCommand]:
    """replaces the argument_thingy from the old API"""
    command_arguments: list[str | Secret] = []
    command_arguments += ["--hostname", params.hostname]
    command_arguments += ["--username", params.username]
    command_arguments += ["--password", params.password.unsafe()]
    command_arguments += ["--auth-typ", params.auth_type]
    if params.no_http_check:
        command_arguments.append("--no-http-check")
    if params.no_tcp_check:
        command_arguments.append("--no-tcp-check")
    if params.no_udp_check:
        command_arguments.append("--no-udp-check")
    if params.no_https:
        command_arguments.append("--no-https")
    if params.no_cert_check:
        command_arguments.append("--no-cert-check")
    if params.port is not None:
        command_arguments += ["--port", str(params.port)]
    yield SpecialAgentCommand(command_arguments=command_arguments)


special_agent_traefik = SpecialAgentConfig(
    # name must be the filename of the executable for the special agent (without prefix)
    name="traefik",
    parameter_parser=TraefikArgs.model_validate,
    commands_function=agent_traefik_arguments,
)