#!/usr/bin/env python3
"""Generate SUPPORTED_DEVICES.md from the device capability registry.

Run manually with `python scripts/generate_supported_devices.py`, or let the
`generate-supported-devices` pre-commit hook regenerate it automatically.
"""

import sys
from pathlib import Path

REPO_ROOT = Path(__file__).resolve().parent.parent

sys.path.insert(0, str(REPO_ROOT / "src"))

from govee_local_api.light_capabilities import (  # noqa: E402
    GOVEE_LIGHT_CAPABILITIES,
    SCENE_CODES,
    GoveeLightFeatures,
)

OUTPUT_FILE = REPO_ROOT / "SUPPORTED_DEVICES.md"

YES = "✅"
NO = "❌"


def _mark(features: GoveeLightFeatures, flag: GoveeLightFeatures) -> str:
    return YES if features & flag else NO


def generate() -> str:
    scene_names = ", ".join(SCENE_CODES)
    lines = [
        "# Supported Devices",
        "",
        "<!-- Auto-generated by scripts/generate_supported_devices.py — do not edit by hand. -->",
        "<!-- Regenerate with: python scripts/generate_supported_devices.py -->",
        "",
        f"The library currently knows **{len(GOVEE_LIGHT_CAPABILITIES)}** device"
        " models and the features each one supports. Models not listed here are"
        " still discovered, but fall back to on/off control only. If your device"
        " is missing or its capabilities are wrong, please"
        " [open an issue](https://github.com/Galorhallen/govee-local-api/issues).",
        "",
        f"Devices with scene support share the same scene set: {scene_names}.",
        "",
        "| Model | RGB | Color Temperature | Brightness | Segments | Scenes |",
        "| ----- | --- | ----------------- | ---------- | -------- | ------ |",
    ]

    for model, capabilities in sorted(GOVEE_LIGHT_CAPABILITIES.items()):
        features = capabilities.features
        segments = (
            str(capabilities.segments_count) if capabilities.segments_count else "-"
        )
        lines.append(
            f"| {model} "
            f"| {_mark(features, GoveeLightFeatures.COLOR_RGB)} "
            f"| {_mark(features, GoveeLightFeatures.COLOR_KELVIN_TEMPERATURE)} "
            f"| {_mark(features, GoveeLightFeatures.BRIGHTNESS)} "
            f"| {segments} "
            f"| {_mark(features, GoveeLightFeatures.SCENES)} |"
        )

    return "\n".join(lines) + "\n"


if __name__ == "__main__":
    OUTPUT_FILE.write_text(generate(), encoding="utf-8")
