using FastEndpoints;
using KArtSell.BuildingBlocks.Data;
using KArtSell.BuildingBlocks.Time;

namespace KArtSell.Modules.ModelOperations.Features.ApproveModel;

public sealed class Endpoint(IDbConnectionFactory connectionFactory, IClock clock) : Endpoint<Request, Response>
{
    public override void Configure()
    {
        Post("/api/v1/approval-queue/{id}/approve");
        Roles("Risk", "Compliance");
        Tags("ModelOperations");
    }

    public override async Task HandleAsync(Request req, CancellationToken ct)
    {
        var handler = new Handler(connectionFactory, clock);

        var userId = User.FindFirst("sub")?.Value ?? throw new UnauthorizedAccessException("User ID not found");
        var approverUserId = Guid.TryParse(userId, out var userIdGuid) ? userIdGuid : Guid.Empty;

        var response = await handler.HandleAsync(req, approverUserId, ct);
        await Send.OkAsync(response, ct);
    }
}
