using NetinaShop.Domain.Enums;

namespace NetinaShop.Api.Controller;
public class OrderController : ICarterModule
{
    public void AddRoutes(IEndpointRouteBuilder app)
    {
        var group = app.NewVersionedApi("Order")
            .MapGroup("api/order")
            .RequireAuthorization(builder => builder.AddAuthenticationSchemes("Bearer").RequireAuthenticatedUser());

        group.MapGet("", GetAllAsync)
            .WithDisplayName("GetAllOrders")
            .HasApiVersion(1.0);

        group.MapGet("{id}", GetAsync)
            .WithDisplayName("GetOneOrder")
            .HasApiVersion(1.0);

        group.MapDelete("{id}", DeleteAsync)
            .WithDisplayName("DeleteOneOrder")
            .HasApiVersion(1.0);

    }

    public async Task<IResult> GetAllAsync(IMediator mediator, [FromQuery]long? selectedDate, [FromQuery] OrderStatus? orderStatus, [FromQuery] OrderQueryDateFilter? dateFilter, [FromQuery] int page = 0, CancellationToken cancellationToken = default)
        => TypedResults.Ok(await mediator.Send(new GetOrdersQuery(Page:page, SelectedDate: selectedDate, OrderStatus:orderStatus, DateFilter:dateFilter), cancellationToken));

    public async Task<IResult> GetAsync(IMediator mediator, Guid id, CancellationToken cancellationToken = default)
    {
        var order = await mediator.Send(new GetOrderLDtoQuery(id), cancellationToken);
        return TypedResults.Ok(order);
    }

    public async Task<IResult> DeleteAsync(IMediator mediator, Guid id, CancellationToken cancellationToken = default)
        => TypedResults.Ok(await mediator.Send(new DeleteOrderCommand(id), cancellationToken));
}
