olai-codegen 0.0.12

Proto-driven code generation for REST handlers, clients, and resource registries
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
//! Analysis module for processing protobuf metadata into code generation plans
//!
//! This module takes the raw metadata extracted from protobuf files and analyzes it
//! to create a structured plan for code generation. It handles:
//!
//! - Grouping methods by service
//! - Extracting HTTP routing information
//! - Determining parameter types and sources
//! - Planning the structure of generated code
//! - Extracting managed resources from method return types
//!
//! ## Managed Resources
//!
//! Services often manage one or more resource types. These resources are automatically
//! extracted from the return types of get, create, and update methods. For example:
//!
//! ```proto
//! message Catalog {
//!   option (google.api.resource) = {
//!     type: "example.io/Catalog"
//!     pattern: "catalogs/{catalog}"
//!     plural: "catalogs"
//!     singular: "catalog"
//!   };
//!   string name = 1;
//!   // ... other fields
//! }
//!
//! service CatalogService {
//!   rpc GetCatalog(GetCatalogRequest) returns (Catalog);
//!   rpc CreateCatalog(CreateCatalogRequest) returns (Catalog);
//!   rpc UpdateCatalog(UpdateCatalogRequest) returns (Catalog);
//! }
//! ```
//!
//! The analysis will extract that `CatalogService` manages the `Catalog` resource,
//! making this information available for subsequent code generation phases.

use std::collections::{HashMap, HashSet};

use tracing::warn;

use crate::Result;
use crate::google::api::FieldBehavior;
use crate::parsing::types::BaseType;
use crate::parsing::{CodeGenMetadata, HttpPattern, MessageField, MethodMetadata, ServiceInfo};
use crate::utils::strings;

pub(crate) use types::MethodPlanner;
pub use types::{
    BodyField, ChildLink, EmitShape, GenerationPlan, ManagedResource, MethodPlan, MethodShape,
    PathParam, QueryParam, RequestParam, RequestType, ResourceHierarchy, ServicePlan,
    SkippedMethod, extract_managed_resources, split_body_fields,
};
pub(crate) use types::{emit_shape, is_collection_method};

mod types;

/// Analyze collected metadata and create a generation plan.
///
/// Analysis is **protocol-agnostic**: every method gets a [`MethodPlan`]. REST analysis is a
/// superset of what ConnectRPC needs, so a method with a `google.api.http` annotation carries the
/// full REST shape (path/query/body split, URL template) and one without carries the routeless
/// shape (all request fields as body). [`MethodPlan::has_http_route`] distinguishes them: the REST
/// emitters generate only routed methods; the ConnectRPC emitter generates all. No method is
/// dropped here, so [`GenerationPlan::skipped_methods`] is empty.
///
/// Hierarchy derivation uses a two-phase cross-service algorithm:
/// 1. Build all service plans (hierarchy empty).
/// 2. Construct a global parent map across all services, then assign depth-sorted hierarchies.
pub fn analyze_metadata(metadata: &CodeGenMetadata) -> Result<GenerationPlan> {
    // Phase 1: analyze all services without hierarchy.
    //
    // `metadata.services` is a `HashMap`, whose iteration order is non-deterministic. Sort by
    // service name first so the resulting plan order (and thus emitted module declarations and the
    // global parent map's insertion order) is reproducible across runs.
    let mut service_infos: Vec<_> = metadata.services.values().collect();
    service_infos.sort_by(|a, b| a.name.cmp(&b.name));

    let plans = service_infos
        .into_iter()
        .map(|service_info| analyze_service(metadata, service_info))
        .collect::<Result<Vec<_>>>()?;

    // Phase 2: build the cross-service global parent map, then assign depth-sorted hierarchies
    // and (once the hierarchy is known) the resource accessor param list.
    let global_map = build_global_parent_map(&plans, metadata);
    let mut services = plans
        .into_iter()
        .map(|mut plan| {
            plan.hierarchy = derive_ordered_hierarchy(&plan, &global_map, metadata)?;
            plan.resource_accessor_params = derive_resource_accessor_params(&plan);
            Ok(plan)
        })
        .collect::<Result<Vec<_>>>()?;

    // Phase 2b: derive direct children. This needs every service's accessor params (filled above),
    // so it runs as a separate sub-pass over the now-complete set.
    assign_direct_children(&mut services);

    Ok(GenerationPlan {
        services,
        skipped_methods: Vec::new(),
    })
}

// ── Cross-service hierarchy resolution ────────────────────────────────────────────────────────

/// Maps `(parent_resource_type, child_resource_type)` → proto field name that carries the
/// parent identifier on the child service's List request (e.g. `"catalog_name"`).
type GlobalParentMap = HashMap<(String, String), String>;

/// Build a map of immediate-parent relationships by scanning every service's List method query
/// params for `child_type`-annotated fields.
///
/// **Key semantic filter:** only params where `child_type == this service's managed resource type`
/// are recorded. This ensures that `catalog_name` on `ListTablesRequest` (child_type = Table) is
/// recorded as `(Catalog, Table)`, and `schema_name` as `(Schema, Table)` — but when walking
/// Schema's own List method, `catalog_name` (child_type = Schema) is recorded as `(Catalog, Schema)`.
/// The chain `Catalog → Schema → Table` is then reconstructable via depth analysis.
fn build_global_parent_map(plans: &[ServicePlan], metadata: &CodeGenMetadata) -> GlobalParentMap {
    let mut map: GlobalParentMap = HashMap::new();

    for plan in plans {
        let managed_type = match plan.managed_resources.first() {
            Some(r) => &r.descriptor.r#type,
            None => continue,
        };
        if managed_type.is_empty() {
            continue;
        }

        for method in &plan.methods {
            if method.request_type != RequestType::List {
                continue;
            }
            for param in method.query_parameters() {
                let Some(ref rr) = param.resource_reference else {
                    continue;
                };
                // Only record when child_type matches this service's own managed resource type.
                if rr.child_type != *managed_type {
                    continue;
                }
                if let Some(parent_type) = resolve_parent_type_from_field(&param.name, metadata) {
                    map.entry((parent_type, managed_type.clone()))
                        .or_insert_with(|| param.name.clone());
                }
            }
        }
    }

    map
}

/// Derive the ordered ancestor chain for a service's managed resource using the global parent map.
///
/// Returns entries sorted **root-first** (shallowest ancestor first), so iterating them gives
/// the correct left-to-right param order (e.g. `[catalog_name, schema_name]` for a Table service).
///
/// Returns `Err` if a cycle is detected in the resource hierarchy annotations.
fn derive_ordered_hierarchy(
    plan: &ServicePlan,
    global_map: &GlobalParentMap,
    metadata: &CodeGenMetadata,
) -> Result<Vec<ResourceHierarchy>> {
    let managed_type = match plan.managed_resources.first() {
        Some(r) => r.descriptor.r#type.clone(),
        None => return Ok(vec![]),
    };

    // Collect all (parent_resource_type, parent_field_name) entries for this managed resource.
    let mut ancestors: Vec<(usize, String, String)> = global_map
        .iter()
        .filter(|((_, child), _)| child == &managed_type)
        .map(|((parent_type, _), field_name)| {
            let depth = compute_depth(parent_type, global_map, &mut HashSet::new())?;
            Ok((depth, parent_type.clone(), field_name.clone()))
        })
        .collect::<Result<Vec<_>>>()?;

    if ancestors.is_empty() {
        return Ok(vec![]);
    }

    // Sort root-first (ascending depth).
    ancestors.sort_by_key(|(depth, _, _)| *depth);

    Ok(ancestors
        .into_iter()
        .map(|(_, parent_type, field_name)| {
            let parent_singular = field_name.strip_suffix("_name").and_then(|stem| {
                metadata
                    .resource_from_singular(stem)
                    .map(|_| stem.to_string())
            });
            ResourceHierarchy {
                child_resource_type: managed_type.clone(),
                parent_resource_type: parent_type,
                parent_field_name: field_name,
                parent_singular,
            }
        })
        .collect())
}

/// Compute the depth of a resource type in the parent chain (0 = root, no parent in map).
///
/// Returns `Err` if a cycle is detected in the resource hierarchy annotations.
fn compute_depth(
    resource_type: &str,
    map: &GlobalParentMap,
    visited: &mut HashSet<String>,
) -> Result<usize> {
    if !visited.insert(resource_type.to_string()) {
        return Err(crate::Error::Build(format!(
            "Cycle detected in resource hierarchy at type: {resource_type}"
        )));
    }
    let parent = map
        .iter()
        .find(|((_, child), _)| child == resource_type)
        .map(|((parent, _), _)| parent.clone());

    match parent {
        None => Ok(0),
        Some(parent_type) => Ok(1 + compute_depth(&parent_type, map, visited)?),
    }
}

/// Resolve the resource type string for a proto field name by stripping `"_name"` and looking up
/// the resulting singular in `metadata`.
///
/// Returns `None` if the field name doesn't end in `"_name"` or no resource matches.
fn resolve_parent_type_from_field(field_name: &str, metadata: &CodeGenMetadata) -> Option<String> {
    field_name
        .strip_suffix("_name")
        .and_then(|s| metadata.resource_from_singular(s))
        .map(|rd| rd.r#type.clone())
}

/// Standard AIP-132 `List` request fields that are never part of a resource's name.
///
/// These are pagination/filtering knobs, not parent-hierarchy components. proto3 scalars carry no
/// presence info, so a required `page_token` looks identical to a real parent name like
/// `catalog_name` — the heuristic derivation must exclude these by name explicitly, or pagination
/// fields leak into accessor params (e.g. a flat Catalog getting a bogus `(page_token, name)`).
pub(crate) fn is_standard_list_field(name: &str) -> bool {
    matches!(
        name,
        "page_token" | "page_size" | "max_results" | "order_by" | "filter"
    )
}

/// Derive the ordered ancestor field names for a service's managed resource, *excluding* the
/// resource's own leaf name. e.g. `["catalog_name"]` for Schema, `[]` for a flat Catalog.
///
/// **Annotation-driven path** (preferred): when the service has `hierarchy` entries from
/// `resource_reference { child_type }` annotations, the parent field names come straight from those
/// entries (in List-method param order).
///
/// **Heuristic fallback** (no annotations): the resource name is composite when either
/// 1. the descriptor declares a `name_field` (an explicit dot-joined `full_name`), or
/// 2. the Get method's path param is `"name"` (an opaque full path) *and* the List method has
///    required string query params that aren't standard pagination fields — those are the parents.
///
/// Returns an empty vec for a flat (top-level) resource.
pub(crate) fn derive_accessor_parent_fields(plan: &ServicePlan) -> Vec<String> {
    let Some(resource) = plan.managed_resources.first() else {
        return vec![];
    };

    // --- Annotation-driven path ---
    let resource_type = &resource.descriptor.r#type;
    if !plan.hierarchy.is_empty() && !resource_type.is_empty() {
        let annotation_parents: Vec<String> = plan
            .hierarchy
            .iter()
            .filter(|h| &h.child_resource_type == resource_type)
            .map(|h| h.parent_field_name.clone())
            .collect();
        if !annotation_parents.is_empty() {
            return annotation_parents;
        }
    }

    // --- Heuristic fallback ---
    let has_explicit_name_field = !resource.descriptor.name_field.is_empty();

    let get_path_param_name = plan
        .methods
        .iter()
        .find(|m| m.request_type == RequestType::Get)
        .and_then(|m| m.path_parameters().next().map(|p| p.name.clone()));

    let parent_params: Vec<String> = plan
        .methods
        .iter()
        .find(|m| m.request_type == RequestType::List)
        .map(|m| {
            m.parameters
                .iter()
                .filter(|p| !p.is_path_param() && !p.is_optional())
                .filter(|p| matches!(p.field_type().base_type, BaseType::String))
                .filter(|p| !is_standard_list_field(p.name()))
                .map(|p| p.name().to_string())
                .collect()
        })
        .unwrap_or_default();

    let should_decompose = has_explicit_name_field
        || (get_path_param_name.as_deref() == Some("name") && !parent_params.is_empty());

    if should_decompose {
        parent_params
    } else {
        vec![]
    }
}

/// Derive the full resource accessor param list (ancestor fields + the resource's own
/// `<singular>_name` leaf), or `None` for resource-less services.
///
/// Single source of truth consumed by all four accessor emitters (Rust aggregate + Python + Node +
/// TypeScript). A returned list of length 1 (`["<singular>_name"]`) is a flat resource; length > 1
/// is nested (its full name is the dot-joined components).
fn derive_resource_accessor_params(plan: &ServicePlan) -> Option<Vec<String>> {
    let resource = plan.managed_resources.first()?;
    let mut params = derive_accessor_parent_fields(plan);
    params.push(format!("{}_name", resource.descriptor.singular));
    Some(params)
}

/// Populate each service's [`ServicePlan::direct_children`] using the accessor-param prefix relation.
///
/// A resource-scoped service C is a *direct child* of resource-scoped service P when C's accessor
/// params extend P's by exactly one trailing component (P's params are a prefix of C's and
/// `C.len() == P.len() + 1`). Children are sorted by singular for deterministic output.
fn assign_direct_children(services: &mut [ServicePlan]) {
    // Snapshot the (accessor params, base_path, singular) of every resource-scoped service so we can
    // scan for children while mutating each parent's `direct_children` in place.
    let candidates: Vec<(Vec<String>, String, String)> = services
        .iter()
        .filter_map(|s| {
            let params = s.resource_accessor_params.clone()?;
            let singular = s.managed_resources.first()?.descriptor.singular.clone();
            Some((params, s.base_path.clone(), singular))
        })
        .collect();

    for plan in services.iter_mut() {
        let Some(parent_params) = plan.resource_accessor_params.clone() else {
            continue;
        };
        let mut children: Vec<ChildLink> = candidates
            .iter()
            .filter(|(child_params, child_base_path, _)| {
                *child_base_path != plan.base_path
                    && child_params.len() == parent_params.len() + 1
                    && child_params[..parent_params.len()] == parent_params[..]
            })
            .map(
                |(child_params, child_base_path, child_singular)| ChildLink {
                    child_singular: child_singular.clone(),
                    child_base_path: child_base_path.clone(),
                    child_accessor_params: child_params.clone(),
                },
            )
            .collect();
        children.sort_by(|a, b| a.child_singular.cmp(&b.child_singular));
        plan.direct_children = children;
    }
}

/// Analyze a single service and create a service plan.
fn analyze_service(metadata: &CodeGenMetadata, info: &ServiceInfo) -> Result<ServicePlan> {
    let handler_name = strings::service_to_handler_name(&info.name);
    let base_path = strings::service_to_base_path(&info.name);

    // Analysis is protocol-agnostic: every method gets a plan. Methods without an HTTP annotation
    // get the routeless shape (`has_http_route == false`); the REST emitters skip them while the
    // ConnectRPC emitter uses them. So no method is dropped at analysis time.
    let mut method_plans = info
        .methods
        .iter()
        .map(|method| analyze_method(metadata, method))
        .collect::<Result<Vec<_>>>()?;

    let managed_resources = types::extract_managed_resources(metadata, &method_plans);

    // Second pass: now that we know whether this service manages a resource, classify each method's
    // shape (collection / instance / unbound). `analyze_method` couldn't do this — it has no view of
    // the service's resources.
    let service_has_resource = !managed_resources.is_empty();
    for plan in &mut method_plans {
        plan.shape = types::method_shape(
            &plan.request_type,
            &plan.metadata.method_name,
            service_has_resource,
        );
    }

    Ok(ServicePlan {
        service_name: info.name.clone(),
        handler_name,
        base_path,
        package: info.package.clone(),
        methods: method_plans,
        managed_resources,
        documentation: info.documentation.clone(),
        hierarchy: vec![],              // filled in Phase 2 of analyze_metadata
        resource_accessor_params: None, // filled in Phase 2 of analyze_metadata
        direct_children: vec![],        // filled in Phase 2b of analyze_metadata
    })
}

/// Analyze a single method and create a method plan.
///
/// Analysis is **protocol-agnostic** and produces the superset of information both the REST and the
/// ConnectRPC emitters consume. Every method gets a plan:
///
/// - With a `google.api.http` annotation: the full REST shape (path/query/body split, URL template,
///   request type). [`MethodPlan::has_http_route`] is `true`. The Connect emitter ignores the
///   routing fields and reads only the body fields + input/output types.
/// - Without one: the routeless shape (all request fields as body, no path/query — see
///   [`analyze_routeless_method`]). `has_http_route` is `false`; the REST emitters skip it (it has
///   no route to call), the Connect emitter emits it.
pub(crate) fn analyze_method(
    metadata: &CodeGenMetadata,
    method: &MethodMetadata,
) -> Result<MethodPlan> {
    let Some(http_method) = method.http_method().map(|m| m.to_string()) else {
        // No HTTP annotation: still produce a plan (the routeless / Connect-degenerate shape) so the
        // ConnectRPC emitter can use it. REST emitters gate on `has_http_route`.
        return analyze_routeless_method(metadata, method);
    };

    let planner = MethodPlanner::try_new(method, metadata)?;
    let request_type = planner.request_type();
    let has_response = planner.has_response();
    let output_resource_type = planner.output_resource_type();
    let http_pattern = planner.into_http_pattern();

    let input_fields = metadata.get_message_fields(&method.input_type);
    let (path_params, query_params, body_fields) = extract_request_fields(method, &input_fields)?;

    let parameters = path_params
        .into_iter()
        .map(Into::into)
        .chain(query_params.into_iter().map(Into::into))
        .chain(body_fields.into_iter().map(Into::into))
        .collect();

    // A method whose HTTP rule names a body has one, whatever the verb.
    let has_request_body =
        types::request_has_body(&request_type) || !method.http_rule.body.is_empty();
    let needs_request_parts = types::request_needs_request_parts(&request_type);
    let scoped_verb = types::scoped_verb(&request_type);

    Ok(MethodPlan {
        metadata: method.clone(),
        handler_function_name: strings::operation_to_method_name(&method.method_name),
        http_method,
        parameters,
        has_response,
        request_type,
        output_resource_type,
        http_pattern,
        has_request_body,
        needs_request_parts,
        scoped_verb,
        // Provisional: `shape` depends on the owning service's managed resources, which aren't
        // known here. `analyze_service` overwrites this once `extract_managed_resources` has run.
        shape: types::MethodShape::Unbound,
        has_http_route: true,
    })
}

/// Analyze a method that has no `google.api.http` annotation (the ConnectRPC-degenerate shape).
///
/// ConnectRPC dispatch sends the whole request message as the body and reads the whole response, so
/// there is no path/query/body split and no URL template. Every non-`OUTPUT_ONLY` request field
/// becomes a [`BodyField`], partitioned required-vs-optional purely from
/// `google.api.field_behavior` — the same `BodyField` shape the REST builders consume, so the
/// builder layer ([`crate::codegen::builder`]) is reused unchanged.
///
/// The HTTP-only fields of [`MethodPlan`] (`http_method`, `http_pattern`) are left empty/default and
/// [`MethodPlan::has_http_route`] is `false`; the REST emitters skip such methods, so those fields
/// are never read for them.
fn analyze_routeless_method(
    metadata: &CodeGenMetadata,
    method: &MethodMetadata,
) -> Result<MethodPlan> {
    let input_fields = metadata.get_message_fields(&method.input_type);
    let body_fields = routeless_body_fields(&input_fields);

    // A routeless method carries no REST request shape. `Custom` keeps it off the REST collection /
    // instance code paths (resource-scoped clients, pagination heuristics) while still flowing
    // through the shared builder generator. The verb is irrelevant for Connect dispatch; `POST`
    // is the conventional placeholder.
    let request_type =
        RequestType::Custom(crate::google::api::http_rule::Pattern::Post(String::new()));
    let has_response = !method.output_type.is_empty() && !method.output_type.ends_with("Empty");

    Ok(MethodPlan {
        metadata: method.clone(),
        handler_function_name: strings::operation_to_method_name(&method.method_name),
        http_method: String::new(),
        parameters: body_fields.into_iter().map(Into::into).collect(),
        has_response,
        request_type,
        output_resource_type: None,
        http_pattern: HttpPattern::default(),
        // The request message is always carried as the body for Connect dispatch.
        has_request_body: true,
        needs_request_parts: false,
        scoped_verb: None,
        shape: types::MethodShape::Unbound,
        has_http_route: false,
    })
}

/// Classify a routeless request message's fields into [`BodyField`]s.
///
/// `OUTPUT_ONLY` fields are dropped (server-generated). Oneof fields are optional body fields with
/// their variants preserved. Every other field is a body field whose `required` flag is taken from
/// `google.api.field_behavior` REQUIRED — mirroring the `body: "*"` branch of
/// [`extract_request_fields`], minus the path/query split that ConnectRPC has no notion of.
fn routeless_body_fields(input_fields: &[MessageField]) -> Vec<BodyField> {
    input_fields
        .iter()
        .filter(|field| !field.field_behavior.contains(&FieldBehavior::OutputOnly))
        .map(body_field)
        .collect()
}

/// The body field for a request field.
///
/// A oneof is always optional and keeps its variants. Otherwise `required` comes from
/// `google.api.field_behavior` REQUIRED, and a non-required singular message is marked optional on
/// the type so it becomes a `with_*(impl Into<Option<T>>)` setter (and an `Option<T>` with a
/// `= None` default in FFI bindings) rather than a required argument. Scalars and collections are
/// unaffected (their optionality is already correct).
fn body_field(field: &MessageField) -> BodyField {
    if matches!(field.unified_type.base_type, BaseType::OneOf(_)) {
        return BodyField {
            name: field.name.clone(),
            field_type: field.unified_type.clone().optional(),
            repeated: false,
            required: false,
            oneof_variants: field.oneof_variants.clone(),
            documentation: field.documentation.clone(),
        };
    }

    let required = field.field_behavior.contains(&FieldBehavior::Required);
    let needs_optional_marker = !required
        && !field.unified_type.is_repeated
        && matches!(field.unified_type.base_type, BaseType::Message(_));
    let field_type = if needs_optional_marker {
        field.unified_type.clone().optional()
    } else {
        field.unified_type.clone()
    };
    BodyField {
        name: field.name.clone(),
        field_type,
        repeated: field.unified_type.is_repeated,
        required,
        oneof_variants: None,
        documentation: field.documentation.clone(),
    }
}

/// Extract and classify request fields from an input message into path, query, and body buckets.
///
/// - Path parameters are matched against URL template parameters and ordered accordingly.
/// - Fields annotated `OUTPUT_ONLY` are excluded entirely — they are server-generated and
///   must not appear in request extractors or client request builders.
/// - Fields matching the `body` spec (`"*"`, `""`, or a specific field name) become body fields.
/// - All remaining fields become query parameters. Fields not explicitly marked optional
///   via `UnifiedType.is_optional` are treated as required query parameters.
/// - Oneof fields are always placed in the body as optional variants.
fn extract_request_fields(
    method: &MethodMetadata,
    input_fields: &[MessageField],
) -> Result<(Vec<PathParam>, Vec<QueryParam>, Vec<BodyField>)> {
    let mut path_params = Vec::new();
    let mut query_params = Vec::new();
    let mut body_fields = Vec::new();

    let path_param_names = method.http_pattern.parameter_names();
    let body_spec = method.http_rule.body.as_str();

    // Build an O(1) lookup map for input fields by name.
    let fields_by_name: HashMap<&str, &MessageField> =
        input_fields.iter().map(|f| (f.name.as_str(), f)).collect();

    let mut processed_fields = HashSet::new();

    // Add path parameters in URL template order. `parameter_names()` yields normalized flat field
    // names from the URL template (segment-binding `=pattern` suffixes already stripped by the
    // template parser; unsupported dotted placeholders are kept out of `parameters` entirely).
    for path_param_name in path_param_names {
        if let Some(field) = fields_by_name.get(path_param_name.as_str()) {
            // Path params are always included even if OUTPUT_ONLY (they appear in the URL, not
            // the request body), but in practice OUTPUT_ONLY fields should never be path params.
            path_params.push(PathParam {
                name: field.name.clone(),
                field_type: field.unified_type.clone(),
                documentation: field.documentation.clone(),
            });
            processed_fields.insert(field.name.as_str());
        } else {
            // Unresolved placeholder: warn rather than silently misrouting the field to query/body.
            warn!(
                "Method {}.{}: URL path placeholder `{}` does not match any request field; \
                 the param will be omitted from the path",
                method.service_name, method.method_name, path_param_name
            );
        }
    }

    // Classify remaining fields as body or query.
    for field in input_fields {
        let field_name = field.name.as_str();

        if processed_fields.contains(field_name) {
            continue;
        }

        // Skip OUTPUT_ONLY fields — they are server-generated and never provided by clients.
        if field.field_behavior.contains(&FieldBehavior::OutputOnly) {
            processed_fields.insert(field_name);
            continue;
        }

        // Oneof fields are always body fields and always optional.
        if matches!(field.unified_type.base_type, BaseType::OneOf(_)) {
            body_fields.push(body_field(field));
            processed_fields.insert(field_name);
            continue;
        }

        let is_body = match body_spec {
            "*" => true,
            "" => false,
            specific => specific == field_name,
        };

        if is_body {
            body_fields.push(body_field(field));
        } else {
            query_params.push(QueryParam {
                name: field.name.clone(),
                json_name: field.json_name.clone(),
                field_type: field.unified_type.clone(),
                documentation: field.documentation.clone(),
                resource_reference: field.resource_reference.clone(),
            });
        }
        processed_fields.insert(field_name);
    }

    Ok((path_params, query_params, body_fields))
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::google::api::{HttpRule, ResourceDescriptor, http_rule::Pattern};
    use crate::parsing::types::UnifiedType;
    use crate::parsing::{CodeGenMetadata, HttpPattern, MessageInfo, MethodMetadata, ServiceInfo};
    use std::collections::HashMap;

    fn make_metadata_with_catalog() -> CodeGenMetadata {
        let catalog_resource = ResourceDescriptor {
            r#type: "example.io/Catalog".to_string(),
            pattern: vec!["catalogs/{catalog}".to_string()],
            name_field: "name".to_string(),
            history: 0,
            plural: "catalogs".to_string(),
            singular: "catalog".to_string(),
            style: vec![],
        };
        let catalog_info = MessageInfo {
            name: "Catalog".to_string(),
            fields: vec![],
            resource_descriptor: Some(catalog_resource),
            documentation: None,
        };
        let mut messages = HashMap::new();
        messages.insert("Catalog".to_string(), catalog_info);
        CodeGenMetadata {
            messages,
            ..Default::default()
        }
    }

    fn make_get_method() -> MethodMetadata {
        MethodMetadata {
            service_name: "CatalogService".to_string(),
            method_name: "GetCatalog".to_string(),
            input_type: "GetCatalogRequest".to_string(),
            output_type: "Catalog".to_string(),
            operation: None,
            http_rule: HttpRule {
                selector: "".to_string(),
                pattern: Some(Pattern::Get("/catalogs/{name}".to_string())),
                body: "".to_string(),
                response_body: "".to_string(),
                additional_bindings: vec![],
            },
            http_pattern: HttpPattern::parse("/catalogs/{name}"),
            documentation: None,
        }
    }

    #[test]
    fn test_managed_resources_extraction() {
        let metadata = make_metadata_with_catalog();
        let service_info = ServiceInfo {
            name: "CatalogService".to_string(),
            package: "example.catalogs.v1".to_string(),
            documentation: None,
            methods: vec![make_get_method()],
        };
        let service_plan = analyze_service(&metadata, &service_info).unwrap();

        assert_eq!(service_plan.managed_resources.len(), 1);
        assert_eq!(service_plan.managed_resources[0].type_name, "Catalog");
        assert_eq!(
            service_plan.managed_resources[0].descriptor.r#type,
            "example.io/Catalog"
        );
        assert_eq!(
            service_plan.managed_resources[0].descriptor.singular,
            "catalog"
        );
        assert_eq!(
            service_plan.managed_resources[0].descriptor.plural,
            "catalogs"
        );
    }

    #[test]
    fn test_no_duplicate_managed_resources() {
        let metadata = make_metadata_with_catalog();
        let update_method = MethodMetadata {
            service_name: "CatalogService".to_string(),
            method_name: "UpdateCatalog".to_string(),
            input_type: "UpdateCatalogRequest".to_string(),
            output_type: "Catalog".to_string(),
            operation: None,
            http_rule: HttpRule {
                selector: "".to_string(),
                pattern: Some(Pattern::Patch("/catalogs/{name}".to_string())),
                body: "*".to_string(),
                response_body: "".to_string(),
                additional_bindings: vec![],
            },
            http_pattern: HttpPattern::parse("/catalogs/{name}"),
            documentation: None,
        };
        let service_info = ServiceInfo {
            name: "CatalogService".to_string(),
            package: "example.catalogs.v1".to_string(),
            documentation: None,
            methods: vec![make_get_method(), update_method],
        };
        let service_plan = analyze_service(&metadata, &service_info).unwrap();

        assert_eq!(service_plan.managed_resources.len(), 1);
        assert_eq!(service_plan.managed_resources[0].type_name, "Catalog");
    }

    #[test]
    fn test_analyze_method_missing_http_pattern_is_routeless() {
        let metadata = CodeGenMetadata::default();
        let method = MethodMetadata {
            service_name: "SomeService".to_string(),
            method_name: "SomeMethod".to_string(),
            input_type: "".to_string(),
            output_type: "".to_string(),
            operation: None,
            http_rule: HttpRule {
                selector: "".to_string(),
                pattern: None,
                body: "".to_string(),
                response_body: "".to_string(),
                additional_bindings: vec![],
            },
            http_pattern: HttpPattern::parse(""),
            documentation: None,
        };
        // Protocol-agnostic analysis still produces a plan, but flags it as having no HTTP route so
        // the REST emitters skip it while the Connect emitter uses it.
        let plan = analyze_method(&metadata, &method).unwrap();
        assert!(!plan.has_http_route);
        assert!(plan.http_method.is_empty());
    }

    #[test]
    fn test_routeless_method_partitions_body_by_field_behavior() {
        use crate::parsing::MessageInfo;

        // A request with one REQUIRED field and one unmarked (optional) field, and NO HTTP rule.
        let req = MessageInfo {
            name: "DoThingRequest".to_string(),
            fields: vec![
                {
                    let mut f = make_string_field("name", false);
                    f.field_behavior = vec![FieldBehavior::Required];
                    f
                },
                make_string_field("comment", false),
            ],
            resource_descriptor: None,
            documentation: None,
        };
        let mut messages = HashMap::new();
        messages.insert("DoThingRequest".to_string(), req);
        let metadata = CodeGenMetadata {
            messages,
            ..Default::default()
        };

        let method = MethodMetadata {
            service_name: "ThingService".to_string(),
            method_name: "DoThing".to_string(),
            input_type: "DoThingRequest".to_string(),
            output_type: "DoThingResponse".to_string(),
            operation: None,
            http_rule: HttpRule {
                selector: "".to_string(),
                pattern: None, // no HTTP annotation
                body: "".to_string(),
                response_body: "".to_string(),
                additional_bindings: vec![],
            },
            http_pattern: HttpPattern::default(),
            documentation: None,
        };

        let plan = analyze_method(&metadata, &method).unwrap();
        assert!(!plan.has_http_route);

        // No path/query params: every field is a body field.
        assert_eq!(plan.path_parameters().count(), 0);
        assert_eq!(plan.query_parameters().count(), 0);
        let body: Vec<_> = plan.body_fields().collect();
        assert_eq!(body.len(), 2);

        // Required vs optional follows field_behavior: `name` (REQUIRED) is required, `comment` not.
        let name = body.iter().find(|b| b.name == "name").unwrap();
        let comment = body.iter().find(|b| b.name == "comment").unwrap();
        assert!(name.required, "REQUIRED field should be a constructor arg");
        assert!(
            !comment.required,
            "unmarked field should be an optional with_* setter"
        );
        assert_eq!(plan.handler_function_name, "do_thing");
        assert!(plan.has_response);
    }

    #[test]
    fn test_routeless_method_drops_output_only_fields() {
        use crate::parsing::MessageInfo;

        let req = MessageInfo {
            name: "MakeReq".to_string(),
            fields: vec![make_string_field("input", false), {
                let mut f = make_string_field("server_set", false);
                f.field_behavior = vec![FieldBehavior::OutputOnly];
                f
            }],
            resource_descriptor: None,
            documentation: None,
        };
        let mut messages = HashMap::new();
        messages.insert("MakeReq".to_string(), req);
        let metadata = CodeGenMetadata {
            messages,
            ..Default::default()
        };

        let method = MethodMetadata {
            service_name: "S".to_string(),
            method_name: "Make".to_string(),
            input_type: "MakeReq".to_string(),
            output_type: "MakeResp".to_string(),
            operation: None,
            http_rule: HttpRule::default(),
            http_pattern: HttpPattern::default(),
            documentation: None,
        };

        let plan = analyze_method(&metadata, &method).unwrap();
        let body: Vec<_> = plan.body_fields().collect();
        assert_eq!(body.len(), 1, "OUTPUT_ONLY field must be dropped");
        assert_eq!(body[0].name, "input");
    }

    // --- extract_request_fields unit tests ---

    fn make_string_field(name: &str, optional: bool) -> MessageField {
        use crate::parsing::types::BaseType;
        MessageField {
            name: name.to_string(),
            json_name: None,
            unified_type: UnifiedType {
                base_type: BaseType::String,
                is_optional: optional,
                is_repeated: false,
            },
            documentation: None,
            oneof_variants: None,
            field_behavior: vec![],
            is_sensitive: false,
            resource_reference: None,
        }
    }

    fn make_repeated_field(name: &str) -> MessageField {
        use crate::parsing::types::BaseType;
        MessageField {
            name: name.to_string(),
            json_name: None,
            unified_type: UnifiedType {
                base_type: BaseType::String,
                is_optional: false,
                is_repeated: true,
            },
            documentation: None,
            oneof_variants: None,
            field_behavior: vec![],
            is_sensitive: false,
            resource_reference: None,
        }
    }

    fn make_method_with_pattern(pattern: Pattern, body: &str, path: &str) -> MethodMetadata {
        MethodMetadata {
            service_name: "Svc".to_string(),
            method_name: "Method".to_string(),
            input_type: "".to_string(),
            output_type: "".to_string(),
            operation: None,
            http_rule: HttpRule {
                selector: "".to_string(),
                pattern: Some(pattern),
                body: body.to_string(),
                response_body: "".to_string(),
                additional_bindings: vec![],
            },
            http_pattern: HttpPattern::parse(path),
            documentation: None,
        }
    }

    #[test]
    fn test_extract_path_params_in_url_order() {
        let method =
            make_method_with_pattern(Pattern::Get("/a/{x}/b/{y}".to_string()), "", "/a/{x}/b/{y}");
        let fields = vec![make_string_field("y", false), make_string_field("x", false)];
        let (path, query, body) = extract_request_fields(&method, &fields).unwrap();
        // Path params should be in URL order: x, y — not field declaration order
        assert_eq!(path.len(), 2);
        assert_eq!(path[0].name, "x");
        assert_eq!(path[1].name, "y");
        assert!(query.is_empty());
        assert!(body.is_empty());
    }

    #[test]
    fn test_extract_body_wildcard() {
        let method = make_method_with_pattern(Pattern::Post("/items".to_string()), "*", "/items");
        let fields = vec![
            make_string_field("name", false),
            make_string_field("description", true),
        ];
        let (path, query, body) = extract_request_fields(&method, &fields).unwrap();
        assert!(path.is_empty());
        assert!(query.is_empty());
        assert_eq!(body.len(), 2);
    }

    #[test]
    fn test_extract_specific_body_field() {
        let method = make_method_with_pattern(
            Pattern::Patch("/items/{name}".to_string()),
            "payload",
            "/items/{name}",
        );
        let fields = vec![
            make_string_field("name", false),    // path
            make_string_field("payload", false), // body (specific)
            make_string_field("extra", true),    // query
        ];
        let (path, query, body) = extract_request_fields(&method, &fields).unwrap();
        assert_eq!(path.len(), 1);
        assert_eq!(path[0].name, "name");
        assert_eq!(body.len(), 1);
        assert_eq!(body[0].name, "payload");
        assert_eq!(query.len(), 1);
        assert_eq!(query[0].name, "extra");
    }

    #[test]
    fn test_extract_no_body_spec_all_query() {
        let method = make_method_with_pattern(Pattern::Get("/items".to_string()), "", "/items");
        let fields = vec![
            make_string_field("filter", true),
            make_string_field("page_size", true),
        ];
        let (path, query, body) = extract_request_fields(&method, &fields).unwrap();
        assert!(path.is_empty());
        assert_eq!(query.len(), 2);
        assert!(body.is_empty());
    }

    #[test]
    fn test_extract_repeated_field_becomes_body_with_repeated_flag() {
        let method = make_method_with_pattern(Pattern::Post("/items".to_string()), "*", "/items");
        let fields = vec![make_repeated_field("tags")];
        let (_, _, body) = extract_request_fields(&method, &fields).unwrap();
        assert_eq!(body.len(), 1);
        assert!(body[0].repeated);
    }

    // ── Cross-service hierarchy chain resolution tests ─────────────────────────────────────────

    /// Build a three-level metadata fixture: Catalog → Schema → Table.
    ///
    /// Three services:
    /// - CatalogService: ListCatalogs (no child_type params), GetCatalog
    /// - SchemaService: ListSchemas (catalog_name with child_type = Schema), GetSchema
    /// - TableService: ListTables (catalog_name and schema_name both with child_type = Table), GetTable
    fn make_three_level_metadata() -> (CodeGenMetadata, ServiceInfo, ServiceInfo, ServiceInfo) {
        use crate::google::api::ResourceReference;
        use crate::parsing::types::BaseType;

        let mut messages = HashMap::new();

        // Catalog resource
        messages.insert(
            "Catalog".to_string(),
            MessageInfo {
                name: "Catalog".to_string(),
                fields: vec![],
                resource_descriptor: Some(ResourceDescriptor {
                    r#type: "example.io/Catalog".to_string(),
                    pattern: vec!["catalogs/{catalog}".to_string()],
                    name_field: "name".to_string(),
                    history: 0,
                    plural: "catalogs".to_string(),
                    singular: "catalog".to_string(),
                    style: vec![],
                }),
                documentation: None,
            },
        );

        // Schema resource
        messages.insert(
            "Schema".to_string(),
            MessageInfo {
                name: "Schema".to_string(),
                fields: vec![],
                resource_descriptor: Some(ResourceDescriptor {
                    r#type: "example.io/Schema".to_string(),
                    pattern: vec!["schemas/{schema}".to_string()],
                    name_field: "name".to_string(),
                    history: 0,
                    plural: "schemas".to_string(),
                    singular: "schema".to_string(),
                    style: vec![],
                }),
                documentation: None,
            },
        );

        // Table resource
        messages.insert(
            "Table".to_string(),
            MessageInfo {
                name: "Table".to_string(),
                fields: vec![],
                resource_descriptor: Some(ResourceDescriptor {
                    r#type: "example.io/Table".to_string(),
                    pattern: vec!["tables/{table}".to_string()],
                    name_field: "full_name".to_string(),
                    history: 0,
                    plural: "tables".to_string(),
                    singular: "table".to_string(),
                    style: vec![],
                }),
                documentation: None,
            },
        );

        // ListCatalogsRequest — no child_type params
        messages.insert(
            "ListCatalogsRequest".to_string(),
            MessageInfo {
                name: "ListCatalogsRequest".to_string(),
                fields: vec![],
                resource_descriptor: None,
                documentation: None,
            },
        );

        // ListSchemasRequest — catalog_name with child_type = Schema
        messages.insert(
            "ListSchemasRequest".to_string(),
            MessageInfo {
                name: "ListSchemasRequest".to_string(),
                fields: vec![MessageField {
                    name: "catalog_name".to_string(),
                    json_name: None,
                    unified_type: UnifiedType {
                        base_type: BaseType::String,
                        is_optional: false,
                        is_repeated: false,
                    },
                    documentation: None,
                    oneof_variants: None,
                    field_behavior: vec![crate::google::api::FieldBehavior::Required],
                    is_sensitive: false,
                    resource_reference: Some(ResourceReference {
                        r#type: String::new(),
                        child_type: "example.io/Schema".to_string(),
                    }),
                }],
                resource_descriptor: None,
                documentation: None,
            },
        );

        // ListTablesRequest — catalog_name and schema_name, both child_type = Table (flat API)
        messages.insert(
            "ListTablesRequest".to_string(),
            MessageInfo {
                name: "ListTablesRequest".to_string(),
                fields: vec![
                    MessageField {
                        name: "catalog_name".to_string(),
                        json_name: None,
                        unified_type: UnifiedType {
                            base_type: BaseType::String,
                            is_optional: false,
                            is_repeated: false,
                        },
                        documentation: None,
                        oneof_variants: None,
                        field_behavior: vec![crate::google::api::FieldBehavior::Required],
                        is_sensitive: false,
                        resource_reference: Some(ResourceReference {
                            r#type: String::new(),
                            child_type: "example.io/Table".to_string(),
                        }),
                    },
                    MessageField {
                        name: "schema_name".to_string(),
                        json_name: None,
                        unified_type: UnifiedType {
                            base_type: BaseType::String,
                            is_optional: false,
                            is_repeated: false,
                        },
                        documentation: None,
                        oneof_variants: None,
                        field_behavior: vec![crate::google::api::FieldBehavior::Required],
                        is_sensitive: false,
                        resource_reference: Some(ResourceReference {
                            r#type: String::new(),
                            child_type: "example.io/Table".to_string(),
                        }),
                    },
                ],
                resource_descriptor: None,
                documentation: None,
            },
        );

        let metadata = CodeGenMetadata {
            messages,
            ..Default::default()
        };

        let catalog_svc = ServiceInfo {
            name: "CatalogService".to_string(),
            package: "example.v1".to_string(),
            documentation: None,
            methods: vec![
                MethodMetadata {
                    service_name: "CatalogService".to_string(),
                    method_name: "ListCatalogs".to_string(),
                    input_type: "ListCatalogsRequest".to_string(),
                    output_type: "ListCatalogsResponse".to_string(),
                    operation: None,
                    http_rule: HttpRule {
                        selector: "".to_string(),
                        pattern: Some(Pattern::Get("/catalogs".to_string())),
                        body: "".to_string(),
                        response_body: "".to_string(),
                        additional_bindings: vec![],
                    },
                    http_pattern: HttpPattern::parse("/catalogs"),
                    documentation: None,
                },
                MethodMetadata {
                    service_name: "CatalogService".to_string(),
                    method_name: "GetCatalog".to_string(),
                    input_type: "GetCatalogRequest".to_string(),
                    output_type: "Catalog".to_string(),
                    operation: None,
                    http_rule: HttpRule {
                        selector: "".to_string(),
                        pattern: Some(Pattern::Get("/catalogs/{name}".to_string())),
                        body: "".to_string(),
                        response_body: "".to_string(),
                        additional_bindings: vec![],
                    },
                    http_pattern: HttpPattern::parse("/catalogs/{name}"),
                    documentation: None,
                },
            ],
        };

        let schema_svc = ServiceInfo {
            name: "SchemaService".to_string(),
            package: "example.v1".to_string(),
            documentation: None,
            methods: vec![
                MethodMetadata {
                    service_name: "SchemaService".to_string(),
                    method_name: "ListSchemas".to_string(),
                    input_type: "ListSchemasRequest".to_string(),
                    output_type: "ListSchemasResponse".to_string(),
                    operation: None,
                    http_rule: HttpRule {
                        selector: "".to_string(),
                        pattern: Some(Pattern::Get("/schemas".to_string())),
                        body: "".to_string(),
                        response_body: "".to_string(),
                        additional_bindings: vec![],
                    },
                    http_pattern: HttpPattern::parse("/schemas"),
                    documentation: None,
                },
                MethodMetadata {
                    service_name: "SchemaService".to_string(),
                    method_name: "GetSchema".to_string(),
                    input_type: "GetSchemaRequest".to_string(),
                    output_type: "Schema".to_string(),
                    operation: None,
                    http_rule: HttpRule {
                        selector: "".to_string(),
                        pattern: Some(Pattern::Get("/schemas/{full_name}".to_string())),
                        body: "".to_string(),
                        response_body: "".to_string(),
                        additional_bindings: vec![],
                    },
                    http_pattern: HttpPattern::parse("/schemas/{full_name}"),
                    documentation: None,
                },
            ],
        };

        let table_svc = ServiceInfo {
            name: "TableService".to_string(),
            package: "example.v1".to_string(),
            documentation: None,
            methods: vec![
                MethodMetadata {
                    service_name: "TableService".to_string(),
                    method_name: "ListTables".to_string(),
                    input_type: "ListTablesRequest".to_string(),
                    output_type: "ListTablesResponse".to_string(),
                    operation: None,
                    http_rule: HttpRule {
                        selector: "".to_string(),
                        pattern: Some(Pattern::Get("/tables".to_string())),
                        body: "".to_string(),
                        response_body: "".to_string(),
                        additional_bindings: vec![],
                    },
                    http_pattern: HttpPattern::parse("/tables"),
                    documentation: None,
                },
                MethodMetadata {
                    service_name: "TableService".to_string(),
                    method_name: "GetTable".to_string(),
                    input_type: "GetTableRequest".to_string(),
                    output_type: "Table".to_string(),
                    operation: None,
                    http_rule: HttpRule {
                        selector: "".to_string(),
                        pattern: Some(Pattern::Get("/tables/{full_name}".to_string())),
                        body: "".to_string(),
                        response_body: "".to_string(),
                        additional_bindings: vec![],
                    },
                    http_pattern: HttpPattern::parse("/tables/{full_name}"),
                    documentation: None,
                },
            ],
        };

        (metadata, catalog_svc, schema_svc, table_svc)
    }

    /// Build service plans from three-level fixture metadata for hierarchy testing.
    fn make_plans_from_fixture() -> (Vec<ServicePlan>, CodeGenMetadata) {
        let (metadata, catalog_svc, schema_svc, table_svc) = make_three_level_metadata();
        let mut plans = Vec::new();
        for svc in &[&catalog_svc, &schema_svc, &table_svc] {
            let plan = analyze_service(&metadata, svc).unwrap();
            plans.push(plan);
        }
        (plans, metadata)
    }

    #[test]
    fn test_build_global_parent_map() {
        let (plans, metadata) = make_plans_from_fixture();
        let map = build_global_parent_map(&plans, &metadata);

        // Catalog → Schema (from ListSchemasRequest.catalog_name with child_type=Schema)
        assert_eq!(
            map.get(&(
                "example.io/Catalog".to_string(),
                "example.io/Schema".to_string()
            )),
            Some(&"catalog_name".to_string()),
            "Catalog→Schema mapping missing"
        );
        // Schema → Table (from ListTablesRequest.schema_name with child_type=Table)
        assert_eq!(
            map.get(&(
                "example.io/Schema".to_string(),
                "example.io/Table".to_string()
            )),
            Some(&"schema_name".to_string()),
            "Schema→Table mapping missing"
        );
        // Catalog → Table (flat-API artifact from ListTablesRequest.catalog_name)
        assert_eq!(
            map.get(&(
                "example.io/Catalog".to_string(),
                "example.io/Table".to_string()
            )),
            Some(&"catalog_name".to_string()),
            "Catalog→Table flat-API mapping missing"
        );
    }

    #[test]
    fn test_derive_ordered_hierarchy_three_levels() {
        let (plans, metadata) = make_plans_from_fixture();
        let map = build_global_parent_map(&plans, &metadata);
        let table_plan = plans
            .iter()
            .find(|p| p.service_name == "TableService")
            .unwrap();
        let hierarchy = derive_ordered_hierarchy(table_plan, &map, &metadata)
            .expect("no cycles in test fixture");

        assert_eq!(hierarchy.len(), 2, "expected 2 ancestors for Table");
        // Root-first: catalog_name (depth 0) before schema_name (depth 1)
        assert_eq!(hierarchy[0].parent_field_name, "catalog_name");
        assert_eq!(hierarchy[1].parent_field_name, "schema_name");
        assert_eq!(hierarchy[0].parent_resource_type, "example.io/Catalog");
        assert_eq!(hierarchy[1].parent_resource_type, "example.io/Schema");
        assert_eq!(hierarchy[0].parent_singular, Some("catalog".to_string()));
        assert_eq!(hierarchy[1].parent_singular, Some("schema".to_string()));
        // child_resource_type is the managed resource (Table) for all entries
        assert!(
            hierarchy
                .iter()
                .all(|h| h.child_resource_type == "example.io/Table")
        );
    }

    #[test]
    fn test_derive_ordered_hierarchy_two_levels() {
        let (plans, metadata) = make_plans_from_fixture();
        let map = build_global_parent_map(&plans, &metadata);
        let schema_plan = plans
            .iter()
            .find(|p| p.service_name == "SchemaService")
            .unwrap();
        let hierarchy = derive_ordered_hierarchy(schema_plan, &map, &metadata)
            .expect("no cycles in test fixture");

        assert_eq!(hierarchy.len(), 1);
        assert_eq!(hierarchy[0].parent_field_name, "catalog_name");
        assert_eq!(hierarchy[0].parent_resource_type, "example.io/Catalog");
    }

    #[test]
    fn test_derive_ordered_hierarchy_root_resource() {
        let (plans, metadata) = make_plans_from_fixture();
        let map = build_global_parent_map(&plans, &metadata);
        let catalog_plan = plans
            .iter()
            .find(|p| p.service_name == "CatalogService")
            .unwrap();
        let hierarchy = derive_ordered_hierarchy(catalog_plan, &map, &metadata)
            .expect("no cycles in test fixture");

        assert!(
            hierarchy.is_empty(),
            "root resource should have empty hierarchy"
        );
    }

    #[test]
    fn test_compute_depth_cycle_guard() {
        // A ↔ B mutual cycle: compute_depth should return Err, not panic or recurse infinitely.
        let mut map: GlobalParentMap = HashMap::new();
        map.insert(("A".to_string(), "B".to_string()), "a_name".to_string());
        map.insert(("B".to_string(), "A".to_string()), "b_name".to_string());

        assert!(compute_depth("A", &map, &mut HashSet::new()).is_err());
        assert!(compute_depth("B", &map, &mut HashSet::new()).is_err());
    }

    #[test]
    fn test_full_analyze_metadata_hierarchy_ordering() {
        let (metadata, catalog_svc, schema_svc, table_svc) = make_three_level_metadata();
        let mut services_map = HashMap::new();
        services_map.insert("CatalogService".to_string(), catalog_svc);
        services_map.insert("SchemaService".to_string(), schema_svc);
        services_map.insert("TableService".to_string(), table_svc);
        let full_metadata = CodeGenMetadata {
            messages: metadata.messages,
            services: services_map,
            ..Default::default()
        };

        let plan = analyze_metadata(&full_metadata).unwrap();
        let table_svc_plan = plan
            .services
            .iter()
            .find(|s| s.service_name == "TableService")
            .expect("TableService plan not found");

        assert_eq!(table_svc_plan.hierarchy.len(), 2);
        assert_eq!(
            table_svc_plan.hierarchy[0].parent_field_name,
            "catalog_name"
        );
        assert_eq!(table_svc_plan.hierarchy[1].parent_field_name, "schema_name");
    }

    #[test]
    fn resource_accessor_params_nested_and_flat() {
        let (metadata, catalog_svc, schema_svc, table_svc) = make_three_level_metadata();
        let mut services_map = HashMap::new();
        services_map.insert("CatalogService".to_string(), catalog_svc);
        services_map.insert("SchemaService".to_string(), schema_svc);
        services_map.insert("TableService".to_string(), table_svc);
        let full_metadata = CodeGenMetadata {
            messages: metadata.messages,
            services: services_map,
            ..Default::default()
        };

        let plan = analyze_metadata(&full_metadata).unwrap();
        let params = |name: &str| {
            plan.services
                .iter()
                .find(|s| s.service_name == name)
                .unwrap()
                .resource_accessor_params
                .clone()
        };

        // Nested resource (annotation-driven): ancestors root-first + own leaf.
        assert_eq!(
            params("TableService"),
            Some(vec![
                "catalog_name".to_string(),
                "schema_name".to_string(),
                "table_name".to_string(),
            ])
        );
        // One ancestor.
        assert_eq!(
            params("SchemaService"),
            Some(vec!["catalog_name".to_string(), "schema_name".to_string()])
        );
        // Flat top-level resource: just its own name.
        assert_eq!(
            params("CatalogService"),
            Some(vec!["catalog_name".to_string()])
        );
    }

    #[test]
    fn direct_children_via_accessor_param_prefix() {
        let (metadata, catalog_svc, schema_svc, table_svc) = make_three_level_metadata();
        let mut services_map = HashMap::new();
        services_map.insert("CatalogService".to_string(), catalog_svc);
        services_map.insert("SchemaService".to_string(), schema_svc);
        services_map.insert("TableService".to_string(), table_svc);
        let full_metadata = CodeGenMetadata {
            messages: metadata.messages,
            services: services_map,
            ..Default::default()
        };

        let plan = analyze_metadata(&full_metadata).unwrap();
        let children = |name: &str| -> Vec<String> {
            plan.services
                .iter()
                .find(|s| s.service_name == name)
                .unwrap()
                .direct_children
                .iter()
                .map(|c| c.child_singular.clone())
                .collect()
        };

        // Catalog → Schema (params extend by one); Catalog is NOT a direct parent of Table (extends
        // by two), so Table must not appear under Catalog.
        assert_eq!(children("CatalogService"), vec!["schema".to_string()]);
        // Schema → Table.
        assert_eq!(children("SchemaService"), vec!["table".to_string()]);
        // Table is a leaf: no children.
        assert!(children("TableService").is_empty());

        // The child link carries enough to construct the child clients.
        let catalog = plan
            .services
            .iter()
            .find(|s| s.service_name == "CatalogService")
            .unwrap();
        let schema_link = &catalog.direct_children[0];
        // base_path comes from the child service name (here `SchemaService` → `schema`).
        let schema_base_path = plan
            .services
            .iter()
            .find(|s| s.service_name == "SchemaService")
            .unwrap()
            .base_path
            .clone();
        assert_eq!(schema_link.child_base_path, schema_base_path);
        assert_eq!(
            schema_link.child_accessor_params,
            vec!["catalog_name".to_string(), "schema_name".to_string()]
        );
    }

    // The pagination-leak regression (a flat resource's List `page_token`/`max_results` no longer
    // bleed into its accessor params) is covered end-to-end by the golden snapshot test
    // (`tests/golden_integration.rs`), whose `example.bin` ListCatalogs request carries those very
    // fields. `is_standard_list_field` is unit-tested below.

    #[test]
    fn standard_list_fields_recognized() {
        for f in [
            "page_token",
            "page_size",
            "max_results",
            "order_by",
            "filter",
        ] {
            assert!(
                is_standard_list_field(f),
                "{f} should be a standard list field"
            );
        }
        for f in ["name", "catalog_name", "schema_name"] {
            assert!(
                !is_standard_list_field(f),
                "{f} should NOT be a standard list field"
            );
        }
    }
}