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

namespace Web.Api.Endpoints.Crypto;

internal sealed class CryptoCandles : IEndpoint
{
    public void MapEndpoint(IEndpointRouteBuilder app)
    {
        app.MapGet("api/crypto/{market}/candles/seconds", async (
            string market,
            int? count,
            ISender sender,
            CancellationToken ct
        ) =>
        {
            var response = await sender.Send(new Application.Features.Api.Crypto.Candle.GetSeconds.Query(NormalizeMarket(market), count ?? 200), ct);
            return ApiResponse.Ok(response.Candles);
        })
        .WithTags("Crypto")
        .AllowAnonymous();

        app.MapGet("api/crypto/{market}/candles/minutes/{unit:int}", async (
            string market,
            int unit,
            int? count,
            ISender sender,
            CancellationToken ct
        ) =>
        {
            var response = await sender.Send(new Application.Features.Api.Crypto.Candle.GetMinutes.Query(NormalizeMarket(market), unit, count ?? 200), ct);
            return ApiResponse.Ok(response.Candles);
        })
        .WithTags("Crypto")
        .AllowAnonymous();

        app.MapGet("api/crypto/{market}/candles/days", async (
            string market,
            int? count,
            ISender sender,
            CancellationToken ct
        ) =>
        {
            var response = await sender.Send(new Application.Features.Api.Crypto.Candle.GetDays.Query(NormalizeMarket(market), count ?? 200), ct);
            return ApiResponse.Ok(response.Candles);
        })
        .WithTags("Crypto")
        .AllowAnonymous();

        app.MapGet("api/crypto/{market}/candles/weeks", async (
            string market,
            int? count,
            ISender sender,
            CancellationToken ct
        ) =>
        {
            var response = await sender.Send(new Application.Features.Api.Crypto.Candle.GetWeeks.Query(NormalizeMarket(market), count ?? 200), ct);
            return ApiResponse.Ok(response.Candles);
        })
        .WithTags("Crypto")
        .AllowAnonymous();

        app.MapGet("api/crypto/{market}/candles/months", async (
            string market,
            int? count,
            ISender sender,
            CancellationToken ct
        ) =>
        {
            var response = await sender.Send(new Application.Features.Api.Crypto.Candle.GetMonths.Query(NormalizeMarket(market), count ?? 200), ct);
            return ApiResponse.Ok(response.Candles);
        })
        .WithTags("Crypto")
        .AllowAnonymous();

        app.MapGet("api/crypto/{market}/candles/years", async (
            string market,
            int? count,
            ISender sender,
            CancellationToken ct
        ) =>
        {
            var response = await sender.Send(new Application.Features.Api.Crypto.Candle.GetYears.Query(NormalizeMarket(market), count ?? 200), ct);
            return ApiResponse.Ok(response.Candles);
        })
        .WithTags("Crypto")
        .AllowAnonymous();

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

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