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

namespace Indotalent.Features.Utilities.ProgramManager;

public static class ProgramManagerEndpoint
{
    public static void MapProgramManagerEndpoints(this IEndpointRouteBuilder app)
    {
        var group = app.MapGroup("/program-manager").WithTags("ProgramManager")
            .RequireAuthorization(policy => policy.AddAuthenticationSchemes(JwtBearerDefaults.AuthenticationScheme)
            .RequireAuthenticatedUser());

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

        group.MapGet("/", async (IMediator mediator) =>
        {
            var result = await mediator.Send(new GetProgramManagerListQuery());
            return result.ToApiResponse("Program list retrieved successfully");
        })
        .WithName("GetProgramManagerList");

        group.MapGet("/{id}", async (string id, IMediator mediator) =>
        {
            var result = await mediator.Send(new GetProgramManagerByIdQuery(id));
            return result.ToApiResponse(result is not null
                ? "Program detail retrieved successfully"
                : "Program not found");
        })
        .WithName("GetProgramManagerById");

        group.MapPost("/", async (CreateProgramManagerRequest request, IMediator mediator) =>
        {
            var result = await mediator.Send(new CreateProgramManagerCommand(request));
            return result.ToApiResponse("Program created successfully", StatusCodes.Status201Created);
        })
        .WithName("CreateProgramManager");

        group.MapPost("/update", async (UpdateProgramManagerRequest request, IMediator mediator) =>
        {
            var result = await mediator.Send(new UpdateProgramManagerCommand(request));
            return result.ToApiResponse("Program updated successfully");
        })
        .WithName("UpdateProgramManager");

        group.MapPost("/delete/{id}", async (string id, IMediator mediator) =>
        {
            var result = await mediator.Send(new DeleteProgramManagerCommand(id));
            return result.ToApiResponse("Program deleted successfully");
        })
        .WithName("DeleteProgramManager");
    }
}