using Application.Abstractions.Messaging;
using Web.Api.Common;

namespace Web.Api.Endpoints.Crypto;

internal sealed class CryptoOrderbook : IEndpoint
{
    public void MapEndpoint(IEndpointRouteBuilder app)
    {
        app.MapGet("api/crypto/{market}/orderbook", async (
            string market,
            ISender sender,
            CancellationToken ct
        ) =>
        {
            return ApiResponse.Ok(
                await sender.Send(new Application.Features.Api.Crypto.Orderbook.Get.Query(NormalizeMarket(market)), ct)
            );
        })
        .WithTags("Crypto")
        .AllowAnonymous();

        app.MapGet("api/crypto/{market}/orderbook/live", async (
            string market,
            ISender sender,
            CancellationToken ct
        ) =>
        {
            return ApiResponse.Ok(
                await sender.Send(new Application.Features.Api.Crypto.Orderbook.GetLive.Query(NormalizeMarket(market)), ct)
            );
        })
        .WithTags("Crypto")
        .AllowAnonymous();
    }

    private static string NormalizeMarket(string input) =>
        input.Contains('-') ? input.ToUpper() : $"KRW-{input.ToUpper()}";
}