using Indotalent.ConfigBackEnd.Extensions;
using Indotalent.Features.Multitenant.TenantUser.Cqrs;
using MediatR;
using Microsoft.AspNetCore.Authentication.JwtBearer;

namespace Indotalent.Features.Multitenant.TenantUser;

public static class TenantUserEndpoint
{
    public static void MapTenantUserEndpoints(this IEndpointRouteBuilder app)
    {
        var group = app.MapGroup("/tenant-user").WithTags("TenantUsers")
            .RequireAuthorization(policy => policy.AddAuthenticationSchemes(JwtBearerDefaults.AuthenticationScheme)
            .RequireAuthenticatedUser());

        group.MapGet("/", async (IMediator mediator) =>
        {
            var result = await mediator.Send(new GetTenantUserListQuery());
            return result.ToApiResponse("Tenant user list retrieved successfully");
        })
        .WithName("GetTenantUserList")
        .WithTags("TenantUsers");

        group.MapGet("/{id}", async (string id, IMediator mediator) =>
        {
            var result = await mediator.Send(new GetTenantUserByIdQuery(id));
            return result.ToApiResponse(result is not null
                ? "Tenant user detail retrieved successfully"
                : $"Tenant user with ID {id} not found");
        })
        .WithName("GetTenantUserById")
        .WithTags("TenantUsers");

        group.MapGet("/lookup", async (IMediator mediator) =>
        {
            var result = await mediator.Send(new LookupTenantUserQuery());
            return result.ToApiResponse("Lookup data retrieved successfully");
        })
        .WithName("GetTenantUserLookup")
        .WithTags("TenantUsers");

        group.MapPost("/", async (CreateTenantUserRequest request, IMediator mediator) =>
        {
            var result = await mediator.Send(new CreateTenantUserCommand(request));
            return result.ToApiResponse("Tenant user has been created successfully", StatusCodes.Status201Created);
        })
        .WithName("CreateTenantUser")
        .WithTags("TenantUsers");

        group.MapPost("/update", async (UpdateTenantUserRequest request, IMediator mediator) =>
        {
            var result = await mediator.Send(new UpdateTenantUserCommand(request));
            if (!result.Success)
            {
                return ((object?)null).ToApiResponse("Update failed. The tenant user data could not be found.");
            }
            return result.ToApiResponse("Tenant user has been updated successfully");
        })
        .WithName("UpdateTenantUser")
        .WithTags("TenantUsers");

        group.MapPost("/delete/{id}", async (string id, IMediator mediator) =>
        {
            var result = await mediator.Send(new DeleteTenantUserByIdCommand(new DeleteTenantUserByIdRequest(id)));
            if (!result)
            {
                return ((object?)null).ToApiResponse("Delete failed. The tenant user data could not be found.");
            }
            return true.ToApiResponse("Tenant user has been deleted successfully");
        })
        .WithName("DeleteTenantUserById")
        .WithTags("TenantUsers");
    }
}