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

namespace Indotalent.Features.Utilities.ProgramResource;

public static class ProgramResourceEndpoint
{
    public static void MapProgramResourceEndpoints(this IEndpointRouteBuilder app)
    {
        var group = app.MapGroup("/program-resource").WithTags("ProgramResource")
            .RequireAuthorization(policy => policy.AddAuthenticationSchemes(JwtBearerDefaults.AuthenticationScheme)
            .RequireAuthenticatedUser());

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

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

        group.MapPost("/", async (CreateProgramResourceRequest request, IMediator mediator) =>
        {
            var result = await mediator.Send(new CreateProgramResourceCommand(request));
            return result.ToApiResponse("Resource created successfully", StatusCodes.Status201Created);
        })
        .WithName("CreateProgramResource");

        group.MapPost("/update", async (UpdateProgramResourceRequest request, IMediator mediator) =>
        {
            var result = await mediator.Send(new UpdateProgramResourceCommand(request));
            return result.ToApiResponse("Resource updated successfully");
        })
        .WithName("UpdateProgramResource");

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