# Copyright: (c) 2024, Luca Bilke <luca@bil.ke>
# MIT License (see LICENSE)

from __future__ import annotations

from typing import TYPE_CHECKING, Any

import ansible_collections.ssnailed.ez_compose.plugins.module_utils.service.common as service  # type: ignore[reportMissingTypeStubs]

if TYPE_CHECKING:
    from ansible_collections.ssnailed.ez_compose.plugins.module_utils.common import (  # type: ignore[reportMissingTypeStubs]
        State,
    )

EXTRA_ARGS = {
    "proxy_type": {"type": "str"},
    "middleware_name": {"type": "string"},
    "middleware": {"type": "str", "required": True},
    "settings": {"type": "list", "required": True},
}


def helper(state: State, service_name: str, params: dict[str, Any]) -> State:
    project_name: str = state.module.params["name"]
    middleware: str = params["middleware"]
    settings: dict[str, str] = params["settings"]
    proxy_type: str = state.module.params.get("proxy_type", "http")
    middleware_name: str = state.module.params.get(
        "middleware_name",
        f"{project_name}_{service_name}_{proxy_type}_{middleware.lower()}",
    )

    prefix = f"traefik.{proxy_type}.middlewares.{middleware_name}"

    labels = {f"{prefix}.{middleware}.{key}": var for key, var in settings.items()}

    update = {
        "labels": labels,
    }

    return service.update(service_name, state, update)