using NetinaShop.Core.EntityServices.Abstracts;

namespace NetinaShop.Api.Controller;

public class RoleController : ICarterModule
{
    public void AddRoutes(IEndpointRouteBuilder app)
    {

        var group = app.NewVersionedApi("Roles")
            .MapGroup($"api/user/role")
            .RequireAuthorization(builder => builder.AddAuthenticationSchemes("Bearer").RequireAuthenticatedUser());

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

        group.MapGet("permission", GetAllPermissions)
            .WithDisplayName("GetAllPermissions")
            .HasApiVersion(1.0);

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

        group.MapPost("", Post)
            .HasApiVersion(1.0);

        group.MapPut("", Put)
            .HasApiVersion(1.0);

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

    // GET:Get All Entity
    public async Task<IResult> GetAllAsync([FromQuery] int? page, [FromQuery]string? roleName, IUserService userService, CancellationToken cancellationToken)
        => TypedResults.Ok(await userService.GetRolesAsync(page,roleName, cancellationToken));

    // GET:Get An Entity By Id
    public async Task<IResult> GetAsync(Guid id, IUserService userService, CancellationToken cancellationToken)
        => TypedResults.Ok(await userService.GetRoleAsync(id, cancellationToken));

    // POST:Create Entity
    public async Task<IResult> Post([FromBody] RoleActionRequestDto request, IUserService userService, CancellationToken cancellationToken)
        => TypedResults.Ok(await userService.CreateRoleAsync(request, cancellationToken));

    // PUT:Update Entity
    public async Task<IResult> Put([FromBody] RoleActionRequestDto request, IUserService userService, CancellationToken cancellationToken)
        => TypedResults.Ok(await userService.EditRoleAsync(request, cancellationToken));

    // DELETE:Delete Entity
    public async Task<IResult> Delete(Guid id, IUserService userService, CancellationToken cancellationToken)
        => TypedResults.Ok(await userService.RemoveRoleAsync(id, cancellationToken));

    // DELETE:Delete Entity
    public async Task<IResult> GetAllPermissions(IUserService userService)
        => TypedResults.Ok(userService.GetPermissions());
}