nika 0.35.4

Semantic YAML workflow engine for AI tasks - DAG execution, MCP integration, multi-provider LLM support
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
//! Completion Handler
//!
//! Schema-aware completions for `.nika.yaml` workflow files.
//!
//! ## Phase 2 Architecture
//!
//! The completion handler can use AstIndex for semantic completions:
//! - Use `compute_completions_with_ast` when AstIndex is available
//! - Falls back to text-based task extraction for task references
//! - AstIndex provides accurate task names and MCP servers

#[cfg(feature = "lsp")]
use tower_lsp_server::ls_types::*;

#[cfg(feature = "lsp")]
pub use tower_lsp_server::ls_types::Uri;

#[cfg(feature = "lsp")]
use super::super::ast_index::AstIndex;
#[cfg(feature = "lsp")]
use super::super::utils::extract_task_ids;

/// Completion context based on cursor position
#[cfg(feature = "lsp")]
#[derive(Debug, Clone, PartialEq)]
pub enum CompletionContext {
    /// Top-level workflow keys (schema, tasks, mcp, etc.)
    TopLevel,
    /// Inside a task definition
    TaskField,
    /// After a verb keyword (infer:, exec:, etc.)
    VerbValue(String),
    /// Inside a with: block (binding references)
    WithBinding,
    /// Inside mcp: block
    McpServer,
    /// Inside a template {{ }}
    Template,
    /// Unknown context
    Unknown,
}

/// Compute completions using AstIndex for semantic context
///
/// Uses the parsed AST for accurate task names and MCP server references,
/// while still using text-based context analysis for cursor position.
///
/// # Arguments
/// * `ast_index` - The AST cache for semantic lookups
/// * `uri` - Document URI for AST lookup
/// * `text` - Document text for context analysis
/// * `position` - Cursor position
#[cfg(feature = "lsp")]
pub fn compute_completions_with_ast(
    ast_index: &AstIndex,
    uri: &Uri,
    text: &str,
    position: Position,
) -> Vec<CompletionItem> {
    let context = analyze_completion_context(text, position);

    match context {
        CompletionContext::TopLevel => top_level_completions(),
        CompletionContext::TaskField => task_field_completions(),
        CompletionContext::VerbValue(verb) => {
            verb_value_completions_with_ast(&verb, ast_index, uri)
        }
        CompletionContext::WithBinding => binding_completions_with_ast(ast_index, uri, text),
        CompletionContext::McpServer => mcp_server_completions(),
        CompletionContext::Template => template_completions_with_ast(ast_index, uri, text),
        CompletionContext::Unknown => vec![],
    }
}

/// Compute completions based on cursor position
#[cfg(feature = "lsp")]
pub fn compute_completions(text: &str, position: Position) -> Vec<CompletionItem> {
    let context = analyze_completion_context(text, position);

    match context {
        CompletionContext::TopLevel => top_level_completions(),
        CompletionContext::TaskField => task_field_completions(),
        CompletionContext::VerbValue(verb) => verb_value_completions(&verb),
        CompletionContext::WithBinding => binding_completions(text),
        CompletionContext::McpServer => mcp_server_completions(),
        CompletionContext::Template => template_completions(text),
        CompletionContext::Unknown => vec![],
    }
}

/// Analyze the text to determine completion context
#[cfg(feature = "lsp")]
fn analyze_completion_context(text: &str, position: Position) -> CompletionContext {
    let lines: Vec<&str> = text.lines().collect();

    if position.line as usize >= lines.len() {
        return CompletionContext::TopLevel;
    }

    let current_line = lines[position.line as usize];
    // Ensure we don't slice past the line length or into a multi-byte char
    let char_pos = (position.character as usize).min(current_line.len());
    // Find valid UTF-8 boundary at or before char_pos
    let prefix_end = current_line
        .char_indices()
        .take_while(|(i, _)| *i < char_pos)
        .last()
        .map(|(i, c)| i + c.len_utf8())
        .unwrap_or(0);
    let prefix = &current_line[..prefix_end];

    // Check for template context {{ }}
    if prefix.contains("{{") && !prefix.contains("}}") {
        return CompletionContext::Template;
    }

    // Check indentation level
    let indent = current_line.len() - current_line.trim_start().len();

    // Top level (no indentation)
    if indent == 0 {
        return CompletionContext::TopLevel;
    }

    // Look for context in preceding lines
    for i in (0..position.line as usize).rev() {
        let line = lines[i].trim();

        // Inside tasks array
        if line.starts_with("- id:") || line.starts_with("-id:") {
            let task_indent = lines[i].len() - lines[i].trim_start().len();
            if indent > task_indent {
                // Check for specific contexts
                if prefix.trim().starts_with("with") {
                    return CompletionContext::WithBinding;
                }
                if line_contains_verb(prefix) {
                    return CompletionContext::VerbValue(extract_verb(prefix));
                }
                return CompletionContext::TaskField;
            }
        }

        // Inside mcp block
        if line == "mcp:" {
            return CompletionContext::McpServer;
        }

        // At tasks level
        if line == "tasks:" && indent == 2 && prefix.trim().starts_with('-') {
            return CompletionContext::TaskField;
        }
    }

    CompletionContext::Unknown
}

#[cfg(feature = "lsp")]
fn line_contains_verb(line: &str) -> bool {
    let trimmed = line.trim();
    trimmed.starts_with("infer:")
        || trimmed.starts_with("exec:")
        || trimmed.starts_with("fetch:")
        || trimmed.starts_with("invoke:")
        || trimmed.starts_with("agent:")
}

#[cfg(feature = "lsp")]
fn extract_verb(line: &str) -> String {
    let trimmed = line.trim();
    for verb in ["infer", "exec", "fetch", "invoke", "agent"] {
        if trimmed.starts_with(&format!("{}:", verb)) {
            return verb.to_string();
        }
    }
    String::new()
}

/// Top-level workflow completions
#[cfg(feature = "lsp")]
fn top_level_completions() -> Vec<CompletionItem> {
    vec![
        CompletionItem {
            label: "schema".to_string(),
            kind: Some(CompletionItemKind::KEYWORD),
            insert_text: Some("schema: nika/workflow@0.12".to_string()),
            documentation: Some(Documentation::String(
                "Required. Schema version for this workflow.".to_string(),
            )),
            ..Default::default()
        },
        CompletionItem {
            label: "workflow".to_string(),
            kind: Some(CompletionItemKind::KEYWORD),
            insert_text: Some("workflow: ${1:workflow-name}".to_string()),
            insert_text_format: Some(InsertTextFormat::SNIPPET),
            documentation: Some(Documentation::String(
                "Optional. Workflow name/identifier.".to_string(),
            )),
            ..Default::default()
        },
        CompletionItem {
            label: "tasks".to_string(),
            kind: Some(CompletionItemKind::KEYWORD),
            insert_text: Some(
                "tasks:\n  - id: ${1:task-id}\n    ${2:infer}: ${3:prompt}".to_string(),
            ),
            insert_text_format: Some(InsertTextFormat::SNIPPET),
            documentation: Some(Documentation::String(
                "Required. List of tasks to execute.".to_string(),
            )),
            ..Default::default()
        },
        CompletionItem {
            label: "mcp".to_string(),
            kind: Some(CompletionItemKind::KEYWORD),
            insert_text: Some(
                "mcp:\n  ${1:server-name}:\n    command: ${2:command}\n    args: [${3}]"
                    .to_string(),
            ),
            insert_text_format: Some(InsertTextFormat::SNIPPET),
            documentation: Some(Documentation::String(
                "Optional. MCP server configurations.".to_string(),
            )),
            ..Default::default()
        },
        CompletionItem {
            label: "context".to_string(),
            kind: Some(CompletionItemKind::KEYWORD),
            insert_text: Some(
                "context:\n  files:\n    ${1:alias}: ${2:./path/to/file}".to_string(),
            ),
            insert_text_format: Some(InsertTextFormat::SNIPPET),
            documentation: Some(Documentation::String(
                "Optional. Load files at workflow start.".to_string(),
            )),
            ..Default::default()
        },
        CompletionItem {
            label: "include".to_string(),
            kind: Some(CompletionItemKind::KEYWORD),
            insert_text: Some(
                "include:\n  - path: ${1:./partial.nika.yaml}\n    prefix: ${2:partial_}"
                    .to_string(),
            ),
            insert_text_format: Some(InsertTextFormat::SNIPPET),
            documentation: Some(Documentation::String(
                "Optional. Include tasks from other workflows.".to_string(),
            )),
            ..Default::default()
        },
    ]
}

/// Task field completions (inside a task definition)
#[cfg(feature = "lsp")]
fn task_field_completions() -> Vec<CompletionItem> {
    let mut items = vec![
        CompletionItem {
            label: "id".to_string(),
            kind: Some(CompletionItemKind::PROPERTY),
            insert_text: Some("id: ${1:task-id}".to_string()),
            insert_text_format: Some(InsertTextFormat::SNIPPET),
            documentation: Some(Documentation::String(
                "Required. Unique task identifier.".to_string(),
            )),
            ..Default::default()
        },
        CompletionItem {
            label: "with".to_string(),
            kind: Some(CompletionItemKind::PROPERTY),
            insert_text: Some("with:\n  ${1:alias}: ${2:task-id}".to_string()),
            insert_text_format: Some(InsertTextFormat::SNIPPET),
            documentation: Some(Documentation::String(
                "Bind outputs from previous tasks.".to_string(),
            )),
            ..Default::default()
        },
        CompletionItem {
            label: "for_each".to_string(),
            kind: Some(CompletionItemKind::PROPERTY),
            insert_text: Some("for_each: [${1}]\nas: ${2:item}\nconcurrency: ${3:3}".to_string()),
            insert_text_format: Some(InsertTextFormat::SNIPPET),
            documentation: Some(Documentation::String(
                "Parallel iteration over an array.".to_string(),
            )),
            ..Default::default()
        },
        CompletionItem {
            label: "retry".to_string(),
            kind: Some(CompletionItemKind::PROPERTY),
            insert_text: Some("retry:\n  max_attempts: ${1:3}\n  delay: ${2:1s}".to_string()),
            insert_text_format: Some(InsertTextFormat::SNIPPET),
            documentation: Some(Documentation::String(
                "Retry configuration for failed tasks.".to_string(),
            )),
            ..Default::default()
        },
        CompletionItem {
            label: "timeout".to_string(),
            kind: Some(CompletionItemKind::PROPERTY),
            insert_text: Some("timeout: ${1:30s}".to_string()),
            insert_text_format: Some(InsertTextFormat::SNIPPET),
            documentation: Some(Documentation::String(
                "Maximum execution time for this task.".to_string(),
            )),
            ..Default::default()
        },
    ];

    // Task-level overrides
    items.push(CompletionItem {
        label: "provider".to_string(),
        kind: Some(CompletionItemKind::PROPERTY),
        insert_text: Some(
            "provider: ${1|claude,openai,mistral,groq,deepseek,gemini,xai|}".to_string(),
        ),
        insert_text_format: Some(InsertTextFormat::SNIPPET),
        documentation: Some(Documentation::String(
            "Override LLM provider for this task.".to_string(),
        )),
        ..Default::default()
    });
    items.push(CompletionItem {
        label: "model".to_string(),
        kind: Some(CompletionItemKind::PROPERTY),
        insert_text: Some("model: ${1}".to_string()),
        insert_text_format: Some(InsertTextFormat::SNIPPET),
        documentation: Some(Documentation::String(
            "Override model for this task.".to_string(),
        )),
        ..Default::default()
    });
    items.push(CompletionItem {
        label: "depends_on".to_string(),
        kind: Some(CompletionItemKind::PROPERTY),
        insert_text: Some("depends_on: [${1}]".to_string()),
        insert_text_format: Some(InsertTextFormat::SNIPPET),
        documentation: Some(Documentation::String(
            "Task IDs that must complete before this task runs.".to_string(),
        )),
        ..Default::default()
    });
    items.push(CompletionItem {
        label: "description".to_string(),
        kind: Some(CompletionItemKind::PROPERTY),
        insert_text: Some("description: \"${1}\"".to_string()),
        insert_text_format: Some(InsertTextFormat::SNIPPET),
        ..Default::default()
    });
    items.push(CompletionItem {
        label: "artifact".to_string(),
        kind: Some(CompletionItemKind::PROPERTY),
        insert_text: Some(
            "artifact:\n  path: ${1:output.txt}\n  format: ${2|text,json|}".to_string(),
        ),
        insert_text_format: Some(InsertTextFormat::SNIPPET),
        documentation: Some(Documentation::String(
            "Persist task output to a file.".to_string(),
        )),
        ..Default::default()
    });
    items.push(CompletionItem {
        label: "log".to_string(),
        kind: Some(CompletionItemKind::PROPERTY),
        insert_text: Some("log:\n  level: ${1|debug,info,warn|}".to_string()),
        insert_text_format: Some(InsertTextFormat::SNIPPET),
        documentation: Some(Documentation::String(
            "Task-level log configuration override.".to_string(),
        )),
        ..Default::default()
    });
    items.push(CompletionItem {
        label: "structured".to_string(),
        kind: Some(CompletionItemKind::PROPERTY),
        insert_text: Some("structured:\n  schema:\n    type: object\n    properties:\n      ${1:field}:\n        type: ${2:string}\n    required: [${1:field}]".to_string()),
        insert_text_format: Some(InsertTextFormat::SNIPPET),
        documentation: Some(Documentation::String(
            "Enforce JSON schema on task output.".to_string(),
        )),
        ..Default::default()
    });

    // Add verb completions
    items.extend(verb_completions());

    items
}

/// Completions for the 5 semantic verbs
///
/// Each verb inserts a multi-line scaffold with tab stops so the user
/// lands directly on the first meaningful field after accepting the
/// completion.
#[cfg(feature = "lsp")]
fn verb_completions() -> Vec<CompletionItem> {
    vec![
        CompletionItem {
            label: "infer".to_string(),
            kind: Some(CompletionItemKind::KEYWORD),
            insert_text: Some("infer:\n  prompt: ${1:your prompt here}\n  ${0}".to_string()),
            insert_text_format: Some(InsertTextFormat::SNIPPET),
            documentation: Some(Documentation::String(
                "LLM text generation. Shorthand accepts a string.".to_string(),
            )),
            detail: Some("Verb".to_string()),
            ..Default::default()
        },
        CompletionItem {
            label: "exec".to_string(),
            kind: Some(CompletionItemKind::KEYWORD),
            insert_text: Some("exec: ${1:command}\n${0}".to_string()),
            insert_text_format: Some(InsertTextFormat::SNIPPET),
            documentation: Some(Documentation::String(
                "Shell command execution. Defaults to shell: false for security.".to_string(),
            )),
            detail: Some("Verb".to_string()),
            ..Default::default()
        },
        CompletionItem {
            label: "fetch".to_string(),
            kind: Some(CompletionItemKind::KEYWORD),
            insert_text: Some("fetch:\n  url: ${1:https://}\n  ${0}".to_string()),
            insert_text_format: Some(InsertTextFormat::SNIPPET),
            documentation: Some(Documentation::String("HTTP request.".to_string())),
            detail: Some("Verb".to_string()),
            ..Default::default()
        },
        CompletionItem {
            label: "invoke".to_string(),
            kind: Some(CompletionItemKind::KEYWORD),
            insert_text: Some(
                "invoke:\n  tool: ${1:nika:tool}\n  params:\n    ${2:key}: ${3:value}\n${0}"
                    .to_string(),
            ),
            insert_text_format: Some(InsertTextFormat::SNIPPET),
            documentation: Some(Documentation::String("MCP tool invocation.".to_string())),
            detail: Some("Verb".to_string()),
            ..Default::default()
        },
        CompletionItem {
            label: "agent".to_string(),
            kind: Some(CompletionItemKind::KEYWORD),
            insert_text: Some(
                "agent:\n  prompt: ${1:agent goal}\n  mcp: [${2}]\n  max_turns: ${3:10}\n${0}"
                    .to_string(),
            ),
            insert_text_format: Some(InsertTextFormat::SNIPPET),
            documentation: Some(Documentation::String(
                "Multi-turn agentic loop with tool calling.".to_string(),
            )),
            detail: Some("Verb".to_string()),
            ..Default::default()
        },
    ]
}

/// Completions for verb values (after infer:, exec:, etc.)
#[cfg(feature = "lsp")]
fn verb_value_completions(verb: &str) -> Vec<CompletionItem> {
    match verb {
        "infer" => vec![
            CompletionItem {
                label: "prompt".to_string(),
                kind: Some(CompletionItemKind::PROPERTY),
                insert_text: Some("prompt: ${1}".to_string()),
                insert_text_format: Some(InsertTextFormat::SNIPPET),
                ..Default::default()
            },
            CompletionItem {
                label: "model".to_string(),
                kind: Some(CompletionItemKind::PROPERTY),
                insert_text: Some("model: ${1:claude-sonnet-4-6}".to_string()),
                insert_text_format: Some(InsertTextFormat::SNIPPET),
                ..Default::default()
            },
            CompletionItem {
                label: "temperature".to_string(),
                kind: Some(CompletionItemKind::PROPERTY),
                insert_text: Some("temperature: ${1:0.7}".to_string()),
                insert_text_format: Some(InsertTextFormat::SNIPPET),
                ..Default::default()
            },
            CompletionItem {
                label: "system".to_string(),
                kind: Some(CompletionItemKind::PROPERTY),
                insert_text: Some("system: ${1:You are a helpful assistant.}".to_string()),
                insert_text_format: Some(InsertTextFormat::SNIPPET),
                ..Default::default()
            },
            CompletionItem {
                label: "max_tokens".to_string(),
                kind: Some(CompletionItemKind::PROPERTY),
                insert_text: Some("max_tokens: ${1:1000}".to_string()),
                insert_text_format: Some(InsertTextFormat::SNIPPET),
                ..Default::default()
            },
        ],
        "exec" => vec![
            CompletionItem {
                label: "command".to_string(),
                kind: Some(CompletionItemKind::PROPERTY),
                insert_text: Some("command: ${1}".to_string()),
                insert_text_format: Some(InsertTextFormat::SNIPPET),
                ..Default::default()
            },
            CompletionItem {
                label: "shell".to_string(),
                kind: Some(CompletionItemKind::PROPERTY),
                insert_text: Some("shell: ${1|true,false|}".to_string()),
                insert_text_format: Some(InsertTextFormat::SNIPPET),
                documentation: Some(Documentation::String(
                    "Enable shell mode for pipes/redirects. Default: false (secure).".to_string(),
                )),
                ..Default::default()
            },
        ],
        "fetch" => vec![
            CompletionItem {
                label: "url".to_string(),
                kind: Some(CompletionItemKind::PROPERTY),
                insert_text: Some("url: ${1:https://}".to_string()),
                insert_text_format: Some(InsertTextFormat::SNIPPET),
                documentation: Some(Documentation::String("Required. Request URL.".to_string())),
                ..Default::default()
            },
            CompletionItem {
                label: "method".to_string(),
                kind: Some(CompletionItemKind::PROPERTY),
                insert_text: Some("method: ${1|GET,POST,PUT,DELETE,PATCH|}".to_string()),
                insert_text_format: Some(InsertTextFormat::SNIPPET),
                documentation: Some(Documentation::String(
                    "HTTP method. Default: GET.".to_string(),
                )),
                ..Default::default()
            },
            CompletionItem {
                label: "headers".to_string(),
                kind: Some(CompletionItemKind::PROPERTY),
                insert_text: Some(
                    "headers:\n  ${1:Content-Type}: ${2:application/json}".to_string(),
                ),
                insert_text_format: Some(InsertTextFormat::SNIPPET),
                documentation: Some(Documentation::String("HTTP request headers.".to_string())),
                ..Default::default()
            },
            CompletionItem {
                label: "body".to_string(),
                kind: Some(CompletionItemKind::PROPERTY),
                insert_text: Some("body: ${1}".to_string()),
                insert_text_format: Some(InsertTextFormat::SNIPPET),
                documentation: Some(Documentation::String(
                    "Request body (string or object).".to_string(),
                )),
                ..Default::default()
            },
            CompletionItem {
                label: "retry".to_string(),
                kind: Some(CompletionItemKind::PROPERTY),
                insert_text: Some("retry:\n  max_attempts: ${1:3}\n  delay: ${2:1s}".to_string()),
                insert_text_format: Some(InsertTextFormat::SNIPPET),
                documentation: Some(Documentation::String(
                    "Retry configuration for failed requests.".to_string(),
                )),
                ..Default::default()
            },
        ],
        "invoke" => vec![
            CompletionItem {
                label: "mcp".to_string(),
                kind: Some(CompletionItemKind::PROPERTY),
                insert_text: Some("mcp: ${1:server}".to_string()),
                insert_text_format: Some(InsertTextFormat::SNIPPET),
                documentation: Some(Documentation::String(
                    "Required. MCP server name (from mcp: block).".to_string(),
                )),
                ..Default::default()
            },
            CompletionItem {
                label: "tool".to_string(),
                kind: Some(CompletionItemKind::PROPERTY),
                insert_text: Some("tool: ${1:tool-name}".to_string()),
                insert_text_format: Some(InsertTextFormat::SNIPPET),
                documentation: Some(Documentation::String(
                    "Required. MCP tool name to invoke.".to_string(),
                )),
                ..Default::default()
            },
            CompletionItem {
                label: "params".to_string(),
                kind: Some(CompletionItemKind::PROPERTY),
                insert_text: Some("params:\n  ${1:key}: ${2:value}".to_string()),
                insert_text_format: Some(InsertTextFormat::SNIPPET),
                documentation: Some(Documentation::String(
                    "Tool parameters (key-value pairs).".to_string(),
                )),
                ..Default::default()
            },
            CompletionItem {
                label: "resource".to_string(),
                kind: Some(CompletionItemKind::PROPERTY),
                insert_text: Some("resource: ${1:resource-uri}".to_string()),
                insert_text_format: Some(InsertTextFormat::SNIPPET),
                documentation: Some(Documentation::String(
                    "MCP resource URI to read (alternative to tool).".to_string(),
                )),
                ..Default::default()
            },
        ],
        "agent" => vec![
            CompletionItem {
                label: "prompt".to_string(),
                kind: Some(CompletionItemKind::PROPERTY),
                insert_text: Some("prompt: ${1}".to_string()),
                insert_text_format: Some(InsertTextFormat::SNIPPET),
                ..Default::default()
            },
            CompletionItem {
                label: "mcp".to_string(),
                kind: Some(CompletionItemKind::PROPERTY),
                insert_text: Some("mcp: [${1}]".to_string()),
                insert_text_format: Some(InsertTextFormat::SNIPPET),
                ..Default::default()
            },
            CompletionItem {
                label: "max_turns".to_string(),
                kind: Some(CompletionItemKind::PROPERTY),
                insert_text: Some("max_turns: ${1:10}".to_string()),
                insert_text_format: Some(InsertTextFormat::SNIPPET),
                ..Default::default()
            },
            CompletionItem {
                label: "depth_limit".to_string(),
                kind: Some(CompletionItemKind::PROPERTY),
                insert_text: Some("depth_limit: ${1:3}".to_string()),
                insert_text_format: Some(InsertTextFormat::SNIPPET),
                documentation: Some(Documentation::String(
                    "Max spawn_agent recursion depth. Default: 3.".to_string(),
                )),
                ..Default::default()
            },
            CompletionItem {
                label: "extended_thinking".to_string(),
                kind: Some(CompletionItemKind::PROPERTY),
                insert_text: Some(
                    "extended_thinking: true\nthinking_budget: ${1:8192}".to_string(),
                ),
                insert_text_format: Some(InsertTextFormat::SNIPPET),
                documentation: Some(Documentation::String(
                    "Enable Claude's extended thinking mode.".to_string(),
                )),
                ..Default::default()
            },
            CompletionItem {
                label: "provider".to_string(),
                kind: Some(CompletionItemKind::PROPERTY),
                insert_text: Some(
                    "provider: ${1|claude,openai,mistral,groq,deepseek,gemini,xai|}".to_string(),
                ),
                insert_text_format: Some(InsertTextFormat::SNIPPET),
                documentation: Some(Documentation::String(
                    "LLM provider override for this agent.".to_string(),
                )),
                ..Default::default()
            },
            CompletionItem {
                label: "model".to_string(),
                kind: Some(CompletionItemKind::PROPERTY),
                insert_text: Some("model: ${1}".to_string()),
                insert_text_format: Some(InsertTextFormat::SNIPPET),
                documentation: Some(Documentation::String(
                    "Model override (e.g., gpt-4.1-mini, grok-3-fast).".to_string(),
                )),
                ..Default::default()
            },
            CompletionItem {
                label: "system".to_string(),
                kind: Some(CompletionItemKind::PROPERTY),
                insert_text: Some("system: |\n  ${1}".to_string()),
                insert_text_format: Some(InsertTextFormat::SNIPPET),
                documentation: Some(Documentation::String(
                    "System prompt defining the agent's persona.".to_string(),
                )),
                ..Default::default()
            },
            CompletionItem {
                label: "tools".to_string(),
                kind: Some(CompletionItemKind::PROPERTY),
                insert_text: Some(
                    "tools: [${1|builtin,nika:read,nika:write,nika:edit|}]".to_string(),
                ),
                insert_text_format: Some(InsertTextFormat::SNIPPET),
                documentation: Some(Documentation::String(
                    "Builtin tools: [builtin] for all, or specific nika:* tools.".to_string(),
                )),
                ..Default::default()
            },
        ],
        _ => vec![],
    }
}

/// Completions for with: block (binding references)
#[cfg(feature = "lsp")]
fn binding_completions(text: &str) -> Vec<CompletionItem> {
    // Extract task IDs from the text
    let task_ids = extract_task_ids(text);

    task_ids
        .into_iter()
        .map(|id| CompletionItem {
            label: id.clone(),
            kind: Some(CompletionItemKind::REFERENCE),
            insert_text: Some(id.clone()),
            documentation: Some(Documentation::String(format!(
                "Reference output from task '{}'",
                id
            ))),
            ..Default::default()
        })
        .collect()
}

/// Completions for MCP server configuration
#[cfg(feature = "lsp")]
fn mcp_server_completions() -> Vec<CompletionItem> {
    vec![
        CompletionItem {
            label: "command".to_string(),
            kind: Some(CompletionItemKind::PROPERTY),
            insert_text: Some("command: ${1:npx}".to_string()),
            insert_text_format: Some(InsertTextFormat::SNIPPET),
            documentation: Some(Documentation::String(
                "Command to start the MCP server.".to_string(),
            )),
            ..Default::default()
        },
        CompletionItem {
            label: "args".to_string(),
            kind: Some(CompletionItemKind::PROPERTY),
            insert_text: Some("args: [${1}]".to_string()),
            insert_text_format: Some(InsertTextFormat::SNIPPET),
            documentation: Some(Documentation::String(
                "Arguments to pass to the command.".to_string(),
            )),
            ..Default::default()
        },
        CompletionItem {
            label: "env".to_string(),
            kind: Some(CompletionItemKind::PROPERTY),
            insert_text: Some("env:\n  ${1:KEY}: ${2:value}".to_string()),
            insert_text_format: Some(InsertTextFormat::SNIPPET),
            documentation: Some(Documentation::String(
                "Environment variables for the MCP server.".to_string(),
            )),
            ..Default::default()
        },
    ]
}

/// Completions inside templates {{ }}
#[cfg(feature = "lsp")]
fn template_completions(text: &str) -> Vec<CompletionItem> {
    let mut items = vec![
        CompletionItem {
            label: "with.".to_string(),
            kind: Some(CompletionItemKind::VARIABLE),
            insert_text: Some("with.${1:alias}".to_string()),
            insert_text_format: Some(InsertTextFormat::SNIPPET),
            documentation: Some(Documentation::String(
                "Reference bound task output.".to_string(),
            )),
            ..Default::default()
        },
        CompletionItem {
            label: "context.files.".to_string(),
            kind: Some(CompletionItemKind::VARIABLE),
            insert_text: Some("context.files.${1:alias}".to_string()),
            insert_text_format: Some(InsertTextFormat::SNIPPET),
            documentation: Some(Documentation::String(
                "Reference loaded context file.".to_string(),
            )),
            ..Default::default()
        },
        CompletionItem {
            label: "inputs.".to_string(),
            kind: Some(CompletionItemKind::VARIABLE),
            insert_text: Some("inputs.${1:name}".to_string()),
            insert_text_format: Some(InsertTextFormat::SNIPPET),
            documentation: Some(Documentation::String(
                "Reference workflow input parameter.".to_string(),
            )),
            ..Default::default()
        },
    ];

    // Add task IDs for $task shorthand
    for id in extract_task_ids(text) {
        items.push(CompletionItem {
            label: format!("${}", id),
            kind: Some(CompletionItemKind::REFERENCE),
            insert_text: Some(format!("${}", id)),
            documentation: Some(Documentation::String(format!(
                "Implicit output from task '{}' (shorthand)",
                id
            ))),
            ..Default::default()
        });
    }

    items
}

/// Verb value completions using AstIndex for MCP server names
///
/// For `invoke:` and `agent:` verbs, provides MCP server names from the AST.
#[cfg(feature = "lsp")]
fn verb_value_completions_with_ast(
    verb: &str,
    ast_index: &AstIndex,
    uri: &Uri,
) -> Vec<CompletionItem> {
    // Get base verb completions
    let mut items = verb_value_completions(verb);

    // For invoke and agent verbs, add MCP server completions from AST
    if verb == "invoke" || verb == "agent" {
        let servers = ast_index.get_mcp_server_names(uri);
        for server in servers {
            items.push(CompletionItem {
                label: server.clone(),
                kind: Some(CompletionItemKind::MODULE),
                insert_text: Some(server.clone()),
                documentation: Some(Documentation::String(format!(
                    "MCP server '{}' (from workflow)",
                    server
                ))),
                ..Default::default()
            });
        }
    }

    // For infer and agent verbs, add model names from the catalog
    if verb == "infer" || verb == "agent" {
        items.extend(model_catalog_completions());
    }

    items
}

/// Generate completion items from the ModelCatalog.
///
/// Each known cloud model becomes a completion item with provider, tier,
/// and pricing information. Items are grouped by provider via sort_text.
#[cfg(feature = "lsp")]
fn model_catalog_completions() -> Vec<CompletionItem> {
    use crate::lsp::model_intel::{LifecycleStatus, MODEL_CATALOG};

    MODEL_CATALOG
        .iter()
        .map(|info| {
            let deprecated_note = match &info.lifecycle {
                LifecycleStatus::Deprecated { replacement, .. } => {
                    format!(" (deprecated, use {})", replacement)
                }
                LifecycleStatus::Preview => " (preview)".to_string(),
                LifecycleStatus::Active => String::new(),
            };

            CompletionItem {
                label: info.id.to_string(),
                kind: Some(CompletionItemKind::VALUE),
                detail: Some(format!(
                    "{} -- {} | {}{}",
                    info.display_name,
                    info.provider.name(),
                    info.tier.label(),
                    deprecated_note,
                )),
                documentation: Some(Documentation::String(info.description.to_string())),
                sort_text: Some(format!("model_{}_{}", info.provider.name(), info.id,)),
                deprecated: Some(matches!(info.lifecycle, LifecycleStatus::Deprecated { .. })),
                ..Default::default()
            }
        })
        .collect()
}

/// Binding completions using AstIndex for accurate task names
///
/// Uses the parsed AST to get task IDs instead of text extraction.
#[cfg(feature = "lsp")]
fn binding_completions_with_ast(
    ast_index: &AstIndex,
    uri: &Uri,
    text: &str,
) -> Vec<CompletionItem> {
    // Try to get task names from AST first
    let task_ids = ast_index.get_task_names(uri);

    // If AST has tasks, use them; otherwise fall back to text-based extraction
    if !task_ids.is_empty() {
        return task_ids
            .into_iter()
            .map(|id| CompletionItem {
                label: id.clone(),
                kind: Some(CompletionItemKind::REFERENCE),
                insert_text: Some(id.clone()),
                documentation: Some(Documentation::String(format!(
                    "Reference output from task '{}' (from AST)",
                    id
                ))),
                ..Default::default()
            })
            .collect();
    }

    // Fall back to text-based extraction
    binding_completions(text)
}

/// Template completions using AstIndex for task names and context files
///
/// Provides accurate task IDs and context file names from the parsed AST.
#[cfg(feature = "lsp")]
fn template_completions_with_ast(
    ast_index: &AstIndex,
    uri: &Uri,
    text: &str,
) -> Vec<CompletionItem> {
    let mut items = vec![
        CompletionItem {
            label: "with.".to_string(),
            kind: Some(CompletionItemKind::VARIABLE),
            insert_text: Some("with.${1:alias}".to_string()),
            insert_text_format: Some(InsertTextFormat::SNIPPET),
            documentation: Some(Documentation::String(
                "Reference bound task output.".to_string(),
            )),
            ..Default::default()
        },
        CompletionItem {
            label: "context.files.".to_string(),
            kind: Some(CompletionItemKind::VARIABLE),
            insert_text: Some("context.files.${1:alias}".to_string()),
            insert_text_format: Some(InsertTextFormat::SNIPPET),
            documentation: Some(Documentation::String(
                "Reference loaded context file.".to_string(),
            )),
            ..Default::default()
        },
        CompletionItem {
            label: "inputs.".to_string(),
            kind: Some(CompletionItemKind::VARIABLE),
            insert_text: Some("inputs.${1:name}".to_string()),
            insert_text_format: Some(InsertTextFormat::SNIPPET),
            documentation: Some(Documentation::String(
                "Reference workflow input parameter.".to_string(),
            )),
            ..Default::default()
        },
    ];

    // Get task IDs from AST, fall back to text extraction if empty
    let task_ids = {
        let ast_task_ids = ast_index.get_task_names(uri);
        if ast_task_ids.is_empty() {
            extract_task_ids(text)
        } else {
            ast_task_ids
        }
    };

    // Add task IDs for $task shorthand
    for id in task_ids {
        items.push(CompletionItem {
            label: format!("${}", id),
            kind: Some(CompletionItemKind::REFERENCE),
            insert_text: Some(format!("${}", id)),
            documentation: Some(Documentation::String(format!(
                "Implicit output from task '{}' (shorthand)",
                id
            ))),
            ..Default::default()
        });
    }

    // Add context file names from AST if available
    let context_files = ast_index.get_context_file_names(uri);
    for file_name in context_files {
        items.push(CompletionItem {
            label: format!("context.files.{}", file_name),
            kind: Some(CompletionItemKind::VARIABLE),
            insert_text: Some(format!("context.files.{}", file_name)),
            documentation: Some(Documentation::String(format!(
                "Context file '{}' (from AST)",
                file_name
            ))),
            ..Default::default()
        });
    }

    items
}

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

    #[test]
    #[cfg(feature = "lsp")]
    fn test_extract_task_ids() {
        let text = r#"
tasks:
  - id: step1
    infer: "Hello"
  - id: step2
    exec: "echo hi"
"#;
        let ids = extract_task_ids(text);
        assert_eq!(ids, vec!["step1", "step2"]);
    }

    #[test]
    #[cfg(feature = "lsp")]
    fn test_top_level_completions() {
        let items = top_level_completions();
        assert!(items.iter().any(|i| i.label == "schema"));
        assert!(items.iter().any(|i| i.label == "tasks"));
        assert!(items.iter().any(|i| i.label == "mcp"));
    }

    #[test]
    #[cfg(feature = "lsp")]
    fn test_task_field_completions_include_verbs() {
        let items = task_field_completions();
        assert!(items.iter().any(|i| i.label == "infer"));
        assert!(items.iter().any(|i| i.label == "exec"));
        assert!(items.iter().any(|i| i.label == "agent"));
    }

    #[test]
    #[cfg(feature = "lsp")]
    fn test_analyze_context_top_level() {
        let text = "";
        let ctx = analyze_completion_context(
            text,
            Position {
                line: 0,
                character: 0,
            },
        );
        assert_eq!(ctx, CompletionContext::TopLevel);
    }

    #[test]
    #[cfg(feature = "lsp")]
    fn test_binding_completions_with_ast() {
        use super::super::super::ast_index::AstIndex;

        let index = AstIndex::new();
        let uri = "file:///test.nika.yaml".parse::<Uri>().unwrap();
        let text = r#"schema: nika/workflow@0.12
workflow: test

tasks:
  - id: step1
    infer: "Hello"
  - id: step2
    exec: "echo hello"
"#;

        // Parse the document to populate the AST cache
        index.parse_document(&uri, text, 1);

        // Get binding completions using AST
        let items = binding_completions_with_ast(&index, &uri, text);

        // Should contain task IDs from AST
        assert!(items.iter().any(|i| i.label == "step1"));
        assert!(items.iter().any(|i| i.label == "step2"));
        assert_eq!(items.len(), 2);
    }

    #[test]
    #[cfg(feature = "lsp")]
    fn test_binding_completions_with_ast_fallback() {
        use super::super::super::ast_index::AstIndex;

        let index = AstIndex::new();
        let uri = "file:///test.nika.yaml".parse::<Uri>().unwrap();
        let text = r#"
tasks:
  - id: step1
    infer: "Hello"
"#;

        // Don't parse the document - AST will be empty
        // Should fall back to text-based extraction
        let items = binding_completions_with_ast(&index, &uri, text);

        // Should contain task ID from text extraction
        assert!(items.iter().any(|i| i.label == "step1"));
    }

    #[test]
    #[cfg(feature = "lsp")]
    fn test_template_completions_with_ast() {
        use super::super::super::ast_index::AstIndex;

        let index = AstIndex::new();
        let uri = "file:///test.nika.yaml".parse::<Uri>().unwrap();
        let text = r#"schema: nika/workflow@0.12
workflow: test

tasks:
  - id: generate
    infer: "Hello"
  - id: process
    exec: "echo"
"#;

        // Parse the document
        index.parse_document(&uri, text, 1);

        // Get template completions
        let items = template_completions_with_ast(&index, &uri, text);

        // Should have base template completions
        assert!(items.iter().any(|i| i.label == "with."));
        assert!(items.iter().any(|i| i.label == "context.files."));
        assert!(items.iter().any(|i| i.label == "inputs."));

        // Should have $task shorthand completions
        assert!(items.iter().any(|i| i.label == "$generate"));
        assert!(items.iter().any(|i| i.label == "$process"));
    }

    #[test]
    #[cfg(feature = "lsp")]
    fn test_verb_value_completions_with_ast_agent() {
        use super::super::super::ast_index::AstIndex;

        let index = AstIndex::new();
        let uri = "file:///test.nika.yaml".parse::<Uri>().unwrap();
        let text = r#"schema: nika/workflow@0.12
workflow: test

mcp:
  novanet:
    command: "cargo run"
    args: []
  perplexity:
    command: "npx"
    args: ["-y", "@anthropic/mcp-server-perplexity"]

tasks:
  - id: step1
    agent:
      prompt: "test"
      mcp: [novanet]
"#;

        // Parse the document
        index.parse_document(&uri, text, 1);

        // Get agent verb completions (which has MCP field support)
        let items = verb_value_completions_with_ast("agent", &index, &uri);

        // Should have base agent completions
        assert!(items.iter().any(|i| i.label == "prompt"));
        assert!(items.iter().any(|i| i.label == "mcp"));
        assert!(items.iter().any(|i| i.label == "max_turns"));

        // Verify that we have the base completions (5 for agent)
        // Plus any MCP server names if parsed correctly
        assert!(items.len() >= 5);
    }

    #[test]
    #[cfg(feature = "lsp")]
    fn test_model_catalog_completions_not_empty() {
        let items = model_catalog_completions();
        assert!(!items.is_empty(), "Model catalog should have entries");
    }

    #[test]
    #[cfg(feature = "lsp")]
    fn test_model_catalog_completions_contain_known_models() {
        let items = model_catalog_completions();
        assert!(
            items.iter().any(|i| i.label == "claude-sonnet-4-6"),
            "Should contain Claude Sonnet 4"
        );
        assert!(
            items.iter().any(|i| i.label == "gpt-4o"),
            "Should contain GPT-4o"
        );
        assert!(
            items.iter().any(|i| i.label == "gemini-2.0-flash"),
            "Should contain Gemini 2.0 Flash"
        );
    }

    #[test]
    #[cfg(feature = "lsp")]
    fn test_model_catalog_completions_have_provider_detail() {
        let items = model_catalog_completions();
        let sonnet = items
            .iter()
            .find(|i| i.label == "claude-sonnet-4-6")
            .expect("Should have claude-sonnet-4-6");
        let detail = sonnet.detail.as_ref().unwrap();
        assert!(detail.contains("Claude"), "Detail should mention provider");
        assert!(
            detail.contains("Standard"),
            "Detail should mention tier: {}",
            detail
        );
    }

    #[test]
    #[cfg(feature = "lsp")]
    fn test_model_catalog_completions_deprecated_flagged() {
        let items = model_catalog_completions();
        let gpt4_turbo = items
            .iter()
            .find(|i| i.label == "gpt-4-turbo")
            .expect("Should have gpt-4-turbo");
        assert_eq!(
            gpt4_turbo.deprecated,
            Some(true),
            "gpt-4-turbo should be flagged as deprecated"
        );
        let detail = gpt4_turbo.detail.as_ref().unwrap();
        assert!(
            detail.contains("deprecated"),
            "Detail should mention deprecation: {}",
            detail
        );
    }

    #[test]
    #[cfg(feature = "lsp")]
    fn test_model_catalog_completions_active_not_deprecated() {
        let items = model_catalog_completions();
        let gpt4o = items
            .iter()
            .find(|i| i.label == "gpt-4o")
            .expect("Should have gpt-4o");
        assert_eq!(
            gpt4o.deprecated,
            Some(false),
            "gpt-4o should NOT be flagged as deprecated"
        );
    }

    #[test]
    #[cfg(feature = "lsp")]
    fn test_verb_value_completions_with_ast_infer_includes_models() {
        use super::super::super::ast_index::AstIndex;
        let index = AstIndex::new();
        let uri = "file:///test.nika.yaml".parse::<Uri>().unwrap();
        let items = verb_value_completions_with_ast("infer", &index, &uri);
        assert!(
            items.iter().any(|i| i.label == "prompt"),
            "Should have prompt field"
        );
        assert!(
            items.iter().any(|i| i.label == "claude-sonnet-4-6"),
            "Should have model catalog entries"
        );
    }

    #[test]
    #[cfg(feature = "lsp")]
    fn test_verb_value_completions_with_ast_agent_includes_models() {
        use super::super::super::ast_index::AstIndex;
        let index = AstIndex::new();
        let uri = "file:///test.nika.yaml".parse::<Uri>().unwrap();
        let items = verb_value_completions_with_ast("agent", &index, &uri);
        assert!(
            items.iter().any(|i| i.label == "gpt-4o"),
            "Agent completions should include model catalog"
        );
    }

    #[test]
    #[cfg(feature = "lsp")]
    fn test_verb_value_completions_with_ast_exec_no_models() {
        use super::super::super::ast_index::AstIndex;
        let index = AstIndex::new();
        let uri = "file:///test.nika.yaml".parse::<Uri>().unwrap();
        let items = verb_value_completions_with_ast("exec", &index, &uri);
        assert!(
            !items.iter().any(|i| i.label == "claude-sonnet-4-6"),
            "exec completions should NOT include model catalog"
        );
    }

    #[test]
    #[cfg(feature = "lsp")]
    fn test_verb_value_completions_invoke_has_sub_fields() {
        // invoke verb should have mcp, tool, params, resource completions
        let items = verb_value_completions("invoke");
        assert!(items.iter().any(|i| i.label == "mcp"));
        assert!(items.iter().any(|i| i.label == "tool"));
        assert!(items.iter().any(|i| i.label == "params"));
        assert!(items.iter().any(|i| i.label == "resource"));
        assert_eq!(items.len(), 4);
    }

    #[test]
    #[cfg(feature = "lsp")]
    fn test_verb_value_completions_fetch_has_sub_fields() {
        // fetch verb should have url, method, headers, body, retry completions
        let items = verb_value_completions("fetch");
        assert!(items.iter().any(|i| i.label == "url"));
        assert!(items.iter().any(|i| i.label == "method"));
        assert!(items.iter().any(|i| i.label == "headers"));
        assert!(items.iter().any(|i| i.label == "body"));
        assert!(items.iter().any(|i| i.label == "retry"));
        assert_eq!(items.len(), 5);
    }

    #[test]
    #[cfg(feature = "lsp")]
    fn test_compute_completions_with_ast_integration() {
        use super::super::super::ast_index::AstIndex;

        let index = AstIndex::new();
        let uri = "file:///test.nika.yaml".parse::<Uri>().unwrap();
        let text = r#"schema: nika/workflow@0.12
workflow: test

tasks:
  - id: step1
    infer: "Hello"
  - id: step2
    with:
      result:
"#;

        // Parse the document
        index.parse_document(&uri, text, 1);

        // Position at the binding value location (after "result: ")
        // Line 8 (0-indexed: 7), character 14
        let position = Position {
            line: 7,
            character: 14,
        };

        // This should detect WithBinding context and provide task names
        let items = compute_completions_with_ast(&index, &uri, text, position);

        // The context analysis may or may not detect WithBinding correctly
        // depending on indentation detection. This test verifies the function runs
        // without panic - actual completions depend on detection accuracy.
        let _ = items; // Allow empty results for now
    }

    #[test]
    #[cfg(feature = "lsp")]
    fn test_verb_completions_are_multiline_snippets() {
        let verbs = verb_completions();

        for item in &verbs {
            assert_eq!(
                item.insert_text_format,
                Some(InsertTextFormat::SNIPPET),
                "Verb '{}' must use SNIPPET format",
                item.label
            );
        }

        let infer = verbs.iter().find(|i| i.label == "infer").unwrap();
        let infer_text = infer.insert_text.as_deref().unwrap();
        assert!(
            infer_text.contains("prompt:"),
            "infer scaffold must contain prompt: field"
        );
        assert!(
            infer_text.contains('\n'),
            "infer scaffold must be multi-line"
        );
        assert!(
            infer_text.contains("${0}"),
            "infer scaffold must contain final tab stop ${{0}}"
        );

        let exec = verbs.iter().find(|i| i.label == "exec").unwrap();
        let exec_text = exec.insert_text.as_deref().unwrap();
        assert!(
            exec_text.starts_with("exec:"),
            "exec scaffold must start with exec:"
        );
        assert!(
            exec_text.contains("${0}"),
            "exec scaffold must contain final tab stop ${{0}}"
        );

        let fetch = verbs.iter().find(|i| i.label == "fetch").unwrap();
        let fetch_text = fetch.insert_text.as_deref().unwrap();
        assert!(
            fetch_text.contains("url:"),
            "fetch scaffold must contain url: field"
        );
        assert!(
            fetch_text.contains("${0}"),
            "fetch scaffold must contain final tab stop ${{0}}"
        );

        let invoke = verbs.iter().find(|i| i.label == "invoke").unwrap();
        let invoke_text = invoke.insert_text.as_deref().unwrap();
        assert!(
            invoke_text.contains("tool:"),
            "invoke scaffold must contain tool: field"
        );
        assert!(
            invoke_text.contains("params:"),
            "invoke scaffold must contain params: block"
        );
        assert!(
            invoke_text.contains("${0}"),
            "invoke scaffold must contain final tab stop ${{0}}"
        );

        let agent = verbs.iter().find(|i| i.label == "agent").unwrap();
        let agent_text = agent.insert_text.as_deref().unwrap();
        assert!(
            agent_text.contains("prompt:"),
            "agent scaffold must contain prompt: field"
        );
        assert!(
            agent_text.contains("mcp:"),
            "agent scaffold must contain mcp: field"
        );
        assert!(
            agent_text.contains("max_turns:"),
            "agent scaffold must contain max_turns: field"
        );
        assert!(
            agent_text.contains("${0}"),
            "agent scaffold must contain final tab stop ${{0}}"
        );
    }
}