oximedia-transcode 0.1.3

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

#![allow(dead_code)]

use std::collections::HashSet;
use std::path::{Path, PathBuf};
use std::time::Duration;

use crate::{Result, TranscodeConfig, TranscodeError};

/// Action to take when a file has been processed.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum PostProcessAction {
    /// Leave the file in the watch folder.
    Leave,
    /// Move the file to the `done` sub-directory.
    MoveToDone,
    /// Delete the file from the watch folder.
    Delete,
}

/// Policy for selecting the output directory of each transcode job.
#[derive(Debug, Clone)]
pub enum OutputLocation {
    /// Place output in a fixed directory.
    Fixed(PathBuf),
    /// Place output next to the input, using the same name with a new extension.
    SiblingWithExtension(String),
    /// Place output in a `done/` sub-directory of the watch folder.
    DoneSubDir,
}

/// Configuration for `TranscodeWatcher`.
#[derive(Debug, Clone)]
pub struct WatchConfig {
    /// Directory to monitor for incoming files.
    pub watch_dir: PathBuf,
    /// File extensions to accept (lower-case, without leading dot).
    pub accepted_extensions: Vec<String>,
    /// How to determine the output path for each new file.
    pub output_location: OutputLocation,
    /// What to do with an input file after a successful transcode.
    pub on_success: PostProcessAction,
    /// What to do with an input file after a failed transcode.
    pub on_failure: PostProcessAction,
    /// How often to scan the watch directory (milliseconds).
    pub poll_interval_ms: u64,
    /// Base `TranscodeConfig` applied to every discovered file.
    pub base_config: TranscodeConfig,
    /// Maximum number of concurrent jobs.
    pub max_concurrent: usize,
}

impl WatchConfig {
    /// Creates a `WatchConfig` with sensible defaults.
    ///
    /// Accepts common video extensions, moves successful files to `done/`,
    /// and polls every 5 seconds.
    #[must_use]
    pub fn new(watch_dir: impl Into<PathBuf>) -> Self {
        Self {
            watch_dir: watch_dir.into(),
            accepted_extensions: vec![
                "mp4".into(),
                "mkv".into(),
                "mov".into(),
                "avi".into(),
                "webm".into(),
                "mxf".into(),
                "ts".into(),
                "m2ts".into(),
            ],
            output_location: OutputLocation::DoneSubDir,
            on_success: PostProcessAction::MoveToDone,
            on_failure: PostProcessAction::Leave,
            poll_interval_ms: 5_000,
            base_config: TranscodeConfig::default(),
            max_concurrent: 2,
        }
    }

    /// Sets the output location policy.
    #[must_use]
    pub fn output_location(mut self, loc: OutputLocation) -> Self {
        self.output_location = loc;
        self
    }

    /// Sets what happens to the source file after a successful transcode.
    #[must_use]
    pub fn on_success(mut self, action: PostProcessAction) -> Self {
        self.on_success = action;
        self
    }

    /// Sets what happens to the source file after a failed transcode.
    #[must_use]
    pub fn on_failure(mut self, action: PostProcessAction) -> Self {
        self.on_failure = action;
        self
    }

    /// Sets the polling interval in milliseconds.
    #[must_use]
    pub fn poll_interval_ms(mut self, ms: u64) -> Self {
        self.poll_interval_ms = ms;
        self
    }

    /// Overrides the base `TranscodeConfig`.
    #[must_use]
    pub fn base_config(mut self, config: TranscodeConfig) -> Self {
        self.base_config = config;
        self
    }

    /// Sets the maximum number of concurrent jobs.
    #[must_use]
    pub fn max_concurrent(mut self, n: usize) -> Self {
        self.max_concurrent = n;
        self
    }

    /// Validates the configuration.
    ///
    /// # Errors
    ///
    /// Returns an error if the watch directory does not exist.
    pub fn validate(&self) -> Result<()> {
        if !self.watch_dir.exists() {
            return Err(TranscodeError::InvalidInput(format!(
                "Watch directory does not exist: {}",
                self.watch_dir.display()
            )));
        }
        if self.max_concurrent == 0 {
            return Err(TranscodeError::InvalidInput(
                "max_concurrent must be at least 1".into(),
            ));
        }
        Ok(())
    }
}

/// Status of a single watched file.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum WatchFileStatus {
    /// Waiting to be processed.
    Pending,
    /// Currently being transcoded.
    Processing,
    /// Transcoded successfully.
    Done,
    /// Transcode failed; message contains the reason.
    Failed(String),
}

/// A discovered file entry in the watch queue.
#[derive(Debug, Clone)]
pub struct WatchEntry {
    /// Original source file path.
    pub source: PathBuf,
    /// Computed output path.
    pub output: PathBuf,
    /// Current status.
    pub status: WatchFileStatus,
}

impl WatchEntry {
    /// Creates a new watch entry.
    #[must_use]
    pub fn new(source: PathBuf, output: PathBuf) -> Self {
        Self {
            source,
            output,
            status: WatchFileStatus::Pending,
        }
    }
}

/// Directory-based transcode watcher.
///
/// Call [`TranscodeWatcher::scan`] to detect new files, and
/// [`TranscodeWatcher::drain_pending`] to obtain `TranscodeConfig` values
/// ready for submission to the job queue.
pub struct TranscodeWatcher {
    config: WatchConfig,
    /// Paths already seen (regardless of processing status).
    seen: HashSet<PathBuf>,
    /// Queue of watch entries waiting to be processed.
    queue: Vec<WatchEntry>,
}

impl TranscodeWatcher {
    /// Creates a new watcher from `config`.
    #[must_use]
    pub fn new(config: WatchConfig) -> Self {
        Self {
            config,
            seen: HashSet::new(),
            queue: Vec::new(),
        }
    }

    /// Returns the watcher configuration.
    #[must_use]
    pub fn config(&self) -> &WatchConfig {
        &self.config
    }

    /// Returns the poll interval as a [`Duration`].
    #[must_use]
    pub fn poll_interval(&self) -> Duration {
        Duration::from_millis(self.config.poll_interval_ms)
    }

    /// Scans the watch directory for new eligible files.
    ///
    /// Returns the number of new files enqueued.
    ///
    /// # Errors
    ///
    /// Returns an error if the directory cannot be read.
    pub fn scan(&mut self) -> Result<usize> {
        let entries = std::fs::read_dir(&self.config.watch_dir).map_err(|e| {
            TranscodeError::IoError(format!(
                "Cannot read watch dir '{}': {e}",
                self.config.watch_dir.display()
            ))
        })?;

        let mut new_count = 0usize;

        for entry in entries.flatten() {
            let path = entry.path();
            if !path.is_file() {
                continue;
            }
            if self.seen.contains(&path) {
                continue;
            }
            let ext = path
                .extension()
                .and_then(|e| e.to_str())
                .map(str::to_lowercase)
                .unwrap_or_default();
            if !self.config.accepted_extensions.iter().any(|a| a == &ext) {
                continue;
            }

            let output = self.resolve_output(&path);
            self.seen.insert(path.clone());
            self.queue.push(WatchEntry::new(path, output));
            new_count += 1;
        }

        Ok(new_count)
    }

    /// Returns all pending entries (without removing them from the queue).
    #[must_use]
    pub fn pending(&self) -> Vec<&WatchEntry> {
        self.queue
            .iter()
            .filter(|e| e.status == WatchFileStatus::Pending)
            .collect()
    }

    /// Drains all pending entries into a `Vec<TranscodeConfig>` for submission
    /// to the job queue, marking each entry as `Processing`.
    pub fn drain_pending(&mut self) -> Vec<(WatchEntry, TranscodeConfig)> {
        let mut out = Vec::new();

        for entry in &mut self.queue {
            if entry.status != WatchFileStatus::Pending {
                continue;
            }
            entry.status = WatchFileStatus::Processing;

            let mut job = self.config.base_config.clone();
            job.input = entry.source.to_str().map(String::from);
            job.output = entry.output.to_str().map(String::from);

            out.push((entry.clone(), job));
        }

        out
    }

    /// Marks a watch entry as successfully processed and applies the configured
    /// post-process action (move / delete / leave).
    ///
    /// # Errors
    ///
    /// Returns an error if the file move or delete operation fails.
    pub fn mark_done(&mut self, source: &Path) -> Result<()> {
        self.update_status(source, WatchFileStatus::Done);

        match self.config.on_success {
            PostProcessAction::Leave => {}
            PostProcessAction::Delete => {
                std::fs::remove_file(source).map_err(|e| {
                    TranscodeError::IoError(format!("Failed to delete '{}': {e}", source.display()))
                })?;
            }
            PostProcessAction::MoveToDone => {
                self.move_to_done_dir(source)?;
            }
        }

        Ok(())
    }

    /// Marks a watch entry as failed and applies the configured on-failure action.
    ///
    /// # Errors
    ///
    /// Returns an error if the file operation fails.
    pub fn mark_failed(&mut self, source: &Path, reason: &str) -> Result<()> {
        self.update_status(source, WatchFileStatus::Failed(reason.to_string()));

        match self.config.on_failure {
            PostProcessAction::Leave => {}
            PostProcessAction::Delete => {
                std::fs::remove_file(source).map_err(|e| {
                    TranscodeError::IoError(format!("Failed to delete '{}': {e}", source.display()))
                })?;
            }
            PostProcessAction::MoveToDone => {
                self.move_to_done_dir(source)?;
            }
        }

        Ok(())
    }

    /// Returns the total number of queued entries.
    #[must_use]
    pub fn queue_len(&self) -> usize {
        self.queue.len()
    }

    /// Returns the number of entries in each status category.
    #[must_use]
    pub fn status_counts(&self) -> WatchStatusCounts {
        let mut counts = WatchStatusCounts::default();
        for entry in &self.queue {
            match entry.status {
                WatchFileStatus::Pending => counts.pending += 1,
                WatchFileStatus::Processing => counts.processing += 1,
                WatchFileStatus::Done => counts.done += 1,
                WatchFileStatus::Failed(_) => counts.failed += 1,
            }
        }
        counts
    }

    // ── Internal helpers ──────────────────────────────────────────────────────

    fn update_status(&mut self, source: &Path, new_status: WatchFileStatus) {
        for entry in &mut self.queue {
            if entry.source == source {
                entry.status = new_status;
                return;
            }
        }
    }

    fn resolve_output(&self, source: &Path) -> PathBuf {
        match &self.config.output_location {
            OutputLocation::Fixed(dir) => {
                let filename = source
                    .file_name()
                    .map(PathBuf::from)
                    .unwrap_or_else(|| PathBuf::from("output.mkv"));
                dir.join(filename)
            }
            OutputLocation::SiblingWithExtension(ext) => {
                let mut out = source.to_path_buf();
                out.set_extension(ext.trim_start_matches('.'));
                out
            }
            OutputLocation::DoneSubDir => {
                let done_dir = self.config.watch_dir.join("done");
                let filename = source
                    .file_name()
                    .map(PathBuf::from)
                    .unwrap_or_else(|| PathBuf::from("output.mkv"));
                done_dir.join(filename)
            }
        }
    }

    fn move_to_done_dir(&self, source: &Path) -> Result<()> {
        let done_dir = self.config.watch_dir.join("done");
        std::fs::create_dir_all(&done_dir)
            .map_err(|e| TranscodeError::IoError(format!("Cannot create done dir: {e}")))?;
        let dest = done_dir.join(
            source
                .file_name()
                .unwrap_or_else(|| std::ffi::OsStr::new("moved_file")),
        );
        std::fs::rename(source, &dest).map_err(|e| {
            TranscodeError::IoError(format!(
                "Cannot move '{}' → '{}': {e}",
                source.display(),
                dest.display()
            ))
        })
    }
}

/// Snapshot of watch queue status counts.
#[derive(Debug, Clone, Default)]
pub struct WatchStatusCounts {
    /// Number of entries awaiting processing.
    pub pending: usize,
    /// Number of entries currently being transcoded.
    pub processing: usize,
    /// Number of successfully completed entries.
    pub done: usize,
    /// Number of failed entries.
    pub failed: usize,
}

// ─── File stability detection ─────────────────────────────────────────────────

/// Configuration for file stability detection.
///
/// Waits until a file has stopped growing before considering it ready
/// for processing. This prevents partial files (still being copied or
/// written by another process) from entering the transcode queue.
#[derive(Debug, Clone)]
pub struct FileStabilityConfig {
    /// Number of consecutive stable checks required before a file is
    /// considered complete.
    pub required_stable_checks: u32,
    /// Interval between stability checks in milliseconds.
    pub check_interval_ms: u64,
    /// Minimum file size in bytes before stability checks begin.
    pub min_file_size: u64,
}

impl Default for FileStabilityConfig {
    fn default() -> Self {
        Self {
            required_stable_checks: 3,
            check_interval_ms: 2_000,
            min_file_size: 1024,
        }
    }
}

impl FileStabilityConfig {
    /// Creates a new stability config with default values.
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    /// Sets the number of required stable checks.
    #[must_use]
    pub fn required_checks(mut self, n: u32) -> Self {
        self.required_stable_checks = n;
        self
    }

    /// Sets the check interval in milliseconds.
    #[must_use]
    pub fn check_interval_ms(mut self, ms: u64) -> Self {
        self.check_interval_ms = ms;
        self
    }

    /// Sets the minimum file size.
    #[must_use]
    pub fn min_file_size(mut self, size: u64) -> Self {
        self.min_file_size = size;
        self
    }
}

/// Tracks stability state for a single file.
#[derive(Debug, Clone)]
pub struct FileStabilityTracker {
    /// Path being tracked.
    path: PathBuf,
    /// Last observed file size.
    last_size: u64,
    /// Number of consecutive stable readings.
    stable_count: u32,
    /// Whether the file has been declared stable.
    is_stable: bool,
}

impl FileStabilityTracker {
    /// Creates a new tracker for the given path.
    #[must_use]
    pub fn new(path: PathBuf) -> Self {
        Self {
            path,
            last_size: 0,
            stable_count: 0,
            is_stable: false,
        }
    }

    /// Checks the file and updates stability state.
    ///
    /// Returns `true` if the file is now considered stable.
    pub fn check(&mut self, config: &FileStabilityConfig) -> bool {
        if self.is_stable {
            return true;
        }
        let current_size = std::fs::metadata(&self.path).map(|m| m.len()).unwrap_or(0);

        if current_size < config.min_file_size {
            self.stable_count = 0;
            self.last_size = current_size;
            return false;
        }

        if current_size == self.last_size {
            self.stable_count += 1;
        } else {
            self.stable_count = 0;
        }
        self.last_size = current_size;

        if self.stable_count >= config.required_stable_checks {
            self.is_stable = true;
        }
        self.is_stable
    }

    /// Returns true if the file has been declared stable.
    #[must_use]
    pub fn is_stable(&self) -> bool {
        self.is_stable
    }

    /// Returns the path being tracked.
    #[must_use]
    pub fn path(&self) -> &Path {
        &self.path
    }

    /// Returns the last observed file size.
    #[must_use]
    pub fn last_size(&self) -> u64 {
        self.last_size
    }
}

// ─── Hot folder chains ────────────────────────────────────────────────────────

/// A chain of watch folders where the output of one feeds into the next.
///
/// This enables multi-step processing workflows, for example:
/// 1. Ingest folder → transcode to intermediate format
/// 2. Intermediate folder → apply effects / normalisation
/// 3. Final folder → encode to delivery format
#[derive(Debug, Clone)]
pub struct HotFolderChain {
    /// Ordered list of watch configurations forming the chain.
    stages: Vec<WatchConfig>,
}

impl HotFolderChain {
    /// Creates a new empty chain.
    #[must_use]
    pub fn new() -> Self {
        Self { stages: Vec::new() }
    }

    /// Appends a stage to the chain.
    ///
    /// The output directory of the previous stage should match the watch
    /// directory of this stage for seamless chaining.
    pub fn add_stage(&mut self, config: WatchConfig) {
        self.stages.push(config);
    }

    /// Returns the number of stages in the chain.
    #[must_use]
    pub fn stage_count(&self) -> usize {
        self.stages.len()
    }

    /// Returns the stages as a slice.
    #[must_use]
    pub fn stages(&self) -> &[WatchConfig] {
        &self.stages
    }

    /// Validates that the chain is well-formed.
    ///
    /// Checks that each stage's output directory matches the next stage's
    /// watch directory (for `DoneSubDir` and `Fixed` output locations).
    ///
    /// # Errors
    ///
    /// Returns an error if the chain is empty or directories don't align.
    pub fn validate(&self) -> Result<()> {
        if self.stages.is_empty() {
            return Err(TranscodeError::InvalidInput(
                "Hot folder chain has no stages".into(),
            ));
        }

        for i in 0..self.stages.len().saturating_sub(1) {
            let current = &self.stages[i];
            let next = &self.stages[i + 1];

            let output_dir = match &current.output_location {
                OutputLocation::Fixed(dir) => Some(dir.clone()),
                OutputLocation::DoneSubDir => Some(current.watch_dir.join("done")),
                OutputLocation::SiblingWithExtension(_) => None,
            };

            if let Some(out_dir) = output_dir {
                if out_dir != next.watch_dir {
                    return Err(TranscodeError::InvalidInput(format!(
                        "Stage {} output dir '{}' does not match stage {} watch dir '{}'",
                        i,
                        out_dir.display(),
                        i + 1,
                        next.watch_dir.display()
                    )));
                }
            }
        }

        Ok(())
    }
}

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

// ─── Filename pattern matching ────────────────────────────────────────────────

/// Pattern-based file filter for selective watch folder processing.
///
/// Uses simple glob-like patterns (not full regex, to avoid a regex dependency)
/// to match filenames. Supports `*` wildcard and case-insensitive matching.
#[derive(Debug, Clone)]
pub struct FilenamePattern {
    /// The raw pattern string.
    pattern: String,
    /// Whether matching is case-insensitive.
    case_insensitive: bool,
}

impl FilenamePattern {
    /// Creates a new filename pattern.
    #[must_use]
    pub fn new(pattern: impl Into<String>) -> Self {
        Self {
            pattern: pattern.into(),
            case_insensitive: true,
        }
    }

    /// Sets case sensitivity.
    #[must_use]
    pub fn case_insensitive(mut self, ci: bool) -> Self {
        self.case_insensitive = ci;
        self
    }

    /// Tests whether the given filename matches this pattern.
    ///
    /// Supports `*` as a wildcard matching zero or more characters.
    #[must_use]
    pub fn matches(&self, filename: &str) -> bool {
        let (pat, name) = if self.case_insensitive {
            (self.pattern.to_lowercase(), filename.to_lowercase())
        } else {
            (self.pattern.clone(), filename.to_string())
        };
        Self::glob_match(&pat, &name)
    }

    /// Simple glob matching with `*` wildcard.
    fn glob_match(pattern: &str, text: &str) -> bool {
        let pat_chars: Vec<char> = pattern.chars().collect();
        let txt_chars: Vec<char> = text.chars().collect();
        let (plen, tlen) = (pat_chars.len(), txt_chars.len());

        // DP approach for wildcard matching
        let mut dp = vec![vec![false; tlen + 1]; plen + 1];
        dp[0][0] = true;

        // Handle leading *
        for (i, &pc) in pat_chars.iter().enumerate() {
            if pc == '*' {
                dp[i + 1][0] = dp[i][0];
            } else {
                break;
            }
        }

        for i in 1..=plen {
            for j in 1..=tlen {
                if pat_chars[i - 1] == '*' {
                    dp[i][j] = dp[i - 1][j] || dp[i][j - 1];
                } else if pat_chars[i - 1] == '?' || pat_chars[i - 1] == txt_chars[j - 1] {
                    dp[i][j] = dp[i - 1][j - 1];
                }
            }
        }

        dp[plen][tlen]
    }

    /// Returns the raw pattern string.
    #[must_use]
    pub fn pattern(&self) -> &str {
        &self.pattern
    }
}

// ─── Retry with exponential backoff ───────────────────────────────────────────

/// Configuration for retry with exponential backoff.
#[derive(Debug, Clone)]
pub struct RetryConfig {
    /// Maximum number of retry attempts.
    pub max_retries: u32,
    /// Initial delay before the first retry (milliseconds).
    pub initial_delay_ms: u64,
    /// Multiplier applied to the delay after each retry.
    pub backoff_multiplier: f64,
    /// Maximum delay between retries (milliseconds).
    pub max_delay_ms: u64,
}

impl Default for RetryConfig {
    fn default() -> Self {
        Self {
            max_retries: 3,
            initial_delay_ms: 1_000,
            backoff_multiplier: 2.0,
            max_delay_ms: 30_000,
        }
    }
}

impl RetryConfig {
    /// Creates a new retry config with default values.
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    /// Sets the maximum number of retries.
    #[must_use]
    pub fn max_retries(mut self, n: u32) -> Self {
        self.max_retries = n;
        self
    }

    /// Sets the initial delay in milliseconds.
    #[must_use]
    pub fn initial_delay_ms(mut self, ms: u64) -> Self {
        self.initial_delay_ms = ms;
        self
    }

    /// Sets the backoff multiplier.
    #[must_use]
    pub fn backoff_multiplier(mut self, m: f64) -> Self {
        self.backoff_multiplier = m;
        self
    }

    /// Sets the maximum delay in milliseconds.
    #[must_use]
    pub fn max_delay_ms(mut self, ms: u64) -> Self {
        self.max_delay_ms = ms;
        self
    }

    /// Computes the delay for the given attempt number (0-based).
    #[must_use]
    pub fn delay_for_attempt(&self, attempt: u32) -> Duration {
        if attempt == 0 {
            return Duration::from_millis(self.initial_delay_ms);
        }
        let delay = self.initial_delay_ms as f64 * self.backoff_multiplier.powi(attempt as i32);
        let clamped = delay.min(self.max_delay_ms as f64) as u64;
        Duration::from_millis(clamped)
    }
}

/// Tracks retry state for a single file.
#[derive(Debug, Clone)]
pub struct RetryTracker {
    /// Path of the file being retried.
    pub path: PathBuf,
    /// Number of attempts made so far.
    pub attempts: u32,
    /// Last error message.
    pub last_error: Option<String>,
}

impl RetryTracker {
    /// Creates a new retry tracker.
    #[must_use]
    pub fn new(path: PathBuf) -> Self {
        Self {
            path,
            attempts: 0,
            last_error: None,
        }
    }

    /// Records a failed attempt.
    pub fn record_failure(&mut self, error: &str) {
        self.attempts += 1;
        self.last_error = Some(error.to_string());
    }

    /// Returns whether more retries are allowed given the config.
    #[must_use]
    pub fn can_retry(&self, config: &RetryConfig) -> bool {
        self.attempts < config.max_retries
    }

    /// Returns the delay before the next retry.
    #[must_use]
    pub fn next_delay(&self, config: &RetryConfig) -> Duration {
        config.delay_for_attempt(self.attempts)
    }
}

// ─── Watch folder statistics ──────────────────────────────────────────────────

/// Statistics for a watch folder's processing activity.
#[derive(Debug, Clone, Default)]
pub struct WatchFolderStats {
    /// Total number of files processed successfully.
    pub processed_count: u64,
    /// Total number of files that failed processing.
    pub error_count: u64,
    /// Total processing time in milliseconds across all successful jobs.
    pub total_processing_time_ms: u64,
    /// Total bytes processed (input file sizes).
    pub total_bytes_processed: u64,
    /// Minimum processing time in milliseconds.
    pub min_processing_time_ms: Option<u64>,
    /// Maximum processing time in milliseconds.
    pub max_processing_time_ms: Option<u64>,
}

impl WatchFolderStats {
    /// Creates a new empty statistics tracker.
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    /// Records a successful processing event.
    pub fn record_success(&mut self, processing_time_ms: u64, file_size_bytes: u64) {
        self.processed_count += 1;
        self.total_processing_time_ms += processing_time_ms;
        self.total_bytes_processed += file_size_bytes;

        self.min_processing_time_ms = Some(
            self.min_processing_time_ms
                .map_or(processing_time_ms, |m| m.min(processing_time_ms)),
        );
        self.max_processing_time_ms = Some(
            self.max_processing_time_ms
                .map_or(processing_time_ms, |m| m.max(processing_time_ms)),
        );
    }

    /// Records a failed processing event.
    pub fn record_error(&mut self) {
        self.error_count += 1;
    }

    /// Returns the average processing time in milliseconds, or `None` if no files processed.
    #[must_use]
    pub fn avg_processing_time_ms(&self) -> Option<u64> {
        if self.processed_count == 0 {
            return None;
        }
        Some(self.total_processing_time_ms / self.processed_count)
    }

    /// Returns the success rate as a fraction [0.0, 1.0].
    #[must_use]
    pub fn success_rate(&self) -> f64 {
        let total = self.processed_count + self.error_count;
        if total == 0 {
            return 1.0;
        }
        self.processed_count as f64 / total as f64
    }

    /// Returns the average throughput in bytes per second, or `None` if no data.
    #[must_use]
    pub fn avg_throughput_bps(&self) -> Option<f64> {
        if self.total_processing_time_ms == 0 || self.total_bytes_processed == 0 {
            return None;
        }
        let secs = self.total_processing_time_ms as f64 / 1000.0;
        Some(self.total_bytes_processed as f64 / secs)
    }
}

// ─── Tests ────────────────────────────────────────────────────────────────────

#[cfg(test)]
mod tests {
    use super::*;
    use std::env::temp_dir;
    use std::fs;

    fn make_temp_dir(suffix: &str) -> PathBuf {
        let dir = temp_dir().join(format!("oximedia_watch_test_{suffix}"));
        fs::create_dir_all(&dir).expect("create temp dir");
        dir
    }

    fn touch(dir: &Path, name: &str) -> PathBuf {
        let path = dir.join(name);
        fs::write(&path, b"fake media").expect("create temp file");
        path
    }

    #[test]
    fn test_watch_config_new() {
        let cfg = WatchConfig::new("/tmp/watch");
        assert!(!cfg.accepted_extensions.is_empty());
        assert_eq!(cfg.max_concurrent, 2);
        assert_eq!(cfg.poll_interval_ms, 5_000);
    }

    #[test]
    fn test_watch_config_validate_missing_dir() {
        let cfg = WatchConfig::new("/nonexistent/path/for/oximedia_test");
        assert!(cfg.validate().is_err());
    }

    #[test]
    fn test_watch_config_validate_ok() {
        let dir = make_temp_dir("cfg_ok");
        let cfg = WatchConfig::new(&dir);
        assert!(cfg.validate().is_ok());
        fs::remove_dir_all(&dir).ok();
    }

    #[test]
    fn test_scan_detects_new_files() {
        let dir = make_temp_dir("scan");
        touch(&dir, "video.mp4");
        touch(&dir, "clip.mkv");
        touch(&dir, "readme.txt"); // ignored

        let cfg = WatchConfig::new(&dir);
        let mut watcher = TranscodeWatcher::new(cfg);
        let count = watcher.scan().expect("scan ok");
        assert_eq!(count, 2);
        assert_eq!(watcher.queue_len(), 2);

        // Second scan should not re-enqueue
        let count2 = watcher.scan().expect("scan ok");
        assert_eq!(count2, 0);

        fs::remove_dir_all(&dir).ok();
    }

    #[test]
    fn test_drain_pending_creates_configs() {
        let dir = make_temp_dir("drain");
        touch(&dir, "a.mp4");

        let cfg = WatchConfig::new(&dir);
        let mut watcher = TranscodeWatcher::new(cfg);
        watcher.scan().expect("scan ok");

        let drained = watcher.drain_pending();
        assert_eq!(drained.len(), 1);
        let (entry, job) = &drained[0];
        assert!(entry.source.ends_with("a.mp4"));
        assert!(job.input.is_some());
        assert!(job.output.is_some());

        // After drain, pending count should be 0
        assert_eq!(watcher.pending().len(), 0);

        fs::remove_dir_all(&dir).ok();
    }

    #[test]
    fn test_mark_done_updates_status() {
        let dir = make_temp_dir("mark_done");
        let file = touch(&dir, "b.mp4");

        let cfg = WatchConfig::new(&dir).on_success(PostProcessAction::Leave);
        let mut watcher = TranscodeWatcher::new(cfg);
        watcher.scan().expect("scan ok");
        watcher.drain_pending();

        watcher.mark_done(&file).expect("mark done ok");

        let counts = watcher.status_counts();
        assert_eq!(counts.done, 1);
        assert_eq!(counts.failed, 0);

        fs::remove_dir_all(&dir).ok();
    }

    #[test]
    fn test_mark_failed_updates_status() {
        let dir = make_temp_dir("mark_failed");
        let file = touch(&dir, "c.mp4");

        let cfg = WatchConfig::new(&dir).on_failure(PostProcessAction::Leave);
        let mut watcher = TranscodeWatcher::new(cfg);
        watcher.scan().expect("scan ok");
        watcher.drain_pending();

        watcher
            .mark_failed(&file, "codec not found")
            .expect("mark failed ok");

        let counts = watcher.status_counts();
        assert_eq!(counts.failed, 1);

        fs::remove_dir_all(&dir).ok();
    }

    #[test]
    fn test_status_counts() {
        let dir = make_temp_dir("counts");
        touch(&dir, "x.mp4");
        touch(&dir, "y.mkv");

        let cfg = WatchConfig::new(&dir);
        let mut watcher = TranscodeWatcher::new(cfg);
        watcher.scan().expect("scan ok");

        let counts = watcher.status_counts();
        assert_eq!(counts.pending, 2);
        assert_eq!(counts.processing, 0);

        fs::remove_dir_all(&dir).ok();
    }

    #[test]
    fn test_poll_interval() {
        let cfg = WatchConfig::new("/tmp").poll_interval_ms(2000);
        let watcher = TranscodeWatcher::new(cfg);
        assert_eq!(watcher.poll_interval(), Duration::from_secs(2));
    }

    #[test]
    fn test_output_location_sibling() {
        let dir = make_temp_dir("sibling");
        touch(&dir, "d.mp4");

        let cfg = WatchConfig::new(&dir)
            .output_location(OutputLocation::SiblingWithExtension("mkv".into()));
        let mut watcher = TranscodeWatcher::new(cfg);
        watcher.scan().expect("scan ok");

        let entry = &watcher.queue[0];
        assert!(entry
            .output
            .extension()
            .map(|e| e == "mkv")
            .unwrap_or(false));

        fs::remove_dir_all(&dir).ok();
    }

    // ── File stability tests ─────────────────────────────────────────────────

    #[test]
    fn test_stability_config_defaults() {
        let cfg = FileStabilityConfig::default();
        assert_eq!(cfg.required_stable_checks, 3);
        assert_eq!(cfg.check_interval_ms, 2000);
        assert_eq!(cfg.min_file_size, 1024);
    }

    #[test]
    fn test_stability_config_builder() {
        let cfg = FileStabilityConfig::new()
            .required_checks(5)
            .check_interval_ms(1000)
            .min_file_size(4096);
        assert_eq!(cfg.required_stable_checks, 5);
        assert_eq!(cfg.check_interval_ms, 1000);
        assert_eq!(cfg.min_file_size, 4096);
    }

    #[test]
    fn test_stability_tracker_stable_file() {
        let dir = make_temp_dir("stability");
        let path = dir.join("stable.mp4");
        // Write a file larger than default min_file_size
        fs::write(&path, vec![0u8; 2048]).expect("write ok");

        let cfg = FileStabilityConfig::new().required_checks(2);
        let mut tracker = FileStabilityTracker::new(path);

        // First check: sets baseline
        assert!(!tracker.check(&cfg));
        // Second check: size unchanged → stable_count = 1
        assert!(!tracker.check(&cfg));
        // Third check: stable_count = 2 → stable!
        assert!(tracker.check(&cfg));
        assert!(tracker.is_stable());
        assert_eq!(tracker.last_size(), 2048);

        fs::remove_dir_all(&dir).ok();
    }

    #[test]
    fn test_stability_tracker_growing_file() {
        let dir = make_temp_dir("growing");
        let path = dir.join("growing.mp4");
        fs::write(&path, vec![0u8; 2048]).expect("write ok");

        let cfg = FileStabilityConfig::new().required_checks(2);
        let mut tracker = FileStabilityTracker::new(path.clone());

        tracker.check(&cfg); // baseline
        tracker.check(&cfg); // stable 1

        // File grows
        fs::write(&path, vec![0u8; 4096]).expect("grow ok");
        assert!(!tracker.check(&cfg)); // reset

        fs::remove_dir_all(&dir).ok();
    }

    #[test]
    fn test_stability_tracker_too_small() {
        let dir = make_temp_dir("small");
        let path = dir.join("tiny.mp4");
        fs::write(&path, b"x").expect("write ok");

        let cfg = FileStabilityConfig::new().min_file_size(1024);
        let mut tracker = FileStabilityTracker::new(path);

        for _ in 0..10 {
            assert!(!tracker.check(&cfg));
        }

        fs::remove_dir_all(&dir).ok();
    }

    // ── Hot folder chain tests ───────────────────────────────────────────────

    #[test]
    fn test_hot_folder_chain_empty() {
        let chain = HotFolderChain::new();
        assert_eq!(chain.stage_count(), 0);
        assert!(chain.validate().is_err());
    }

    #[test]
    fn test_hot_folder_chain_single_stage() {
        let dir = make_temp_dir("chain1");
        let mut chain = HotFolderChain::new();
        chain.add_stage(WatchConfig::new(&dir));
        assert_eq!(chain.stage_count(), 1);
        assert!(chain.validate().is_ok());
        fs::remove_dir_all(&dir).ok();
    }

    #[test]
    fn test_hot_folder_chain_two_stages_aligned() {
        let dir1 = make_temp_dir("chain2a");
        let dir2 = dir1.join("done");
        fs::create_dir_all(&dir2).expect("create done dir");

        let mut chain = HotFolderChain::new();
        chain.add_stage(WatchConfig::new(&dir1)); // output = dir1/done
        chain.add_stage(WatchConfig::new(&dir2)); // watch = dir1/done
        assert_eq!(chain.stage_count(), 2);
        assert!(chain.validate().is_ok());

        fs::remove_dir_all(&dir1).ok();
    }

    #[test]
    fn test_hot_folder_chain_misaligned() {
        let dir1 = make_temp_dir("chain3a");
        let dir2 = make_temp_dir("chain3b");

        let mut chain = HotFolderChain::new();
        chain.add_stage(WatchConfig::new(&dir1)); // output = dir1/done
        chain.add_stage(WatchConfig::new(&dir2)); // watch = dir2 (mismatch)
        assert!(chain.validate().is_err());

        fs::remove_dir_all(&dir1).ok();
        fs::remove_dir_all(&dir2).ok();
    }

    // ── Filename pattern tests ───────────────────────────────────────────────

    #[test]
    fn test_filename_pattern_exact() {
        let p = FilenamePattern::new("video.mp4");
        assert!(p.matches("video.mp4"));
        assert!(p.matches("VIDEO.MP4")); // case insensitive
        assert!(!p.matches("audio.mp4"));
    }

    #[test]
    fn test_filename_pattern_wildcard() {
        let p = FilenamePattern::new("*.mp4");
        assert!(p.matches("video.mp4"));
        assert!(p.matches("CLIP.MP4"));
        assert!(!p.matches("video.mkv"));
    }

    #[test]
    fn test_filename_pattern_wildcard_prefix() {
        let p = FilenamePattern::new("raw_*");
        assert!(p.matches("raw_clip.mp4"));
        assert!(p.matches("raw_"));
        assert!(!p.matches("clip_raw.mp4"));
    }

    #[test]
    fn test_filename_pattern_multiple_wildcards() {
        let p = FilenamePattern::new("*_final_*");
        assert!(p.matches("clip_final_v2.mp4"));
        assert!(!p.matches("clip_draft_v2.mp4"));
    }

    #[test]
    fn test_filename_pattern_case_sensitive() {
        let p = FilenamePattern::new("Video.mp4").case_insensitive(false);
        assert!(p.matches("Video.mp4"));
        assert!(!p.matches("video.mp4"));
    }

    // ── Retry config tests ───────────────────────────────────────────────────

    #[test]
    fn test_retry_config_defaults() {
        let cfg = RetryConfig::default();
        assert_eq!(cfg.max_retries, 3);
        assert_eq!(cfg.initial_delay_ms, 1000);
        assert!((cfg.backoff_multiplier - 2.0).abs() < 1e-6);
    }

    #[test]
    fn test_retry_delay_exponential() {
        let cfg = RetryConfig::new()
            .initial_delay_ms(1000)
            .backoff_multiplier(2.0)
            .max_delay_ms(10_000);

        assert_eq!(cfg.delay_for_attempt(0), Duration::from_secs(1));
        assert_eq!(cfg.delay_for_attempt(1), Duration::from_secs(2));
        assert_eq!(cfg.delay_for_attempt(2), Duration::from_secs(4));
        assert_eq!(cfg.delay_for_attempt(3), Duration::from_secs(8));
        // Clamped to max
        assert_eq!(cfg.delay_for_attempt(4), Duration::from_secs(10));
    }

    #[test]
    fn test_retry_tracker() {
        let cfg = RetryConfig::new().max_retries(3);
        let mut tracker = RetryTracker::new(PathBuf::from("/tmp/test.mp4"));

        assert!(tracker.can_retry(&cfg));
        assert_eq!(tracker.attempts, 0);

        tracker.record_failure("codec error");
        assert_eq!(tracker.attempts, 1);
        assert_eq!(tracker.last_error.as_deref(), Some("codec error"));
        assert!(tracker.can_retry(&cfg));

        tracker.record_failure("timeout");
        tracker.record_failure("timeout");
        assert!(!tracker.can_retry(&cfg));
    }

    // ── Watch folder statistics tests ────────────────────────────────────────

    #[test]
    fn test_stats_empty() {
        let stats = WatchFolderStats::new();
        assert_eq!(stats.processed_count, 0);
        assert_eq!(stats.error_count, 0);
        assert!(stats.avg_processing_time_ms().is_none());
        assert!((stats.success_rate() - 1.0).abs() < 1e-6);
    }

    #[test]
    fn test_stats_record_success() {
        let mut stats = WatchFolderStats::new();
        stats.record_success(1000, 10_000_000);
        stats.record_success(2000, 20_000_000);

        assert_eq!(stats.processed_count, 2);
        assert_eq!(stats.total_processing_time_ms, 3000);
        assert_eq!(stats.avg_processing_time_ms(), Some(1500));
        assert_eq!(stats.min_processing_time_ms, Some(1000));
        assert_eq!(stats.max_processing_time_ms, Some(2000));
        assert_eq!(stats.total_bytes_processed, 30_000_000);
    }

    #[test]
    fn test_stats_success_rate() {
        let mut stats = WatchFolderStats::new();
        stats.record_success(100, 1000);
        stats.record_success(100, 1000);
        stats.record_error();

        let rate = stats.success_rate();
        assert!((rate - 2.0 / 3.0).abs() < 1e-6);
    }

    #[test]
    fn test_stats_throughput() {
        let mut stats = WatchFolderStats::new();
        stats.record_success(1000, 1_000_000); // 1 MB in 1 second

        let bps = stats.avg_throughput_bps().expect("should have throughput");
        assert!((bps - 1_000_000.0).abs() < 1.0);
    }
}