using Indotalent.ConfigBackEnd.Extensions;
using Indotalent.Features.Utilities.Todo.Cqrs;
using MediatR;
using Microsoft.AspNetCore.Authentication.JwtBearer;

namespace Indotalent.Features.Utilities.Todo;

public static class TodoEndpoint
{
    public static void MapTodoEndpoints(this IEndpointRouteBuilder app)
    {
        var group = app.MapGroup("/todo").WithTags("Todos")
            .RequireAuthorization(policy => policy.AddAuthenticationSchemes(JwtBearerDefaults.AuthenticationScheme)
            .RequireAuthenticatedUser());

        group.MapGet("/", async (IMediator mediator) =>
        {
            var result = await mediator.Send(new GetTodoListQuery());
            return result.ToApiResponse("Todo list retrieved successfully");
        })
        .WithName("GetTodoList")
        .WithTags("Todos");

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

        group.MapPost("/", async (CreateTodoRequest request, IMediator mediator) =>
        {
            var result = await mediator.Send(new CreateTodoCommand(request));
            return result.ToApiResponse("Todo has been created successfully", StatusCodes.Status201Created);
        })
        .WithName("CreateTodo")
        .WithTags("Todos");

        group.MapPost("/update", async (UpdateTodoRequest request, IMediator mediator) =>
        {
            var result = await mediator.Send(new UpdateTodoCommand(request));
            if (!result.Success)
            {
                return ((object?)null).ToApiResponse("Update failed. The todo data could not be found.");
            }
            return result.ToApiResponse("Todo has been updated successfully");
        })
        .WithName("UpdateTodo")
        .WithTags("Todos");

        group.MapPost("/delete/{id}", async (string id, IMediator mediator) =>
        {
            var result = await mediator.Send(new DeleteTodoByIdCommand(new DeleteTodoByIdRequest(id)));
            if (!result)
            {
                return ((object?)null).ToApiResponse("Delete failed. The todo data could not be found.");
            }
            return true.ToApiResponse("Todo has been deleted successfully");
        })
        .WithName("DeleteTodoById")
        .WithTags("Todos");

        group.MapPost("/todo-item", async (CreateTodoItemRequest request, IMediator mediator) =>
        {
            var result = await mediator.Send(new CreateTodoItemCommand(request));
            return result.ToApiResponse("Todo item has been added successfully");
        })
        .WithName("CreateTodoItemChild")
        .WithTags("Todos");

        group.MapPost("/todo-item/update", async (UpdateTodoItemRequest request, IMediator mediator) =>
        {
            var result = await mediator.Send(new UpdateTodoItemCommand(request));
            if (!result.Success)
            {
                return ((object?)null).ToApiResponse("Update failed. Todo item not found.");
            }
            return result.ToApiResponse("Todo item has been updated successfully");
        })
        .WithName("UpdateTodoItemChild")
        .WithTags("Todos");

        group.MapPost("/todo-item/delete/{id}", async (string id, IMediator mediator) =>
        {
            var result = await mediator.Send(new DeleteTodoItemCommand(id));
            if (!result)
            {
                return ((object?)null).ToApiResponse("Delete failed. Todo item not found.");
            }
            return true.ToApiResponse("Todo item has been removed successfully");
        })
        .WithName("DeleteTodoItemChild")
        .WithTags("Todos");
    }
}