using Indotalent.ConfigBackEnd.Extensions;
using Indotalent.Features.Profile.PersonalInformation.Cqrs;
using MediatR;
using Microsoft.AspNetCore.Authentication.JwtBearer;

namespace Indotalent.Features.Profile.PersonalInformation;

public static class PersonalInformationEndpoint
{
    public static void MapPersonalInformationEndpoints(this IEndpointRouteBuilder app)
    {
        var group = app.MapGroup("/personal-information").WithTags("Personal Information")
            .RequireAuthorization(policy => policy.AddAuthenticationSchemes(JwtBearerDefaults.AuthenticationScheme)
            .RequireAuthenticatedUser());

        group.MapGet("/{id}", async (string id, IMediator mediator) =>
        {
            var result = await mediator.Send(new GetProfileByIdQuery(id));
            return result.ToApiResponse(result is not null
                ? "Personal information retrieved successfully"
                : $"User with ID {id} not found");
        })
        .WithName("GetPersonalInformationById");

        group.MapPost("/update", async (UpdateProfileRequest request, IMediator mediator) =>
        {
            var result = await mediator.Send(new UpdateProfileCommand(request));
            if (!result.Success)
                return ((object?)null).ToApiResponse(result.Message ?? "Update failed.", StatusCodes.Status400BadRequest);

            return result.ToApiResponse("Personal information has been updated successfully");
        })
        .WithName("UpdatePersonalInformation");
    }
}