apm-core 0.1.9

Core library for APM — a git-native project manager for parallel AI coding agents.
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
use anyhow::{bail, Context, Result};
use std::collections::{HashMap, HashSet, VecDeque};
use std::path::Path;

use super::ticket_fmt::{parse_checklist, serialize_checklist, id_arg_prefixes, slugify, Frontmatter, Ticket, TicketDocument};

impl Ticket {
    pub fn score(&self, priority_weight: f64, effort_weight: f64, risk_weight: f64) -> f64 {
        let fm = &self.frontmatter;
        fm.priority as f64 * priority_weight
            + fm.effort as f64 * effort_weight
            + fm.risk as f64 * risk_weight
    }
}

impl TicketDocument {
    pub fn unchecked_tasks(&self, section_name: &str) -> Vec<usize> {
        let val = self.sections.get(section_name).map(|s| s.as_str()).unwrap_or("");
        parse_checklist(val).into_iter().enumerate()
            .filter(|(_, c)| !c.checked)
            .map(|(i, _)| i)
            .collect()
    }

    pub fn toggle_criterion(&mut self, index: usize, checked: bool) -> Result<()> {
        let val = self.sections.get("Acceptance criteria").cloned().unwrap_or_default();
        let mut items = parse_checklist(&val);
        if index >= items.len() {
            anyhow::bail!("criterion index {index} out of range (have {})", items.len());
        }
        items[index].checked = checked;
        self.sections.insert("Acceptance criteria".to_string(), serialize_checklist(&items));
        Ok(())
    }
}

/// Build a reverse dependency index: for each ticket ID, collect the tickets
/// that directly depend on it.  Pass only non-terminal, non-satisfies_deps
/// tickets so that closed work does not inflate effective priority.
pub fn build_reverse_index<'a>(tickets: &[&'a Ticket]) -> HashMap<&'a str, Vec<&'a Ticket>> {
    let mut map: HashMap<&'a str, Vec<&'a Ticket>> = HashMap::new();
    for &ticket in tickets {
        if let Some(deps) = &ticket.frontmatter.depends_on {
            for dep_id in deps {
                map.entry(dep_id.as_str()).or_default().push(ticket);
            }
        }
    }
    map
}

/// Return the effective priority of a ticket: the max of its own priority and
/// the priority of all direct and transitive dependents reachable via the
/// reverse index.  Uses a visited set to handle cycles safely.
pub fn effective_priority(ticket: &Ticket, reverse_index: &HashMap<&str, Vec<&Ticket>>) -> u8 {
    let mut max_priority = ticket.frontmatter.priority;
    let mut visited: HashSet<&str> = HashSet::new();
    let mut queue: VecDeque<&str> = VecDeque::new();
    let id = ticket.frontmatter.id.as_str();
    queue.push_back(id);
    visited.insert(id);
    while let Some(cur_id) = queue.pop_front() {
        if let Some(dependents) = reverse_index.get(cur_id) {
            for &dep in dependents {
                let dep_id = dep.frontmatter.id.as_str();
                if visited.insert(dep_id) {
                    if dep.frontmatter.priority > max_priority {
                        max_priority = dep.frontmatter.priority;
                    }
                    queue.push_back(dep_id);
                }
            }
        }
    }
    max_priority
}

/// Return all agent-actionable tickets sorted by descending score.
pub fn sorted_actionable<'a>(
    tickets: &'a [Ticket],
    actionable: &[&str],
    pw: f64,
    ew: f64,
    rw: f64,
    _caller: Option<&str>,
    owner_filter: Option<&str>,
) -> Vec<&'a Ticket> {
    let mut candidates: Vec<&Ticket> = tickets
        .iter()
        .filter(|t| actionable.contains(&t.frontmatter.state.as_str()))
        .filter(|t| owner_filter.is_none_or(|f| t.frontmatter.owner.as_deref() == Some(f)))
        .collect();
    let rev_idx = build_reverse_index(&candidates);
    candidates.sort_by(|a, b| {
        let score_a = effective_priority(a, &rev_idx) as f64 * pw
            + a.frontmatter.effort as f64 * ew
            + a.frontmatter.risk as f64 * rw;
        let score_b = effective_priority(b, &rev_idx) as f64 * pw
            + b.frontmatter.effort as f64 * ew
            + b.frontmatter.risk as f64 * rw;
        score_b.partial_cmp(&score_a).unwrap_or(std::cmp::Ordering::Equal)
    });
    candidates
}

/// Returns true if a ticket in `dep_state` satisfies the dependency gate
/// required by the dependent ticket.  `required_gate` is `Some("tag")` when
/// the dependent's state has `dep_requires = "tag"`, or `None` for the
/// default (requires `satisfies_deps = true` or `terminal = true`).
pub fn dep_satisfied(dep_state: &str, required_gate: Option<&str>, config: &crate::config::Config) -> bool {
    use crate::config::SatisfiesDeps;
    config.workflow.states.iter()
        .find(|s| s.id == dep_state)
        .map(|s| {
            if s.terminal { return true; }
            match &s.satisfies_deps {
                SatisfiesDeps::Bool(true) => true,
                SatisfiesDeps::Tag(tag) => required_gate == Some(tag.as_str()),
                SatisfiesDeps::Bool(false) => false,
            }
        })
        .unwrap_or(false)
}

/// Return the highest-scoring ticket from `tickets` whose state is in
/// `actionable` and (if `startable` is non-empty) also in `startable`,
/// and whose `depends_on` deps are all satisfied.
#[allow(clippy::too_many_arguments)]
pub fn pick_next<'a>(
    tickets: &'a [Ticket],
    actionable: &[&str],
    startable: &[&str],
    pw: f64,
    ew: f64,
    rw: f64,
    config: &crate::config::Config,
    caller: Option<&str>,
    owner_filter: Option<&str>,
) -> Option<&'a Ticket> {
    sorted_actionable(tickets, actionable, pw, ew, rw, caller, owner_filter)
        .into_iter()
        .find(|t| {
            let state = t.frontmatter.state.as_str();
            if !startable.is_empty() && !startable.contains(&state) {
                return false;
            }
            let required_gate = config.workflow.states.iter()
                .find(|s| s.id == state)
                .and_then(|s| s.dep_requires.as_deref());
            if let Some(deps) = &t.frontmatter.depends_on {
                for dep_id in deps {
                    if let Some(dep) = tickets.iter().find(|d| d.frontmatter.id == *dep_id) {
                        if !dep_satisfied(&dep.frontmatter.state, required_gate, config) {
                            return false;
                        }
                    }
                }
            }
            true
        })
}

/// Load all tickets by reading directly from their git branches.
/// No filesystem cache is involved.
pub fn load_all_from_git(root: &Path, tickets_dir_rel: &std::path::Path) -> Result<Vec<Ticket>> {
    let branches = crate::git::ticket_branches(root)?;
    let mut tickets = Vec::new();
    for branch in &branches {
        let suffix = branch.trim_start_matches("ticket/");
        // Skip bare short-ID refs (e.g. ticket/268f5694) created by fetch operations.
        // A real ticket branch always has a slug after the ID: ticket/<id>-<slug>.
        if suffix.len() == 8 && suffix.chars().all(|c| c.is_ascii_hexdigit()) {
            continue;
        }
        let filename = format!("{suffix}.md");
        let rel_path = format!("{}/{}", tickets_dir_rel.to_string_lossy(), filename);
        let dummy_path = root.join(&rel_path);
        if let Ok(content) = crate::git::read_from_branch(root, branch, &rel_path) {
            if let Ok(t) = Ticket::parse(&dummy_path, &content) {
                tickets.push(t);
            }
        }
    }
    tickets.sort_by_key(|t| t.frontmatter.created_at);
    Ok(tickets)
}

/// Read a ticket's state from a specific branch by relative path.
pub fn state_from_branch(root: &Path, branch: &str, rel_path: &str) -> Option<String> {
    let content = crate::git::read_from_branch(root, branch, rel_path).ok()?;
    let dummy = root.join(rel_path);
    Ticket::parse(&dummy, &content).ok().map(|t| t.frontmatter.state)
}

/// Close a ticket from any state.  Commits the change to the ticket branch,
/// pushes it (non-fatal if no remote), then merges into the default branch
/// so that `apm clean` can detect and remove the worktree.
pub fn close(
    root: &Path,
    config: &crate::config::Config,
    id_arg: &str,
    reason: Option<&str>,
    agent: &str,
    aggressive: bool,
) -> Result<Vec<String>> {
    let mut output: Vec<String> = Vec::new();
    let mut tickets = load_all_from_git(root, &config.tickets.dir)?;
    let prefixes = id_arg_prefixes(id_arg)?;

    // Search ticket branches first, then fall back to the default branch.
    // This handles stale "implemented" tickets whose branch was deleted.
    let branch_matches: Vec<usize> = tickets.iter()
        .enumerate()
        .filter(|(_, t)| prefixes.iter().any(|p| t.frontmatter.id.starts_with(p.as_str())))
        .map(|(i, _)| i)
        .collect();
    // Deduplicate in case both prefixes matched the same ticket.
    let branch_matches: Vec<usize> = {
        let mut seen = std::collections::HashSet::new();
        branch_matches.into_iter().filter(|&i| seen.insert(tickets[i].frontmatter.id.clone())).collect()
    };

    let mut from_default: Option<Ticket> = None;
    let id: String = match branch_matches.len() {
        1 => tickets[branch_matches[0]].frontmatter.id.clone(),
        0 => {
            let default_branch = &config.project.default_branch;
            let mut found: Option<Ticket> = None;
            if let Ok(files) = crate::git::list_files_on_branch(root, default_branch, &config.tickets.dir.to_string_lossy()) {
                for rel_path in files {
                    if !rel_path.ends_with(".md") { continue; }
                    if let Ok(content) = crate::git::read_from_branch(root, default_branch, &rel_path) {
                        let dummy = root.join(&rel_path);
                        if let Ok(t) = Ticket::parse(&dummy, &content) {
                            if prefixes.iter().any(|p| t.frontmatter.id.starts_with(p.as_str())) {
                                found = Some(t);
                                break;
                            }
                        }
                    }
                }
            }
            match found {
                Some(t) => { let id = t.frontmatter.id.clone(); from_default = Some(t); id }
                None => bail!("no ticket matches '{id_arg}'"),
            }
        }
        _ => {
            let names: Vec<String> = branch_matches.iter()
                .map(|&i| tickets[i].frontmatter.id.clone())
                .collect();
            bail!("ambiguous prefix '{}', matches: {}", id_arg, names.join(", "));
        }
    };

    let ticket_pos = tickets.iter().position(|t| t.frontmatter.id == id);
    let t: &mut Ticket = match ticket_pos {
        Some(pos) => &mut tickets[pos],
        None => from_default.as_mut().ok_or_else(|| anyhow::anyhow!("ticket {id:?} not found"))?,
    };

    if t.frontmatter.state == "closed" {
        anyhow::bail!("ticket {id:?} is already closed");
    }

    let now = chrono::Utc::now();
    let prev = t.frontmatter.state.clone();
    let when = now.format("%Y-%m-%dT%H:%MZ").to_string();
    let by = match reason {
        Some(r) => format!("{agent} (reason: {r})"),
        None => agent.to_string(),
    };

    t.frontmatter.state = "closed".into();
    t.frontmatter.updated_at = Some(now);

    crate::state::append_history(&mut t.body, &prev, "closed", &when, &by);

    let content = t.serialize()?;
    let rel_path = format!(
        "{}/{}",
        config.tickets.dir.to_string_lossy(),
        t.path.file_name().unwrap().to_string_lossy()
    );
    let branch = t.frontmatter.branch.clone()
        .or_else(|| crate::ticket_fmt::branch_name_from_path(&t.path))
        .unwrap_or_else(|| format!("ticket/{id}"));

    crate::git::commit_to_branch(root, &branch, &rel_path, &content, &format!("ticket({id}): close"))?;
    crate::logger::log("state_transition", &format!("{id:?} {prev} -> closed"));

    let mut merge_warnings: Vec<String> = Vec::new();
    if let Err(e) = crate::git::merge_branch_into_default(root, &branch, &config.project.default_branch, &mut merge_warnings) {
        output.push(format!("warning: merge into {} failed: {e:#}", config.project.default_branch));
    }
    output.extend(merge_warnings);

    if aggressive {
        if let Err(e) = crate::git::push_branch(root, &branch) {
            output.push(format!("warning: push failed for {branch}: {e:#}"));
        }
    }

    output.push(format!("{id}: {prev} → closed"));
    Ok(output)
}

#[allow(clippy::too_many_arguments)]
pub fn create(
    root: &std::path::Path,
    config: &crate::config::Config,
    title: String,
    author: String,
    context: Option<String>,
    context_section: Option<String>,
    aggressive: bool,
    section_sets: Vec<(String, String)>,
    epic: Option<String>,
    target_branch: Option<String>,
    depends_on: Option<Vec<String>>,
    base_branch: Option<String>,
    warnings: &mut Vec<String>,
) -> Result<Ticket> {
    let tickets_dir = root.join(&config.tickets.dir);
    std::fs::create_dir_all(&tickets_dir)?;

    let id = crate::ticket_fmt::gen_hex_id();
    let slug = slugify(&title);
    let filename = format!("{id}-{slug}.md");
    let rel_path = format!("{}/{}", config.tickets.dir.to_string_lossy(), filename);
    let branch = format!("ticket/{id}-{slug}");
    let now = chrono::Utc::now();
    let fm = Frontmatter {
        id: id.clone(),
        title: title.clone(),
        state: "new".into(),
        priority: 0,
        effort: 0,
        risk: 0,
        author: Some(author.clone()),
        owner: Some(author.clone()),
        branch: Some(branch.clone()),
        created_at: Some(now),
        updated_at: Some(now),
        focus_section: None,
        epic,
        target_branch,
        depends_on,
    };
    let when = now.format("%Y-%m-%dT%H:%MZ");
    let history_footer = format!("## History\n\n| When | From | To | By |\n|------|------|----|----|\n| {when} | — | new | {author} |\n");
    let body_template = {
        let mut s = String::from("## Spec\n\n");
        for sec in &config.ticket.sections {
            let placeholder = sec.placeholder.as_deref().unwrap_or("");
            s.push_str(&format!("### {}\n\n{}\n\n", sec.name, placeholder));
        }
        s.push_str(&history_footer);
        s
    };
    let body = if let Some(ctx) = &context {
        let transition_section = config.workflow.states.iter()
            .find(|s| s.id == "new")
            .and_then(|s| s.transitions.iter().find(|tr| tr.to == "in_design"))
            .and_then(|tr| tr.context_section.clone());
        let section = context_section
            .clone()
            .or(transition_section)
            .unwrap_or_else(|| "Problem".to_string());
        if !config.ticket.sections.is_empty()
            && !config.has_section(&section)
        {
            anyhow::bail!("section '### {section}' not found in ticket body template");
        }
        let mut doc = TicketDocument::parse(&body_template)?;
        crate::spec::set_section(&mut doc, &section, ctx.clone());
        doc.serialize()
    } else {
        body_template
    };
    let path = tickets_dir.join(&filename);
    let mut t = Ticket { frontmatter: fm, body, path };

    if !section_sets.is_empty() {
        let mut doc = t.document()?;
        for (name, value) in &section_sets {
            let trimmed = value.trim().to_string();
            let formatted = if !config.ticket.sections.is_empty() {
                let section_config = config.find_section(name)
                    .ok_or_else(|| anyhow::anyhow!("unknown section {:?}", name))?;
                crate::spec::apply_section_type(&section_config.type_, trimmed)
            } else {
                trimmed
            };
            crate::spec::set_section(&mut doc, name, formatted);
        }
        t.body = doc.serialize();
    }

    let content = t.serialize()?;

    if let Some(base) = base_branch {
        let sha = crate::git::resolve_branch_sha(root, &base)?;
        crate::git::create_branch_at(root, &branch, &sha)?;
    }

    crate::git::commit_to_branch(
        root,
        &branch,
        &rel_path,
        &content,
        &format!("ticket({id}): create {title}"),
    )?;

    if aggressive {
        if let Err(e) = crate::git::push_branch_tracking(root, &branch) {
            warnings.push(format!("warning: push failed: {e:#}"));
        }
    }

    Ok(t)
}

#[allow(clippy::too_many_arguments)]
pub fn list_filtered<'a>(
    tickets: &'a [Ticket],
    config: &crate::config::Config,
    state_filter: Option<&str>,
    unassigned: bool,
    all: bool,
    actionable_filter: Option<&str>,
    author_filter: Option<&str>,
    owner_filter: Option<&str>,
    mine_user: Option<&str>,
) -> Vec<&'a Ticket> {
    let terminal = config.terminal_state_ids();
    let actionable_map: std::collections::HashMap<&str, &Vec<String>> = config.workflow.states.iter()
        .map(|s| (s.id.as_str(), &s.actionable))
        .collect();

    tickets.iter().filter(|t| {
        let fm = &t.frontmatter;
        let state_ok = state_filter.is_none_or(|s| fm.state == s);
        let agent_ok = !unassigned || fm.author.as_deref() == Some("unassigned");
        let state_is_terminal = state_filter.is_some_and(|s| terminal.contains(s));
        let terminal_ok = all || state_is_terminal || !terminal.contains(fm.state.as_str());
        let actionable_ok = actionable_filter.is_none_or(|actor| {
            actionable_map.get(fm.state.as_str())
                .is_some_and(|actors| actors.iter().any(|a| a == actor || a == "any"))
        });
        let author_ok = author_filter.is_none_or(|a| fm.author.as_deref() == Some(a));
        let owner_ok = owner_filter.is_none_or(|o| fm.owner.as_deref() == Some(o));
        let mine_ok = mine_user.is_none_or(|me| {
            fm.author.as_deref() == Some(me) || fm.owner.as_deref() == Some(me)
        });
        state_ok && agent_ok && terminal_ok && actionable_ok && author_ok && owner_ok && mine_ok
    }).collect()
}

pub fn check_owner(root: &Path, ticket: &Ticket) -> anyhow::Result<()> {
    let cfg = crate::config::Config::load(root)?;
    let is_terminal = cfg.workflow.states.iter()
        .find(|s| s.id == ticket.frontmatter.state)
        .map(|s| s.terminal)
        .unwrap_or(false);
    if is_terminal {
        anyhow::bail!("cannot change owner of a closed ticket");
    }
    let Some(o) = &ticket.frontmatter.owner else {
        return Ok(());
    };
    let identity = crate::config::resolve_identity(root);
    if identity == "unassigned" {
        anyhow::bail!(
            "cannot reassign: identity not configured (set local.user in .apm/local.toml or configure a GitHub token)"
        );
    }
    if &identity != o {
        anyhow::bail!("only the current owner ({o}) can reassign this ticket");
    }
    Ok(())
}

pub fn set_field(fm: &mut Frontmatter, field: &str, value: &str) -> anyhow::Result<()> {
    match field {
        "priority" => fm.priority = value.parse().map_err(|_| anyhow::anyhow!("priority must be 0–255"))?,
        "effort"   => fm.effort   = value.parse().map_err(|_| anyhow::anyhow!("effort must be 0–255"))?,
        "risk"     => fm.risk     = value.parse().map_err(|_| anyhow::anyhow!("risk must be 0–255"))?,
        "author"   => anyhow::bail!("author is immutable"),
        "owner"    => fm.owner    = if value == "-" { None } else { Some(value.to_string()) },
        "branch"   => fm.branch   = if value == "-" { None } else { Some(value.to_string()) },
        "title"    => fm.title    = value.to_string(),
        "depends_on" => {
            if value == "-" {
                fm.depends_on = None;
            } else {
                let ids: Vec<String> = value
                    .split(',')
                    .map(|s| s.trim().to_string())
                    .filter(|s| !s.is_empty())
                    .collect();
                fm.depends_on = if ids.is_empty() { None } else { Some(ids) };
            }
        }
        other => anyhow::bail!("unknown field: {other}"),
    }
    Ok(())
}

#[derive(serde::Serialize, Clone, Debug)]
pub struct BlockingDep {
    pub id: String,
    pub state: String,
}

pub fn compute_blocking_deps(
    ticket: &Ticket,
    all_tickets: &[Ticket],
    config: &crate::config::Config,
) -> Vec<BlockingDep> {
    let deps = match &ticket.frontmatter.depends_on {
        Some(d) if !d.is_empty() => d,
        _ => return vec![],
    };
    let state_map: std::collections::HashMap<&str, &str> = all_tickets
        .iter()
        .map(|t| (t.frontmatter.id.as_str(), t.frontmatter.state.as_str()))
        .collect();
    deps.iter()
        .filter_map(|dep_id| {
            state_map.get(dep_id.as_str()).and_then(|&s| {
                if dep_satisfied(s, None, config) {
                    None
                } else {
                    Some(BlockingDep { id: dep_id.clone(), state: s.to_string() })
                }
            })
        })
        .collect()
}

/// Move a ticket into (or out of) an epic by rebasing its branch onto the new base.
///
/// `target` values:
/// - `Some(epic_id_prefix)` — move into the named epic; resolves by prefix match
/// - `None` or `Some("-")` — remove from any epic, rebase onto main
///
/// Returns an informational message on success (including no-op cases).
pub fn move_to_epic(
    root: &Path,
    config: &crate::config::Config,
    ticket_id_arg: &str,
    target: Option<&str>,
) -> Result<String> {
    // 1. Load tickets and resolve the ticket by prefix.
    let tickets = load_all_from_git(root, &config.tickets.dir)?;
    let id = super::ticket_fmt::resolve_id_in_slice(&tickets, ticket_id_arg)?;
    let ticket = tickets.iter().find(|t| t.frontmatter.id == id).unwrap();

    // 2. Reject terminal tickets.
    let terminal = config.terminal_state_ids();
    if terminal.contains(&ticket.frontmatter.state) {
        bail!(
            "cannot move ticket {}: it is in a terminal state ({})",
            id,
            ticket.frontmatter.state
        );
    }

    // 3. Resolve the ticket's git branch name.
    let ticket_branch = ticket
        .frontmatter
        .branch
        .clone()
        .or_else(|| super::ticket_fmt::branch_name_from_path(&ticket.path))
        .unwrap_or_else(|| format!("ticket/{id}"));

    // 4. Determine the old base ref (where the ticket currently branches from).
    let old_base_ref = ticket
        .frontmatter
        .target_branch
        .as_deref()
        .unwrap_or("main")
        .to_string();

    // 5. Determine new base ref and updated frontmatter fields.
    let target_is_clear = target.is_none() || target == Some("-");

    let (new_base_ref, new_epic, new_target_branch) = if target_is_clear {
        if ticket.frontmatter.epic.is_none() {
            return Ok(format!("ticket {id} is not in any epic; nothing to do"));
        }
        ("main".to_string(), None::<String>, None::<String>)
    } else {
        let epic_id_arg = target.unwrap();
        let matches = crate::epic::find_epic_branches(root, epic_id_arg);
        let epic_branch = match matches.len() {
            0 => bail!("no epic found matching '{epic_id_arg}'"),
            1 => matches.into_iter().next().unwrap(),
            _ => bail!(
                "ambiguous epic prefix '{}': matches {}",
                epic_id_arg,
                matches.join(", ")
            ),
        };
        let epic_id = crate::epic::epic_id_from_branch(&epic_branch).to_string();

        if ticket.frontmatter.epic.as_deref() == Some(&epic_id) {
            return Ok(format!(
                "ticket {id} is already in epic {epic_id}; nothing to do"
            ));
        }

        (epic_branch.clone(), Some(epic_id), Some(epic_branch))
    };

    // 6. Reject if branch is checked out in a worktree.
    if crate::worktree::find_worktree_for_branch(root, &ticket_branch).is_some() {
        bail!(
            "branch {} is checked out in a worktree; close the worktree first",
            ticket_branch
        );
    }

    // 7. Find old divergence point: git merge-base <ticket_branch> <old_base_ref>.
    let old_base_sha = crate::git_util::resolve_branch_sha(root, &old_base_ref)
        .with_context(|| format!("cannot resolve old base '{old_base_ref}'"))?;
    let old_upstream_sha =
        crate::git_util::merge_base(root, &ticket_branch, &old_base_sha)
            .with_context(|| {
                format!(
                    "cannot find merge-base of {ticket_branch} and {old_base_ref}"
                )
            })?;

    // 8. Resolve the new base to a SHA.
    let new_base_sha = crate::git_util::resolve_branch_sha(root, &new_base_ref)
        .with_context(|| format!("cannot resolve new base '{new_base_ref}'"))?;

    // 9. Rebase: replay (old_upstream..ticket_branch] onto new_base.
    let rebase_result = crate::git_util::run(
        root,
        &[
            "rebase",
            "--onto",
            &new_base_sha,
            &old_upstream_sha,
            &ticket_branch,
        ],
    );

    if let Err(e) = rebase_result {
        let _ = crate::git_util::run(root, &["rebase", "--abort"]);
        let err_str = e.to_string();
        if err_str.contains("checked out") || err_str.contains("worktree") {
            bail!(
                "branch {} is checked out in a worktree; close the worktree first",
                ticket_branch
            );
        }
        bail!(
            "rebase onto {new_base_ref} failed (conflicts or other error); \
             resolve manually or create a new ticket with `apm new --epic`\n{e:#}"
        );
    }

    // 10. Read the ticket file from the rebased branch tip, update frontmatter, commit.
    let rel_path = format!(
        "{}/{}",
        config.tickets.dir.to_string_lossy(),
        ticket.path.file_name().unwrap().to_string_lossy()
    );

    let updated_content =
        crate::git_util::read_from_branch(root, &ticket_branch, &rel_path)
            .with_context(|| {
                format!("cannot read ticket file from {ticket_branch} after rebase")
            })?;
    let mut updated_ticket = Ticket::parse(&ticket.path, &updated_content)?;

    let now = chrono::Utc::now();
    updated_ticket.frontmatter.epic = new_epic.clone();
    updated_ticket.frontmatter.target_branch = new_target_branch;
    updated_ticket.frontmatter.updated_at = Some(now);

    let when = now.format("%Y-%m-%dT%H:%MZ").to_string();
    let history_note = format!("move: {old_base_ref} \u{2192} {new_base_ref}");
    crate::state::append_history(
        &mut updated_ticket.body,
        "\u{2014}",
        "\u{2014}",
        &when,
        &history_note,
    );

    let content = updated_ticket.serialize()?;
    crate::git_util::commit_to_branch(
        root,
        &ticket_branch,
        &rel_path,
        &content,
        &format!("ticket({id}): move to {new_base_ref}"),
    )?;

    let msg = if target_is_clear {
        format!("{id}: moved out of epic, rebased onto main")
    } else {
        format!(
            "{id}: moved into epic {}",
            new_epic.as_deref().unwrap_or("")
        )
    };

    Ok(msg)
}

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

    fn dummy_path() -> &'static Path {
        Path::new("test.md")
    }

    fn full_body(ac: &str) -> String {
        format!(
            "## Spec\n\n### Problem\n\nSome problem.\n\n### Acceptance criteria\n\n{ac}\n\n### Out of scope\n\nNothing.\n\n### Approach\n\nDo it.\n\n## History\n\n| When | From | To | By |\n|------|------|----|----|"
        )
    }

    // ── compute_blocking_deps ─────────────────────────────────────────────

    fn make_simple_ticket(id: &str, state: &str, depends_on: Option<Vec<&str>>) -> Ticket {
        let deps_line = match &depends_on {
            None => String::new(),
            Some(ids) => {
                let items: Vec<String> = ids.iter().map(|i| format!("\"{}\"", i)).collect();
                format!("depends_on = [{}]\n", items.join(", "))
            }
        };
        let raw = format!(
            "+++\nid = \"{id}\"\ntitle = \"T\"\nstate = \"{state}\"\n{deps_line}+++\n\nbody\n"
        );
        Ticket::parse(Path::new("test.md"), &raw).unwrap()
    }

    #[test]
    fn compute_blocking_deps_no_depends_on_returns_empty() {
        let config = test_config_with_states(&["closed"]);
        let ticket = make_simple_ticket("aaaa0001", "new", None);
        let all = vec![ticket.clone()];
        let result = compute_blocking_deps(&ticket, &all, &config);
        assert!(result.is_empty());
    }

    #[test]
    fn compute_blocking_deps_dep_in_non_terminal_state_returns_it() {
        let config = test_config_with_states(&["closed"]);
        let dep = make_simple_ticket("bbbb0001", "new", None);
        let ticket = make_simple_ticket("aaaa0001", "new", Some(vec!["bbbb0001"]));
        let all = vec![dep.clone(), ticket.clone()];
        let result = compute_blocking_deps(&ticket, &all, &config);
        assert_eq!(result.len(), 1);
        assert_eq!(result[0].id, "bbbb0001");
        assert_eq!(result[0].state, "new");
    }

    #[test]
    fn compute_blocking_deps_all_deps_satisfied_returns_empty() {
        let config = test_config_with_states(&["closed"]);
        let dep = make_simple_ticket("bbbb0001", "closed", None);
        let ticket = make_simple_ticket("aaaa0001", "new", Some(vec!["bbbb0001"]));
        let all = vec![dep.clone(), ticket.clone()];
        let result = compute_blocking_deps(&ticket, &all, &config);
        assert!(result.is_empty());
    }

    #[test]
    fn document_toggle_criterion() {
        let body = full_body("- [ ] item one\n- [ ] item two");
        let mut doc = TicketDocument::parse(&body).unwrap();
        let ac = doc.sections.get("Acceptance criteria").unwrap();
        assert!(ac.contains("- [ ] item one"));
        doc.toggle_criterion(0, true).unwrap();
        let ac = doc.sections.get("Acceptance criteria").unwrap();
        assert!(ac.contains("- [x] item one"));
    }

    #[test]
    fn document_unchecked_tasks() {
        let body = full_body("- [ ] one\n- [x] two\n- [ ] three");
        let doc = TicketDocument::parse(&body).unwrap();
        assert_eq!(doc.unchecked_tasks("Acceptance criteria"), vec![0, 2]);
    }

    // ── list_filtered ─────────────────────────────────────────────────────

    fn test_config_with_states(terminal_states: &[&str]) -> crate::config::Config {
        let mut states_toml = String::new();
        for s in ["new", "ready", "in_progress"] {
            states_toml.push_str(&format!(
                "[[workflow.states]]\nid = \"{s}\"\nlabel = \"{s}\"\nterminal = false\nactionable = [\"agent\"]\n\n"
            ));
        }
        for s in terminal_states {
            states_toml.push_str(&format!(
                "[[workflow.states]]\nid = \"{s}\"\nlabel = \"{s}\"\nterminal = true\n\n"
            ));
        }
        let full = format!(
            "[project]\nname = \"test\"\n\n[tickets]\ndir = \"tickets\"\n\n{states_toml}"
        );
        toml::from_str(&full).unwrap()
    }

    fn make_ticket(id: &str, state: &str, agent: Option<&str>) -> Ticket {
        let agent_line = agent.map(|a| format!("agent = \"{a}\"\n")).unwrap_or_default();
        let raw = format!(
            "+++\nid = \"{id}\"\ntitle = \"T{id}\"\nstate = \"{state}\"\n{agent_line}+++\n\n"
        );
        Ticket::parse(dummy_path(), &raw).unwrap()
    }

    #[test]
    fn list_filtered_by_state() {
        let config = test_config_with_states(&["closed"]);
        let tickets = vec![
            make_ticket("0001", "new", None),
            make_ticket("0002", "ready", None),
            make_ticket("0003", "new", None),
        ];
        let result = list_filtered(&tickets, &config, Some("new"), false, false, None, None, None, None);
        assert_eq!(result.len(), 2);
        assert!(result.iter().all(|t| t.frontmatter.state == "new"));
    }

    #[test]
    fn list_filtered_terminal_hidden_by_default() {
        let config = test_config_with_states(&["closed"]);
        let tickets = vec![
            make_ticket("0001", "new", None),
            make_ticket("0002", "closed", None),
        ];
        // By default, terminal states are hidden.
        let result = list_filtered(&tickets, &config, None, false, false, None, None, None, None);
        assert_eq!(result.len(), 1);
        assert_eq!(result[0].frontmatter.state, "new");

        // With all=true, terminal states are shown.
        let result_all = list_filtered(&tickets, &config, None, false, true, None, None, None, None);
        assert_eq!(result_all.len(), 2);

        // With state_filter matching the terminal state, it's shown.
        let result_filtered = list_filtered(&tickets, &config, Some("closed"), false, false, None, None, None, None);
        assert_eq!(result_filtered.len(), 1);
        assert_eq!(result_filtered[0].frontmatter.state, "closed");
    }

    #[test]
    fn list_filtered_unassigned() {
        let config = test_config_with_states(&[]);
        let make_with_author = |id: &str, author: Option<&str>| {
            let author_line = author.map(|a| format!("author = \"{a}\"\n")).unwrap_or_default();
            let raw = format!(
                "+++\nid = \"{id}\"\ntitle = \"T{id}\"\nstate = \"new\"\n{author_line}+++\n\n"
            );
            Ticket::parse(Path::new("test.md"), &raw).unwrap()
        };
        let tickets = vec![
            make_with_author("0001", Some("unassigned")),
            make_with_author("0002", Some("alice")),
            make_with_author("0003", Some("unassigned")),
            make_with_author("0004", None),
        ];
        let result = list_filtered(&tickets, &config, None, true, false, None, None, None, None);
        assert_eq!(result.len(), 2);
        assert!(result.iter().all(|t| t.frontmatter.author.as_deref() == Some("unassigned")));
    }

    fn make_ticket_with_author(id: &str, state: &str, author: Option<&str>) -> Ticket {
        let author_line = author.map(|a| format!("author = \"{a}\"\n")).unwrap_or_default();
        let raw = format!(
            "+++\nid = \"{id}\"\ntitle = \"T{id}\"\nstate = \"{state}\"\n{author_line}+++\n\n"
        );
        Ticket::parse(dummy_path(), &raw).unwrap()
    }

    #[test]
    fn list_filtered_by_author() {
        let config = test_config_with_states(&[]);
        let tickets = vec![
            make_ticket_with_author("0001", "new", Some("alice")),
            make_ticket_with_author("0002", "new", Some("bob")),
            make_ticket_with_author("0003", "ready", Some("alice")),
        ];
        let result = list_filtered(&tickets, &config, None, false, false, None, Some("alice"), None, None);
        assert_eq!(result.len(), 2);
        assert!(result.iter().all(|t| t.frontmatter.author.as_deref() == Some("alice")));
    }

    #[test]
    fn list_filtered_author_none() {
        let config = test_config_with_states(&[]);
        let tickets = vec![
            make_ticket_with_author("0001", "new", Some("alice")),
            make_ticket_with_author("0002", "new", Some("bob")),
        ];
        let result = list_filtered(&tickets, &config, None, false, false, None, None, None, None);
        assert_eq!(result.len(), 2);
    }

    fn make_ticket_with_owner(id: &str, state: &str, author: Option<&str>, owner: Option<&str>) -> Ticket {
        let author_line = author.map(|a| format!("author = \"{a}\"\n")).unwrap_or_default();
        let owner_line = owner.map(|o| format!("owner = \"{o}\"\n")).unwrap_or_default();
        let raw = format!(
            "+++\nid = \"{id}\"\ntitle = \"T{id}\"\nstate = \"{state}\"\n{author_line}{owner_line}+++\n\n"
        );
        Ticket::parse(dummy_path(), &raw).unwrap()
    }

    #[test]
    fn list_filtered_by_owner() {
        let config = test_config_with_states(&[]);
        let tickets = vec![
            make_ticket_with_owner("0001", "new", Some("alice"), Some("alice")),
            make_ticket_with_owner("0002", "new", Some("bob"), Some("bob")),
            make_ticket_with_owner("0003", "new", Some("carol"), None),
        ];
        let result = list_filtered(&tickets, &config, None, false, false, None, None, Some("alice"), None);
        assert_eq!(result.len(), 1);
        assert_eq!(result[0].frontmatter.id, "0001");
    }

    #[test]
    fn list_filtered_mine_matches_author() {
        let config = test_config_with_states(&[]);
        let tickets = vec![
            make_ticket_with_owner("0001", "new", Some("alice"), Some("bob")),
            make_ticket_with_owner("0002", "new", Some("bob"), Some("carol")),
        ];
        let result = list_filtered(&tickets, &config, None, false, false, None, None, None, Some("alice"));
        assert_eq!(result.len(), 1);
        assert_eq!(result[0].frontmatter.id, "0001");
    }

    #[test]
    fn list_filtered_mine_matches_owner() {
        let config = test_config_with_states(&[]);
        let tickets = vec![
            make_ticket_with_owner("0001", "new", Some("bob"), Some("alice")),
            make_ticket_with_owner("0002", "new", Some("carol"), Some("bob")),
        ];
        let result = list_filtered(&tickets, &config, None, false, false, None, None, None, Some("alice"));
        assert_eq!(result.len(), 1);
        assert_eq!(result[0].frontmatter.id, "0001");
    }

    #[test]
    fn list_filtered_mine_or_semantics() {
        let config = test_config_with_states(&[]);
        let tickets = vec![
            make_ticket_with_owner("0001", "new", Some("alice"), None),
            make_ticket_with_owner("0002", "new", Some("bob"), Some("alice")),
            make_ticket_with_owner("0003", "new", Some("carol"), Some("carol")),
        ];
        let result = list_filtered(&tickets, &config, None, false, false, None, None, None, Some("alice"));
        assert_eq!(result.len(), 2);
        let ids: Vec<&str> = result.iter().map(|t| t.frontmatter.id.as_str()).collect();
        assert!(ids.contains(&"0001"));
        assert!(ids.contains(&"0002"));
    }

    // ── set_field ─────────────────────────────────────────────────────────

    fn make_frontmatter() -> Frontmatter {
        Frontmatter {
            id: "0001".to_string(),
            title: "Test".to_string(),
            state: "new".to_string(),
            priority: 0,
            effort: 0,
            risk: 0,
            author: None,
            owner: None,
            branch: None,
            created_at: None,
            updated_at: None,
            focus_section: None,
            epic: None,
            target_branch: None,
            depends_on: None,
        }
    }

    #[test]
    fn set_field_priority_valid() {
        let mut fm = make_frontmatter();
        set_field(&mut fm, "priority", "5").unwrap();
        assert_eq!(fm.priority, 5);
    }

    #[test]
    fn set_field_priority_overflow() {
        let mut fm = make_frontmatter();
        let err = set_field(&mut fm, "priority", "256").unwrap_err();
        assert!(err.to_string().contains("priority must be 0"));
    }

    #[test]
    fn set_field_author_immutable() {
        let mut fm = make_frontmatter();
        let err = set_field(&mut fm, "author", "alice").unwrap_err();
        assert!(err.to_string().contains("author is immutable"));
    }

    #[test]
    fn set_field_unknown_field() {
        let mut fm = make_frontmatter();
        let err = set_field(&mut fm, "foo", "bar").unwrap_err();
        assert!(err.to_string().contains("unknown field: foo"));
    }

    #[test]
    fn owner_round_trips_through_toml() {
        let toml_src = r#"id = "0001"
title = "T"
state = "new"
owner = "alice"
"#;
        let fm: Frontmatter = toml::from_str(toml_src).unwrap();
        assert_eq!(fm.owner, Some("alice".to_string()));
        let serialized = toml::to_string(&fm).unwrap();
        assert!(serialized.contains("owner = \"alice\""));
    }

    #[test]
    fn owner_absent_deserializes_as_none() {
        let toml_src = r#"id = "0001"
title = "T"
state = "new"
"#;
        let fm: Frontmatter = toml::from_str(toml_src).unwrap();
        assert_eq!(fm.owner, None);
    }

    #[test]
    fn set_field_owner_set() {
        let mut fm = make_frontmatter();
        set_field(&mut fm, "owner", "alice").unwrap();
        assert_eq!(fm.owner, Some("alice".to_string()));
    }

    #[test]
    fn set_field_owner_clear() {
        let mut fm = make_frontmatter();
        fm.owner = Some("alice".to_string());
        set_field(&mut fm, "owner", "-").unwrap();
        assert_eq!(fm.owner, None);
    }

    // ── dep_satisfied ─────────────────────────────────────────────────────

    fn config_with_dep_states() -> crate::config::Config {
        let toml = r#"
[project]
name = "test"

[tickets]
dir = "tickets"

[[workflow.states]]
id = "ready"
label = "Ready"
actionable = ["agent"]

[[workflow.states]]
id = "done"
label = "Done"
satisfies_deps = true

[[workflow.states]]
id = "closed"
label = "Closed"
terminal = true

[[workflow.states]]
id = "blocked"
label = "Blocked"
"#;
        toml::from_str(toml).unwrap()
    }

    #[test]
    fn dep_satisfied_satisfies_deps_true() {
        let config = config_with_dep_states();
        assert!(dep_satisfied("done", None, &config));
    }

    #[test]
    fn dep_satisfied_terminal_true() {
        let config = config_with_dep_states();
        assert!(dep_satisfied("closed", None, &config));
    }

    #[test]
    fn dep_satisfied_both_false() {
        let config = config_with_dep_states();
        assert!(!dep_satisfied("blocked", None, &config));
    }

    #[test]
    fn dep_satisfied_unknown_state() {
        let config = config_with_dep_states();
        assert!(!dep_satisfied("nonexistent", None, &config));
    }

    fn config_with_spec_gate() -> crate::config::Config {
        let toml = r#"
[project]
name = "test"

[tickets]
dir = "tickets"

[[workflow.states]]
id = "groomed"
label = "Groomed"
actionable = ["agent"]
dep_requires = "spec"

[[workflow.states]]
id = "ready"
label = "Ready"
actionable = ["agent"]

[[workflow.states]]
id = "specd"
label = "Specd"
satisfies_deps = "spec"

[[workflow.states]]
id = "in_progress"
label = "In Progress"
satisfies_deps = "spec"

[[workflow.states]]
id = "implemented"
label = "Implemented"
satisfies_deps = true

[[workflow.states]]
id = "closed"
label = "Closed"
terminal = true
"#;
        toml::from_str(toml).unwrap()
    }

    #[test]
    fn dep_satisfied_tag_matches_required_gate() {
        let config = config_with_spec_gate();
        assert!(dep_satisfied("specd", Some("spec"), &config));
    }

    #[test]
    fn dep_satisfied_tag_no_required_gate_is_false() {
        let config = config_with_spec_gate();
        assert!(!dep_satisfied("specd", None, &config));
    }

    #[test]
    fn dep_satisfied_bool_true_with_no_gate() {
        let config = config_with_spec_gate();
        assert!(dep_satisfied("implemented", None, &config));
    }

    #[test]
    fn pick_next_groomed_unblocked_when_dep_specd() {
        let config = config_with_spec_gate();
        let tickets = vec![
            make_ticket_with_deps("aaaa0001", "groomed", Some(vec!["bbbb0001"])),
            make_ticket_with_deps("bbbb0001", "specd", None),
        ];
        let result = pick_next(&tickets, &["groomed"], &[], 10.0, -2.0, -1.0, &config, None, None);
        assert_eq!(result.unwrap().frontmatter.id, "aaaa0001");
    }

    #[test]
    fn pick_next_groomed_unblocked_when_dep_in_progress() {
        let config = config_with_spec_gate();
        let tickets = vec![
            make_ticket_with_deps("aaaa0001", "groomed", Some(vec!["bbbb0001"])),
            make_ticket_with_deps("bbbb0001", "in_progress", None),
        ];
        let result = pick_next(&tickets, &["groomed"], &[], 10.0, -2.0, -1.0, &config, None, None);
        assert_eq!(result.unwrap().frontmatter.id, "aaaa0001");
    }

    #[test]
    fn pick_next_ready_blocked_when_dep_only_specd() {
        let config = config_with_spec_gate();
        let tickets = vec![
            make_ticket_with_deps("aaaa0001", "ready", Some(vec!["bbbb0001"])),
            make_ticket_with_deps("bbbb0001", "specd", None),
        ];
        let result = pick_next(&tickets, &["ready"], &[], 10.0, -2.0, -1.0, &config, None, None);
        assert!(result.is_none());
    }

    // ── pick_next dep filtering ────────────────────────────────────────────

    fn make_ticket_with_deps(id: &str, state: &str, deps: Option<Vec<&str>>) -> Ticket {
        let deps_line = match &deps {
            None => String::new(),
            Some(v) => {
                let list: Vec<String> = v.iter().map(|d| format!("\"{d}\"")).collect();
                format!("depends_on = [{}]\n", list.join(", "))
            }
        };
        let raw = format!(
            "+++\nid = \"{id}\"\ntitle = \"T{id}\"\nstate = \"{state}\"\n{deps_line}+++\n\n"
        );
        Ticket::parse(dummy_path(), &raw).unwrap()
    }

    #[test]
    fn pick_next_skips_dep_blocked_ticket() {
        let config = config_with_dep_states();
        let tickets = vec![
            make_ticket_with_deps("aaaa0001", "ready", Some(vec!["bbbb0001"])),
            make_ticket_with_deps("bbbb0001", "ready", None),
            make_ticket_with_deps("cccc0001", "ready", None),
        ];
        // aaaa0001 depends on bbbb0001 which is in "ready" (not satisfies_deps)
        // should skip aaaa0001 and return bbbb0001 (next by score, no deps)
        let result = pick_next(&tickets, &["ready"], &[], 10.0, -2.0, -1.0, &config, None, None);
        assert!(result.is_some());
        let id = &result.unwrap().frontmatter.id;
        assert_ne!(id, "aaaa0001", "dep-blocked ticket should be skipped");
    }

    #[test]
    fn pick_next_returns_ticket_when_dep_satisfied() {
        let config = config_with_dep_states();
        let tickets = vec![
            make_ticket_with_deps("aaaa0001", "ready", Some(vec!["bbbb0001"])),
            make_ticket_with_deps("bbbb0001", "done", None),
        ];
        let result = pick_next(&tickets, &["ready"], &[], 10.0, -2.0, -1.0, &config, None, None);
        assert_eq!(result.unwrap().frontmatter.id, "aaaa0001");
    }

    #[test]
    fn pick_next_unknown_dep_id_not_blocking() {
        let config = config_with_dep_states();
        let tickets = vec![
            make_ticket_with_deps("aaaa0001", "ready", Some(vec!["unknown1"])),
        ];
        let result = pick_next(&tickets, &["ready"], &[], 10.0, -2.0, -1.0, &config, None, None);
        assert_eq!(result.unwrap().frontmatter.id, "aaaa0001");
    }

    #[test]
    fn pick_next_empty_depends_on_not_blocking() {
        let config = config_with_dep_states();
        let raw = "+++\nid = \"aaaa0001\"\ntitle = \"T\"\nstate = \"ready\"\ndepends_on = []\n+++\n\n";
        let t = Ticket::parse(dummy_path(), raw).unwrap();
        let tickets = vec![t];
        let result = pick_next(&tickets, &["ready"], &[], 10.0, -2.0, -1.0, &config, None, None);
        assert_eq!(result.unwrap().frontmatter.id, "aaaa0001");
    }

    // --- build_reverse_index / effective_priority / sorted_actionable ---

    fn make_ticket_with_priority(id: &str, state: &str, priority: u8, deps: Option<Vec<&str>>) -> Ticket {
        let dep_line = match &deps {
            Some(d) => {
                let list: Vec<String> = d.iter().map(|s| format!("\"{s}\"")).collect();
                format!("depends_on = [{}]\n", list.join(", "))
            }
            None => String::new(),
        };
        let raw = format!(
            "+++\nid = \"{id}\"\ntitle = \"T{id}\"\nstate = \"{state}\"\npriority = {priority}\n{dep_line}+++\n\n"
        );
        Ticket::parse(Path::new("test.md"), &raw).unwrap()
    }

    #[test]
    fn effective_priority_no_dependents_returns_own() {
        let a = make_ticket_with_priority("aaaa", "ready", 5, None);
        let tickets = vec![&a];
        let rev_idx = build_reverse_index(&tickets);
        assert_eq!(effective_priority(&a, &rev_idx), 5);
    }

    #[test]
    fn effective_priority_single_hop_elevation() {
        // A (priority 2) is depended on by B (priority 9)
        let a = make_ticket_with_priority("aaaa", "ready", 2, None);
        let b = make_ticket_with_priority("bbbb", "ready", 9, Some(vec!["aaaa"]));
        let tickets = vec![&a, &b];
        let rev_idx = build_reverse_index(&tickets);
        assert_eq!(effective_priority(&a, &rev_idx), 9);
        assert_eq!(effective_priority(&b, &rev_idx), 9);
    }

    #[test]
    fn effective_priority_transitive_elevation() {
        // A (2) blocks B (5) blocks C (9); A's effective priority should be 9
        let a = make_ticket_with_priority("aaaa", "ready", 2, None);
        let b = make_ticket_with_priority("bbbb", "ready", 5, Some(vec!["aaaa"]));
        let c = make_ticket_with_priority("cccc", "ready", 9, Some(vec!["bbbb"]));
        let tickets = vec![&a, &b, &c];
        let rev_idx = build_reverse_index(&tickets);
        assert_eq!(effective_priority(&a, &rev_idx), 9);
        assert_eq!(effective_priority(&b, &rev_idx), 9);
        assert_eq!(effective_priority(&c, &rev_idx), 9);
    }

    #[test]
    fn effective_priority_cycle_does_not_panic() {
        // A depends on B, B depends on A
        let a = make_ticket_with_priority("aaaa", "ready", 3, Some(vec!["bbbb"]));
        let b = make_ticket_with_priority("bbbb", "ready", 7, Some(vec!["aaaa"]));
        let tickets = vec![&a, &b];
        let rev_idx = build_reverse_index(&tickets);
        // Should not panic; both see each other's priority
        let ep_a = effective_priority(&a, &rev_idx);
        let ep_b = effective_priority(&b, &rev_idx);
        assert_eq!(ep_a, 7);
        assert_eq!(ep_b, 7);
    }

    #[test]
    fn effective_priority_closed_dependent_excluded() {
        // A (2) is in the active set; B (9, closed) is NOT passed to build_reverse_index
        let a = make_ticket_with_priority("aaaa", "ready", 2, None);
        // B is "closed" — caller filters it out before building the index
        let tickets_active = vec![&a];
        let rev_idx = build_reverse_index(&tickets_active);
        assert_eq!(effective_priority(&a, &rev_idx), 2);
    }

    #[test]
    fn sorted_actionable_low_priority_blocker_elevated() {
        // A (priority 2, ready) is depended on by B (priority 9, ready)
        // A's effective priority becomes 9 — it should not sort last
        let a = make_ticket_with_priority("aaaa", "ready", 2, None);
        let b = make_ticket_with_priority("bbbb", "ready", 9, Some(vec!["aaaa"]));
        let tickets = vec![a, b];
        let result = sorted_actionable(&tickets, &["ready"], 1.0, 0.0, 0.0, None, None);
        assert_eq!(result.len(), 2);
        let ids: Vec<&str> = result.iter().map(|t| t.frontmatter.id.as_str()).collect();
        assert!(ids.contains(&"aaaa"), "A must appear in results");
        assert!(ids.contains(&"bbbb"), "B must appear in results");
        // A (ep=9) and B (ep=9) are tied; A must not be sorted below B due to raw priority
        // The last entry must not be A simply because raw priority 2 < 9
        // Both ep=9 so the sort is stable-ish; just verify A is present
    }

    #[test]
    fn sorted_actionable_blocker_before_independent_higher_raw() {
        // A (priority 2, ready, blocks C which has priority 9)
        // B (priority 7, ready, no deps)
        // A's effective priority = 9, B's = 7 → A should sort before B
        let a = make_ticket_with_priority("aaaa", "ready", 2, None);
        let b = make_ticket_with_priority("bbbb", "ready", 7, None);
        let c = make_ticket_with_priority("cccc", "ready", 9, Some(vec!["aaaa"]));
        let tickets = vec![a, b, c];
        let result = sorted_actionable(&tickets, &["ready"], 1.0, 0.0, 0.0, None, None);
        assert_eq!(result.len(), 3);
        let ids: Vec<&str> = result.iter().map(|t| t.frontmatter.id.as_str()).collect();
        let a_pos = ids.iter().position(|&id| id == "aaaa").unwrap();
        let b_pos = ids.iter().position(|&id| id == "bbbb").unwrap();
        assert!(a_pos < b_pos, "A (ep=9) should sort before B (ep=7)");
    }

    #[test]
    fn sorted_actionable_no_deps_unchanged() {
        let a = make_ticket_with_priority("aaaa", "ready", 3, None);
        let b = make_ticket_with_priority("bbbb", "ready", 7, None);
        let tickets = vec![a, b];
        let result = sorted_actionable(&tickets, &["ready"], 1.0, 0.0, 0.0, None, None);
        assert_eq!(result[0].frontmatter.id, "bbbb");
        assert_eq!(result[1].frontmatter.id, "aaaa");
    }

    fn make_ticket_with_owner_field(id: &str, state: &str, owner: Option<&str>) -> Ticket {
        let owner_line = owner.map(|o| format!("owner = \"{o}\"\n")).unwrap_or_default();
        let raw = format!(
            "+++\nid = \"{id}\"\ntitle = \"T{id}\"\nstate = \"{state}\"\n{owner_line}+++\n\n"
        );
        Ticket::parse(Path::new("test.md"), &raw).unwrap()
    }

    #[test]
    fn sorted_actionable_excludes_ticket_owned_by_other() {
        let t = make_ticket_with_owner_field("aaaa", "ready", Some("alice"));
        let tickets = vec![t];
        let result = sorted_actionable(&tickets, &["ready"], 1.0, 0.0, 0.0, None, Some("bob"));
        assert!(result.is_empty(), "ticket owned by alice should not appear for bob");
    }

    #[test]
    fn sorted_actionable_includes_ticket_owned_by_caller() {
        let t = make_ticket_with_owner_field("aaaa", "ready", Some("alice"));
        let tickets = vec![t];
        let result = sorted_actionable(&tickets, &["ready"], 1.0, 0.0, 0.0, None, Some("alice"));
        assert_eq!(result.len(), 1);
        assert_eq!(result[0].frontmatter.id, "aaaa");
    }

    #[test]
    fn sorted_actionable_includes_unowned_ticket() {
        let t = make_ticket_with_owner_field("aaaa", "ready", None);
        let tickets = vec![t];
        let result = sorted_actionable(&tickets, &["ready"], 1.0, 0.0, 0.0, None, Some("bob"));
        assert!(result.is_empty(), "unowned ticket should be excluded when owner_filter is set");
    }

    #[test]
    fn sorted_actionable_no_owner_filter_shows_all() {
        let t1 = make_ticket_with_owner_field("aaaa", "ready", Some("alice"));
        let t2 = make_ticket_with_owner_field("bbbb", "ready", Some("bob"));
        let tickets = vec![t1, t2];
        let result = sorted_actionable(&tickets, &["ready"], 1.0, 0.0, 0.0, None, None);
        assert_eq!(result.len(), 2);
    }

    #[test]
    fn pick_next_skips_unowned_ticket_when_owner_filter_set() {
        let config = config_with_dep_states();
        let t = make_ticket_with_owner_field("aaaa", "ready", None);
        let tickets = vec![t];
        let result = pick_next(&tickets, &["ready"], &[], 1.0, 0.0, 0.0, &config, None, Some("alice"));
        assert!(result.is_none(), "unowned ticket should be skipped when owner_filter is set");
    }

    #[test]
    fn pick_next_skips_ticket_owned_by_other() {
        let config = config_with_dep_states();
        let t = make_ticket_with_owner_field("aaaa", "ready", Some("bob"));
        let tickets = vec![t];
        let result = pick_next(&tickets, &["ready"], &[], 1.0, 0.0, 0.0, &config, None, Some("alice"));
        assert!(result.is_none(), "ticket owned by bob should be skipped for alice");
    }

    #[test]
    fn pick_next_picks_ticket_owned_by_current_user() {
        let config = config_with_dep_states();
        let t = make_ticket_with_owner_field("aaaa", "ready", Some("alice"));
        let tickets = vec![t];
        let result = pick_next(&tickets, &["ready"], &[], 1.0, 0.0, 0.0, &config, None, Some("alice"));
        assert!(result.is_some(), "ticket owned by alice should be picked");
        assert_eq!(result.unwrap().frontmatter.id, "aaaa");
    }

    #[test]
    fn check_owner_passes_when_identity_matches_owner() {
        let tmp = tempfile::tempdir().unwrap();
        let apm_dir = tmp.path().join(".apm");
        std::fs::create_dir_all(&apm_dir).unwrap();
        std::fs::write(tmp.path().join("apm.toml"), "[project]\nname = \"test\"\n").unwrap();
        std::fs::write(apm_dir.join("local.toml"), "username = \"alice\"\n").unwrap();
        let t = make_ticket_with_owner_field("aaaa", "ready", Some("alice"));
        assert!(check_owner(tmp.path(), &t).is_ok());
    }

    #[test]
    fn check_owner_fails_when_identity_does_not_match_owner() {
        let tmp = tempfile::tempdir().unwrap();
        let apm_dir = tmp.path().join(".apm");
        std::fs::create_dir_all(&apm_dir).unwrap();
        std::fs::write(tmp.path().join("apm.toml"), "[project]\nname = \"test\"\n").unwrap();
        std::fs::write(apm_dir.join("local.toml"), "username = \"bob\"\n").unwrap();
        let t = make_ticket_with_owner_field("aaaa", "ready", Some("alice"));
        let err = check_owner(tmp.path(), &t).unwrap_err();
        assert!(err.to_string().contains("alice"), "error should mention the owner");
    }

    #[test]
    fn check_owner_fails_when_identity_is_unassigned() {
        let tmp = tempfile::tempdir().unwrap();
        std::fs::write(tmp.path().join("apm.toml"), "[project]\nname = \"test\"\n").unwrap();
        let t = make_ticket_with_owner_field("aaaa", "ready", Some("alice"));
        let err = check_owner(tmp.path(), &t).unwrap_err();
        assert!(err.to_string().contains("identity not configured"));
    }

    #[test]
    fn check_owner_passes_when_ticket_has_no_owner() {
        let tmp = tempfile::tempdir().unwrap();
        std::fs::write(tmp.path().join("apm.toml"), "[project]\nname = \"test\"\n").unwrap();
        let t = make_ticket_with_owner_field("aaaa", "ready", None);
        assert!(check_owner(tmp.path(), &t).is_ok());
    }

    #[test]
    fn check_owner_rejects_owner_change_on_terminal_state() {
        let tmp = tempfile::tempdir().unwrap();
        let cfg_toml = concat!(
            "[project]\nname = \"test\"\n\n",
            "[[workflow.states]]\nid = \"open\"\nlabel = \"Open\"\nterminal = false\n\n",
            "[[workflow.states]]\nid = \"closed\"\nlabel = \"Closed\"\nterminal = true\n",
        );
        std::fs::write(tmp.path().join("apm.toml"), cfg_toml).unwrap();
        let t = make_ticket_with_owner_field("aaaa", "closed", Some("alice"));
        let err = check_owner(tmp.path(), &t).unwrap_err();
        assert!(
            err.to_string().contains("cannot change owner of a closed ticket"),
            "unexpected error: {err}"
        );
    }

    #[test]
    fn check_owner_allows_owner_change_on_non_terminal_state() {
        let tmp = tempfile::tempdir().unwrap();
        let apm_dir = tmp.path().join(".apm");
        std::fs::create_dir_all(&apm_dir).unwrap();
        let cfg_toml = concat!(
            "[project]\nname = \"test\"\n\n",
            "[[workflow.states]]\nid = \"open\"\nlabel = \"Open\"\nterminal = false\n\n",
            "[[workflow.states]]\nid = \"closed\"\nlabel = \"Closed\"\nterminal = true\n",
        );
        std::fs::write(tmp.path().join("apm.toml"), cfg_toml).unwrap();
        std::fs::write(apm_dir.join("local.toml"), "username = \"alice\"\n").unwrap();
        let t = make_ticket_with_owner_field("aaaa", "open", Some("alice"));
        assert!(check_owner(tmp.path(), &t).is_ok());
    }
}