// Groups tutorial sample
// Demonstrates:
// 1) list groups
// 2) select a groupdId
// 3) list devices in that group

using System;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text.Json;
using System.Threading.Tasks;

class DeviceGroupsSamples
{
    static async Task Main()
    {
        var baseUrl = "OMS_API_BASE_URL";
        var omsToken = "YOUR_OMS_TOKEN";

        using var client = new HttpClient();

        void AddAuth(HttpRequestMessage req)
        {
            req.Headers.Add("x-oms-token", omsToken);
            req.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
        }

        async Task<string> SendGetAsync(string relativeUrl)
        {
            var req = new HttpRequestMessage(HttpMethod.Get, $"{baseUrl}{relativeUrl}");
            AddAuth(req);

            var resp = await client.SendAsync(req);
            var body = await resp.Content.ReadAsStringAsync();

            Console.WriteLine($"GET {relativeUrl}");
            Console.WriteLine($"-> {(int)resp.StatusCode} {resp.ReasonPhrase}");
            Console.WriteLine(body);
            Console.WriteLine();

            if (!resp.IsSuccessStatusCode)
            {
                throw new Exception($"Request failed: {(int)resp.StatusCode} {resp.ReasonPhrase}");
            }

            return body;
        }

        // 1) List groups
        var groupsBody = await SendGetAsync("/groups?num=1&size=10");
        var groupsJson = JsonDocument.Parse(groupsBody);
        var groups = groupsJson.RootElement.GetProperty("data");

        if (groups.ValueKind != JsonValueKind.Array || groups.GetArrayLength() == 0)
        {
            Console.WriteLine("No groups available.");
            return;
        }

        // 2) Pick the first group from the response
        var firstGroup = groups.EnumerateArray().First();
        var groupId = firstGroup.GetProperty("groupdId").GetString();
        var groupName = firstGroup.GetProperty("name").GetString();

        Console.WriteLine($"Using group: {groupName} ({groupId})");
        Console.WriteLine();

        // 3) List devices in the selected group
        var devicesBody = await SendGetAsync($"/groups/{groupId}/devices?num=1&size=10");
        var devicesJson = JsonDocument.Parse(devicesBody);
        var devices = devicesJson.RootElement.GetProperty("data");

        if (devices.ValueKind != JsonValueKind.Array || devices.GetArrayLength() == 0)
        {
            Console.WriteLine("This group has no devices.");
            return;
        }

        foreach (var device in devices.EnumerateArray())
        {
            var deviceName = device.TryGetProperty("name", out var nameProp) ? nameProp.GetString() : "";
            var powerState = device.TryGetProperty("powerState", out var powerProp) ? powerProp.GetString() : "";
            var networkState = device.TryGetProperty("networkState", out var networkProp) ? networkProp.GetString() : "";
            var inputSource = device.TryGetProperty("inputSource", out var inputProp) ? inputProp.GetString() : "";

            var hardware = device.GetProperty("hardwareIdentity");
            var deviceId = hardware.TryGetProperty("deviceId", out var deviceIdProp) ? deviceIdProp.GetString() : "";
            var deviceModel = hardware.TryGetProperty("deviceModel", out var modelProp) ? modelProp.GetString() : "";

            Console.WriteLine($"Device: {deviceName}");
            Console.WriteLine($"  deviceId: {deviceId}");
            Console.WriteLine($"  model: {deviceModel}");
            Console.WriteLine($"  power: {powerState}");
            Console.WriteLine($"  network: {networkState}");
            Console.WriteLine($"  inputSource: {inputSource}");
            Console.WriteLine();
        }
    }
}