standout-render 7.2.0

Styled terminal rendering with templates, themes, and adaptive color support
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
//! Template registry for file-based and inline templates.
//!
//! This module provides [`TemplateRegistry`], which manages template resolution
//! from multiple sources: inline strings, filesystem directories, or embedded content.
//!
//! # Design
//!
//! The registry is a thin wrapper around [`FileRegistry<String>`](crate::file_loader::FileRegistry),
//! providing template-specific functionality while reusing the generic file loading infrastructure.
//!
//! The registry uses a two-phase approach:
//!
//! 1. Collection: Templates are collected from various sources (inline, directories, embedded)
//! 2. Resolution: A unified map resolves template names to their content or file paths
//!
//! This separation enables:
//! - Testability: Resolution logic can be tested without filesystem access
//! - Flexibility: Same resolution rules apply regardless of template source
//! - Hot reloading: File paths can be re-read on each render in development mode
//!
//! # Template Resolution
//!
//! Templates are resolved by name using these rules:
//!
//! 1. Inline templates (added via [`TemplateRegistry::add_inline`]) have highest priority
//! 2. File templates are searched in directory registration order (first directory wins)
//! 3. Names can be specified with or without extension: both `"config"` and `"config.jinja"` resolve
//!
//! # Supported Extensions
//!
//! Template files are recognized by extension, in priority order:
//!
//! | Priority | Extension | Description |
//! |----------|-----------|-------------|
//! | 1 (highest) | `.jinja` | Standard Jinja extension (MiniJinja engine) |
//! | 2 | `.jinja2` | Full Jinja2 extension (MiniJinja engine) |
//! | 3 | `.j2` | Short Jinja2 extension (MiniJinja engine) |
//! | 4 | `.stpl` | Simple template (SimpleEngine - format strings) |
//! | 5 (lowest) | `.txt` | Plain text templates |
//!
//! If multiple files exist with the same base name but different extensions
//! (e.g., `config.jinja` and `config.j2`), the higher-priority extension wins.
//!
//! # Collision Handling
//!
//! The registry enforces strict collision rules:
//!
//! - Same-directory, different extensions: Higher priority extension wins (no error)
//! - Cross-directory collisions: Panic with detailed message listing conflicting files
//!
//! This strict behavior catches configuration mistakes early rather than silently
//! using an arbitrary winner.
//!
//! # Example
//!
//! ```rust,ignore
//! use standout_render::TemplateRegistry;
//!
//! let mut registry = TemplateRegistry::new();
//! registry.add_template_dir("./templates")?;
//! registry.add_inline("override", "Custom content");
//!
//! // Resolve templates
//! let content = registry.get_content("config")?;
//! ```

use std::collections::HashMap;
use std::path::{Path, PathBuf};

use crate::file_loader::{
    self, build_embedded_registry, resolve_in_map, FileRegistry, FileRegistryConfig, LoadError,
    LoadedEntry, LoadedFile,
};

/// Recognized template file extensions in priority order.
///
/// When multiple files exist with the same base name but different extensions,
/// the extension appearing earlier in this list takes precedence.
///
/// # Priority Order
///
/// 1. `.jinja` - Standard Jinja extension (MiniJinja engine)
/// 2. `.jinja2` - Full Jinja2 extension (MiniJinja engine)
/// 3. `.j2` - Short Jinja2 extension (MiniJinja engine)
/// 4. `.stpl` - Simple template (SimpleEngine - `{var}` format strings)
/// 5. `.txt` - Plain text templates
pub const TEMPLATE_EXTENSIONS: &[&str] = &[".jinja", ".jinja2", ".j2", ".stpl", ".txt"];

/// A template file discovered during directory walking.
///
/// This struct captures the essential information about a template file
/// without reading its content, enabling lazy loading and hot reloading.
///
/// # Fields
///
/// - `name`: The resolution name without extension (e.g., `"todos/list"`)
/// - `name_with_ext`: The resolution name with extension (e.g., `"todos/list.jinja"`)
/// - `absolute_path`: Full filesystem path for reading content
/// - `source_dir`: The template directory this file came from (for collision reporting)
///
/// # Example
///
/// For a file at `/app/templates/todos/list.jinja` with root `/app/templates`:
///
/// ```rust,ignore
/// TemplateFile {
///     name: "todos/list".to_string(),
///     name_with_ext: "todos/list.jinja".to_string(),
///     absolute_path: PathBuf::from("/app/templates/todos/list.jinja"),
///     source_dir: PathBuf::from("/app/templates"),
/// }
/// ```
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct TemplateFile {
    /// Resolution name without extension (e.g., "config" or "todos/list")
    pub name: String,
    /// Resolution name with extension (e.g., "config.jinja" or "todos/list.jinja")
    pub name_with_ext: String,
    /// Absolute path to the template file
    pub absolute_path: PathBuf,
    /// The template directory root this file belongs to
    pub source_dir: PathBuf,
}

impl TemplateFile {
    /// Creates a new template file descriptor.
    pub fn new(
        name: impl Into<String>,
        name_with_ext: impl Into<String>,
        absolute_path: impl Into<PathBuf>,
        source_dir: impl Into<PathBuf>,
    ) -> Self {
        Self {
            name: name.into(),
            name_with_ext: name_with_ext.into(),
            absolute_path: absolute_path.into(),
            source_dir: source_dir.into(),
        }
    }

    /// Returns the extension priority (lower is higher priority).
    ///
    /// Returns `usize::MAX` if the extension is not recognized.
    pub fn extension_priority(&self) -> usize {
        for (i, ext) in TEMPLATE_EXTENSIONS.iter().enumerate() {
            if self.name_with_ext.ends_with(ext) {
                return i;
            }
        }
        usize::MAX
    }
}

impl From<LoadedFile> for TemplateFile {
    fn from(file: LoadedFile) -> Self {
        Self {
            name: file.name,
            name_with_ext: file.name_with_ext,
            absolute_path: file.path,
            source_dir: file.source_dir,
        }
    }
}

impl From<TemplateFile> for LoadedFile {
    fn from(file: TemplateFile) -> Self {
        Self {
            name: file.name,
            name_with_ext: file.name_with_ext,
            path: file.absolute_path,
            source_dir: file.source_dir,
        }
    }
}

/// How a template's content is stored or accessed.
///
/// This enum enables different storage strategies:
/// - `Inline`: Content is stored directly (for inline templates or embedded builds)
/// - `File`: Content is read from disk on demand (for hot reloading in development)
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ResolvedTemplate {
    /// Template content stored directly in memory.
    ///
    /// Used for:
    /// - Inline templates added via `add_inline()`
    /// - Embedded templates in release builds
    Inline(String),

    /// Template loaded from filesystem on demand.
    ///
    /// The path is read on each render in development mode,
    /// enabling hot reloading without recompilation.
    File(PathBuf),
}

impl From<&LoadedEntry<String>> for ResolvedTemplate {
    fn from(entry: &LoadedEntry<String>) -> Self {
        match entry {
            LoadedEntry::Embedded(content) => ResolvedTemplate::Inline(content.clone()),
            LoadedEntry::File(path) => ResolvedTemplate::File(path.clone()),
        }
    }
}

/// Error type for template registry operations.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum RegistryError {
    /// Two template directories contain files that resolve to the same name.
    ///
    /// This is an unrecoverable configuration error that must be fixed
    /// by the application developer.
    Collision {
        /// The template name that has conflicting sources
        name: String,
        /// Path to the existing template
        existing_path: PathBuf,
        /// Directory containing the existing template
        existing_dir: PathBuf,
        /// Path to the conflicting template
        conflicting_path: PathBuf,
        /// Directory containing the conflicting template
        conflicting_dir: PathBuf,
    },

    /// Template not found in registry.
    NotFound {
        /// The name that was requested
        name: String,
    },

    /// Failed to read template file from disk.
    ReadError {
        /// Path that failed to read
        path: PathBuf,
        /// Error message
        message: String,
    },
}

impl std::fmt::Display for RegistryError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            RegistryError::Collision {
                name,
                existing_path,
                existing_dir,
                conflicting_path,
                conflicting_dir,
            } => {
                write!(
                    f,
                    "Template collision detected for \"{}\":\n  \
                     - {} (from {})\n  \
                     - {} (from {})",
                    name,
                    existing_path.display(),
                    existing_dir.display(),
                    conflicting_path.display(),
                    conflicting_dir.display()
                )
            }
            RegistryError::NotFound { name } => {
                write!(f, "Template not found: \"{}\"", name)
            }
            RegistryError::ReadError { path, message } => {
                write!(
                    f,
                    "Failed to read template \"{}\": {}",
                    path.display(),
                    message
                )
            }
        }
    }
}

impl std::error::Error for RegistryError {}

impl From<LoadError> for RegistryError {
    fn from(err: LoadError) -> Self {
        match err {
            LoadError::NotFound { name } => RegistryError::NotFound { name },
            LoadError::Io { path, message } => RegistryError::ReadError { path, message },
            LoadError::Collision {
                name,
                existing_path,
                existing_dir,
                conflicting_path,
                conflicting_dir,
            } => RegistryError::Collision {
                name,
                existing_path,
                existing_dir,
                conflicting_path,
                conflicting_dir,
            },
            LoadError::DirectoryNotFound { path } => RegistryError::ReadError {
                path: path.clone(),
                message: format!("Directory not found: {}", path.display()),
            },
            LoadError::Transform { name, message } => RegistryError::ReadError {
                path: PathBuf::from(&name),
                message,
            },
        }
    }
}

/// Creates the file registry configuration for templates.
fn template_config() -> FileRegistryConfig<String> {
    FileRegistryConfig {
        extensions: TEMPLATE_EXTENSIONS,
        transform: |content| Ok(content.to_string()),
    }
}

/// Registry for template resolution from multiple sources.
///
/// The registry maintains a unified view of templates from:
/// - Inline strings (highest priority)
/// - Multiple filesystem directories
/// - Embedded content (for release builds)
///
/// # Resolution Order
///
/// When looking up a template name:
///
/// 1. Check inline templates first
/// 2. Check file-based templates in registration order
/// 3. Return error if not found
///
/// # Thread Safety
///
/// The registry is not thread-safe. For concurrent access, wrap in appropriate
/// synchronization primitives.
///
/// # Example
///
/// ```rust,ignore
/// let mut registry = TemplateRegistry::new();
///
/// // Add inline template (highest priority)
/// registry.add_inline("header", "{{ title }}");
///
/// // Add from directory
/// registry.add_template_dir("./templates")?;
///
/// // Resolve and get content
/// let content = registry.get_content("header")?;
/// ```
pub struct TemplateRegistry {
    /// The underlying file registry for directory-based file loading.
    inner: FileRegistry<String>,

    /// Inline templates (stored separately for highest priority).
    inline: HashMap<String, String>,

    /// File-based templates from add_from_files (maps name → path).
    /// These are separate from directory-based loading.
    files: HashMap<String, PathBuf>,

    /// Tracks source info for collision detection: name → (path, source_dir).
    sources: HashMap<String, (PathBuf, PathBuf)>,

    /// Framework templates (lowest priority fallback).
    /// These are provided by the standout framework and can be overridden
    /// by user templates with the same name.
    framework: HashMap<String, String>,
}

impl Default for TemplateRegistry {
    fn default() -> Self {
        Self::new()
    }
}

impl TemplateRegistry {
    /// Creates an empty template registry.
    pub fn new() -> Self {
        Self {
            inner: FileRegistry::new(template_config()),
            inline: HashMap::new(),
            files: HashMap::new(),
            sources: HashMap::new(),
            framework: HashMap::new(),
        }
    }

    /// Adds an inline template with the given name.
    ///
    /// Inline templates have the highest priority and will shadow any
    /// file-based templates with the same name.
    ///
    /// # Arguments
    ///
    /// * `name` - The template name for resolution
    /// * `content` - The template content
    ///
    /// # Example
    ///
    /// ```rust,ignore
    /// registry.add_inline("header", "{{ title | style(\"title\") }}");
    /// ```
    pub fn add_inline(&mut self, name: impl Into<String>, content: impl Into<String>) {
        self.inline.insert(name.into(), content.into());
    }

    /// Adds a template directory to search for files.
    ///
    /// Templates in the directory are resolved by their relative path without
    /// extension. For example, with directory `./templates`:
    ///
    /// - `"config"` → `./templates/config.jinja`
    /// - `"todos/list"` → `./templates/todos/list.jinja`
    ///
    /// # Errors
    ///
    /// Returns an error if the directory doesn't exist.
    pub fn add_template_dir<P: AsRef<Path>>(&mut self, path: P) -> Result<(), RegistryError> {
        self.inner.add_dir(path).map_err(RegistryError::from)
    }

    /// Adds templates discovered from a directory scan.
    ///
    /// This method processes a list of [`TemplateFile`] entries, typically
    /// produced by [`walk_template_dir`], and registers them for resolution.
    ///
    /// # Resolution Names
    ///
    /// Each file is registered under two names:
    /// - Without extension: `"config"` for `config.jinja`
    /// - With extension: `"config.jinja"` for `config.jinja`
    ///
    /// # Extension Priority
    ///
    /// If multiple files share the same base name with different extensions
    /// (e.g., `config.jinja` and `config.j2`), the higher-priority extension wins
    /// for the extensionless name. Both can still be accessed by full name.
    ///
    /// # Collision Detection
    ///
    /// If a template name conflicts with one from a different source directory,
    /// an error is returned with details about both files.
    ///
    /// # Arguments
    ///
    /// * `files` - Template files discovered during directory walking
    ///
    /// # Errors
    ///
    /// Returns [`RegistryError::Collision`] if templates from different
    /// directories resolve to the same name.
    pub fn add_from_files(&mut self, files: Vec<TemplateFile>) -> Result<(), RegistryError> {
        // Sort by extension priority so higher-priority extensions are processed first
        let mut sorted_files = files;
        sorted_files.sort_by_key(|f| f.extension_priority());

        for file in sorted_files {
            // Check for cross-directory collision on the base name
            if let Some((existing_path, existing_dir)) = self.sources.get(&file.name) {
                // Only error if from different source directories
                if existing_dir != &file.source_dir {
                    return Err(RegistryError::Collision {
                        name: file.name.clone(),
                        existing_path: existing_path.clone(),
                        existing_dir: existing_dir.clone(),
                        conflicting_path: file.absolute_path.clone(),
                        conflicting_dir: file.source_dir.clone(),
                    });
                }
                // Same directory, different extension - skip (higher priority already registered)
                continue;
            }

            // Track source for collision detection
            self.sources.insert(
                file.name.clone(),
                (file.absolute_path.clone(), file.source_dir.clone()),
            );

            // Register the template under extensionless name
            self.files
                .insert(file.name.clone(), file.absolute_path.clone());

            // Register under name with extension (allows explicit access)
            self.files
                .insert(file.name_with_ext.clone(), file.absolute_path);
        }

        Ok(())
    }

    /// Adds pre-embedded templates (for release builds).
    ///
    /// Embedded templates are treated as inline templates, stored directly
    /// in memory without filesystem access.
    ///
    /// # Arguments
    ///
    /// * `templates` - Map of template name to content
    pub fn add_embedded(&mut self, templates: HashMap<String, String>) {
        for (name, content) in templates {
            self.inline.insert(name, content);
        }
    }

    /// Adds framework templates (lowest priority fallback).
    ///
    /// Framework templates are provided by the standout framework and serve as
    /// defaults that can be overridden by user templates with the same name.
    /// They are checked last during resolution.
    ///
    /// Framework templates typically use the `standout/` namespace to avoid
    /// accidental collision with user templates (e.g., `standout/list-view`).
    ///
    /// # Arguments
    ///
    /// * `name` - The template name (e.g., `"standout/list-view"`)
    /// * `content` - The template content
    ///
    /// # Example
    ///
    /// ```rust,ignore
    /// registry.add_framework("standout/list-view", include_str!("templates/list-view.jinja"));
    /// ```
    pub fn add_framework(&mut self, name: impl Into<String>, content: impl Into<String>) {
        self.framework.insert(name.into(), content.into());
    }

    /// Adds multiple framework templates from embedded entries.
    ///
    /// This is similar to [`from_embedded_entries`] but adds templates to the
    /// framework (lowest priority) tier instead of inline (highest priority).
    ///
    /// # Arguments
    ///
    /// * `entries` - Slice of `(name_with_ext, content)` pairs
    pub fn add_framework_entries(&mut self, entries: &[(&str, &str)]) {
        let framework: HashMap<String, String> =
            build_embedded_registry(entries, TEMPLATE_EXTENSIONS, |content| {
                Ok::<_, std::convert::Infallible>(content.to_string())
            })
            .unwrap(); // Safe: Infallible error type

        for (name, content) in framework {
            self.framework.insert(name, content);
        }
    }

    /// Clears all framework templates.
    ///
    /// This is useful when you want to disable all framework-provided defaults
    /// and require explicit template configuration.
    pub fn clear_framework(&mut self) {
        self.framework.clear();
    }

    /// Creates a registry from embedded template entries.
    ///
    /// This is the primary entry point for compile-time embedded templates,
    /// typically called by the `embed_templates!` macro.
    ///
    /// # Arguments
    ///
    /// * `entries` - Slice of `(name_with_ext, content)` pairs where `name_with_ext`
    ///   is the relative path including extension (e.g., `"report/summary.jinja"`)
    ///
    /// # Processing
    ///
    /// This method applies the same logic as runtime file loading:
    ///
    /// 1. Extension stripping: `"report/summary.jinja"` → `"report/summary"`
    /// 2. Extension priority: When multiple files share a base name, the
    ///    higher-priority extension wins (see [`TEMPLATE_EXTENSIONS`])
    /// 3. Dual registration: Each template is accessible by both its base
    ///    name and its full name with extension
    ///
    /// # Example
    ///
    /// ```rust
    /// use standout_render::TemplateRegistry;
    ///
    /// // Typically generated by embed_templates! macro
    /// let entries: &[(&str, &str)] = &[
    ///     ("list.jinja", "Hello {{ name }}"),
    ///     ("report/summary.jinja", "Report: {{ title }}"),
    /// ];
    ///
    /// let registry = TemplateRegistry::from_embedded_entries(entries);
    ///
    /// // Access by base name or full name
    /// assert!(registry.get("list").is_ok());
    /// assert!(registry.get("list.jinja").is_ok());
    /// assert!(registry.get("report/summary").is_ok());
    /// ```
    pub fn from_embedded_entries(entries: &[(&str, &str)]) -> Self {
        let mut registry = Self::new();

        // Use shared helper - infallible transform for templates
        let inline: HashMap<String, String> =
            build_embedded_registry(entries, TEMPLATE_EXTENSIONS, |content| {
                Ok::<_, std::convert::Infallible>(content.to_string())
            })
            .unwrap(); // Safe: Infallible error type

        registry.inline = inline;
        registry
    }

    /// Looks up a template by name.
    ///
    /// Names are resolved with extension-agnostic fallback: if the exact name
    /// isn't found and it has a recognized extension, the extension is stripped
    /// and the base name is tried. This allows lookups like `"list.j2"` to
    /// find a template registered as `"list"` (from `list.jinja`).
    ///
    /// # Resolution Priority
    ///
    /// Templates are resolved in this order:
    /// 1. Inline templates (highest priority)
    /// 2. File-based templates from `add_from_files`
    /// 3. Directory-based templates from `add_template_dir`
    /// 4. Framework templates (lowest priority)
    ///
    /// This allows user templates to override framework defaults.
    ///
    /// # Errors
    ///
    /// Returns [`RegistryError::NotFound`] if the template doesn't exist.
    pub fn get(&self, name: &str) -> Result<ResolvedTemplate, RegistryError> {
        // Check inline first (highest priority)
        if let Some(content) = resolve_in_map(&self.inline, name, TEMPLATE_EXTENSIONS) {
            return Ok(ResolvedTemplate::Inline(content.clone()));
        }

        // Check file-based templates from add_from_files
        if let Some(path) = resolve_in_map(&self.files, name, TEMPLATE_EXTENSIONS) {
            return Ok(ResolvedTemplate::File(path.clone()));
        }

        // Check directory-based file registry (has its own extension fallback)
        if let Some(entry) = self.inner.get_entry(name) {
            return Ok(ResolvedTemplate::from(entry));
        }

        // Check framework templates (lowest priority)
        if let Some(content) = resolve_in_map(&self.framework, name, TEMPLATE_EXTENSIONS) {
            return Ok(ResolvedTemplate::Inline(content.clone()));
        }

        Err(RegistryError::NotFound {
            name: name.to_string(),
        })
    }

    /// Gets the content of a template, reading from disk if necessary.
    ///
    /// For inline templates, returns the stored content directly.
    /// For file templates, reads the file from disk (enabling hot reload).
    ///
    /// # Errors
    ///
    /// Returns an error if the template is not found or cannot be read from disk.
    pub fn get_content(&self, name: &str) -> Result<String, RegistryError> {
        let resolved = self.get(name)?;
        match resolved {
            ResolvedTemplate::Inline(content) => Ok(content),
            ResolvedTemplate::File(path) => {
                std::fs::read_to_string(&path).map_err(|e| RegistryError::ReadError {
                    path,
                    message: e.to_string(),
                })
            }
        }
    }

    /// Refreshes the registry from registered directories.
    ///
    /// This re-walks all registered template directories and rebuilds the
    /// resolution map. Call this if:
    ///
    /// - You've added template directories after the first render
    /// - Template files have been added/removed from disk
    ///
    /// # Panics
    ///
    /// Panics if a collision is detected (same name from different directories).
    pub fn refresh(&mut self) -> Result<(), RegistryError> {
        self.inner.refresh().map_err(RegistryError::from)
    }

    /// Returns the number of registered templates.
    ///
    /// Note: This counts both extensionless and with-extension entries,
    /// so it may be higher than the number of unique template files.
    pub fn len(&self) -> usize {
        self.inline.len() + self.files.len() + self.inner.len() + self.framework.len()
    }

    /// Returns true if no templates are registered.
    pub fn is_empty(&self) -> bool {
        self.inline.is_empty()
            && self.files.is_empty()
            && self.inner.is_empty()
            && self.framework.is_empty()
    }

    /// Returns an iterator over all registered template names.
    pub fn names(&self) -> impl Iterator<Item = &str> {
        self.inline
            .keys()
            .map(|s| s.as_str())
            .chain(self.files.keys().map(|s| s.as_str()))
            .chain(self.inner.names())
            .chain(self.framework.keys().map(|s| s.as_str()))
    }

    /// Clears all templates from the registry.
    pub fn clear(&mut self) {
        self.inline.clear();
        self.files.clear();
        self.sources.clear();
        self.inner.clear();
        self.framework.clear();
    }

    /// Returns true if the registry has framework templates.
    pub fn has_framework_templates(&self) -> bool {
        !self.framework.is_empty()
    }

    /// Returns an iterator over framework template names.
    pub fn framework_names(&self) -> impl Iterator<Item = &str> {
        self.framework.keys().map(|s| s.as_str())
    }
}

/// Walks a template directory and collects template files.
///
/// This function traverses the directory recursively, finding all files
/// with recognized template extensions ([`TEMPLATE_EXTENSIONS`]).
///
/// # Arguments
///
/// * `root` - The template directory root to walk
///
/// # Returns
///
/// A vector of [`TemplateFile`] entries, one for each discovered template.
/// The vector is not sorted; use [`TemplateFile::extension_priority`] for ordering.
///
/// # Errors
///
/// Returns an error if the directory cannot be read or traversed.
///
/// # Example
///
/// ```rust,ignore
/// let files = walk_template_dir("./templates")?;
/// for file in &files {
///     println!("{} -> {}", file.name, file.absolute_path.display());
/// }
/// ```
pub fn walk_template_dir(root: impl AsRef<Path>) -> Result<Vec<TemplateFile>, std::io::Error> {
    let files = file_loader::walk_dir(root.as_ref(), TEMPLATE_EXTENSIONS)
        .map_err(|e| std::io::Error::other(e.to_string()))?;

    Ok(files.into_iter().map(TemplateFile::from).collect())
}

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

    // =========================================================================
    // TemplateFile tests
    // =========================================================================

    #[test]
    fn test_template_file_extension_priority() {
        let jinja = TemplateFile::new("config", "config.jinja", "/a/config.jinja", "/a");
        let jinja2 = TemplateFile::new("config", "config.jinja2", "/a/config.jinja2", "/a");
        let j2 = TemplateFile::new("config", "config.j2", "/a/config.j2", "/a");
        let stpl = TemplateFile::new("config", "config.stpl", "/a/config.stpl", "/a");
        let txt = TemplateFile::new("config", "config.txt", "/a/config.txt", "/a");
        let unknown = TemplateFile::new("config", "config.xyz", "/a/config.xyz", "/a");

        assert_eq!(jinja.extension_priority(), 0);
        assert_eq!(jinja2.extension_priority(), 1);
        assert_eq!(j2.extension_priority(), 2);
        assert_eq!(stpl.extension_priority(), 3);
        assert_eq!(txt.extension_priority(), 4);
        assert_eq!(unknown.extension_priority(), usize::MAX);
    }

    // =========================================================================
    // TemplateRegistry inline tests
    // =========================================================================

    #[test]
    fn test_registry_add_inline() {
        let mut registry = TemplateRegistry::new();
        registry.add_inline("header", "{{ title }}");

        assert_eq!(registry.len(), 1);
        assert!(!registry.is_empty());

        let content = registry.get_content("header").unwrap();
        assert_eq!(content, "{{ title }}");
    }

    #[test]
    fn test_registry_inline_overwrites() {
        let mut registry = TemplateRegistry::new();
        registry.add_inline("header", "first");
        registry.add_inline("header", "second");

        let content = registry.get_content("header").unwrap();
        assert_eq!(content, "second");
    }

    #[test]
    fn test_registry_not_found() {
        let registry = TemplateRegistry::new();
        let result = registry.get("nonexistent");

        assert!(matches!(result, Err(RegistryError::NotFound { .. })));
    }

    // =========================================================================
    // File-based template tests (using synthetic data)
    // =========================================================================

    #[test]
    fn test_registry_add_from_files() {
        let mut registry = TemplateRegistry::new();

        let files = vec![
            TemplateFile::new(
                "config",
                "config.jinja",
                "/templates/config.jinja",
                "/templates",
            ),
            TemplateFile::new(
                "todos/list",
                "todos/list.jinja",
                "/templates/todos/list.jinja",
                "/templates",
            ),
        ];

        registry.add_from_files(files).unwrap();

        // Should have 4 entries: 2 names + 2 names with extension
        assert_eq!(registry.len(), 4);

        // Can access by name without extension
        assert!(registry.get("config").is_ok());
        assert!(registry.get("todos/list").is_ok());

        // Can access by name with extension
        assert!(registry.get("config.jinja").is_ok());
        assert!(registry.get("todos/list.jinja").is_ok());
    }

    #[test]
    fn test_registry_extension_priority() {
        let mut registry = TemplateRegistry::new();

        // Add files with different extensions for same base name
        // (j2 should be ignored because jinja has higher priority)
        let files = vec![
            TemplateFile::new("config", "config.j2", "/templates/config.j2", "/templates"),
            TemplateFile::new(
                "config",
                "config.jinja",
                "/templates/config.jinja",
                "/templates",
            ),
        ];

        registry.add_from_files(files).unwrap();

        // Extensionless name should resolve to .jinja
        let resolved = registry.get("config").unwrap();
        match resolved {
            ResolvedTemplate::File(path) => {
                assert!(path.to_string_lossy().ends_with("config.jinja"));
            }
            _ => panic!("Expected file template"),
        }
    }

    #[test]
    fn test_registry_collision_different_dirs() {
        let mut registry = TemplateRegistry::new();

        let files = vec![
            TemplateFile::new(
                "config",
                "config.jinja",
                "/app/templates/config.jinja",
                "/app/templates",
            ),
            TemplateFile::new(
                "config",
                "config.jinja",
                "/plugins/templates/config.jinja",
                "/plugins/templates",
            ),
        ];

        let result = registry.add_from_files(files);

        assert!(matches!(result, Err(RegistryError::Collision { .. })));

        if let Err(RegistryError::Collision { name, .. }) = result {
            assert_eq!(name, "config");
        }
    }

    #[test]
    fn test_registry_inline_shadows_file() {
        let mut registry = TemplateRegistry::new();

        // Add file-based template first
        let files = vec![TemplateFile::new(
            "config",
            "config.jinja",
            "/templates/config.jinja",
            "/templates",
        )];
        registry.add_from_files(files).unwrap();

        // Add inline with same name (should shadow)
        registry.add_inline("config", "inline content");

        let content = registry.get_content("config").unwrap();
        assert_eq!(content, "inline content");
    }

    #[test]
    fn test_registry_names_iterator() {
        let mut registry = TemplateRegistry::new();
        registry.add_inline("a", "content a");
        registry.add_inline("b", "content b");

        let names: Vec<&str> = registry.names().collect();
        assert!(names.contains(&"a"));
        assert!(names.contains(&"b"));
    }

    #[test]
    fn test_registry_clear() {
        let mut registry = TemplateRegistry::new();
        registry.add_inline("a", "content");

        assert!(!registry.is_empty());
        registry.clear();
        assert!(registry.is_empty());
    }

    // =========================================================================
    // Error display tests
    // =========================================================================

    #[test]
    fn test_error_display_collision() {
        let err = RegistryError::Collision {
            name: "config".to_string(),
            existing_path: PathBuf::from("/a/config.jinja"),
            existing_dir: PathBuf::from("/a"),
            conflicting_path: PathBuf::from("/b/config.jinja"),
            conflicting_dir: PathBuf::from("/b"),
        };

        let display = err.to_string();
        assert!(display.contains("config"));
        assert!(display.contains("/a/config.jinja"));
        assert!(display.contains("/b/config.jinja"));
    }

    #[test]
    fn test_error_display_not_found() {
        let err = RegistryError::NotFound {
            name: "missing".to_string(),
        };

        let display = err.to_string();
        assert!(display.contains("missing"));
    }

    // =========================================================================
    // from_embedded_entries tests
    // =========================================================================

    #[test]
    fn test_from_embedded_entries_single() {
        let entries: &[(&str, &str)] = &[("hello.jinja", "Hello {{ name }}")];
        let registry = TemplateRegistry::from_embedded_entries(entries);

        // Should be accessible by both names
        assert!(registry.get("hello").is_ok());
        assert!(registry.get("hello.jinja").is_ok());

        let content = registry.get_content("hello").unwrap();
        assert_eq!(content, "Hello {{ name }}");
    }

    #[test]
    fn test_from_embedded_entries_multiple() {
        let entries: &[(&str, &str)] = &[
            ("header.jinja", "{{ title }}"),
            ("footer.jinja", "Copyright {{ year }}"),
        ];
        let registry = TemplateRegistry::from_embedded_entries(entries);

        assert_eq!(registry.len(), 4); // 2 base + 2 with ext
        assert!(registry.get("header").is_ok());
        assert!(registry.get("footer").is_ok());
    }

    #[test]
    fn test_from_embedded_entries_nested_paths() {
        let entries: &[(&str, &str)] = &[
            ("report/summary.jinja", "Summary: {{ text }}"),
            ("report/details.jinja", "Details: {{ info }}"),
        ];
        let registry = TemplateRegistry::from_embedded_entries(entries);

        assert!(registry.get("report/summary").is_ok());
        assert!(registry.get("report/summary.jinja").is_ok());
        assert!(registry.get("report/details").is_ok());
    }

    #[test]
    fn test_from_embedded_entries_extension_priority() {
        // .jinja has higher priority than .txt (index 0 vs index 3)
        let entries: &[(&str, &str)] = &[
            ("config.txt", "txt content"),
            ("config.jinja", "jinja content"),
        ];
        let registry = TemplateRegistry::from_embedded_entries(entries);

        // Base name should resolve to higher priority (.jinja)
        let content = registry.get_content("config").unwrap();
        assert_eq!(content, "jinja content");

        // Both can still be accessed by full name
        assert_eq!(registry.get_content("config.txt").unwrap(), "txt content");
        assert_eq!(
            registry.get_content("config.jinja").unwrap(),
            "jinja content"
        );
    }

    #[test]
    fn test_from_embedded_entries_extension_priority_reverse_order() {
        // Same test but with entries in reverse order to ensure sorting works
        let entries: &[(&str, &str)] = &[
            ("config.jinja", "jinja content"),
            ("config.txt", "txt content"),
        ];
        let registry = TemplateRegistry::from_embedded_entries(entries);

        // Base name should still resolve to higher priority (.jinja)
        let content = registry.get_content("config").unwrap();
        assert_eq!(content, "jinja content");
    }

    #[test]
    fn test_from_embedded_entries_names_iterator() {
        let entries: &[(&str, &str)] = &[("a.jinja", "content a"), ("nested/b.jinja", "content b")];
        let registry = TemplateRegistry::from_embedded_entries(entries);

        let names: Vec<&str> = registry.names().collect();
        assert!(names.contains(&"a"));
        assert!(names.contains(&"a.jinja"));
        assert!(names.contains(&"nested/b"));
        assert!(names.contains(&"nested/b.jinja"));
    }

    #[test]
    fn test_from_embedded_entries_empty() {
        let entries: &[(&str, &str)] = &[];
        let registry = TemplateRegistry::from_embedded_entries(entries);

        assert!(registry.is_empty());
        assert_eq!(registry.len(), 0);
    }

    #[test]
    fn test_extensionless_includes_work() {
        // Simulates the user's report: {% include "_partial" %} should work
        // when the file is actually "_partial.jinja"
        let entries: &[(&str, &str)] = &[
            ("main.jinja", "Start {% include '_partial' %} End"),
            ("_partial.jinja", "PARTIAL_CONTENT"),
        ];
        let registry = TemplateRegistry::from_embedded_entries(entries);

        // Build MiniJinja environment the same way App.render() does
        let mut env = minijinja::Environment::new();
        for name in registry.names() {
            if let Ok(content) = registry.get_content(name) {
                env.add_template_owned(name.to_string(), content).unwrap();
            }
        }

        // Verify extensionless include works
        let tmpl = env.get_template("main").unwrap();
        let output = tmpl.render(()).unwrap();
        assert_eq!(output, "Start PARTIAL_CONTENT End");
    }

    #[test]
    fn test_extensionless_includes_with_extension_syntax() {
        // Also verify that {% include "_partial.jinja" %} works
        let entries: &[(&str, &str)] = &[
            ("main.jinja", "Start {% include '_partial.jinja' %} End"),
            ("_partial.jinja", "PARTIAL_CONTENT"),
        ];
        let registry = TemplateRegistry::from_embedded_entries(entries);

        let mut env = minijinja::Environment::new();
        for name in registry.names() {
            if let Ok(content) = registry.get_content(name) {
                env.add_template_owned(name.to_string(), content).unwrap();
            }
        }

        let tmpl = env.get_template("main").unwrap();
        let output = tmpl.render(()).unwrap();
        assert_eq!(output, "Start PARTIAL_CONTENT End");
    }

    // =========================================================================
    // Framework templates tests
    // =========================================================================

    #[test]
    fn test_framework_add_and_get() {
        let mut registry = TemplateRegistry::new();
        registry.add_framework("standout/list-view", "Framework list view");

        assert!(registry.has_framework_templates());
        let content = registry.get_content("standout/list-view").unwrap();
        assert_eq!(content, "Framework list view");
    }

    #[test]
    fn test_framework_lowest_priority() {
        let mut registry = TemplateRegistry::new();

        // Add framework template
        registry.add_framework("config", "framework content");

        // Add inline template with same name (should shadow)
        registry.add_inline("config", "inline content");

        // Inline should win
        let content = registry.get_content("config").unwrap();
        assert_eq!(content, "inline content");
    }

    #[test]
    fn test_framework_user_can_override() {
        let mut registry = TemplateRegistry::new();

        // Add framework template in standout/ namespace
        registry.add_framework("standout/list-view", "framework default");

        // User creates their own version
        registry.add_inline("standout/list-view", "user override");

        // User version should win
        let content = registry.get_content("standout/list-view").unwrap();
        assert_eq!(content, "user override");
    }

    #[test]
    fn test_framework_entries() {
        let mut registry = TemplateRegistry::new();

        let entries: &[(&str, &str)] = &[
            ("standout/list-view.jinja", "List view content"),
            ("standout/detail-view.jinja", "Detail view content"),
        ];

        registry.add_framework_entries(entries);

        // Should be accessible by both names
        assert!(registry.get("standout/list-view").is_ok());
        assert!(registry.get("standout/list-view.jinja").is_ok());
        assert!(registry.get("standout/detail-view").is_ok());
    }

    #[test]
    fn test_framework_names_iterator() {
        let mut registry = TemplateRegistry::new();
        registry.add_framework("standout/a", "content a");
        registry.add_framework("standout/b", "content b");

        let names: Vec<&str> = registry.framework_names().collect();
        assert_eq!(names.len(), 2);
        assert!(names.contains(&"standout/a"));
        assert!(names.contains(&"standout/b"));
    }

    #[test]
    fn test_framework_clear() {
        let mut registry = TemplateRegistry::new();
        registry.add_framework("standout/list-view", "content");

        assert!(registry.has_framework_templates());

        registry.clear_framework();

        assert!(!registry.has_framework_templates());
        assert!(registry.get("standout/list-view").is_err());
    }

    #[test]
    fn test_framework_included_in_len_and_names() {
        let mut registry = TemplateRegistry::new();
        registry.add_inline("user-template", "user content");
        registry.add_framework("standout/framework", "framework content");

        // Both should be counted
        assert_eq!(registry.len(), 2);

        let names: Vec<&str> = registry.names().collect();
        assert!(names.contains(&"user-template"));
        assert!(names.contains(&"standout/framework"));
    }

    #[test]
    fn test_framework_clear_all_clears_framework() {
        let mut registry = TemplateRegistry::new();
        registry.add_framework("standout/test", "content");

        registry.clear();

        assert!(registry.is_empty());
        assert!(!registry.has_framework_templates());
    }

    // =========================================================================
    // Extension-agnostic resolution tests
    // =========================================================================

    #[test]
    fn test_inline_cross_extension_lookup() {
        // Inline registered as "list.jinja", looked up as "list.j2"
        let entries: &[(&str, &str)] = &[("list.jinja", "List content")];
        let registry = TemplateRegistry::from_embedded_entries(entries);

        // "list.j2" should fall back to "list" (base name)
        let content = registry.get_content("list.j2").unwrap();
        assert_eq!(content, "List content");
    }

    #[test]
    fn test_inline_cross_extension_nested_path() {
        let entries: &[(&str, &str)] = &[("todos/list.jinja", "Todos")];
        let registry = TemplateRegistry::from_embedded_entries(entries);

        assert_eq!(registry.get_content("todos/list.j2").unwrap(), "Todos");
        assert_eq!(registry.get_content("todos/list.stpl").unwrap(), "Todos");
        assert_eq!(registry.get_content("todos/list").unwrap(), "Todos");
    }

    #[test]
    fn test_framework_cross_extension_lookup() {
        let mut registry = TemplateRegistry::new();
        let entries: &[(&str, &str)] = &[("standout/list-view.jinja", "Framework view")];
        registry.add_framework_entries(entries);

        // Look up with different extension
        let content = registry.get_content("standout/list-view.j2").unwrap();
        assert_eq!(content, "Framework view");
    }

    #[test]
    fn test_files_cross_extension_lookup() {
        let mut registry = TemplateRegistry::new();
        let files = vec![TemplateFile::new(
            "config",
            "config.jinja",
            "/templates/config.jinja",
            "/templates",
        )];
        registry.add_from_files(files).unwrap();

        // Look up with different extension should find via base name
        assert!(registry.get("config.j2").is_ok());
        assert!(registry.get("config.stpl").is_ok());
        assert!(registry.get("config.txt").is_ok());
    }
}