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

namespace Indotalent.Features.Multitenant.Tenant;

public static class TenantEndpoint
{
    public static void MapTenantEndpoints(this IEndpointRouteBuilder app)
    {
        var group = app.MapGroup("/tenant").WithTags("Tenants")
            .RequireAuthorization(policy => policy.AddAuthenticationSchemes(JwtBearerDefaults.AuthenticationScheme)
            .RequireAuthenticatedUser());

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

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

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

        group.MapPost("/", async (CreateTenantRequest request, IMediator mediator) =>
        {
            var result = await mediator.Send(new CreateTenantCommand(request));
            return result.ToApiResponse("Tenant has been created successfully", StatusCodes.Status201Created);
        })
        .WithName("CreateTenant")
        .WithTags("Tenants");

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

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

        group.MapPost("/tenant-user", async (CreateTenantUserRequest request, IMediator mediator) =>
        {
            var result = await mediator.Send(new CreateTenantUserCommand(request));
            return result.ToApiResponse("Tenant user has been added successfully");
        })
        .WithName("CreateTenantUserChild")
        .WithTags("Tenants");

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

        group.MapPost("/tenant-user/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. Tenant user not found.");
            }
            return true.ToApiResponse("Tenant user has been removed successfully");
        })
        .WithName("DeleteTenantUserChild")
        .WithTags("Tenants");
    }
}