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

from __future__ import annotations

from typing import TYPE_CHECKING, Any

from ansible_collections.snailed.ez_docker.plugins.module_utils import common, label, spec

if TYPE_CHECKING:
    from ansible_collections.snailed.ez_docker.plugins.module_utils.models import State

DOCUMENTATION = """
custom:
    description:
        - Configuration for custom service.
    type: list
    elements: dict
    suboptions:
        name:
            description:
                - Name of the service
            type: str
            required: true
        definition:
            description:
                - Service definition
            type: dict
            required: true
        label_helpers:
            description:
                - Label helper configurations
            type: dict
            suboptions:
"""  # NOTE: label helpers are added programatically

FORCE_ARGS = {
    "name": {"type": "str", "required": True},
    "definition": {"type": "dict", "required": True},
    "label_helpers": spec.label_argument_spec(),
}


def helper(state: State, service_params: dict[str, Any]) -> dict[str, Any]:
    update: dict[str, Any] = {}

    for name, labels_params in service_params.get("label_helpers", {}).items():
        for label_params in labels_params:
            default_params = label.common.get_default_args(state, name)
            params = common.recursive_update(default_params, label_params)
            helper = getattr(label, name).helper
            update |= label.common.run_helper(state, service_params["name"], update, params, helper)

    return update