using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text.Json;

// Variant "framework-lookup" (2026-11-21): the handler returns the objects and ASP.NET Core finds the JSON
// description of each body type in the configured resolver chain. No GetTypeInfo call in our code.
var builder = WebApplication.CreateSlimBuilder(args);
builder.Logging.AddFilter("Microsoft.AspNetCore.Hosting.Diagnostics", LogLevel.Warning);
builder.Services.ConfigureHttpJsonOptions(options =>
    options.SerializerOptions.TypeInfoResolverChain.Insert(0, JobJson.Default));

var app = builder.Build();
var journal = new JobJournal();

app.MapGet("/health", () => TypedResults.Text("ready"));
app.MapGet("/jobs", () => TypedResults.Ok(journal.Snapshot()));
app.MapPost("/jobs", IResult (SubmitJob? request) =>
{
    if (string.IsNullOrWhiteSpace(request?.Name))
        return TypedResults.BadRequest(new JobProblem("name-required", "A job name is required."));
    var queued = journal.Enqueue(request.Name.Trim());
    return TypedResults.Accepted("/jobs", queued);
});

Console.WriteLine($"Runtime {RuntimeInformation.FrameworkDescription}; dynamic code {RuntimeFeature.IsDynamicCodeSupported}; JSON reflection {JsonSerializer.IsReflectionEnabledByDefault}");
app.Run();
