using Dapper;
using Npgsql;

namespace Modules.WMS.Picking.Shared;

public static class PickingTransitions
{
    public static async Task AutoConfirmNextSameLocationAsync(
        NpgsqlConnection connection,
        NpgsqlTransaction transaction,
        Guid taskId,
        string completedLocation,
        CancellationToken ct)
    {
        await connection.ExecuteAsync(new CommandDefinition("""
            update wms.picking_lines
               set location_confirmed=true, updated_at=now()
             where id = (
                 select id
                   from wms.picking_lines
                  where task_id=@TaskId
                    and status <> 'COMPLETED'
                  order by line_no
                  limit 1
             )
               and location_code=@LocationCode;
            """, new { TaskId = taskId, LocationCode = completedLocation }, transaction, cancellationToken: ct));
    }
}
