using Netina.Core.EntityServices.Abstracts;
using Netina.Repository.Abstracts;

namespace Netina.Api.Controllers;

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

        var group = app.NewVersionedApi("Users")
            .MapGroup($"api/user");

        group.MapGet("info", GetUserInfoAsync)
            .WithDisplayName("GetUserInfo")
            .RequireAuthorization(builder => builder.AddAuthenticationSchemes("Bearer").RequireAuthenticatedUser())
            .HasApiVersion(1.0);

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

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

        group.MapGet("/order", GetUserOrdersAsync)
            .WithDisplayName("GetUserOrders")
            .RequireAuthorization(builder => builder.AddAuthenticationSchemes("Bearer").RequireAuthenticatedUser().RequireClaim(CustomClaimType.Permission, ApplicationPermission.ViewMineOrders))
            .HasApiVersion(1.0);

        group.MapGet("{id}/order", GetUserOrdersByIdAsync)
            .WithDisplayName("GetUserOrders")
            .RequireAuthorization(builder => builder.AddAuthenticationSchemes("Bearer").RequireAuthenticatedUser().RequireClaim(CustomClaimType.Permission, ApplicationPermission.ViewAllOrders))
            .HasApiVersion(1.0);

        group.MapGet("/changelog", GetChangeLogAsync)
            .WithDisplayName("GetChangeLog")
            .RequireAuthorization(builder => builder.AddAuthenticationSchemes("Bearer").RequireAuthenticatedUser().RequireClaim(CustomClaimType.Permission, ApplicationPermission.ManageDashboard))
            .HasApiVersion(1.0);

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

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

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

    private async Task<IResult> GetUserOrdersAsync([FromServices] IMediator mediator, CancellationToken cancellationToken)
        => TypedResults.Ok(await mediator.Send(new GetUserOrdersQuery(default), cancellationToken));

    private async Task<IResult> GetUserOrdersByIdAsync(Guid id,[FromServices]IMediator mediator, CancellationToken cancellationToken)
        => TypedResults.Ok(await mediator.Send(new GetUserOrdersQuery(id), cancellationToken));

    private async Task<IResult> GetChangeLogAsync(IUserService userService, CancellationToken cancellationToken)
        => TypedResults.Ok(await userService.GetAdminChangeLogAsync(cancellationToken));

    public async Task<IResult> GetUserInfoAsync(IUserService userService,ICurrentUserService currentUserService, CancellationToken cancellationToken)
    {

        if (!Guid.TryParse(currentUserService.UserId, out var userId))
            throw new AppException("Wrong Token", ApiResultStatusCode.UnAuthorized);
        return TypedResults.Ok(await userService.GetUserAsync(userId, cancellationToken));
    }
    // GET:Get All Entity
    public async Task<IResult> GetAllAsync([FromQuery] int page, [FromQuery]string? phoneNumber, IUserService userService, CancellationToken cancellationToken)
        => TypedResults.Ok(await userService.GetUsersAsync(page,phoneNumber,cancellationToken));

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

    // 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.RemoveUserAsync(id,cancellationToken));
}