namespace Netina.Api.Controllers;

public class DiscountController : ICarterModule
{
    public virtual void AddRoutes(IEndpointRouteBuilder app)
    {
        var group = app.NewVersionedApi("Discount")
            .MapGroup($"api/discount")
            .RequireAuthorization(builder => builder.AddAuthenticationSchemes("Bearer").RequireAuthenticatedUser());

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

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

        group.MapPost("", Post)
            .HasApiVersion(1.0)
            .RequireAuthorization(builder => builder.AddAuthenticationSchemes("Bearer").RequireAuthenticatedUser().RequireClaim(CustomClaimType.Permission, ApplicationPermission.ManageDiscounts));

        group.MapPut("", Put)
            .HasApiVersion(1.0)
            .RequireAuthorization(builder => builder.AddAuthenticationSchemes("Bearer").RequireAuthenticatedUser().RequireClaim(CustomClaimType.Permission, ApplicationPermission.ManageDiscounts));

        group.MapDelete("{id}", Delete)
            .HasApiVersion(1.0)
            .RequireAuthorization(builder => builder.AddAuthenticationSchemes("Bearer").RequireAuthenticatedUser().RequireClaim(CustomClaimType.Permission, ApplicationPermission.ManageDiscounts));
    }

    // GET:Get All Entity
    private async Task<IResult> GetAllAsync([FromQuery] int page, IMediator mediator,
        CancellationToken cancellationToken)
        => TypedResults.Ok(await mediator.Send(new GetDiscountsQuery(page), cancellationToken));

    // GET:Get An Entity By Id
    private async Task<IResult> GetAsync(Guid id, IMediator mediator, CancellationToken cancellationToken)
        => TypedResults.Ok(await mediator.Send(new GetDiscountQuery(id), cancellationToken));

    // POST:Create Entity
    private async Task<IResult> Post([FromBody] CreateDiscountCommand request, IMediator mediator,
        CancellationToken cancellationToken)
        => TypedResults.Ok(await mediator.Send(request, cancellationToken));

    // PUT:Update Entity
    private async Task<IResult> Put([FromBody] UpdateDiscountCommand request, IMediator mediator,
        CancellationToken cancellationToken)
        => TypedResults.Ok(await mediator.Send(request, cancellationToken));

    // DELETE:Delete Entity
    private async Task<IResult> Delete(Guid id, IMediator mediator, CancellationToken cancellationToken)
        => TypedResults.Ok(await mediator.Send(new DeleteDiscountCommand(id), cancellationToken));
}