namespace DragonISO.App.Services;

// GET / â€” server info + endpoint catalogue. Returned as the JSON
// homepage when a Companion / Stream Deck plugin first probes the
// surface; humans see it via curl http://127.0.0.1:9755/.
public sealed partial class ControlSurfaceServer
{
    private object GetServerInfo()
    {
        // Best-effort engine snapshot â€” wrapped in TryRead so a transient
        // controller error doesn't 500 the homepage poll.
        var settings = TryRead(() => _controller.GlobalSettings);
        var groups = TryRead(() => _controller.GroupSettings);
        return new
        {
            product = "Dragon-ISO",
            version = typeof(ControlSurfaceServer).Assembly.GetName().Version?.ToString() ?? "unknown",
            engine = new
            {
                framerateHz = settings?.FramerateHz,
                targetResolution = settings?.Resolution.ToString(),
                aspectMode = settings?.Aspect.ToString(),
                audioMode = settings?.Audio.ToString(),
                discoveryGroups = groups?.DiscoveryGroups,
                outputGroups = groups?.OutputGroups,
            },
            endpoints = new[]
            {
                "GET  /  (this)",
                "GET  /ui  (HTML control panel)",
                "GET  /participants",
                "GET  /ws  (WebSocket: live participant snapshots)",
                "POST /participants/{id}/iso",
                "POST /participants/iso (body: displayName + enabled)",
                "POST /presets/{name}/apply",
                "POST /presets/refresh-discovery",
                "POST /presets/stop-all",
                "POST /teams/mute, /camera, /leave, /share, /raise-hand",
                "POST /notes (body: text)",
            },
        };
    }

    private static T? TryRead<T>(Func<T> reader) where T : class
    {
        try { return reader(); }
        catch { return null; }
    }
}
