namespace Netina.Api.Controllers;

public class ProductCategoryController : ICarterModule
{

    public virtual void AddRoutes(IEndpointRouteBuilder app)
    {
        var group = app.NewVersionedApi("ProductCategory")
            .MapGroup($"api/product/category");

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

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

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

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

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

    // GET:Get All Entity
    public async Task<IResult> GetAllAsync([FromQuery]int? page, [FromQuery]bool? sortByMain , [FromQuery]string? categoryName,IMediator mediator, CancellationToken cancellationToken)
        => TypedResults.Ok(await mediator.Send(new GetProductCategoriesQuery(Page: page,SortByMain: sortByMain , CategoryName: categoryName),cancellationToken));

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

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

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

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