# 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_compose.plugins.module_utils import label, spec
from ansible_collections.snailed.ez_compose.plugins.module_utils.common import clean_none

if TYPE_CHECKING:
    from ansible_collections.snailed.ez_compose.plugins.module_utils.models import (
        State,
    )

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


def helper(state: State, params: dict[str, Any]) -> dict[str, Any]:
    internal_network: bool = params["internal_network"]

    update: dict[str, Any] = {}

    if internal_network:
        networks = update.get("networks", {})
        networks["internal"] = None
        update["networks"] = networks

    for name, args in [(x, y) for x, y in params.get("label_helpers", {}).items() if y]:
        label_params = label.common.get_default_args(state, name) | clean_none(args)
        helper = getattr(label, name).helper
        update = label.common.run_helper(state, params["name"], update, label_params, helper)

    return update