namespace NetinaShop.Api.Controller;

public class ProductReviewController : ICarterModule
{
    public void AddRoutes(IEndpointRouteBuilder app)
    {
        var group = app.NewVersionedApi("ProductReview")
            .MapGroup("product/review")
            .RequireAuthorization(builder => builder.AddAuthenticationSchemes("Bearer").RequireAuthenticatedUser());

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

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

        group.MapPost("", PostAsync)
            .WithDisplayName("PostReview")
            .HasApiVersion(1.0);

        group.MapPut("confirm/{id}", ConfirmAsync)
            .WithDisplayName("ConfirmAsync")
            .HasApiVersion(1.0);

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

    public async Task<IResult> GetAllAsync([FromQuery] int page, IMediator mediator, CancellationToken cancellationToken)
        => TypedResults.Ok(await mediator.Send(new GetReviewsQuery(page), cancellationToken));

    public async Task<IResult> GetAsync(Guid id, IMediator mediator, CancellationToken cancellationToken)
        => TypedResults.Ok(await mediator.Send(new GetReviewQuery(id), cancellationToken));

    public async Task<IResult> PostAsync(CreateReviewCommand request, IMediator mediator, CancellationToken cancellationToken)
        => TypedResults.Ok(await mediator.Send(request, cancellationToken));

    public async Task<IResult> ConfirmAsync(Guid id, IMediator mediator, CancellationToken cancellationToken)
        => TypedResults.Ok(await mediator.Send(new ConfirmReviewCommand(id), cancellationToken));

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