using Dapper;
using FastEndpoints;
using Modules.WMS.Picking.Shared;
using Npgsql;

namespace Modules.WMS.Picking.GetTask;

public sealed class Endpoint(NpgsqlDataSource dataSource)
    : EndpointWithoutRequest<PickingTaskDto>
{
    public override void Configure()
    {
        Get("/api/wms/picking/tasks/{taskId:guid}");
        Permissions("wms.picking.execute");
    }

    public override async Task HandleAsync(CancellationToken ct)
    {
        var taskId = Route<Guid>("taskId");
        await using var connection = await dataSource.OpenConnectionAsync(ct);
        var task = await PickingTaskQueries.GetAsync(connection, null, taskId, ct);
        if (task is null)
        {
            await SendNotFoundAsync(ct);
            return;
        }

        await SendOkAsync(task, ct);
    }
}
