using GerbilManagerWebAPI.Import;
using Microsoft.AspNetCore.Http.HttpResults;

namespace GerbilManagerWebAPI.Endpoints
{
    /// <summary>
    /// FEAT-8 stage 3 — spreadsheet import loader. Consumes tools/import/output.
    ///   POST /import/dry-run  -> report what WOULD be created/linked/quarantined (no writes)
    ///   POST /import/execute  -> load the conflict-free subset (idempotent). GATED: only run
    ///                            against Julian's DB under god-supervision after he approves the dry-run.
    /// </summary>
    public static class ImportEndpoints
    {
        public static IEndpointRouteBuilder MapImportEndpoints(this IEndpointRouteBuilder app)
        {
            var group = app.MapGroup("/import").WithTags("Import");

            group.MapPost("/dry-run", async Task<Ok<ImportReport>> (
                ApplicationContext db, IConfiguration config, IWebHostEnvironment env) =>
            {
                var report = await new ImportService(db, config, env).RunAsync(execute: false);
                return TypedResults.Ok(report);
            });

            group.MapPost("/execute", async Task<Ok<ImportReport>> (
                ApplicationContext db, IConfiguration config, IWebHostEnvironment env) =>
            {
                var report = await new ImportService(db, config, env).RunAsync(execute: true);
                return TypedResults.Ok(report);
            });

            return app;
        }
    }
}
