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

namespace KArtSell.Modules.ModelOperations.Features.RejectModel;

public sealed class Endpoint(IDbConnectionFactory connectionFactory, IClock clock) : Endpoint<Request, Response>
{
    public override void Configure()
    {
        Post("/api/v1/approval-queue/{id}/reject");
        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 rejecterUserId = Guid.TryParse(userId, out var userIdGuid) ? userIdGuid : Guid.Empty;

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