qail-qdrant 1.1.1

QAIL driver for Qdrant vector database
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
//! Zero-copy Protobuf encoder for Qdrant gRPC protocol.
//!
//! This module implements direct wire format encoding without intermediate
//! struct allocations. Key optimizations:
//! - Pre-computed field tag bytes
//! - Buffer reuse via BytesMut
//! - Direct memcpy for vectors (no per-element loop)
//!
//! ## Supported Operations
//! - Search (with filters)
//! - Upsert (with payload)
//! - Delete points (numeric + UUID)
//! - Get points by ID
//! - Scroll (paginated iteration)
//! - Create / Delete collection
//! - Update payload
//! - Create field index

use crate::error::{QdrantError, QdrantResult};
use bytes::{BufMut, BytesMut};

// ============================================================================
// SearchPoints Field Tags (pre-computed)
// ============================================================================
// Tag = (field_number << 3) | wire_type

/// Field 1: collection_name (string) -> (1 << 3) | 2 = 0x0A
const SEARCH_COLLECTION: u8 = 0x0A;
/// Field 2: vector (repeated float, packed) -> (2 << 3) | 2 = 0x12
const SEARCH_VECTOR: u8 = 0x12;
/// Field 3: filter (message) -> (3 << 3) | 2 = 0x1A
const SEARCH_FILTER: u8 = 0x1A;
/// Field 4: limit (uint64) -> (4 << 3) | 0 = 0x20
const SEARCH_LIMIT: u8 = 0x20;
/// Field 6: with_payload (message) -> (6 << 3) | 2 = 0x32
const SEARCH_WITH_PAYLOAD: u8 = 0x32;
/// Field 8: score_threshold (float) -> (8 << 3) | 5 = 0x45
const SEARCH_SCORE_THRESHOLD: u8 = 0x45;
/// Field 10: vector_name (string) -> (10 << 3) | 2 = 0x52
const SEARCH_VECTOR_NAME: u8 = 0x52;
/// Field 11: with_vectors (message) -> (11 << 3) | 2 = 0x5A
const SEARCH_WITH_VECTORS: u8 = 0x5A;

// ============================================================================
// ScrollPoints Field Tags
// ============================================================================

/// Field 1: collection_name (string) -> 0x0A
const SCROLL_COLLECTION: u8 = 0x0A;
/// Field 2: filter (message) -> 0x12
const SCROLL_FILTER: u8 = 0x12;
/// Field 3: offset (PointId) -> 0x1A
const SCROLL_OFFSET: u8 = 0x1A;
/// Field 4: limit (uint32) -> 0x20
const SCROLL_LIMIT: u8 = 0x20;
/// Field 6: with_payload selector -> 0x32
const SCROLL_WITH_PAYLOAD: u8 = 0x32;
/// Field 7: with_vectors selector -> 0x3A
const SCROLL_WITH_VECTORS: u8 = 0x3A;

// ============================================================================
// UpsertPoints Field Tags
// ============================================================================

/// Field 1: collection_name (string) -> 0x0A
const UPSERT_COLLECTION: u8 = 0x0A;
/// Field 2: wait (bool) -> (2 << 3) | 0 = 0x10
const UPSERT_WAIT: u8 = 0x10;
/// Field 3: points (repeated PointStruct) -> (3 << 3) | 2 = 0x1A
const UPSERT_POINTS: u8 = 0x1A;

// ============================================================================
// PointStruct Field Tags
// ============================================================================

/// Field 1: id (PointId) -> 0x0A
const POINT_ID: u8 = 0x0A;
/// Field 4: vectors (Vectors) -> (4 << 3) | 2 = 0x22 (field 2 is deprecated)
const POINT_VECTORS: u8 = 0x22;
/// Field 3: payload (map) -> (3 << 3) | 2 = 0x1A
const POINT_PAYLOAD: u8 = 0x1A;

// ============================================================================
// PointId Field Tags
// ============================================================================

/// Field 1: num (uint64) -> (1 << 3) | 0 = 0x08
const POINT_ID_NUM: u8 = 0x08;
/// Field 2: uuid (string) -> (2 << 3) | 2 = 0x12
const POINT_ID_UUID: u8 = 0x12;

// ============================================================================
// Filter Field Tags (qdrant.Filter message)
// ============================================================================

/// Filter.must (field 1, repeated Condition) -> 0x0A
const FILTER_MUST: u8 = 0x0A;
/// Filter.should (field 2, repeated Condition) -> 0x12
const FILTER_SHOULD: u8 = 0x12;
/// Condition.filter (field 4, nested Filter message) -> 0x22
const CONDITION_FILTER: u8 = 0x22;
/// Condition.is_null (field 5, IsNullCondition message) -> 0x2A
const CONDITION_IS_NULL: u8 = 0x2A;

// ============================================================================
// Varint Encoding
// ============================================================================

/// Encode a varint (variable-length integer) into the buffer.
/// Uses 7 bits per byte, MSB indicates continuation.
#[inline]
pub fn encode_varint(buf: &mut BytesMut, mut value: usize) {
    loop {
        let byte = (value & 0x7F) as u8;
        value >>= 7;
        if value == 0 {
            buf.put_u8(byte);
            break;
        } else {
            buf.put_u8(byte | 0x80);
        }
    }
}

/// Encode a u64 varint.
#[inline]
pub fn encode_varint_u64(buf: &mut BytesMut, mut value: u64) {
    loop {
        let byte = (value & 0x7F) as u8;
        value >>= 7;
        if value == 0 {
            buf.put_u8(byte);
            break;
        } else {
            buf.put_u8(byte | 0x80);
        }
    }
}

// ============================================================================
// SearchPoints Encoder
// ============================================================================

/// Common search request fields shared by all search encoders.
#[derive(Clone, Copy)]
pub struct SearchRequest<'a> {
    pub collection: &'a str,
    pub vector: &'a [f32],
    pub limit: u64,
    pub score_threshold: Option<f32>,
    pub vector_name: Option<&'a str>,
    pub with_vectors: bool,
}

/// Encode a SearchPoints request directly to protobuf wire format.
///
/// # Arguments
/// * `buf` - Reusable buffer (cleared before writing)
/// * `collection` - Collection name
/// * `vector` - Query vector (directly memcpy'd)
/// * `limit` - Max results
/// * `score_threshold` - Optional minimum score
/// * `vector_name` - Optional named vector field
///
/// # Zero-Copy Optimization
/// The vector is written via direct memory copy, avoiding per-element encoding.
pub fn encode_search_proto(
    buf: &mut BytesMut,
    collection: &str,
    vector: &[f32],
    limit: u64,
    score_threshold: Option<f32>,
    vector_name: Option<&str>,
    with_vectors: bool,
) {
    buf.clear();

    // Field 1: collection_name (string)
    buf.put_u8(SEARCH_COLLECTION);
    encode_varint(buf, collection.len());
    buf.extend_from_slice(collection.as_bytes());

    // Field 2: vector (packed repeated float)
    // This is the key optimization - direct memcpy of float bytes!
    buf.put_u8(SEARCH_VECTOR);
    let vector_bytes_len = vector.len() * 4; // f32 = 4 bytes
    encode_varint(buf, vector_bytes_len);

    // ZERO-COPY: Write floats directly as bytes via bytemuck (safe, zero-cost)
    let float_bytes: &[u8] = bytemuck::cast_slice(vector);
    buf.extend_from_slice(float_bytes);

    // Field 4: limit (varint)
    buf.put_u8(SEARCH_LIMIT);
    encode_varint_u64(buf, limit);

    // Field 6: with_payload = true
    encode_with_payload_true(buf);

    // Field 8: score_threshold (float, optional)
    if let Some(threshold) = score_threshold {
        buf.put_u8(SEARCH_SCORE_THRESHOLD);
        buf.put_f32_le(threshold);
    }

    // Field 10: vector_name (string, optional)
    if let Some(name) = vector_name {
        buf.put_u8(SEARCH_VECTOR_NAME);
        encode_varint(buf, name.len());
        buf.extend_from_slice(name.as_bytes());
    }

    // Field 11: with_vectors = true (optional)
    if with_vectors {
        encode_search_with_vectors_true(buf);
    }
}

/// Encode a SearchPoints request with QAIL AST filter conditions.
///
/// This is the filtered search path — translates QAIL conditions into
/// Qdrant's protobuf Filter message (must/should arrays of Condition).
pub fn encode_search_with_filter_proto(
    buf: &mut BytesMut,
    request: SearchRequest<'_>,
    conditions: &[qail_core::ast::Condition],
    is_or: bool,
) -> QdrantResult<()> {
    let (must_conditions, should_conditions): (
        &[qail_core::ast::Condition],
        &[qail_core::ast::Condition],
    ) = if is_or {
        (&[], conditions)
    } else {
        (conditions, &[])
    };

    encode_search_with_filter_groups_proto(buf, request, must_conditions, should_conditions)
}

/// Encode a SearchPoints request with grouped filter conditions.
///
/// `must_conditions` are combined as AND, `should_conditions` as OR.
pub fn encode_search_with_filter_groups_proto(
    buf: &mut BytesMut,
    request: SearchRequest<'_>,
    must_conditions: &[qail_core::ast::Condition],
    should_conditions: &[qail_core::ast::Condition],
) -> QdrantResult<()> {
    buf.clear();

    // Field 1: collection_name
    buf.put_u8(SEARCH_COLLECTION);
    encode_varint(buf, request.collection.len());
    buf.extend_from_slice(request.collection.as_bytes());

    // Field 2: vector (packed floats, zero-copy)
    buf.put_u8(SEARCH_VECTOR);
    let vector_bytes_len = request.vector.len() * 4;
    encode_varint(buf, vector_bytes_len);
    let float_bytes: &[u8] = bytemuck::cast_slice(request.vector);
    buf.extend_from_slice(float_bytes);

    // Field 3: filter (Filter message)
    if !must_conditions.is_empty() || !should_conditions.is_empty() {
        let filter_buf = encode_filter_message_grouped(must_conditions, should_conditions)?;
        buf.put_u8(SEARCH_FILTER);
        encode_varint(buf, filter_buf.len());
        buf.extend_from_slice(&filter_buf);
    }

    // Field 4: limit
    buf.put_u8(SEARCH_LIMIT);
    encode_varint_u64(buf, request.limit);

    // Field 6: with_payload = true
    encode_with_payload_true(buf);

    // Field 8: score_threshold
    if let Some(threshold) = request.score_threshold {
        buf.put_u8(SEARCH_SCORE_THRESHOLD);
        buf.put_f32_le(threshold);
    }

    // Field 10: vector_name
    if let Some(name) = request.vector_name {
        buf.put_u8(SEARCH_VECTOR_NAME);
        encode_varint(buf, name.len());
        buf.extend_from_slice(name.as_bytes());
    }

    // Field 11: with_vectors = true (optional)
    if request.with_vectors {
        encode_search_with_vectors_true(buf);
    }

    Ok(())
}

/// Encode a SearchPoints request where OR conditions are preserved per-cage.
///
/// Every OR cage is encoded as a nested `Filter { should: [...] }` wrapped
/// in the outer filter's `must`, preserving:
/// `(A OR B) AND (C OR D)` instead of flattening to `A OR B OR C OR D`.
pub fn encode_search_with_filter_grouped_cages_proto(
    buf: &mut BytesMut,
    request: SearchRequest<'_>,
    must_conditions: &[qail_core::ast::Condition],
    should_groups: &[Vec<qail_core::ast::Condition>],
) -> QdrantResult<()> {
    buf.clear();

    // Field 1: collection_name
    buf.put_u8(SEARCH_COLLECTION);
    encode_varint(buf, request.collection.len());
    buf.extend_from_slice(request.collection.as_bytes());

    // Field 2: vector (packed floats, zero-copy)
    buf.put_u8(SEARCH_VECTOR);
    let vector_bytes_len = request.vector.len() * 4;
    encode_varint(buf, vector_bytes_len);
    let float_bytes: &[u8] = bytemuck::cast_slice(request.vector);
    buf.extend_from_slice(float_bytes);

    // Field 3: filter (Filter message)
    if !must_conditions.is_empty() || !should_groups.is_empty() {
        let filter_buf = encode_filter_message_grouped_cages(must_conditions, should_groups)?;
        buf.put_u8(SEARCH_FILTER);
        encode_varint(buf, filter_buf.len());
        buf.extend_from_slice(&filter_buf);
    }

    // Field 4: limit
    buf.put_u8(SEARCH_LIMIT);
    encode_varint_u64(buf, request.limit);

    // Field 6: with_payload = true
    encode_with_payload_true(buf);

    // Field 8: score_threshold
    if let Some(threshold) = request.score_threshold {
        buf.put_u8(SEARCH_SCORE_THRESHOLD);
        buf.put_f32_le(threshold);
    }

    // Field 10: vector_name
    if let Some(name) = request.vector_name {
        buf.put_u8(SEARCH_VECTOR_NAME);
        encode_varint(buf, name.len());
        buf.extend_from_slice(name.as_bytes());
    }

    // Field 11: with_vectors = true (optional)
    if request.with_vectors {
        encode_search_with_vectors_true(buf);
    }

    Ok(())
}

/// Encode with_payload = true as a sub-message.
pub fn encode_with_payload_true(buf: &mut BytesMut) {
    // WithPayloadSelector { enable = true }
    // Field 1: enable (bool) = 0x08, value = 1
    buf.put_u8(SEARCH_WITH_PAYLOAD);
    encode_varint(buf, 2); // submessage length
    buf.put_u8(0x08); // field 1, varint
    buf.put_u8(0x01); // true
}

/// Encode search with_vectors = true as a sub-message.
pub fn encode_search_with_vectors_true(buf: &mut BytesMut) {
    // WithVectorsSelector { enable = true }
    // Field 1: enable (bool) = 0x08, value = 1
    buf.put_u8(SEARCH_WITH_VECTORS);
    encode_varint(buf, 2); // submessage length
    buf.put_u8(0x08); // field 1, varint
    buf.put_u8(0x01); // true
}

// ============================================================================
// Filter Encoder (QAIL Conditions → Qdrant Protobuf Filter)
// ============================================================================

/// Encode a Filter message from QAIL AST conditions.
///
/// Qdrant's Filter proto:
/// ```text
/// message Filter {
///   repeated Condition must = 1;
///   repeated Condition should = 2;
///   repeated Condition must_not = 3;
/// }
/// ```
///
/// Each Condition wraps a FieldCondition:
/// ```text
/// message Condition {
///   oneof condition_one_of {
///     FieldCondition field = 1;
///     IsEmptyCondition is_empty = 2;
///     HasIdCondition has_id = 3;
///     IsNullCondition is_null = 5;
///     Filter filter = 4;     // nested filters
///   }
/// }
/// ```
fn encode_filter_message_grouped(
    must_conditions: &[qail_core::ast::Condition],
    should_conditions: &[qail_core::ast::Condition],
) -> QdrantResult<BytesMut> {
    let mut filter_buf =
        BytesMut::with_capacity((must_conditions.len() + should_conditions.len()) * 32);

    let mut encode_clause =
        |conditions: &[qail_core::ast::Condition], clause_tag: u8| -> QdrantResult<()> {
            for cond in conditions {
                let cond_buf = encode_condition_message(cond)?;

                // Write as repeated Condition in the filter's must/should field
                filter_buf.put_u8(clause_tag);
                encode_varint(&mut filter_buf, cond_buf.len());
                filter_buf.extend_from_slice(&cond_buf);
            }
            Ok(())
        };

    encode_clause(must_conditions, FILTER_MUST)?;
    encode_clause(should_conditions, FILTER_SHOULD)?;

    Ok(filter_buf)
}

/// Encode a grouped filter preserving each OR cage as its own nested should-group.
fn encode_filter_message_grouped_cages(
    must_conditions: &[qail_core::ast::Condition],
    should_groups: &[Vec<qail_core::ast::Condition>],
) -> QdrantResult<BytesMut> {
    let grouped_condition_count: usize = should_groups.iter().map(Vec::len).sum();
    let mut filter_buf =
        BytesMut::with_capacity((must_conditions.len() + grouped_condition_count) * 32);

    for cond in must_conditions {
        let cond_buf = encode_condition_message(cond)?;
        filter_buf.put_u8(FILTER_MUST);
        encode_varint(&mut filter_buf, cond_buf.len());
        filter_buf.extend_from_slice(&cond_buf);
    }

    for group in should_groups {
        if group.is_empty() {
            continue;
        }

        if group.len() == 1 {
            let cond_buf = encode_condition_message(&group[0])?;
            filter_buf.put_u8(FILTER_MUST);
            encode_varint(&mut filter_buf, cond_buf.len());
            filter_buf.extend_from_slice(&cond_buf);
            continue;
        }

        let nested_filter = encode_filter_message_grouped(&[], group)?;
        let mut nested_condition = BytesMut::with_capacity(nested_filter.len() + 4);
        nested_condition.put_u8(CONDITION_FILTER);
        encode_varint(&mut nested_condition, nested_filter.len());
        nested_condition.extend_from_slice(&nested_filter);

        filter_buf.put_u8(FILTER_MUST);
        encode_varint(&mut filter_buf, nested_condition.len());
        filter_buf.extend_from_slice(&nested_condition);
    }

    Ok(filter_buf)
}

/// Encode a single `Condition` message for Qdrant filters.
fn encode_condition_message(cond: &qail_core::ast::Condition) -> QdrantResult<BytesMut> {
    use qail_core::ast::{Expr, Operator, Value};

    let key = match &cond.left {
        Expr::Named(name) => name.as_str(),
        Expr::Aliased { name, .. } => name.as_str(),
        other => {
            return Err(QdrantError::Encode(format!(
                "Unsupported filter left expression for Qdrant: {:?}",
                other
            )));
        }
    };

    match (&cond.op, &cond.value) {
        // Match (equality) conditions
        (Operator::Eq, Value::String(s)) => Ok(encode_field_condition_match_keyword(key, s)),
        (Operator::Eq, Value::Int(n)) => Ok(encode_field_condition_match_integer(key, *n)),
        (Operator::Eq, Value::Float(f)) => Ok(encode_field_condition_match_float(key, *f)),
        (Operator::Eq, Value::Bool(b)) => Ok(encode_field_condition_match_bool(key, *b)),

        // Range conditions
        (Operator::Gt, Value::Int(n)) => Ok(encode_field_condition_range(
            key,
            None,
            None,
            Some(*n as f64),
            None,
        )),
        (Operator::Gt, Value::Float(f)) => Ok(encode_field_condition_range(
            key,
            None,
            None,
            Some(*f),
            None,
        )),
        (Operator::Gte, Value::Int(n)) => Ok(encode_field_condition_range(
            key,
            None,
            None,
            None,
            Some(*n as f64),
        )),
        (Operator::Gte, Value::Float(f)) => Ok(encode_field_condition_range(
            key,
            None,
            None,
            None,
            Some(*f),
        )),
        (Operator::Lt, Value::Int(n)) => Ok(encode_field_condition_range(
            key,
            Some(*n as f64),
            None,
            None,
            None,
        )),
        (Operator::Lt, Value::Float(f)) => Ok(encode_field_condition_range(
            key,
            Some(*f),
            None,
            None,
            None,
        )),
        (Operator::Lte, Value::Int(n)) => Ok(encode_field_condition_range(
            key,
            None,
            Some(*n as f64),
            None,
            None,
        )),
        (Operator::Lte, Value::Float(f)) => Ok(encode_field_condition_range(
            key,
            None,
            Some(*f),
            None,
            None,
        )),

        // Text match (contains / like)
        (Operator::Contains | Operator::Like, Value::String(s)) => {
            Ok(encode_field_condition_match_text(key, s))
        }

        (Operator::IsNull, Value::Null) => Ok(encode_is_null_condition(key)),

        _ => Err(QdrantError::Encode(format!(
            "Unsupported Qdrant filter condition: op={:?}, value={:?}",
            cond.op, cond.value
        ))),
    }
}

/// Encode a Condition { is_null: IsNullCondition { key } }.
fn encode_is_null_condition(key: &str) -> BytesMut {
    let mut is_null_buf = BytesMut::with_capacity(key.len() + 8);
    is_null_buf.put_u8(0x0A); // IsNullCondition.key, field 1, LEN
    encode_varint(&mut is_null_buf, key.len());
    is_null_buf.extend_from_slice(key.as_bytes());

    let mut cond_buf = BytesMut::with_capacity(is_null_buf.len() + 8);
    cond_buf.put_u8(CONDITION_IS_NULL);
    encode_varint(&mut cond_buf, is_null_buf.len());
    cond_buf.extend_from_slice(&is_null_buf);
    cond_buf
}

/// Encode a FieldCondition with Match { keyword } for string equality.
///
/// ```text
/// Condition {
///   field = FieldCondition {
///     key = "field_name",
///     match = Match { keyword = "value" }
///   }
/// }
/// ```
fn encode_field_condition_match_keyword(key: &str, value: &str) -> BytesMut {
    // Match message: field 1 = keyword (string)
    let mut match_buf = BytesMut::with_capacity(value.len() + 8);
    match_buf.put_u8(0x0A); // field 1 (keyword), wire LEN
    encode_varint(&mut match_buf, value.len());
    match_buf.extend_from_slice(value.as_bytes());

    // FieldCondition message
    let mut fc_buf = BytesMut::with_capacity(key.len() + match_buf.len() + 16);
    // field 1: key (string)
    fc_buf.put_u8(0x0A);
    encode_varint(&mut fc_buf, key.len());
    fc_buf.extend_from_slice(key.as_bytes());
    // field 2: match (Match message)
    fc_buf.put_u8(0x12);
    encode_varint(&mut fc_buf, match_buf.len());
    fc_buf.extend_from_slice(&match_buf);

    // Condition: field 1 = FieldCondition
    let mut cond_buf = BytesMut::with_capacity(fc_buf.len() + 4);
    cond_buf.put_u8(0x0A); // field 1
    encode_varint(&mut cond_buf, fc_buf.len());
    cond_buf.extend_from_slice(&fc_buf);

    cond_buf
}

/// Encode a FieldCondition with Match { integer } for int equality.
fn encode_field_condition_match_integer(key: &str, value: i64) -> BytesMut {
    // Match message: field 2 = integer (int64)
    let mut match_buf = BytesMut::with_capacity(16);
    match_buf.put_u8(0x10); // field 2 (integer), wire VARINT
    encode_varint_u64(&mut match_buf, value as u64);

    let mut fc_buf = BytesMut::with_capacity(key.len() + match_buf.len() + 16);
    fc_buf.put_u8(0x0A);
    encode_varint(&mut fc_buf, key.len());
    fc_buf.extend_from_slice(key.as_bytes());
    fc_buf.put_u8(0x12);
    encode_varint(&mut fc_buf, match_buf.len());
    fc_buf.extend_from_slice(&match_buf);

    let mut cond_buf = BytesMut::with_capacity(fc_buf.len() + 4);
    cond_buf.put_u8(0x0A);
    encode_varint(&mut cond_buf, fc_buf.len());
    cond_buf.extend_from_slice(&fc_buf);

    cond_buf
}

/// Encode a FieldCondition with Match { integer } using float payload.
fn encode_field_condition_match_float(key: &str, value: f64) -> BytesMut {
    // Match message: field 5 = double (double)
    let mut match_buf = BytesMut::with_capacity(10);
    match_buf.put_u8(0x29); // field 5 (double), wire FIXED64
    match_buf.put_f64_le(value);

    let mut fc_buf = BytesMut::with_capacity(key.len() + match_buf.len() + 16);
    fc_buf.put_u8(0x0A);
    encode_varint(&mut fc_buf, key.len());
    fc_buf.extend_from_slice(key.as_bytes());
    fc_buf.put_u8(0x12);
    encode_varint(&mut fc_buf, match_buf.len());
    fc_buf.extend_from_slice(&match_buf);

    let mut cond_buf = BytesMut::with_capacity(fc_buf.len() + 4);
    cond_buf.put_u8(0x0A);
    encode_varint(&mut cond_buf, fc_buf.len());
    cond_buf.extend_from_slice(&fc_buf);

    cond_buf
}

/// Encode a FieldCondition with Match { boolean }.
fn encode_field_condition_match_bool(key: &str, value: bool) -> BytesMut {
    // Match message: field 4 = boolean (bool)
    let mut match_buf = BytesMut::with_capacity(4);
    match_buf.put_u8(0x20); // field 4 (boolean), wire VARINT
    match_buf.put_u8(if value { 1 } else { 0 });

    let mut fc_buf = BytesMut::with_capacity(key.len() + match_buf.len() + 16);
    fc_buf.put_u8(0x0A);
    encode_varint(&mut fc_buf, key.len());
    fc_buf.extend_from_slice(key.as_bytes());
    fc_buf.put_u8(0x12);
    encode_varint(&mut fc_buf, match_buf.len());
    fc_buf.extend_from_slice(&match_buf);

    let mut cond_buf = BytesMut::with_capacity(fc_buf.len() + 4);
    cond_buf.put_u8(0x0A);
    encode_varint(&mut cond_buf, fc_buf.len());
    cond_buf.extend_from_slice(&fc_buf);

    cond_buf
}

/// Encode a FieldCondition with Match { text } for full-text search.
fn encode_field_condition_match_text(key: &str, value: &str) -> BytesMut {
    // Match message: field 3 = text (string)
    let mut match_buf = BytesMut::with_capacity(value.len() + 8);
    match_buf.put_u8(0x1A); // field 3 (text), wire LEN
    encode_varint(&mut match_buf, value.len());
    match_buf.extend_from_slice(value.as_bytes());

    let mut fc_buf = BytesMut::with_capacity(key.len() + match_buf.len() + 16);
    fc_buf.put_u8(0x0A);
    encode_varint(&mut fc_buf, key.len());
    fc_buf.extend_from_slice(key.as_bytes());
    fc_buf.put_u8(0x12);
    encode_varint(&mut fc_buf, match_buf.len());
    fc_buf.extend_from_slice(&match_buf);

    let mut cond_buf = BytesMut::with_capacity(fc_buf.len() + 4);
    cond_buf.put_u8(0x0A);
    encode_varint(&mut cond_buf, fc_buf.len());
    cond_buf.extend_from_slice(&fc_buf);

    cond_buf
}

/// Encode a FieldCondition with Range.
///
/// Range proto: { lt, gt, gte, lte } — all optional f64.
fn encode_field_condition_range(
    key: &str,
    lt: Option<f64>,
    lte: Option<f64>,
    gt: Option<f64>,
    gte: Option<f64>,
) -> BytesMut {
    // Range message fields (all double / f64)
    // field 1: lt  -> 0x09 (field 1, wire FIXED64)
    // field 2: gt  -> 0x11 (field 2, wire FIXED64)
    // field 3: gte -> 0x19 (field 3, wire FIXED64)
    // field 4: lte -> 0x21 (field 4, wire FIXED64)
    let mut range_buf = BytesMut::with_capacity(40);
    if let Some(v) = lt {
        range_buf.put_u8(0x09); // field 1, wire 1 (64-bit)
        range_buf.put_f64_le(v);
    }
    if let Some(v) = gt {
        range_buf.put_u8(0x11); // field 2, wire 1
        range_buf.put_f64_le(v);
    }
    if let Some(v) = gte {
        range_buf.put_u8(0x19); // field 3, wire 1
        range_buf.put_f64_le(v);
    }
    if let Some(v) = lte {
        range_buf.put_u8(0x21); // field 4, wire 1
        range_buf.put_f64_le(v);
    }

    // FieldCondition: key + range
    let mut fc_buf = BytesMut::with_capacity(key.len() + range_buf.len() + 16);
    fc_buf.put_u8(0x0A); // field 1: key
    encode_varint(&mut fc_buf, key.len());
    fc_buf.extend_from_slice(key.as_bytes());
    fc_buf.put_u8(0x1A); // field 3: range (Range message)
    encode_varint(&mut fc_buf, range_buf.len());
    fc_buf.extend_from_slice(&range_buf);

    // Condition: field 1 = FieldCondition
    let mut cond_buf = BytesMut::with_capacity(fc_buf.len() + 4);
    cond_buf.put_u8(0x0A);
    encode_varint(&mut cond_buf, fc_buf.len());
    cond_buf.extend_from_slice(&fc_buf);

    cond_buf
}

// ============================================================================
// UpsertPoints Encoder
// ============================================================================

/// Encode an UpsertPoints request to protobuf wire format.
pub fn encode_upsert_proto(
    buf: &mut BytesMut,
    collection: &str,
    points: &[crate::Point],
    wait: bool,
) {
    buf.clear();

    // Field 1: collection_name
    buf.put_u8(UPSERT_COLLECTION);
    encode_varint(buf, collection.len());
    buf.extend_from_slice(collection.as_bytes());

    // Field 2: wait (bool)
    if wait {
        buf.put_u8(UPSERT_WAIT);
        buf.put_u8(0x01);
    }

    // Field 3: points (repeated PointStruct)
    for point in points {
        encode_point_struct(buf, point);
    }
}

/// Encode a single PointStruct (with payload support).
fn encode_point_struct(buf: &mut BytesMut, point: &crate::Point) {
    // We need to encode into a temp buffer first to get length,
    // since PointStruct is length-delimited
    let mut point_buf = BytesMut::with_capacity(point.vector.len() * 4 + 64);

    // Field 1: id (PointId oneof)
    encode_point_id_field(&mut point_buf, &point.id);

    // Field 3: payload (map<string, Value>)
    if !point.payload.is_empty() {
        encode_payload_map(&mut point_buf, &point.payload);
    }

    // Field 4: vectors (Vectors -> Vector)
    let vector_bytes_len = point.vector.len() * 4;
    let vector_inner_len = 1 + varint_len(vector_bytes_len as u64) + vector_bytes_len;
    let vectors_len = 1 + varint_len(vector_inner_len as u64) + vector_inner_len;

    point_buf.put_u8(POINT_VECTORS);
    encode_varint(&mut point_buf, vectors_len);
    point_buf.put_u8(0x0A); // Vectors.vector (field 1)
    encode_varint(&mut point_buf, vector_inner_len);
    point_buf.put_u8(0x0A); // Vector.data (field 1, packed floats)
    encode_varint(&mut point_buf, vector_bytes_len);
    let float_bytes: &[u8] = bytemuck::cast_slice(&point.vector);
    point_buf.extend_from_slice(float_bytes);

    // Write to main buffer with length prefix
    buf.put_u8(UPSERT_POINTS);
    encode_varint(buf, point_buf.len());
    buf.extend_from_slice(&point_buf);
}

/// Encode a PointId into a buffer as field 1 of PointStruct.
fn encode_point_id_field(buf: &mut BytesMut, id: &crate::PointId) {
    match id {
        crate::PointId::Num(n) => {
            buf.put_u8(POINT_ID);
            let id_len = 1 + varint_len(*n);
            encode_varint(buf, id_len);
            buf.put_u8(POINT_ID_NUM);
            encode_varint_u64(buf, *n);
        }
        crate::PointId::Uuid(s) => {
            buf.put_u8(POINT_ID);
            let id_len = 1 + varint_len(s.len() as u64) + s.len();
            encode_varint(buf, id_len);
            buf.put_u8(POINT_ID_UUID);
            encode_varint(buf, s.len());
            buf.extend_from_slice(s.as_bytes());
        }
    }
}

/// Encode a payload map as protobuf map<string, Value>.
///
/// Protobuf maps are encoded as repeated field with key-value pair messages.
fn encode_payload_map(buf: &mut BytesMut, payload: &crate::point::Payload) {
    for (key, value) in payload {
        let mut entry_buf = BytesMut::with_capacity(key.len() + 32);

        // Map entry field 1: key (string)
        entry_buf.put_u8(0x0A);
        encode_varint(&mut entry_buf, key.len());
        entry_buf.extend_from_slice(key.as_bytes());

        // Map entry field 2: value (Value message)
        let value_buf = encode_payload_value(value);
        entry_buf.put_u8(0x12);
        encode_varint(&mut entry_buf, value_buf.len());
        entry_buf.extend_from_slice(&value_buf);

        // Write map entry as field 3 of PointStruct (payload)
        buf.put_u8(POINT_PAYLOAD);
        encode_varint(buf, entry_buf.len());
        buf.extend_from_slice(&entry_buf);
    }
}

/// Encode a single PayloadValue to protobuf Value message.
///
/// ```text
/// message Value {
///   oneof kind {
///     NullValue null_value = 1;
///     double double_value = 2;
///     int64 integer_value = 3;
///     string string_value = 4;
///     bool bool_value = 5;
///     Struct struct_value = 6;
///     ListValue list_value = 7;
///   }
/// }
/// ```
fn encode_payload_value(value: &crate::point::PayloadValue) -> BytesMut {
    use crate::point::PayloadValue;
    let mut buf = BytesMut::with_capacity(32);

    match value {
        PayloadValue::Null => {
            // field 1: null_value (enum, always 0)
            buf.put_u8(0x08);
            buf.put_u8(0x00);
        }
        PayloadValue::Float(f) => {
            // field 2: double_value (double, wire type 1 = fixed64)
            buf.put_u8(0x11); // (2 << 3) | 1 = 0x11
            buf.put_f64_le(*f);
        }
        PayloadValue::Integer(n) => {
            // field 3: integer_value (int64, wire type 0 = varint)
            buf.put_u8(0x18); // (3 << 3) | 0 = 0x18
            encode_varint_u64(&mut buf, *n as u64);
        }
        PayloadValue::String(s) => {
            // field 4: string_value (string, wire type 2 = len-delimited)
            buf.put_u8(0x22); // (4 << 3) | 2 = 0x22
            encode_varint(&mut buf, s.len());
            buf.extend_from_slice(s.as_bytes());
        }
        PayloadValue::Bool(b) => {
            // field 5: bool_value (bool, wire type 0 = varint)
            buf.put_u8(0x28); // (5 << 3) | 0 = 0x28
            buf.put_u8(if *b { 1 } else { 0 });
        }
        PayloadValue::List(items) => {
            // field 7: list_value (ListValue message)
            let mut list_buf = BytesMut::with_capacity(items.len() * 16);
            for item in items {
                let val_buf = encode_payload_value(item);
                // ListValue.values (field 1, repeated Value)
                list_buf.put_u8(0x0A);
                encode_varint(&mut list_buf, val_buf.len());
                list_buf.extend_from_slice(&val_buf);
            }
            buf.put_u8(0x3A); // (7 << 3) | 2 = 0x3A
            encode_varint(&mut buf, list_buf.len());
            buf.extend_from_slice(&list_buf);
        }
        PayloadValue::Object(map) => {
            // field 6: struct_value (Struct message)
            // Struct.fields is map<string, Value> → repeated MapEntry
            let mut struct_buf = BytesMut::with_capacity(map.len() * 32);
            for (k, v) in map {
                let val_buf = encode_payload_value(v);
                let mut entry_buf = BytesMut::with_capacity(k.len() + val_buf.len() + 8);
                // key (field 1)
                entry_buf.put_u8(0x0A);
                encode_varint(&mut entry_buf, k.len());
                entry_buf.extend_from_slice(k.as_bytes());
                // value (field 2)
                entry_buf.put_u8(0x12);
                encode_varint(&mut entry_buf, val_buf.len());
                entry_buf.extend_from_slice(&val_buf);
                // Struct.fields (field 1, repeated)
                struct_buf.put_u8(0x0A);
                encode_varint(&mut struct_buf, entry_buf.len());
                struct_buf.extend_from_slice(&entry_buf);
            }
            buf.put_u8(0x32); // (6 << 3) | 2 = 0x32
            encode_varint(&mut buf, struct_buf.len());
            buf.extend_from_slice(&struct_buf);
        }
    }

    buf
}

// ============================================================================
// GetPoints Encoder
// ============================================================================

/// Encode a GetPoints request to protobuf wire format.
///
/// ```text
/// message GetPoints {
///   string collection_name = 1;
///   repeated PointId ids = 2;
///   WithPayloadSelector with_payload = 4;
///   WithVectorsSelector with_vectors = 5;
/// }
/// ```
pub fn encode_get_points_proto(
    buf: &mut BytesMut,
    collection: &str,
    ids: &[crate::PointId],
    with_vectors: bool,
) {
    buf.clear();

    // Field 1: collection_name
    buf.put_u8(0x0A);
    encode_varint(buf, collection.len());
    buf.extend_from_slice(collection.as_bytes());

    // Field 2: ids (repeated PointId)
    for id in ids {
        let mut id_buf = BytesMut::with_capacity(40);
        match id {
            crate::PointId::Num(n) => {
                id_buf.put_u8(POINT_ID_NUM);
                encode_varint_u64(&mut id_buf, *n);
            }
            crate::PointId::Uuid(s) => {
                id_buf.put_u8(POINT_ID_UUID);
                encode_varint(&mut id_buf, s.len());
                id_buf.extend_from_slice(s.as_bytes());
            }
        }
        buf.put_u8(0x12); // field 2, wire LEN
        encode_varint(buf, id_buf.len());
        buf.extend_from_slice(&id_buf);
    }

    // Field 4: with_payload = true
    buf.put_u8(0x22); // (4 << 3) | 2 = 0x22
    encode_varint(buf, 2);
    buf.put_u8(0x08); // enable = true
    buf.put_u8(0x01);

    // Field 5: with_vectors
    if with_vectors {
        buf.put_u8(0x2A); // (5 << 3) | 2 = 0x2A
        encode_varint(buf, 2);
        buf.put_u8(0x08); // enable = true
        buf.put_u8(0x01);
    }
}

// ============================================================================
// ScrollPoints Encoder
// ============================================================================

/// Encode a ScrollPoints request to protobuf wire format.
///
/// ```text
/// message ScrollPoints {
///   string collection_name = 1;
///   Filter filter = 2;
///   optional PointId offset = 3;
///   uint32 limit = 4;
///   WithPayloadSelector with_payload = 6;
///   WithVectorsSelector with_vectors = 7;
/// }
/// ```
pub fn encode_scroll_points_proto(
    buf: &mut BytesMut,
    collection: &str,
    limit: u32,
    offset: Option<&crate::PointId>,
    with_vectors: bool,
) {
    buf.clear();

    // Field 1: collection_name
    buf.put_u8(SCROLL_COLLECTION);
    encode_varint(buf, collection.len());
    buf.extend_from_slice(collection.as_bytes());

    // Field 3: offset (optional PointId)
    if let Some(id) = offset {
        let mut id_buf = BytesMut::with_capacity(40);
        match id {
            crate::PointId::Num(n) => {
                id_buf.put_u8(POINT_ID_NUM);
                encode_varint_u64(&mut id_buf, *n);
            }
            crate::PointId::Uuid(s) => {
                id_buf.put_u8(POINT_ID_UUID);
                encode_varint(&mut id_buf, s.len());
                id_buf.extend_from_slice(s.as_bytes());
            }
        }
        buf.put_u8(SCROLL_OFFSET);
        encode_varint(buf, id_buf.len());
        buf.extend_from_slice(&id_buf);
    }

    // Field 4: limit (uint32)
    buf.put_u8(SCROLL_LIMIT);
    encode_varint(buf, limit as usize);

    // Field 6: with_payload = true
    buf.put_u8(SCROLL_WITH_PAYLOAD);
    encode_varint(buf, 2);
    buf.put_u8(0x08);
    buf.put_u8(0x01);

    // Field 7: with_vectors
    if with_vectors {
        buf.put_u8(SCROLL_WITH_VECTORS);
        encode_varint(buf, 2);
        buf.put_u8(0x08);
        buf.put_u8(0x01);
    }
}

/// Encode a filtered ScrollPoints request.
pub fn encode_scroll_points_with_filter_grouped_cages_proto(
    buf: &mut BytesMut,
    collection: &str,
    limit: u32,
    offset: Option<&crate::PointId>,
    with_vectors: bool,
    must_conditions: &[qail_core::ast::Condition],
    should_groups: &[Vec<qail_core::ast::Condition>],
) -> QdrantResult<()> {
    buf.clear();

    // Field 1: collection_name
    buf.put_u8(SCROLL_COLLECTION);
    encode_varint(buf, collection.len());
    buf.extend_from_slice(collection.as_bytes());

    // Field 2: filter
    if !must_conditions.is_empty() || !should_groups.is_empty() {
        let filter_buf = encode_filter_message_grouped_cages(must_conditions, should_groups)?;
        buf.put_u8(SCROLL_FILTER);
        encode_varint(buf, filter_buf.len());
        buf.extend_from_slice(&filter_buf);
    }

    // Field 3: offset (optional PointId)
    if let Some(id) = offset {
        let mut id_buf = BytesMut::with_capacity(40);
        match id {
            crate::PointId::Num(n) => {
                id_buf.put_u8(POINT_ID_NUM);
                encode_varint_u64(&mut id_buf, *n);
            }
            crate::PointId::Uuid(s) => {
                id_buf.put_u8(POINT_ID_UUID);
                encode_varint(&mut id_buf, s.len());
                id_buf.extend_from_slice(s.as_bytes());
            }
        }
        buf.put_u8(SCROLL_OFFSET);
        encode_varint(buf, id_buf.len());
        buf.extend_from_slice(&id_buf);
    }

    // Field 4: limit (uint32)
    buf.put_u8(SCROLL_LIMIT);
    encode_varint(buf, limit as usize);

    // Field 6: with_payload = true
    buf.put_u8(SCROLL_WITH_PAYLOAD);
    encode_varint(buf, 2);
    buf.put_u8(0x08);
    buf.put_u8(0x01);

    // Field 7: with_vectors
    if with_vectors {
        buf.put_u8(SCROLL_WITH_VECTORS);
        encode_varint(buf, 2);
        buf.put_u8(0x08);
        buf.put_u8(0x01);
    }

    Ok(())
}

// ============================================================================
// UpdatePayload Encoder
// ============================================================================

/// Encode a SetPayload request to protobuf wire format.
///
/// ```text
/// message SetPayloadPoints {
///   string collection_name = 1;
///   bool wait = 2;
///   map<string, Value> payload = 3;
///   PointsSelector points_selector = 5;
/// }
/// ```
pub fn encode_set_payload_proto(
    buf: &mut BytesMut,
    collection: &str,
    point_ids: &[crate::PointId],
    payload: &crate::point::Payload,
    wait: bool,
) {
    buf.clear();

    // Field 1: collection_name
    buf.put_u8(0x0A);
    encode_varint(buf, collection.len());
    buf.extend_from_slice(collection.as_bytes());

    // Field 2: wait
    if wait {
        buf.put_u8(0x10);
        buf.put_u8(0x01);
    }

    // Field 3: payload (map<string, Value>)
    for (key, value) in payload {
        let mut entry_buf = BytesMut::with_capacity(key.len() + 32);
        entry_buf.put_u8(0x0A); // key (field 1)
        encode_varint(&mut entry_buf, key.len());
        entry_buf.extend_from_slice(key.as_bytes());

        let val_buf = encode_payload_value(value);
        entry_buf.put_u8(0x12); // value (field 2)
        encode_varint(&mut entry_buf, val_buf.len());
        entry_buf.extend_from_slice(&val_buf);

        buf.put_u8(0x1A); // field 3 (payload map entry)
        encode_varint(buf, entry_buf.len());
        buf.extend_from_slice(&entry_buf);
    }

    // Field 5: points_selector -> PointsIdsList
    let selector_buf = encode_points_selector(point_ids);
    buf.put_u8(0x2A); // (5 << 3) | 2 = 0x2A
    encode_varint(buf, selector_buf.len());
    buf.extend_from_slice(&selector_buf);
}

// ============================================================================
// CreateFieldIndex Encoder
// ============================================================================

/// Encode a CreateFieldIndexCollection request.
///
/// ```text
/// message CreateFieldIndexCollection {
///   string collection_name = 1;
///   bool wait = 2;
///   string field_name = 3;
///   optional FieldType field_type = 4;
/// }
/// ```
///
/// FieldType enum: 0=Keyword, 1=Integer, 2=Float, 3=Geo, 4=Text, 5=Bool, 6=Datetime
pub fn encode_create_field_index_proto(
    buf: &mut BytesMut,
    collection: &str,
    field_name: &str,
    field_type: FieldType,
    wait: bool,
) {
    buf.clear();

    // Field 1: collection_name
    buf.put_u8(0x0A);
    encode_varint(buf, collection.len());
    buf.extend_from_slice(collection.as_bytes());

    // Field 2: wait
    if wait {
        buf.put_u8(0x10);
        buf.put_u8(0x01);
    }

    // Field 3: field_name
    buf.put_u8(0x1A);
    encode_varint(buf, field_name.len());
    buf.extend_from_slice(field_name.as_bytes());

    // Field 4: field_type (optional enum)
    buf.put_u8(0x20); // (4 << 3) | 0 = 0x20
    encode_varint(buf, field_type as usize);
}

/// Qdrant payload index field types.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(u8)]
pub enum FieldType {
    /// Keyword (exact-match string) index.
    Keyword = 0,
    /// Integer index.
    Integer = 1,
    /// Float index.
    Float = 2,
    /// Geolocation index.
    Geo = 3,
    /// Full-text search index.
    Text = 4,
    /// Boolean index.
    Bool = 5,
    /// Datetime index.
    Datetime = 6,
}

// ============================================================================
// CreateCollection Field Tags
// ============================================================================

/// Field 1: collection_name (string) -> 0x0A
const CREATE_COLLECTION_NAME: u8 = 0x0A;
/// Field 10: vectors_config (VectorsConfig) -> (10 << 3) | 2 = 0x52
const CREATE_VECTORS_CONFIG: u8 = 0x52;
/// Field 8: on_disk_payload (bool) -> (8 << 3) | 0 = 0x40
const CREATE_ON_DISK: u8 = 0x40;

// ============================================================================
// DeleteCollection Field Tags
// ============================================================================

/// Field 1: collection_name (string) -> 0x0A
const DELETE_COLLECTION_NAME: u8 = 0x0A;

/// Encode CreateCollection request to protobuf wire format.
pub fn encode_create_collection_proto(
    buf: &mut BytesMut,
    collection_name: &str,
    vector_size: u64,
    distance: crate::Distance,
    on_disk: bool,
) {
    buf.clear();

    // Field 1: collection_name
    buf.put_u8(CREATE_COLLECTION_NAME);
    encode_varint(buf, collection_name.len());
    buf.extend_from_slice(collection_name.as_bytes());

    // Field 2: vectors_config (VectorsConfig -> VectorParams)
    let mut params_buf = BytesMut::with_capacity(32);

    // VectorParams.size (field 1, uint64)
    params_buf.put_u8(0x08);
    encode_varint_u64(&mut params_buf, vector_size);

    // VectorParams.distance (field 2, enum)
    params_buf.put_u8(0x10);
    let distance_val = match distance {
        crate::Distance::Cosine => 1,
        crate::Distance::Euclidean => 2,
        crate::Distance::Dot => 3,
    };
    encode_varint(&mut params_buf, distance_val);

    // VectorParams.on_disk (field 5, bool)
    if on_disk {
        params_buf.put_u8(0x28);
        params_buf.put_u8(0x01);
    }

    // VectorsConfig.params (field 1) wraps VectorParams
    let mut config_buf = BytesMut::with_capacity(params_buf.len() + 4);
    config_buf.put_u8(0x0A);
    encode_varint(&mut config_buf, params_buf.len());
    config_buf.extend_from_slice(&params_buf);

    // Write to main buffer
    buf.put_u8(CREATE_VECTORS_CONFIG);
    encode_varint(buf, config_buf.len());
    buf.extend_from_slice(&config_buf);

    if on_disk {
        buf.put_u8(CREATE_ON_DISK);
        buf.put_u8(0x01);
    }
}

/// Encode DeleteCollection request.
pub fn encode_delete_collection_proto(buf: &mut BytesMut, collection_name: &str) {
    buf.clear();
    buf.put_u8(DELETE_COLLECTION_NAME);
    encode_varint(buf, collection_name.len());
    buf.extend_from_slice(collection_name.as_bytes());
}

// ============================================================================
// DeletePoints Encoder (supports both numeric and UUID IDs)
// ============================================================================

/// Encode a DeletePoints request with support for both numeric and UUID point IDs.
///
/// ```text
/// message DeletePoints {
///   string collection_name = 1;
///   bool wait = 2;
///   PointsSelector points = 4;
/// }
/// ```
pub fn encode_delete_points_mixed_proto(
    buf: &mut BytesMut,
    collection_name: &str,
    point_ids: &[crate::PointId],
) {
    buf.clear();

    // Field 1: collection_name
    buf.put_u8(0x0A);
    encode_varint(buf, collection_name.len());
    buf.put_slice(collection_name.as_bytes());

    // Field 2: wait = true
    buf.put_u8(0x10);
    buf.put_u8(1);

    // Field 4: points (PointsSelector -> PointsIdsList)
    let selector_buf = encode_points_selector(point_ids);
    buf.put_u8(0x22);
    encode_varint(buf, selector_buf.len());
    buf.extend_from_slice(&selector_buf);
}

/// Legacy: Encode a DeletePoints request (numeric IDs only).
pub fn encode_delete_points_proto(buf: &mut BytesMut, collection_name: &str, point_ids: &[u64]) {
    let ids: Vec<crate::PointId> = point_ids
        .iter()
        .map(|&id| crate::PointId::Num(id))
        .collect();
    encode_delete_points_mixed_proto(buf, collection_name, &ids);
}

/// Encode PointsSelector containing a PointsIdsList.
fn encode_points_selector(ids: &[crate::PointId]) -> BytesMut {
    // Build PointsIdsList (field 1: repeated PointId)
    let mut ids_list = BytesMut::with_capacity(ids.len() * 40);
    for id in ids {
        let mut id_buf = BytesMut::with_capacity(40);
        match id {
            crate::PointId::Num(n) => {
                id_buf.put_u8(POINT_ID_NUM);
                encode_varint_u64(&mut id_buf, *n);
            }
            crate::PointId::Uuid(s) => {
                id_buf.put_u8(POINT_ID_UUID);
                encode_varint(&mut id_buf, s.len());
                id_buf.extend_from_slice(s.as_bytes());
            }
        }
        ids_list.put_u8(0x0A); // PointId message (field 1, LEN)
        encode_varint(&mut ids_list, id_buf.len());
        ids_list.extend_from_slice(&id_buf);
    }

    // PointsSelector.points (field 1 = PointsIdsList)
    let mut selector = BytesMut::with_capacity(ids_list.len() + 8);
    selector.put_u8(0x0A); // field 1, LEN
    encode_varint(&mut selector, ids_list.len());
    selector.extend_from_slice(&ids_list);

    selector
}

// ============================================================================
// ListCollections Encoder
// ============================================================================

/// Encode a ListCollections request (empty message).
pub fn encode_list_collections_proto(buf: &mut BytesMut) {
    buf.clear();
    // ListCollectionsRequest is an empty message
}

/// Encode a GetCollectionInfo request.
///
/// ```text
/// message GetCollectionInfoRequest {
///   string collection_name = 1;
/// }
/// ```
pub fn encode_collection_info_proto(buf: &mut BytesMut, collection_name: &str) {
    buf.clear();
    buf.put_u8(0x0A);
    encode_varint(buf, collection_name.len());
    buf.extend_from_slice(collection_name.as_bytes());
}

// ============================================================================
// Utility
// ============================================================================

/// Calculate the byte length of a varint.
#[inline]
pub fn varint_len(value: u64) -> usize {
    if value == 0 {
        1
    } else {
        let bits = 64 - value.leading_zeros() as usize;
        bits.div_ceil(7)
    }
}

// ============================================================================
// Tests
// ============================================================================

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_varint_encoding() {
        let mut buf = BytesMut::new();

        // Single byte
        encode_varint(&mut buf, 1);
        assert_eq!(&buf[..], &[0x01]);

        buf.clear();
        encode_varint(&mut buf, 127);
        assert_eq!(&buf[..], &[0x7F]);

        // Two bytes
        buf.clear();
        encode_varint(&mut buf, 128);
        assert_eq!(&buf[..], &[0x80, 0x01]);

        buf.clear();
        encode_varint(&mut buf, 300);
        assert_eq!(&buf[..], &[0xAC, 0x02]);
    }

    #[test]
    fn test_encode_search_basic() {
        let mut buf = BytesMut::with_capacity(1024);
        let vector = vec![0.1f32, 0.2, 0.3, 0.4];

        encode_search_proto(&mut buf, "test_collection", &vector, 10, None, None, false);

        // Verify starts with collection name field
        assert_eq!(buf[0], SEARCH_COLLECTION);

        // Verify buffer is not empty
        assert!(buf.len() > 20);
    }

    #[test]
    fn test_encode_search_with_vectors_selector() {
        let mut buf = BytesMut::with_capacity(1024);
        let vector = vec![0.1f32, 0.2, 0.3, 0.4];

        encode_search_proto(&mut buf, "test_collection", &vector, 10, None, None, true);

        let selector_offset = buf
            .iter()
            .position(|tag| *tag == SEARCH_WITH_VECTORS)
            .expect("search request should include with_vectors field");
        assert_eq!(
            &buf[selector_offset..selector_offset + 4],
            &[SEARCH_WITH_VECTORS, 0x02, 0x08, 0x01]
        );
    }

    #[test]
    fn test_zero_copy_vector() {
        let mut buf = BytesMut::with_capacity(1024);
        let vector = vec![1.0f32, 2.0, 3.0, 4.0];

        encode_search_proto(&mut buf, "test", &vector, 5, None, None, false);

        // Find where vector data starts (after collection name + vector tag + length)
        // collection: 0x0A, len(4), "test" = 6 bytes
        // vector tag: 0x12 = 1 byte
        // vector len: 16 (4 floats * 4 bytes) = 1 byte varint
        // Total header: 8 bytes
        let vector_start = 8;
        let vector_bytes = &buf[vector_start..vector_start + 16];

        // Verify floats are correctly encoded as little-endian bytes
        let float_bytes: [u8; 4] = 1.0f32.to_le_bytes();
        assert_eq!(&vector_bytes[0..4], &float_bytes);
    }

    #[test]
    fn test_varint_len() {
        assert_eq!(varint_len(0), 1);
        assert_eq!(varint_len(1), 1);
        assert_eq!(varint_len(127), 1);
        assert_eq!(varint_len(128), 2);
        assert_eq!(varint_len(16383), 2);
        assert_eq!(varint_len(16384), 3);
    }

    #[test]
    fn test_encode_search_with_filter() {
        use qail_core::ast::{Condition, Expr, Operator, Value};

        let mut buf = BytesMut::with_capacity(1024);
        let vector = vec![0.1f32, 0.2, 0.3];
        let conditions = vec![
            Condition {
                left: Expr::Named("category".to_string()),
                op: Operator::Eq,
                value: Value::String("electronics".to_string()),
                is_array_unnest: false,
            },
            Condition {
                left: Expr::Named("price".to_string()),
                op: Operator::Lt,
                value: Value::Int(1000),
                is_array_unnest: false,
            },
        ];

        encode_search_with_filter_proto(
            &mut buf,
            SearchRequest {
                collection: "products",
                vector: &vector,
                limit: 10,
                score_threshold: None,
                vector_name: None,
                with_vectors: false,
            },
            &conditions,
            false,
        )
        .expect("filter encoding should succeed");

        // Should contain collection, vector, filter, limit, with_payload
        assert!(buf.len() > 50);
        // First byte should be collection tag
        assert_eq!(buf[0], SEARCH_COLLECTION);
        // Filter tag (0x1A) should appear somewhere after vector
        assert!(buf.contains(&SEARCH_FILTER));
    }

    #[test]
    fn test_encode_filtered_search_with_vectors_selector() {
        use qail_core::ast::{Condition, Expr, Operator, Value};

        let mut buf = BytesMut::with_capacity(1024);
        let vector = vec![0.1f32, 0.2, 0.3];
        let conditions = vec![Condition {
            left: Expr::Named("tenant_id".to_string()),
            op: Operator::Eq,
            value: Value::String("tenant-1".to_string()),
            is_array_unnest: false,
        }];

        encode_search_with_filter_proto(
            &mut buf,
            SearchRequest {
                collection: "products",
                vector: &vector,
                limit: 10,
                score_threshold: None,
                vector_name: None,
                with_vectors: true,
            },
            &conditions,
            false,
        )
        .expect("filter encoding should succeed");

        assert!(
            buf.contains(&SEARCH_WITH_VECTORS),
            "filtered search should preserve the with_vectors selector"
        );
    }

    #[test]
    fn test_encode_get_points() {
        let mut buf = BytesMut::with_capacity(1024);
        let ids = vec![
            crate::PointId::Num(42),
            crate::PointId::Uuid("abc-123".to_string()),
        ];

        encode_get_points_proto(&mut buf, "my_collection", &ids, true);

        assert_eq!(buf[0], 0x0A); // collection name tag
        assert!(buf.len() > 20);
    }

    #[test]
    fn test_encode_scroll_points() {
        let mut buf = BytesMut::with_capacity(1024);

        encode_scroll_points_proto(&mut buf, "my_collection", 100, None, false);

        assert_eq!(buf[0], 0x0A);
        assert!(buf.len() > 10);
    }

    #[test]
    fn test_encode_filtered_scroll_points() {
        use qail_core::ast::{Condition, Expr, Operator, Value};

        let mut buf = BytesMut::with_capacity(1024);
        let must = vec![Condition {
            left: Expr::Named("tenant_id".to_string()),
            op: Operator::Eq,
            value: Value::String("tenant-1".to_string()),
            is_array_unnest: false,
        }];

        encode_scroll_points_with_filter_grouped_cages_proto(
            &mut buf,
            "my_collection",
            100,
            None,
            false,
            &must,
            &[],
        )
        .expect("filtered scroll should encode");

        assert_eq!(buf[0], SCROLL_COLLECTION);
        assert!(
            buf.contains(&SCROLL_FILTER),
            "filtered scroll should include filter field"
        );
    }

    #[test]
    fn test_encode_delete_points_uuid() {
        let mut buf = BytesMut::with_capacity(1024);
        let ids = vec![
            crate::PointId::Uuid("test-uuid-1".to_string()),
            crate::PointId::Num(99),
        ];

        encode_delete_points_mixed_proto(&mut buf, "products", &ids);

        assert_eq!(buf[0], 0x0A); // collection name
        assert!(buf.len() > 20);
    }

    #[test]
    fn test_encode_set_payload() {
        let mut buf = BytesMut::with_capacity(1024);
        let ids = vec![crate::PointId::Num(1)];
        let mut payload = crate::point::Payload::new();
        payload.insert(
            "name".to_string(),
            crate::point::PayloadValue::String("updated".to_string()),
        );

        encode_set_payload_proto(&mut buf, "my_col", &ids, &payload, true);

        assert_eq!(buf[0], 0x0A);
        assert!(buf.len() > 15);
    }

    #[test]
    fn test_encode_search_with_filter_rejects_unsupported_operator() {
        use qail_core::ast::{Condition, Expr, Operator, Value};

        let mut buf = BytesMut::with_capacity(512);
        let vector = vec![0.1f32, 0.2];
        let conditions = vec![Condition {
            left: Expr::Named("status".to_string()),
            op: Operator::NotLike,
            value: Value::String("%inactive%".to_string()),
            is_array_unnest: false,
        }];

        let err = encode_search_with_filter_proto(
            &mut buf,
            SearchRequest {
                collection: "products",
                vector: &vector,
                limit: 5,
                score_threshold: None,
                vector_name: None,
                with_vectors: false,
            },
            &conditions,
            false,
        )
        .expect_err("unsupported operator must return an explicit error");

        match err {
            QdrantError::Encode(message) => {
                assert!(message.contains("Unsupported Qdrant filter condition"));
            }
            other => panic!("expected encode error, got {:?}", other),
        }
    }

    #[test]
    fn test_encode_search_with_filter_supports_is_null() {
        use qail_core::ast::{Condition, Expr, Operator, Value};

        let mut buf = BytesMut::with_capacity(512);
        let vector = vec![0.1f32, 0.2];
        let conditions = vec![Condition {
            left: Expr::Named("tenant_id".to_string()),
            op: Operator::IsNull,
            value: Value::Null,
            is_array_unnest: false,
        }];

        encode_search_with_filter_proto(
            &mut buf,
            SearchRequest {
                collection: "products",
                vector: &vector,
                limit: 5,
                score_threshold: None,
                vector_name: None,
                with_vectors: false,
            },
            &conditions,
            false,
        )
        .expect("IS NULL filters should encode as Qdrant IsNullCondition");

        assert!(
            buf.contains(&CONDITION_IS_NULL),
            "encoded request should contain an IsNullCondition"
        );
    }

    #[test]
    fn test_encode_search_with_filter_grouped_cages_includes_nested_filter_conditions() {
        use qail_core::ast::{Condition, Expr, Operator, Value};

        let mut buf = BytesMut::with_capacity(1024);
        let vector = vec![0.1f32, 0.2, 0.3];
        let must_conditions = vec![Condition {
            left: Expr::Named("tenant_id".to_string()),
            op: Operator::Eq,
            value: Value::String("t1".to_string()),
            is_array_unnest: false,
        }];
        let should_groups = vec![
            vec![
                Condition {
                    left: Expr::Named("city".to_string()),
                    op: Operator::Eq,
                    value: Value::String("London".to_string()),
                    is_array_unnest: false,
                },
                Condition {
                    left: Expr::Named("city".to_string()),
                    op: Operator::Eq,
                    value: Value::String("Paris".to_string()),
                    is_array_unnest: false,
                },
            ],
            vec![
                Condition {
                    left: Expr::Named("country".to_string()),
                    op: Operator::Eq,
                    value: Value::String("UK".to_string()),
                    is_array_unnest: false,
                },
                Condition {
                    left: Expr::Named("country".to_string()),
                    op: Operator::Eq,
                    value: Value::String("FR".to_string()),
                    is_array_unnest: false,
                },
            ],
        ];

        encode_search_with_filter_grouped_cages_proto(
            &mut buf,
            SearchRequest {
                collection: "products",
                vector: &vector,
                limit: 10,
                score_threshold: None,
                vector_name: None,
                with_vectors: false,
            },
            &must_conditions,
            &should_groups,
        )
        .expect("grouped-cage filter encoding should succeed");

        assert!(buf.contains(&SEARCH_FILTER));
        assert!(
            buf.contains(&CONDITION_FILTER),
            "expected nested filter condition tag for OR groups"
        );
    }

    #[test]
    fn test_encode_create_field_index() {
        let mut buf = BytesMut::with_capacity(256);

        encode_create_field_index_proto(&mut buf, "products", "category", FieldType::Keyword, true);

        assert_eq!(buf[0], 0x0A);
        assert!(buf.len() > 10);
    }

    #[test]
    fn test_encode_payload_value_string() {
        let val = crate::point::PayloadValue::String("hello".to_string());
        let buf = encode_payload_value(&val);

        // field 4 tag (0x22) + length + "hello"
        assert_eq!(buf[0], 0x22);
        assert!(buf.len() > 5);
    }

    #[test]
    fn test_encode_payload_value_integer() {
        let val = crate::point::PayloadValue::Integer(42);
        let buf = encode_payload_value(&val);

        // field 3 tag (0x18) + varint(42)
        assert_eq!(buf[0], 0x18);
    }
}