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

static string Required(string name) =>
    Environment.GetEnvironmentVariable(name) ??
    throw new InvalidOperationException($"Set {name} before running this example.");

static Dictionary<string, double> Values(double proximity, double certification, double load, double safety, double rating) => new()
{
    ["proximity_score"] = proximity,
    ["certification_match_score"] = certification,
    ["current_job_load"] = load,
    ["safety_history_score"] = safety,
    ["customer_rating_score"] = rating
};

var identityUrl = Environment.GetEnvironmentVariable("DECISIOQ_IDENTITY_URL") ?? "https://identity.vinquery.com/connect/token";
var catalogUrl = (Environment.GetEnvironmentVariable("DECISIOQ_DKS_URL") ?? "https://dks.vinquery.com").TrimEnd('/');
var decisionServiceUrl = (Environment.GetEnvironmentVariable("DECISIOQ_DDE_URL") ?? "https://dde.vinquery.com").TrimEnd('/');
var audience = Environment.GetEnvironmentVariable("DECISIOQ_AUDIENCE") ?? "vinquery:api:decisioq";
var correlationId = $"auto-tow-019-csharp-{DateTimeOffset.UtcNow:yyyyMMddHHmmss}";
using var http = new HttpClient { Timeout = TimeSpan.FromSeconds(45) };

var tokenResponse = await http.PostAsJsonAsync(identityUrl, new
{
    clientId = Required("DECISIOQ_CLIENT_ID"),
    clientSecret = Required("DECISIOQ_CLIENT_SECRET"),
    audience
});
tokenResponse.EnsureSuccessStatusCode();
var jwtToken = JsonDocument.Parse(await tokenResponse.Content.ReadAsStringAsync()).RootElement.GetProperty("jwtToken").GetString();

using var metadataRequest = new HttpRequestMessage(HttpMethod.Get, $"{catalogUrl}/decisioncatalog/decisions/AUTO-TOW-019");
metadataRequest.Headers.Authorization = new AuthenticationHeaderValue("Bearer", jwtToken);
metadataRequest.Headers.TryAddWithoutValidation("X-Correlation-Id", correlationId);
using var metadataResponse = await http.SendAsync(metadataRequest);
metadataResponse.EnsureSuccessStatusCode();

var executionRequest = new
{
    decisionId = "AUTO-TOW-019",
    profileId = "balanced",
    scenarioId = "standard",
    algorithm = "TOPSIS",
    weightStrategy = "Expert",
    runSensitivity = false,
    requestContext = new { correlationId },
    options = new object[]
    {
        new { optionId = "OPTION-001", name = "Operator North Zone", values = Values(88, 88, 1, 88, 88) },
        new { optionId = "OPTION-002", name = "Operator Central Zone", values = Values(80, 80, 3, 80, 80) },
        new { optionId = "OPTION-003", name = "Operator East Zone", values = Values(72, 72, 5, 72, 72) }
    }
};

using var execution = new HttpRequestMessage(HttpMethod.Post, $"{decisionServiceUrl}/api/v1/decide")
{
    Content = JsonContent.Create(executionRequest)
};
execution.Headers.Authorization = new AuthenticationHeaderValue("Bearer", jwtToken);
execution.Headers.TryAddWithoutValidation("X-Correlation-Id", correlationId);
using var executionResponse = await http.SendAsync(execution);
Console.WriteLine(await executionResponse.Content.ReadAsStringAsync());
executionResponse.EnsureSuccessStatusCode();



