namespace Netina.Api.Controllers;

public class ShippingController : ICarterModule
{

    public virtual void AddRoutes(IEndpointRouteBuilder app)
    {
        var group = app.NewVersionedApi("WarehouseShipping")
            .MapGroup($"api/warehouse/shipping");

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

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

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

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

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

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

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

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

    // PUT:Update Entity
    public async Task<IResult> Put([FromBody] UpdateShippingCommand 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 DeleteShippingCommand(id), cancellationToken));
}