namespace Brizco.Api.Controllers;

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

        group.MapGet("", GetAllAsync)
            .WithDisplayName("GetAllUser")
            .RequireAuthorization(builder => builder.RequireClaim(CustomClaimType.Permission, ApplicationPermission.ViewUsers,ApplicationPermission.ManageUsers))
            .HasApiVersion(1.0);

        group.MapGet("{id}", GetAsync)
            .WithDisplayName("GetOneUser")
            .RequireAuthorization(builder => builder.RequireClaim(CustomClaimType.Permission, ApplicationPermission.ViewUsers, ApplicationPermission.ManageUsers))
            .HasApiVersion(1.0);

        group.MapGet("/profile", GetUserProfileAsync)
            .WithDisplayName("GetUserProfile")
            .HasApiVersion(1.0);

        group.MapPost("/role/change/{roleId}", ChangeUserRoleAsync)
            .WithDisplayName("GetUserProfile")
            .HasApiVersion(1.0);

        group.MapPost("", Post)
            .RequireAuthorization(builder => builder.RequireClaim(CustomClaimType.Permission,  ApplicationPermission.ManageUsers))
            .HasApiVersion(1.0);

        group.MapPut("", Put)
            .RequireAuthorization(builder => builder.RequireClaim(CustomClaimType.Permission, ApplicationPermission.ManageUsers))
            .HasApiVersion(1.0);

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


    public async Task<IResult> ChangeUserRoleAsync(Guid roleId,IUserService userService, CancellationToken cancellationToken)
        => TypedResults.Ok(await userService.ChangeUserRoleAsync(roleId,cancellationToken));


    public async Task<IResult> GetUserProfileAsync(IUserService userService, CancellationToken cancellationToken)
        => TypedResults.Ok(await userService.GetUserProfileAsync(cancellationToken));

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

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

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

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

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