tkt 0.1.0

Git-native ticket CLI — frontmatter-driven work tracking in .tickets/
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
use std::path::{Path, PathBuf};
use std::sync::LazyLock;

use anyhow::{bail, Context, Result};
use regex::Regex;

// --- Constants ---

pub const STATUS_VALUES: &[&str] = &["open", "in_progress", "done", "backlog"];
pub const ENV_VALUES: &[&str] = &["corp", "personal", "either"];

// --- Compiled regex patterns ---

static RE_FM_KEY: LazyLock<Regex> =
    LazyLock::new(|| Regex::new(r"^([A-Za-z_][A-Za-z0-9_-]*):(.*)$").unwrap());
static RE_NUMERIC_PREFIX: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"^(\d+)(.*)$").unwrap());
static RE_FILENAME_ID: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"^(\d+)-").unwrap());

// --- Enums ---

/// Ticket lifecycle status.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Status {
    Backlog,
    Open,
    InProgress,
    Done,
}

impl Status {
    pub fn parse(s: &str) -> Result<Self> {
        match s {
            "backlog" => Ok(Status::Backlog),
            "open" => Ok(Status::Open),
            "in_progress" => Ok(Status::InProgress),
            "done" => Ok(Status::Done),
            other => bail!(
                "invalid status {:?} (expected backlog/open/in_progress/done)",
                other
            ),
        }
    }

    pub fn as_str(&self) -> &'static str {
        match self {
            Status::Backlog => "backlog",
            Status::Open => "open",
            Status::InProgress => "in_progress",
            Status::Done => "done",
        }
    }
}

impl std::fmt::Display for Status {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(self.as_str())
    }
}

/// Environment filter for tickets.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Env {
    Corp,
    Personal,
    Either,
}

impl Env {
    pub fn parse(s: &str) -> Result<Self> {
        match s {
            "corp" => Ok(Env::Corp),
            "personal" => Ok(Env::Personal),
            "either" => Ok(Env::Either),
            other => bail!("invalid env {:?} (expected corp/personal/either)", other),
        }
    }

    pub fn as_str(&self) -> &'static str {
        match self {
            Env::Corp => "corp",
            Env::Personal => "personal",
            Env::Either => "either",
        }
    }
}

impl std::fmt::Display for Env {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(self.as_str())
    }
}

/// Ticket priority level.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Priority {
    Urgent,
    High,
    Medium,
    Low,
}

impl Priority {
    pub fn as_str(&self) -> &'static str {
        match self {
            Priority::Urgent => "urgent",
            Priority::High => "high",
            Priority::Medium => "medium",
            Priority::Low => "low",
        }
    }

    /// Numeric sort key: lower = higher priority.
    pub fn sort_key(&self) -> u8 {
        match self {
            Priority::Urgent => 0,
            Priority::High => 1,
            Priority::Medium => 2,
            Priority::Low => 3,
        }
    }

    /// Parse a priority string. Returns None for unknown values (lenient).
    pub fn parse(s: &str) -> Option<Self> {
        match s.trim().trim_matches('"') {
            "urgent" => Some(Priority::Urgent),
            "high" => Some(Priority::High),
            "medium" => Some(Priority::Medium),
            "low" => Some(Priority::Low),
            _ => None,
        }
    }
}

impl std::fmt::Display for Priority {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(self.as_str())
    }
}

// --- AC types ---

/// Specifies which acceptance criteria boxes to check.
pub enum AcSelection<'a> {
    /// Check all unchecked boxes.
    All,
    /// Check specific 1-based indices.
    Indices(&'a [u32]),
}

/// Stats about acceptance criteria state after a check operation.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct AcStats {
    pub checked: usize,
    pub unchecked: usize,
    pub total: usize,
}

// --- Compiled regex for AC manipulation ---

static RE_UNCHECKED_AC: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"- \[ \]").unwrap());
static RE_CHECKED_AC: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"- \[x\]").unwrap());

// --- TicketFile ---

/// Raw frontmatter editor. Preserves field ordering and unknown fields for surgical edits.
#[derive(Debug, Clone)]
pub struct TicketFile {
    pub path: PathBuf,
    /// Ordered frontmatter entries: (key, raw_value) — key="" for blank lines.
    pub fm: Vec<(String, String)>,
    /// Everything after the closing --- fence.
    pub body: String,
}

impl TicketFile {
    /// Parse a ticket file from disk.
    pub fn parse(path: &Path) -> Result<Self> {
        let content =
            std::fs::read_to_string(path).with_context(|| format!("reading {}", path.display()))?;
        Self::parse_str(&content, path)
    }

    /// Parse from a string (for testing).
    pub fn parse_str(content: &str, path: &Path) -> Result<Self> {
        let lines: Vec<&str> = content.split('\n').collect();

        if lines.is_empty() || !is_fence(lines[0]) {
            bail!("{}: no opening frontmatter fence on line 1", path.display());
        }

        let key_re = &*RE_FM_KEY;
        let mut fm: Vec<(String, String)> = Vec::new();
        let mut close_idx = None;

        for (i, line) in lines.iter().enumerate().skip(1) {
            if is_fence(line) {
                close_idx = Some(i);
                break;
            }
            if let Some(caps) = key_re.captures(line) {
                fm.push((caps[1].to_string(), caps[2].to_string()));
            } else if (line.starts_with(' ') || line.starts_with('\t')) && !fm.is_empty() {
                let last = fm.last_mut().unwrap();
                last.1.push('\n');
                last.1.push_str(line);
            } else if line.trim().is_empty() {
                fm.push((String::new(), (*line).to_string()));
            } else {
                bail!(
                    "{}: unparseable frontmatter line {}: {:?}",
                    path.display(),
                    i + 1,
                    line
                );
            }
        }

        let close_idx = close_idx
            .ok_or_else(|| anyhow::anyhow!("{}: no closing frontmatter fence", path.display()))?;

        for req in &["id", "title", "status", "blocked_by"] {
            if !fm.iter().any(|(k, _)| k == req) {
                bail!("{}: missing required field: {}", path.display(), req);
            }
        }

        let body = lines[close_idx + 1..].join("\n");

        Ok(TicketFile {
            path: path.to_owned(),
            fm,
            body,
        })
    }

    /// Get a field's trimmed value.
    pub fn get(&self, key: &str) -> Option<&str> {
        self.fm
            .iter()
            .find(|(k, _)| k == key)
            .map(|(_, v)| v.trim())
    }

    /// Set a field value (raw text). Replaces if exists, appends if not.
    pub fn set_field(&mut self, key: &str, value: &str) {
        for (k, v) in self.fm.iter_mut() {
            if k == key {
                *v = format!(" {}", value);
                return;
            }
        }
        self.fm.push((key.to_string(), format!(" {}", value)));
    }

    /// Remove a field entirely (for clearing optional fields).
    pub fn remove_field(&mut self, key: &str) {
        self.fm.retain(|(k, _)| k != key);
    }

    // --- Typed mutation methods ---

    /// Set the ticket status (typed — no raw strings in callers).
    pub fn set_status(&mut self, status: Status) {
        self.set_field("status", status.as_str());
    }

    /// Set the blocked_by array (handles YAML formatting internally).
    pub fn set_blocked_by(&mut self, ids: &[impl AsRef<str>]) {
        let formatted = ids
            .iter()
            .map(|d| format!("\"{}\"", yaml_scalar_escape(d.as_ref())))
            .collect::<Vec<_>>()
            .join(", ");
        self.set_field("blocked_by", &format!("[{}]", formatted));
    }

    /// Set or clear the priority field.
    pub fn set_priority(&mut self, priority: Option<Priority>) {
        match priority {
            Some(p) => self.set_field("priority", p.as_str()),
            None => self.remove_field("priority"),
        }
    }

    /// Set or clear the env field.
    pub fn set_env(&mut self, env: Option<Env>) {
        match env {
            Some(e) => self.set_field("env", e.as_str()),
            None => self.remove_field("env"),
        }
    }

    /// Append a resolution section to the body (idempotent — skips if already present).
    pub fn append_resolution(&mut self, date: &str, note: &str, spike_branch: Option<&str>) {
        if self.body.contains("## Resolution") {
            return;
        }
        let branch_note = spike_branch
            .map(|b| format!("\n\nSpike branch: {}", b))
            .unwrap_or_default();
        self.body = format!(
            "{}\n\n## Resolution ({})\n\n{}{}\n",
            self.body.trim_end(),
            date,
            note,
            branch_note
        );
    }

    /// Check acceptance criteria boxes. Returns stats about the final state.
    pub fn check_acs(&mut self, selection: AcSelection) -> AcStats {
        let range = match crate::core::ac_section_range(&self.body) {
            Some(r) => r,
            None => {
                return AcStats {
                    checked: 0,
                    unchecked: 0,
                    total: 0,
                }
            }
        };

        match selection {
            AcSelection::All => {
                let section = self.body[range.clone()].replace("- [ ]", "- [x]");
                self.body.replace_range(range.clone(), &section);
            }
            AcSelection::Indices(indices) => {
                let section = &self.body[range.clone()];
                let offsets: Vec<(usize, usize)> = RE_UNCHECKED_AC
                    .find_iter(section)
                    .map(|m| (range.start + m.start(), range.start + m.end()))
                    .collect();
                // Apply in reverse order to preserve offsets
                for &idx in indices.iter().rev() {
                    let i = (idx as usize).saturating_sub(1);
                    if i < offsets.len() {
                        let (abs_start, abs_end) = offsets[i];
                        self.body.replace_range(abs_start..abs_end, "- [x]");
                    }
                }
            }
        }

        // Compute final stats
        self.ac_stats()
    }

    /// Get current acceptance criteria stats without modifying anything.
    pub fn ac_stats(&self) -> AcStats {
        let range = match crate::core::ac_section_range(&self.body) {
            Some(r) => r,
            None => {
                return AcStats {
                    checked: 0,
                    unchecked: 0,
                    total: 0,
                }
            }
        };
        let section = &self.body[range];
        let unchecked = RE_UNCHECKED_AC.find_iter(section).count();
        let checked = RE_CHECKED_AC.find_iter(section).count();
        AcStats {
            checked,
            unchecked,
            total: checked + unchecked,
        }
    }

    /// Serialize preserving raw frontmatter (surgical writes).
    pub fn serialize(&self) -> String {
        let mut parts = vec!["---".to_string()];
        for (k, v) in &self.fm {
            if k.is_empty() {
                parts.push(v.clone());
            } else {
                parts.push(format!("{}:{}", k, v));
            }
        }
        parts.push("---".to_string());
        let header = parts.join("\n");
        format!("{}\n{}", header, self.body)
    }

    /// Write the ticket file back to disk.
    pub fn write(&self) -> Result<()> {
        std::fs::write(&self.path, self.serialize())
            .with_context(|| format!("writing {}", self.path.display()))
    }
}

// --- Ticket ---

/// A parsed, validated ticket with typed fields. Constructed from a TicketFile.
/// Field access is zero-cost (&str borrows on owned Strings).
#[derive(Debug, Clone)]
pub struct Ticket {
    pub id: String,
    pub title: String,
    pub status: Status,
    pub blocked_by: Vec<String>,
    pub env: Env,
    pub priority: Option<Priority>,
    pub spec: Option<String>,
    pub path: PathBuf,
    pub body: String,
    /// The underlying raw file for mutations.
    pub file: TicketFile,
}

impl Ticket {
    /// Parse and validate a ticket from disk.
    pub fn parse(path: &Path) -> Result<Self> {
        let file = TicketFile::parse(path)?;
        Self::from_file(file)
    }

    /// Parse and validate from a string (for testing).
    #[allow(dead_code)]
    pub fn parse_str(content: &str, path: &Path) -> Result<Self> {
        let file = TicketFile::parse_str(content, path)?;
        Self::from_file(file)
    }

    /// Construct a validated Ticket from a TicketFile.
    fn from_file(file: TicketFile) -> Result<Self> {
        let path_display = file.path.display().to_string();

        let raw_id = file.get("id").unwrap_or("");
        let id = yaml_scalar_unescape(raw_id.trim_matches('"').trim_matches('\''));

        let raw_title = file.get("title").unwrap_or("");
        let title = yaml_scalar_unescape(raw_title.trim_matches('"').trim_matches('\''));

        let raw_status = file.get("status").unwrap_or("");
        let status =
            Status::parse(raw_status).with_context(|| format!("{}: bad status", path_display))?;

        let blocked_by = parse_blocked_by(file.get("blocked_by").unwrap_or(""));

        let raw_env = file
            .get("env")
            .map(|v| v.trim_matches('"'))
            .unwrap_or("either");
        let env = Env::parse(raw_env).with_context(|| format!("{}: bad env", path_display))?;

        let priority = match file.get("priority").map(|v| v.trim_matches('"')) {
            Some(s) => Priority::parse(s),
            None => None,
        };

        let spec = file
            .get("spec")
            .map(|v| yaml_scalar_unescape(v.trim_matches('"').trim_matches('\'')));

        Ok(Ticket {
            id,
            title,
            status,
            blocked_by,
            env,
            priority,
            spec,
            path: file.path.clone(),
            body: file.body.clone(),
            file,
        })
    }

    /// Numeric sort key: (numeric prefix, remainder).
    pub fn numeric_key(&self) -> (u64, String) {
        let re = &*RE_NUMERIC_PREFIX;
        match re.captures(&self.id) {
            Some(caps) => (caps[1].parse().unwrap_or(u64::MAX), caps[2].to_string()),
            None => (u64::MAX, self.id.clone()),
        }
    }

    /// Whether this ticket has high or urgent priority.
    pub fn is_high_priority(&self) -> bool {
        matches!(self.priority, Some(Priority::Urgent) | Some(Priority::High))
    }

    /// Sort key for priority: urgent(0) > high(1) > default/medium(2) > low(3).
    pub fn priority_sort_key(&self) -> u8 {
        self.priority.map(|p| p.sort_key()).unwrap_or(2) // None = medium/default
    }
}

// --- Corpus ---

/// Load all tickets from a .tickets/ directory.
pub fn load_corpus(dir: &Path) -> Result<Vec<Ticket>> {
    let mut tickets = Vec::new();
    let mut entries: Vec<_> = std::fs::read_dir(dir)
        .with_context(|| format!("reading {}", dir.display()))?
        .filter_map(|e| e.ok())
        .filter(|e| e.path().extension().is_some_and(|ext| ext == "md"))
        .collect();
    entries.sort_by_key(|e| e.file_name());

    for entry in entries {
        match Ticket::parse(&entry.path()) {
            Ok(ticket) => tickets.push(ticket),
            Err(e) => {
                eprintln!("⚠ skipping {}: {}", entry.file_name().to_string_lossy(), e);
            }
        }
    }
    Ok(tickets)
}

/// Compute the frontier: open tickets with all deps done, env-filtered, priority-sorted.
pub fn frontier(corpus: &[Ticket]) -> Vec<&Ticket> {
    frontier_with_default_env(corpus, "")
}

/// Compute the frontier with a fallback default_env (from project config).
/// CREW_ENV env var takes priority over the default.
pub fn frontier_with_default_env<'a>(corpus: &'a [Ticket], default_env: &str) -> Vec<&'a Ticket> {
    let crew_env = std::env::var("CREW_ENV").unwrap_or_default();
    let effective_env = if crew_env.is_empty() {
        default_env
    } else {
        &crew_env
    };
    let done: std::collections::HashSet<&str> = corpus
        .iter()
        .filter(|t| t.status == Status::Done)
        .map(|t| t.id.as_str())
        .collect();

    let mut pool: Vec<&Ticket> = corpus
        .iter()
        .filter(|t| {
            if t.status != Status::Open {
                return false;
            }
            if !t.blocked_by.iter().all(|dep| done.contains(dep.as_str())) {
                return false;
            }
            if !effective_env.is_empty() && t.env != Env::Either && t.env.as_str() != effective_env
            {
                return false;
            }
            true
        })
        .collect();

    pool.sort_by_key(|t| (t.priority_sort_key(), t.numeric_key()));
    pool
}

/// Find the maximum numeric id in a list of filenames.
pub fn max_id(names: &[String]) -> u64 {
    names
        .iter()
        .filter_map(|n| {
            RE_FILENAME_ID
                .captures(n)
                .map(|c| c[1].parse::<u64>().unwrap_or(0))
        })
        .max()
        .unwrap_or(0)
}

/// Determine the id zero-padding width from existing filenames.
pub fn id_width(names: &[String]) -> usize {
    names
        .iter()
        .filter_map(|n| RE_FILENAME_ID.captures(n).map(|c| c[1].len()))
        .max()
        .unwrap_or(2)
}

/// Find a ticket by id in the corpus.
pub fn find_ticket<'a>(corpus: &'a [Ticket], id: &str) -> Result<&'a Ticket> {
    corpus
        .iter()
        .find(|t| t.id == id)
        .ok_or_else(|| anyhow::anyhow!("no ticket with id {}", id))
}

// --- Helpers ---

fn is_fence(line: &str) -> bool {
    let trimmed = line.trim();
    trimmed == "---"
}

/// Parse a blocked_by field value into a Vec of IDs.
fn parse_blocked_by(raw: &str) -> Vec<String> {
    let trimmed = raw.trim();
    if !trimmed.starts_with('[') || !trimmed.ends_with(']') {
        return Vec::new();
    }
    let inner = &trimmed[1..trimmed.len() - 1];
    inner
        .split(',')
        .map(|s| s.trim().trim_matches('"').trim_matches('\'').to_string())
        .filter(|s| !s.is_empty())
        .collect()
}

// --- YAML/JSON escaping ---

/// Escape a string for use inside YAML double-quoted scalars.
pub fn yaml_scalar_escape(s: &str) -> String {
    let mut out = String::with_capacity(s.len() + 8);
    for c in s.chars() {
        match c {
            '\\' => out.push_str("\\\\"),
            '"' => out.push_str("\\\""),
            '\n' => out.push_str("\\n"),
            '\r' => out.push_str("\\r"),
            '\t' => out.push_str("\\t"),
            '\0' => out.push_str("\\0"),
            c if c.is_control() => {
                out.push_str(&format!("\\x{:02X}", c as u32));
            }
            _ => out.push(c),
        }
    }
    out
}

/// Decode YAML double-quoted scalar escapes when reading values.
pub fn yaml_scalar_unescape(s: &str) -> String {
    let mut out = String::with_capacity(s.len());
    let mut chars = s.chars();
    while let Some(c) = chars.next() {
        if c == '\\' {
            match chars.next() {
                Some('\\') => out.push('\\'),
                Some('"') => out.push('"'),
                Some('n') => out.push('\n'),
                Some('r') => out.push('\r'),
                Some('t') => out.push('\t'),
                Some('0') => out.push('\0'),
                Some('x') => {
                    let hex: String = chars.by_ref().take(2).collect();
                    if let Ok(byte) = u8::from_str_radix(&hex, 16) {
                        out.push(byte as char);
                    } else {
                        out.push_str("\\x");
                        out.push_str(&hex);
                    }
                }
                Some(other) => {
                    out.push('\\');
                    out.push(other);
                }
                None => out.push('\\'),
            }
        } else {
            out.push(c);
        }
    }
    out
}

/// Escape a string for use inside a JSON string value (between quotes).
pub fn json_string_escape(s: &str) -> String {
    let mut out = String::with_capacity(s.len() + 8);
    for c in s.chars() {
        match c {
            '\\' => out.push_str("\\\\"),
            '"' => out.push_str("\\\""),
            '\n' => out.push_str("\\n"),
            '\r' => out.push_str("\\r"),
            '\t' => out.push_str("\\t"),
            c if c.is_control() => {
                out.push_str(&format!("\\u{:04X}", c as u32));
            }
            _ => out.push(c),
        }
    }
    out
}

/// Generate the text for a new ticket file.
pub fn new_ticket_text(
    id: &str,
    title: &str,
    blocked_by: &[String],
    env: Option<&str>,
    spec: Option<&str>,
    priority: Option<&str>,
    status: Option<&str>,
) -> String {
    let status_val = status.unwrap_or("open");
    let mut fm_lines = vec![
        format!("id: \"{}\"", yaml_scalar_escape(id)),
        format!("title: \"{}\"", yaml_scalar_escape(title)),
        format!("status: {}", status_val),
    ];
    let deps = blocked_by
        .iter()
        .map(|d| format!("\"{}\"", yaml_scalar_escape(d)))
        .collect::<Vec<_>>()
        .join(", ");
    fm_lines.push(format!("blocked_by: [{}]", deps));
    if let Some(e) = env {
        fm_lines.push(format!("env: {}", e));
    }
    if let Some(s) = spec {
        fm_lines.push(format!("spec: \"{}\"", yaml_scalar_escape(s)));
    }
    if let Some(p) = priority {
        fm_lines.push(format!("priority: {}", p));
    }
    let body = format!(
        "\n# {}\n\n## What to build\n\nTBD\n\n## Acceptance criteria\n\n- [ ] TBD\n",
        title
    );
    format!("---\n{}\n---\n{}", fm_lines.join("\n"), body)
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::io::Write;
    use tempfile::NamedTempFile;

    #[test]
    fn parse_basic_ticket() {
        let mut f = NamedTempFile::new().unwrap();
        write!(f, "---\nid: \"01\"\ntitle: \"Test ticket\"\nstatus: open\nblocked_by: []\n---\n\n# Test\n\n- [ ] AC1\n").unwrap();
        let t = Ticket::parse(f.path()).unwrap();
        assert_eq!(t.id, "01");
        assert_eq!(t.title, "Test ticket");
        assert_eq!(t.status, Status::Open);
        assert!(t.blocked_by.is_empty());
    }

    #[test]
    fn parse_ticket_with_deps() {
        let mut f = NamedTempFile::new().unwrap();
        write!(f, "---\nid: \"05\"\ntitle: \"Depends on others\"\nstatus: open\nblocked_by: [\"01\", \"03\"]\npriority: high\nenv: corp\nspec: \"my-spec\"\n---\n\n# Body\n").unwrap();
        let t = Ticket::parse(f.path()).unwrap();
        assert_eq!(t.blocked_by, vec!["01", "03"]);
        assert!(t.is_high_priority());
        assert_eq!(t.env, Env::Corp);
        assert_eq!(t.spec.as_deref(), Some("my-spec"));
    }

    #[test]
    fn frontier_filters_correctly() {
        let content_done = "---\nid: \"01\"\ntitle: \"Done\"\nstatus: done\nblocked_by: []\n---\n";
        let content_open =
            "---\nid: \"02\"\ntitle: \"Open\"\nstatus: open\nblocked_by: [\"01\"]\n---\n";
        let content_blocked =
            "---\nid: \"03\"\ntitle: \"Blocked\"\nstatus: open\nblocked_by: [\"99\"]\n---\n";

        let dir = tempfile::tempdir().unwrap();
        std::fs::write(dir.path().join("01-done.md"), content_done).unwrap();
        std::fs::write(dir.path().join("02-open.md"), content_open).unwrap();
        std::fs::write(dir.path().join("03-blocked.md"), content_blocked).unwrap();

        let corpus = load_corpus(dir.path()).unwrap();
        let front = frontier(&corpus);
        assert_eq!(front.len(), 1);
        assert_eq!(front[0].id, "02");
    }

    #[test]
    fn frontier_excludes_backlog() {
        let dir = tempfile::tempdir().unwrap();
        std::fs::write(
            dir.path().join("01-open.md"),
            "---\nid: \"01\"\ntitle: \"Open\"\nstatus: open\nblocked_by: []\n---\n",
        )
        .unwrap();
        std::fs::write(
            dir.path().join("02-backlog.md"),
            "---\nid: \"02\"\ntitle: \"Backlog\"\nstatus: backlog\nblocked_by: []\n---\n",
        )
        .unwrap();

        let corpus = load_corpus(dir.path()).unwrap();
        let front = frontier(&corpus);
        assert_eq!(front.len(), 1);
        assert_eq!(front[0].id, "01");
    }

    #[test]
    fn frontier_sorts_by_priority_buckets() {
        let dir = tempfile::tempdir().unwrap();
        std::fs::write(
            dir.path().join("01-low.md"),
            "---\nid: \"01\"\ntitle: \"Low\"\nstatus: open\nblocked_by: []\npriority: low\n---\n",
        )
        .unwrap();
        std::fs::write(
            dir.path().join("02-none.md"),
            "---\nid: \"02\"\ntitle: \"Default\"\nstatus: open\nblocked_by: []\n---\n",
        )
        .unwrap();
        std::fs::write(
            dir.path().join("03-urgent.md"),
            "---\nid: \"03\"\ntitle: \"Urgent\"\nstatus: open\nblocked_by: []\npriority: urgent\n---\n",
        )
        .unwrap();
        std::fs::write(
            dir.path().join("04-high.md"),
            "---\nid: \"04\"\ntitle: \"High\"\nstatus: open\nblocked_by: []\npriority: high\n---\n",
        )
        .unwrap();
        std::fs::write(
            dir.path().join("05-medium.md"),
            "---\nid: \"05\"\ntitle: \"Medium\"\nstatus: open\nblocked_by: []\npriority: medium\n---\n",
        )
        .unwrap();

        let corpus = load_corpus(dir.path()).unwrap();
        let front = frontier(&corpus);
        let ids: Vec<&str> = front.iter().map(|t| t.id.as_str()).collect();
        // urgent(03) → high(04) → default/medium(02,05) → low(01)
        assert_eq!(ids, vec!["03", "04", "02", "05", "01"]);
    }

    #[test]
    fn surgical_edit_preserves_unknown_fields() {
        let content = "---\nid: \"01\"\ntitle: \"Test\"\nstatus: open\nblocked_by: []\ncustom_field: hello\n---\n\n# Body\n";
        let mut t = Ticket::parse_str(content, Path::new("test.md")).unwrap();
        t.file.set_field("status", "done");
        let out = t.file.serialize();
        assert!(out.contains("status: done"));
        assert!(out.contains("custom_field: hello"));
    }

    #[test]
    fn yaml_escape_handles_special_chars() {
        assert_eq!(yaml_scalar_escape("hello"), "hello");
        assert_eq!(yaml_scalar_escape(""), "");
        assert_eq!(yaml_scalar_escape(r#"Fix "ready""#), r#"Fix \"ready\""#);
        assert_eq!(yaml_scalar_escape("back\\slash"), "back\\\\slash");
        assert_eq!(yaml_scalar_escape("line\nbreak"), "line\\nbreak");
        assert_eq!(yaml_scalar_escape("tab\there"), "tab\\there");
        assert_eq!(yaml_scalar_escape("cr\rhere"), "cr\\rhere");
        assert_eq!(yaml_scalar_escape("null\0byte"), "null\\0byte");
        assert_eq!(yaml_scalar_escape("unicode: café"), "unicode: café");
        assert_eq!(yaml_scalar_escape("a\"b\\c\nd"), "a\\\"b\\\\c\\nd");
    }

    #[test]
    fn json_escape_handles_special_chars() {
        assert_eq!(json_string_escape("hello"), "hello");
        assert_eq!(json_string_escape(""), "");
        assert_eq!(json_string_escape(r#"has "quotes""#), r#"has \"quotes\""#);
        assert_eq!(json_string_escape("back\\slash"), "back\\\\slash");
        assert_eq!(json_string_escape("line\nbreak"), "line\\nbreak");
        assert_eq!(json_string_escape("tab\there"), "tab\\there");
        assert_eq!(json_string_escape("cr\rhere"), "cr\\rhere");
        assert_eq!(json_string_escape("unicode: café"), "unicode: café");
        let with_ctrl = format!("ctrl{}here", '\x01');
        assert_eq!(json_string_escape(&with_ctrl), "ctrl\\u0001here");
        assert_eq!(json_string_escape("a\"b\\c\nd"), "a\\\"b\\\\c\\nd");
    }

    #[test]
    fn new_ticket_text_escapes_title() {
        let text = new_ticket_text("01", "Fix \"ready\" command", &[], None, None, None, None);
        assert!(text.contains(r#"title: "Fix \"ready\" command""#));
        let t = Ticket::parse_str(&text, Path::new("test.md")).unwrap();
        assert!(t.title.contains("ready"));
    }

    #[test]
    fn status_enum_parse() {
        assert_eq!(Status::parse("open").unwrap(), Status::Open);
        assert_eq!(Status::parse("in_progress").unwrap(), Status::InProgress);
        assert_eq!(Status::parse("done").unwrap(), Status::Done);
        assert!(Status::parse("invalid").is_err());
    }

    #[test]
    fn env_enum_parse() {
        assert_eq!(Env::parse("corp").unwrap(), Env::Corp);
        assert_eq!(Env::parse("personal").unwrap(), Env::Personal);
        assert_eq!(Env::parse("either").unwrap(), Env::Either);
        assert!(Env::parse("invalid").is_err());
    }

    #[test]
    fn invalid_status_rejected_at_parse() {
        let content =
            "---\nid: \"01\"\ntitle: \"Bad\"\nstatus: invalid\nblocked_by: []\n---\n\n# Bad\n";
        let result = Ticket::parse_str(content, Path::new("test.md"));
        assert!(result.is_err());
        let msg = result.unwrap_err().to_string();
        assert!(msg.contains("status") || msg.contains("invalid"));
    }

    #[test]
    fn invalid_env_rejected_at_parse() {
        let content =
            "---\nid: \"01\"\ntitle: \"Bad\"\nstatus: open\nblocked_by: []\nenv: bogus\n---\n\n# Bad\n";
        let result = Ticket::parse_str(content, Path::new("test.md"));
        assert!(result.is_err());
        let msg = result.unwrap_err().to_string();
        assert!(msg.contains("env") || msg.contains("bogus"));
    }

    // --- Typed mutation method tests ---

    #[test]
    fn set_status_typed() {
        let content =
            "---\nid: \"01\"\ntitle: \"Test\"\nstatus: open\nblocked_by: []\n---\n\n# Body\n";
        let mut file = TicketFile::parse_str(content, Path::new("t.md")).unwrap();
        file.set_status(Status::Done);
        let out = file.serialize();
        assert!(out.contains("status: done"));
        assert!(!out.contains("status: open"));
    }

    #[test]
    fn set_status_preserves_other_fields() {
        let content = "---\nid: \"01\"\ntitle: \"Test\"\nstatus: open\nblocked_by: [\"02\"]\npriority: high\n---\n\n# Body\n";
        let mut file = TicketFile::parse_str(content, Path::new("t.md")).unwrap();
        file.set_status(Status::InProgress);
        let out = file.serialize();
        assert!(out.contains("status: in_progress"));
        assert!(out.contains("priority: high"));
        assert!(out.contains("blocked_by: [\"02\"]"));
    }

    #[test]
    fn set_blocked_by_empty() {
        let content = "---\nid: \"01\"\ntitle: \"Test\"\nstatus: open\nblocked_by: [\"02\", \"03\"]\n---\n\n# Body\n";
        let mut file = TicketFile::parse_str(content, Path::new("t.md")).unwrap();
        let empty: Vec<&str> = vec![];
        file.set_blocked_by(&empty);
        let out = file.serialize();
        assert!(out.contains("blocked_by: []"));
    }

    #[test]
    fn set_blocked_by_multiple() {
        let content =
            "---\nid: \"01\"\ntitle: \"Test\"\nstatus: open\nblocked_by: []\n---\n\n# Body\n";
        let mut file = TicketFile::parse_str(content, Path::new("t.md")).unwrap();
        file.set_blocked_by(&["05", "10"]);
        let out = file.serialize();
        assert!(out.contains("blocked_by: [\"05\", \"10\"]"));
    }

    #[test]
    fn set_blocked_by_escapes_values() {
        let content =
            "---\nid: \"01\"\ntitle: \"Test\"\nstatus: open\nblocked_by: []\n---\n\n# Body\n";
        let mut file = TicketFile::parse_str(content, Path::new("t.md")).unwrap();
        file.set_blocked_by(&["01"]);
        let out = file.serialize();
        assert!(out.contains("blocked_by: [\"01\"]"));
    }

    #[test]
    fn set_priority_some() {
        let content =
            "---\nid: \"01\"\ntitle: \"Test\"\nstatus: open\nblocked_by: []\n---\n\n# Body\n";
        let mut file = TicketFile::parse_str(content, Path::new("t.md")).unwrap();
        file.set_priority(Some(Priority::Urgent));
        let out = file.serialize();
        assert!(out.contains("priority: urgent"));
    }

    #[test]
    fn set_priority_none_removes_field() {
        let content = "---\nid: \"01\"\ntitle: \"Test\"\nstatus: open\nblocked_by: []\npriority: high\n---\n\n# Body\n";
        let mut file = TicketFile::parse_str(content, Path::new("t.md")).unwrap();
        file.set_priority(None);
        let out = file.serialize();
        assert!(!out.contains("priority"));
    }

    #[test]
    fn set_env_some() {
        let content =
            "---\nid: \"01\"\ntitle: \"Test\"\nstatus: open\nblocked_by: []\n---\n\n# Body\n";
        let mut file = TicketFile::parse_str(content, Path::new("t.md")).unwrap();
        file.set_env(Some(Env::Corp));
        let out = file.serialize();
        assert!(out.contains("env: corp"));
    }

    #[test]
    fn set_env_none_removes_field() {
        let content = "---\nid: \"01\"\ntitle: \"Test\"\nstatus: open\nblocked_by: []\nenv: personal\n---\n\n# Body\n";
        let mut file = TicketFile::parse_str(content, Path::new("t.md")).unwrap();
        file.set_env(None);
        let out = file.serialize();
        assert!(!out.contains("env"));
    }

    #[test]
    fn append_resolution_basic() {
        let content = "---\nid: \"01\"\ntitle: \"Test\"\nstatus: open\nblocked_by: []\n---\n\n# Test\n\n## Acceptance criteria\n\n- [x] Done\n";
        let mut file = TicketFile::parse_str(content, Path::new("t.md")).unwrap();
        file.append_resolution("2026-08-10", "Shipped it", None);
        assert!(file.body.contains("## Resolution (2026-08-10)"));
        assert!(file.body.contains("Shipped it"));
        assert!(!file.body.contains("Spike branch"));
    }

    #[test]
    fn append_resolution_with_spike() {
        let content =
            "---\nid: \"01\"\ntitle: \"Test\"\nstatus: open\nblocked_by: []\n---\n\n# Test\n";
        let mut file = TicketFile::parse_str(content, Path::new("t.md")).unwrap();
        file.append_resolution("2026-08-10", "Validated", Some("spike/experiment"));
        assert!(file.body.contains("## Resolution (2026-08-10)"));
        assert!(file.body.contains("Validated"));
        assert!(file.body.contains("Spike branch: spike/experiment"));
    }

    #[test]
    fn append_resolution_idempotent() {
        let content = "---\nid: \"01\"\ntitle: \"Test\"\nstatus: open\nblocked_by: []\n---\n\n# Test\n\n## Resolution (2026-01-01)\n\nAlready here\n";
        let mut file = TicketFile::parse_str(content, Path::new("t.md")).unwrap();
        let before = file.body.clone();
        file.append_resolution("2026-08-10", "New note", None);
        assert_eq!(file.body, before);
    }

    #[test]
    fn check_acs_all() {
        let content = "---\nid: \"01\"\ntitle: \"Test\"\nstatus: open\nblocked_by: []\n---\n\n# Test\n\n## Acceptance criteria\n\n- [ ] First\n- [ ] Second\n- [x] Already done\n";
        let mut file = TicketFile::parse_str(content, Path::new("t.md")).unwrap();
        let stats = file.check_acs(AcSelection::All);
        assert_eq!(stats.total, 3);
        assert_eq!(stats.checked, 3);
        assert_eq!(stats.unchecked, 0);
        assert!(!file.body.contains("- [ ]"));
    }

    #[test]
    fn check_acs_indices() {
        let content = "---\nid: \"01\"\ntitle: \"Test\"\nstatus: open\nblocked_by: []\n---\n\n# Test\n\n## Acceptance criteria\n\n- [ ] First\n- [ ] Second\n- [ ] Third\n";
        let mut file = TicketFile::parse_str(content, Path::new("t.md")).unwrap();
        let stats = file.check_acs(AcSelection::Indices(&[1, 3]));
        assert_eq!(stats.total, 3);
        assert_eq!(stats.checked, 2);
        assert_eq!(stats.unchecked, 1);
        // Second item should still be unchecked
        assert!(file.body.contains("- [x] First"));
        assert!(file.body.contains("- [ ] Second"));
        assert!(file.body.contains("- [x] Third"));
    }

    #[test]
    fn check_acs_no_section() {
        let content = "---\nid: \"01\"\ntitle: \"Test\"\nstatus: open\nblocked_by: []\n---\n\n# Test\n\nNo AC section here.\n";
        let mut file = TicketFile::parse_str(content, Path::new("t.md")).unwrap();
        let stats = file.check_acs(AcSelection::All);
        assert_eq!(stats.total, 0);
    }

    #[test]
    fn ac_stats_without_mutation() {
        let content = "---\nid: \"01\"\ntitle: \"Test\"\nstatus: open\nblocked_by: []\n---\n\n# Test\n\n## Acceptance criteria\n\n- [x] Done\n- [ ] Not done\n- [ ] Also not\n";
        let file = TicketFile::parse_str(content, Path::new("t.md")).unwrap();
        let stats = file.ac_stats();
        assert_eq!(stats.total, 3);
        assert_eq!(stats.checked, 1);
        assert_eq!(stats.unchecked, 2);
    }
}