using Netina.Core.CoreServices.CommandQueries.Queries;

namespace Netina.Api.Controllers;

public class SearchController : ICarterModule
{
    public void AddRoutes(IEndpointRouteBuilder app)
    {
        var group = app.NewVersionedApi("Search").MapGroup("api/search");


        group.MapGet("/thumb", SearchThumbAsync)
            .WithDisplayName("Thumb Search")
            .HasApiVersion(1.0);

        group.MapGet("zarehbin", ZarehbinAsync)
            .WithDisplayName("Get Zarehbin Product")
            .HasApiVersion(1.0);

        group.MapGet("torob", TorobAsync)
            .WithDisplayName("Get Torob Product")
            .HasApiVersion(1.0);

        group.MapGet("emalls", EmallsAsync)
            .WithDisplayName("Get Emalls Product")
            .HasApiVersion(1.0);
    }

    private async Task<IResult> SearchThumbAsync([FromQuery] string name, [FromServices] IMediator mediator, CancellationToken cancellationToken)
        => TypedResults.Ok(await mediator.Send(new GetThumbSearchProductsQuery(name),cancellationToken));


    private async Task<IResult> ZarehbinAsync([FromQuery] string? product_id, [FromQuery] int? page, [FromServices] IMediator mediator, CancellationToken cancellationToken)
        => TypedResults.Ok(await mediator.Send(new GetZarehbinProductsQuery(Page: page ?? 0, ProductId: product_id), cancellationToken));

    private async Task<IResult> TorobAsync([FromQuery] int page, [FromServices] IMediator mediator, CancellationToken cancellationToken)
        => TypedResults.Ok(await mediator.Send(new GetTorobProductsQuery(page),cancellationToken));

    private async Task<IResult> EmallsAsync([FromQuery] int page, [FromQuery]int? item_per_page, [FromServices] IMediator mediator, CancellationToken cancellationToken)
        => TypedResults.Ok(await mediator.Send(new GetEmallsProductsQuery(page,item_per_page??20), cancellationToken));
}