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

from __future__ import annotations

from typing import TYPE_CHECKING, Any

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

DOCUMENTATION = """
homepage:
    description:
        - Configuration for homepage labels.
    type: list
    elements: dict
    suboptions:
        name:
            description:
                - Name to set on dashboard.
                - Defaults to name of service.
            type: str
        href:
            description:
                - HREF to set on dashboard.
            type: str
        icon:
            description:
                - URL to an image to use as icon.
            type: str
        description:
            description:
                - Description text to set on dashboard.
            type: str
        group:
            description:
                - Group to place service under on dashboard.
            type: str
        widget:
            description:
                - Widget configuration.
                - See U(https://gethomepage.dev/configs/services/) for more info.
            type: dict
"""

EXTRA_ARGS = {
    "name": {"type": "str"},
    "href": {"type": "str"},
    "icon": {"type": "str"},
    "description": {"type": "str"},
    "group": {"type": "str"},
    "widget": {"type": "dict"},
}


def helper(_state: State, service_name: str, params: dict[str, Any]) -> dict[str, Any]:
    name: str = params.get("name", service_name)
    href: str | None = params.get("href")
    icon: str | None = params.get("icon")
    description: str | None = params.get("description")
    group: str | None = params.get("group")
    widget: dict[str, str] | None = params.get("widget")

    labels: dict[str, Any] = {
        "homepage.name": name,
    }

    if href:
        labels["homepage.href"] = href

    if icon:
        labels["homepage.icon"] = icon

    if description:
        labels["homepage.description"] = description

    if group:
        labels["homepage.group"] = group

    if widget:
        for k, v in widget.items():
            labels[f"homepage.widget.{k}"] = v

    return {
        "labels": labels,
    }