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

from __future__ import annotations

from typing import Any

from ansible.module_utils.basic import AnsibleModule  # type: ignore[reportMissingStubFile]
from ansible_collections.ssnailed.ez_compose.plugins.module_utils import (
    common,
    label,
    service,
)


def label_argument_spec() -> dict[str, Any]:
    label_args: dict[str, Any] = {
        "type": "dict",
        "options": {},
    }

    for module in label.__all__:
        if module == "common":
            continue

        options = {
            **label.common.BASE_ARGS,
            **getattr(label, module).EXTRA_ARGS,
        }

        label_args["options"][module] = {
            "type": "dict",
            "options": options,
        }

    return label_args


def service_argument_spec() -> dict[str, Any]:
    service_args: dict[str, Any] = {
        "type": "dict",
        "options": {},
        "required": True,
    }

    for module in service.__all__:
        if module == "common":
            continue

        options = {
            **service.common.BASE_ARGS,
            **getattr(service, module).EXTRA_ARGS,
        }

        if module == "custom":
            options["label_helpers"] = label_argument_spec()

        service_args["options"][module] = {
            "type": "list",
            "elements": "dict",
            "options": options,
        }

    return service_args


def main() -> None:
    module = AnsibleModule(
        argument_spec={
            "name": {
                "type": "str",
                "required": True,
            },
            "project_dir": {
                "type": "path",
                "default": "/var/lib/ez_compose",
            },
            "services": service_argument_spec(),
        },
    )

    state = common.get_state(module)

    for name, args in module.params["services"].items():
        state = getattr(service, name).helper(state, args)


if __name__ == "__main__":
    main()