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

namespace Web.Api.Endpoints.Crypto;

internal sealed class CryptoTickers : IEndpoint
{
    public void MapEndpoint(IEndpointRouteBuilder app)
    {
        // 전체 코인 조회
        app.MapGet("api/crypto/tickers", async (
            string? quote,
            ISender sender,
            CancellationToken ct
        ) =>
        {
            var response = await sender.Send(new Application.Features.Api.Crypto.Ticker.GetAll.Query(quote), ct);
            return ApiResponse.Ok(response.Tickers);
        })
        .WithTags("Crypto")
        .AllowAnonymous();

        // 선별된 코인 조회
        app.MapGet("api/crypto/tickers/featured", async (
            string? quote,
            ISender sender,
            CancellationToken ct
        ) =>
        {
            var response = await sender.Send(new Application.Features.Api.Crypto.Ticker.GetAll.Query(quote, true), ct);
            return ApiResponse.Ok(response.Tickers);
        })
        .WithTags("Crypto")
        .AllowAnonymous();

        // 페어 단위 현재가 조회
        app.MapGet("api/crypto/{market}/ticker", async (
            string market,
            ISender sender,
            CancellationToken ct
        ) =>
        {
            return ApiResponse.Ok(
                await sender.Send(new Application.Features.Api.Crypto.Ticker.GetDetail.Query(NormalizeMarket(market)), ct)
            );
        })
        .WithTags("Crypto")
        .AllowAnonymous();
    }

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