namespace Netina.Api.Controllers;

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

        group.MapGet("{id}", GetAsync)
            .WithDisplayName("GetOneAsync")
            .RequireAuthorization(builder => builder.AddAuthenticationSchemes("Bearer").RequireAuthenticatedUser().RequireClaim(CustomClaimType.Permission, ApplicationPermission.ViewAllReviews,ApplicationPermission.ManageReview))
            .HasApiVersion(1.0);

        group.MapGet("", GetAllAsync)
            .WithDisplayName("GetAllAsync")
            .RequireAuthorization(builder => builder.AddAuthenticationSchemes("Bearer").RequireAuthenticatedUser().RequireClaim(CustomClaimType.Permission, ApplicationPermission.ViewAllReviews, ApplicationPermission.ManageReview))
            .HasApiVersion(1.0);

        group.MapPost("", PostAsync)
            .WithDisplayName("PostReview")
            .RequireAuthorization(builder => builder.AddAuthenticationSchemes("Bearer").RequireAuthenticatedUser().RequireClaim(CustomClaimType.Permission, ApplicationPermission.ManageReview, ApplicationPermission.AddReview))
            .HasApiVersion(1.0);

        group.MapPut("confirm/{id}", ConfirmAsync)
            .WithDisplayName("ConfirmAsync")
            .RequireAuthorization(builder => builder.AddAuthenticationSchemes("Bearer").RequireAuthenticatedUser().RequireClaim(CustomClaimType.Permission, ApplicationPermission.ConfirmReview, ApplicationPermission.ManageReview))
            .HasApiVersion(1.0);

        group.MapDelete("{id}", DeleteAsync)
            .WithDisplayName("DeleteAsync")
            .RequireAuthorization(builder => builder.AddAuthenticationSchemes("Bearer").RequireAuthenticatedUser().RequireClaim(CustomClaimType.Permission, ApplicationPermission.ManageReview))
            .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));
}