using Brizco.Core.EntityServices;

namespace Brizco.Api.Controllers;

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

        group.MapGet("/permission", GetPermissions)
            .WithDisplayName("Permissions")
            .RequireAuthorization(builder => builder.RequireClaim(CustomClaimType.Permission, ApplicationPermission.ManageRoles, ApplicationPermission.ViewRoles))
            .HasApiVersion(1.0);

        group.MapGet("", GetAllAsync)
            .WithDisplayName("GetAllRoles")
            .RequireAuthorization(builder => builder.RequireClaim(CustomClaimType.Permission, ApplicationPermission.ManageRoles , ApplicationPermission.ViewRoles))
            .HasApiVersion(1.0);

        group.MapGet("{id}", GetAsync)
            .WithDisplayName("GetOneRole")
            .RequireAuthorization(builder => builder.RequireClaim(CustomClaimType.Permission, ApplicationPermission.ManageRoles, ApplicationPermission.ViewRoles))
            .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, IUserService userService, CancellationToken cancellationToken)
        => TypedResults.Ok(await userService.GetRolesAsync(page, cancellationToken));

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

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

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

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

    public IResult GetPermissions(IUserService userService)
        => TypedResults.Ok(userService.GetPermissions());
}