using NeonSprawl.Server.Game.PositionState;
using NeonSprawl.Server.Game.Skills;

namespace NeonSprawl.Server.Game.Mastery;

/// <summary>Dev-only route to reset/set mastery fixture state for Bruno and manual QA (NEO-48).</summary>
public static class MasteryFixtureApi
{
    public static WebApplication MapMasteryFixtureApi(this WebApplication app)
    {
        app.MapPost(
            "/game/players/{id}/__dev/mastery-fixture",
            (string id, MasteryFixtureRequest? body, IPositionStateStore positions,
                IPlayerPerkStateStore perkStore, IPlayerSkillProgressionStore xpStore) =>
            {
                if (body is null || body.SchemaVersion != MasteryFixtureRequest.CurrentSchemaVersion)
                {
                    return Results.BadRequest();
                }

                if (!MasteryFixtureOperations.TryNormalizeSkillXp(body.SkillXp, out _))
                {
                    return Results.BadRequest();
                }

                var trimmedId = id.Trim();
                if (trimmedId.Length == 0 || !positions.TryGetPosition(trimmedId, out _))
                {
                    return Results.NotFound();
                }

                if (!MasteryFixtureOperations.TryApply(trimmedId, body, perkStore, xpStore))
                {
                    return Results.NotFound();
                }

                return Results.Json(
                    new MasteryFixtureResponse
                    {
                        Applied = true,
                    });
            });

        return app;
    }
}
