// Device System Update sample
// Usage: copy into a small console app or run with dotnet-script

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

class DeviceSystemUpdateSamples
{
    static async Task Main()
    {
        var baseUrl = "OMS_API_BASE_URL";
        var deviceId = "YOUR_DEVICE_ID"; // replace with your deviceId
        var apiKey = "YOUR_API_KEY"; // or null to use subscription-key
        var subscriptionKey = ""; // or set subscription key and leave apiKey empty

        using var client = new HttpClient();

        // Helper to add authentication
        void AddAuth(HttpRequestMessage req)
        {
            if (!string.IsNullOrEmpty(apiKey))
            {
                req.Headers.Add("x-api-key", apiKey);
            }
            else if (!string.IsNullOrEmpty(subscriptionKey))
            {
                var uri = req.RequestUri;
                var builder = new UriBuilder(uri);
                if (string.IsNullOrEmpty(builder.Query)) builder.Query = "subscription-key=" + Uri.EscapeDataString(subscriptionKey);
                else builder.Query = builder.Query.TrimStart('?') + "&subscription-key=" + Uri.EscapeDataString(subscriptionKey);
                req.RequestUri = builder.Uri;
            }
        }

        // 1) GET current system-update status
        Console.WriteLine("GET system-update status...");
        var getReq = new HttpRequestMessage(HttpMethod.Get, $"{baseUrl}/devices/{deviceId}/system-update");
        AddAuth(getReq);
        getReq.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

        var getResp = await client.SendAsync(getReq);
        Console.WriteLine($"GET -> {(int)getResp.StatusCode} {getResp.ReasonPhrase}");
        if (getResp.Content != null)
        {
            var body = await getResp.Content.ReadAsStringAsync();
            Console.WriteLine("Body: " + body);
        }

        // 2) POST enable system update
        Console.WriteLine("POST enable system-update (enabled=true)...");
        var payload = new { enabled = true };
        var json = JsonSerializer.Serialize(payload);
        var postReq = new HttpRequestMessage(HttpMethod.Post, $"{baseUrl}/devices/{deviceId}/system-update")
        {
            Content = new StringContent(json, Encoding.UTF8, "application/json")
        };
        AddAuth(postReq);

        var postResp = await client.SendAsync(postReq);
        Console.WriteLine($"POST -> {(int)postResp.StatusCode} {postResp.ReasonPhrase}");

        // 3) POST trigger system update
        Console.WriteLine("POST trigger system-update...");
        var triggerReq = new HttpRequestMessage(HttpMethod.Post, $"{baseUrl}/devices/{deviceId}/system-update/trigger");
        AddAuth(triggerReq);
        var triggerResp = await client.SendAsync(triggerReq);
        Console.WriteLine($"TRIGGER -> {(int)triggerResp.StatusCode} {triggerResp.ReasonPhrase}");

        Console.WriteLine("Done");
    }
}