using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Routing;
using Microsoft.Extensions.DependencyInjection;
using ScadaLink.Security;

namespace ScadaLink.CentralUI.ScriptAnalysis;

public static class ScriptAnalysisEndpoints
{
    public static IEndpointRouteBuilder MapScriptAnalysisEndpoints(this IEndpointRouteBuilder endpoints)
    {
        var group = endpoints.MapGroup("/api/script-analysis")
            .RequireAuthorization(AuthorizationPolicies.RequireDesign);

        group.MapPost("/diagnostics", (DiagnoseRequest req, ScriptAnalysisService svc) =>
            Results.Ok(svc.Diagnose(req)));

        group.MapPost("/completions", async (CompletionsRequest req, ScriptAnalysisService svc) =>
            Results.Ok(await svc.CompleteAsync(req)));

        group.MapPost("/hover", async (HoverRequest req, ScriptAnalysisService svc) =>
            Results.Ok(await svc.Hover(req)));

        group.MapPost("/signature-help", async (SignatureHelpRequest req, ScriptAnalysisService svc) =>
            Results.Ok(await svc.SignatureHelp(req)));

        group.MapPost("/format", (FormatRequest req, ScriptAnalysisService svc) =>
            Results.Ok(svc.Format(req)));

        group.MapPost("/inlay-hints", (InlayHintsRequest req, ScriptAnalysisService svc) =>
            Results.Ok(svc.InlayHints(req)));

        group.MapPost("/run", async (SandboxRunRequest req, ScriptAnalysisService svc, HttpContext http) =>
            Results.Ok(await svc.RunInSandboxAsync(req, http.RequestAborted)));

        return endpoints;
    }
}
