paladin-ai 0.1.0

Enterprise AI orchestration framework with multi-agent coordination patterns
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
/*
Scheduler Orchestrator

Application-layer orchestrator for job scheduling. Relocated from
`core/platform/manager/scheduler.rs`.

Renamed `Scheduler` → `SchedulerOrchestrator`. A backwards-compatible type alias
`Scheduler = SchedulerOrchestrator` is provided.

`Schedule`, `ScheduledJob`, `ScheduledJobInfo`, and `SchedulerStats` are re-exported
from their canonical location in `paladin-core::platform::container::schedule`.
`SchedulerError` is defined in `super::types`.
*/

use super::types::SchedulerError;
use crate::core::base::component::action::ActionPriority;
use crate::core::platform::container::job::Job;
use crate::core::platform::container::task::{
    ContentIndexingService, DataBackupService, EmailNotificationService, Task, TaskService,
};
use chrono::{DateTime, Datelike, Utc};
use std::collections::HashMap;
use std::time::Duration;
use tokio::time::interval;
use uuid::Uuid;

pub use crate::core::platform::container::schedule::{
    Schedule, ScheduledJob, ScheduledJobInfo, SchedulerStats,
};

/// Application-layer job scheduler orchestrator.
///
/// Renamed from `Scheduler`. A backwards-compatible type alias is provided.
pub struct SchedulerOrchestrator {
    scheduled_jobs: HashMap<Uuid, ScheduledJob>,
    services: HashMap<String, Box<dyn TaskService>>,
    running: bool,
    tick_interval: Duration,
}

/// Backwards-compatible alias for `SchedulerOrchestrator`.
pub type Scheduler = SchedulerOrchestrator;

impl SchedulerOrchestrator {
    pub fn new() -> Self {
        let mut scheduler = Self {
            scheduled_jobs: HashMap::new(),
            services: HashMap::new(),
            running: false,
            tick_interval: Duration::from_secs(60), // Check every minute
        };

        // Register default services
        scheduler.register_default_services();
        scheduler
    }

    /// Register a task service.
    pub fn register_service(&mut self, service: Box<dyn TaskService>) {
        let service_name = service.name().to_string();
        self.services.insert(service_name.clone(), service);
        println!("Registered service: {}", service_name);
    }

    /// Register default services.
    fn register_default_services(&mut self) {
        self.register_service(Box::new(DataBackupService {
            backup_path: "/var/backups".to_string(),
        }));

        self.register_service(Box::new(ContentIndexingService {
            index_name: "main_index".to_string(),
        }));

        self.register_service(Box::new(EmailNotificationService {
            smtp_server: "localhost:587".to_string(),
        }));
    }

    /// Add a job to the scheduler.
    pub fn add_job(&mut self, job: Job, schedule: Schedule) -> Result<Uuid, SchedulerError> {
        // Validate that all required services are available
        for task in &job.tasks {
            if !self.services.contains_key(&task.service_name) {
                return Err(SchedulerError::ServiceNotFound(task.service_name.clone()));
            }
        }

        let job_id = job.id();
        let next_run = Self::calculate_next_run(&schedule);

        let scheduled_job = ScheduledJob {
            job,
            schedule,
            enabled: true,
            next_run,
            last_run: None,
            run_count: 0,
        };

        self.scheduled_jobs.insert(job_id, scheduled_job);
        println!("Added job {} to scheduler", job_id);
        Ok(job_id)
    }

    /// Remove a job from the scheduler.
    pub fn remove_job(&mut self, job_id: Uuid) -> bool {
        match self.scheduled_jobs.remove(&job_id) {
            Some(scheduled_job) => {
                println!("Removed job '{}' from scheduler", scheduled_job.job.name());
                true
            }
            None => false,
        }
    }

    /// Enable a scheduled job.
    pub fn enable_job(&mut self, job_id: Uuid) -> bool {
        if let Some(scheduled_job) = self.scheduled_jobs.get_mut(&job_id) {
            scheduled_job.enabled = true;
            scheduled_job.next_run = Self::calculate_next_run(&scheduled_job.schedule);
            println!("Enabled job '{}'", scheduled_job.job.name());
            true
        } else {
            false
        }
    }

    /// Disable a scheduled job.
    pub fn disable_job(&mut self, job_id: Uuid) -> bool {
        if let Some(scheduled_job) = self.scheduled_jobs.get_mut(&job_id) {
            scheduled_job.enabled = false;
            scheduled_job.next_run = None;
            println!("Disabled job '{}'", scheduled_job.job.name());
            true
        } else {
            false
        }
    }

    /// Start the scheduler.
    pub async fn start(&mut self) {
        if self.running {
            println!("Scheduler is already running");
            return;
        }

        self.running = true;
        println!(
            "Starting scheduler with {} jobs and {} services",
            self.scheduled_jobs.len(),
            self.services.len()
        );

        // Add default jobs if none exist
        if self.scheduled_jobs.is_empty() {
            self.add_default_jobs();
        }

        // Execute startup jobs
        self.execute_startup_jobs().await;

        let mut interval = interval(self.tick_interval);

        while self.running {
            interval.tick().await;
            self.check_and_execute_jobs().await;
        }
    }

    /// Stop the scheduler.
    pub fn stop(&mut self) {
        self.running = false;
        println!("Scheduler stopped");
    }

    /// Execute jobs that are scheduled to run on startup.
    async fn execute_startup_jobs(&mut self) {
        let startup_job_ids: Vec<_> = self
            .scheduled_jobs
            .iter()
            .filter(|(_, scheduled_job)| {
                scheduled_job.enabled && matches!(scheduled_job.schedule, Schedule::OnStartup)
            })
            .map(|(id, _)| *id)
            .collect();

        for job_id in startup_job_ids {
            self.execute_job(job_id).await;
        }
    }

    /// Check for jobs that need to be executed and execute them.
    async fn check_and_execute_jobs(&mut self) {
        let now = Utc::now();
        let mut jobs_to_execute = Vec::new();

        // Collect jobs that need to be executed
        for (job_id, scheduled_job) in &self.scheduled_jobs {
            if scheduled_job.enabled
                && let Some(next_run) = scheduled_job.next_run
                && now >= next_run
            {
                jobs_to_execute.push(*job_id);
            }
        }

        // Execute collected jobs
        for job_id in jobs_to_execute {
            self.execute_job(job_id).await;
        }
    }

    /// Execute a specific job.
    async fn execute_job(&mut self, job_id: Uuid) {
        if let Some(scheduled_job) = self.scheduled_jobs.get_mut(&job_id) {
            println!("Executing scheduled job: '{}'", scheduled_job.job.name());

            let start_time = Utc::now();

            // Execute the job
            match scheduled_job.job.execute(&self.services).await {
                Ok(_) => {
                    let stats = scheduled_job.job.job_stats();
                    println!(
                        "Job '{}' completed successfully: {} of {} tasks completed ({}% success rate)",
                        scheduled_job.job.name(),
                        stats.completed_tasks,
                        stats.total_tasks,
                        stats.success_rate
                    );
                }
                Err(e) => {
                    println!("Job '{}' failed: {}", scheduled_job.job.name(), e);

                    // Log partial completion info if available
                    let stats = scheduled_job.job.job_stats();
                    if stats.completed_tasks > 0 {
                        println!(
                            "  Partial completion: {} of {} tasks completed",
                            stats.completed_tasks, stats.total_tasks
                        );
                    }
                }
            }

            // Update scheduling info
            scheduled_job.last_run = Some(start_time);
            scheduled_job.run_count += 1;

            // Calculate next run time
            scheduled_job.next_run = match &scheduled_job.schedule {
                Schedule::Once(_) => None,   // One-time jobs don't repeat
                Schedule::OnStartup => None, // Startup jobs only run once
                _ => Self::calculate_next_run(&scheduled_job.schedule),
            };

            if let Some(next_run) = scheduled_job.next_run {
                println!(
                    "Next run for job '{}': {}",
                    scheduled_job.job.name(),
                    next_run
                );
            } else {
                println!("Job '{}' will not run again", scheduled_job.job.name());
            }
        }
    }

    /// Add default jobs to demonstrate the scheduler.
    fn add_default_jobs(&mut self) {
        // Data backup job - runs every 6 hours
        let backup_task = Task::new(
            "Daily Backup".to_string(),
            "Performs daily data backup".to_string(),
            "DataBackupService".to_string(),
        );

        let backup_job = Job::new(
            "Daily Backup Job".to_string(),
            "Automated daily backup job".to_string(),
            vec![backup_task],
        )
        .with_priority(ActionPriority::High);

        if let Err(e) = self.add_job(
            backup_job,
            Schedule::Interval(Duration::from_secs(6 * 3600)),
        ) {
            println!("Failed to add backup job: {}", e);
        }

        // Content indexing job - runs every hour
        let indexing_task = Task::new(
            "Content Indexing".to_string(),
            "Indexes content for search".to_string(),
            "ContentIndexingService".to_string(),
        );

        let indexing_job = Job::new(
            "Content Indexing Job".to_string(),
            "Automated content indexing job".to_string(),
            vec![indexing_task],
        )
        .with_priority(ActionPriority::Normal);

        if let Err(e) = self.add_job(indexing_job, Schedule::Interval(Duration::from_secs(3600))) {
            println!("Failed to add indexing job: {}", e);
        }

        // Weekly maintenance job with multiple tasks
        let cleanup_task = Task::new(
            "System Cleanup".to_string(),
            "Cleans up temporary files".to_string(),
            "DataBackupService".to_string(), // Reusing backup service for demo
        );

        let mut email_task = Task::new(
            "Weekly Report".to_string(),
            "Sends weekly status report".to_string(),
            "EmailNotificationService".to_string(),
        );

        // Add email arguments
        email_task
            .action
            .add_argument("to_email".to_string(), "admin@example.com")
            .unwrap();
        email_task
            .action
            .add_argument("subject".to_string(), "Weekly System Report")
            .unwrap();

        let maintenance_job = Job::new(
            "Weekly Maintenance".to_string(),
            "Weekly system maintenance and reporting".to_string(),
            vec![cleanup_task, email_task],
        )
        .with_priority(ActionPriority::Low);

        // Run every Sunday at 2:00 AM
        if let Err(e) = self.add_job(maintenance_job, Schedule::Weekly(0, 2, 0)) {
            println!("Failed to add maintenance job: {}", e);
        }

        // Startup job to validate system
        let validation_task = Task::new(
            "System Validation".to_string(),
            "Validates system on startup".to_string(),
            "ContentIndexingService".to_string(),
        );

        let startup_job = Job::new(
            "Startup Validation".to_string(),
            "Validates system health on startup".to_string(),
            vec![validation_task],
        )
        .with_priority(ActionPriority::Critical);

        if let Err(e) = self.add_job(startup_job, Schedule::OnStartup) {
            println!("Failed to add startup job: {}", e);
        }
    }

    /// Calculate the next run time for a schedule.
    fn calculate_next_run(schedule: &Schedule) -> Option<DateTime<Utc>> {
        let now = Utc::now();

        match schedule {
            Schedule::Interval(duration) => Some(now + chrono::Duration::from_std(*duration).ok()?),
            Schedule::Daily(hour, minute) => {
                let mut next = now.date_naive().and_hms_opt(*hour, *minute, 0)?;
                if next <= now.naive_utc() {
                    next += chrono::Duration::days(1);
                }
                Some(DateTime::from_naive_utc_and_offset(next, Utc))
            }
            Schedule::Weekly(day_of_week, hour, minute) => {
                let current_weekday = now.weekday().number_from_sunday() as u8;
                let days_until_target = if *day_of_week >= current_weekday {
                    *day_of_week - current_weekday
                } else {
                    7 - current_weekday + *day_of_week
                };

                let target_date =
                    now.date_naive() + chrono::Duration::days(days_until_target as i64);
                let next = target_date.and_hms_opt(*hour, *minute, 0)?;

                // If it's the same day but time has passed, schedule for next week
                if days_until_target == 0 && next <= now.naive_utc() {
                    let next_week = target_date + chrono::Duration::days(7);
                    let next = next_week.and_hms_opt(*hour, *minute, 0)?;
                    Some(DateTime::from_naive_utc_and_offset(next, Utc))
                } else {
                    Some(DateTime::from_naive_utc_and_offset(next, Utc))
                }
            }
            Schedule::Monthly(day, hour, minute) => {
                let mut target_date = now.date_naive();

                // Try to set the day for current month
                if let Some(date_with_day) = target_date.with_day(*day) {
                    let next = date_with_day.and_hms_opt(*hour, *minute, 0)?;
                    if next > now.naive_utc() {
                        return Some(DateTime::from_naive_utc_and_offset(next, Utc));
                    }
                }

                // Move to next month - set to day 1 first to avoid issues with month boundaries
                target_date = target_date.with_day(1)?;
                target_date = if target_date.month() == 12 {
                    target_date
                        .with_year(target_date.year() + 1)?
                        .with_month(1)?
                } else {
                    target_date.with_month(target_date.month() + 1)?
                };

                let next = target_date.with_day(*day)?.and_hms_opt(*hour, *minute, 0)?;
                Some(DateTime::from_naive_utc_and_offset(next, Utc))
            }
            Schedule::Once(datetime) => {
                if *datetime > now {
                    Some(*datetime)
                } else {
                    None // One-time job already passed
                }
            }
            Schedule::OnStartup => {
                None // Startup jobs don't have a next run time
            }
        }
    }

    /// List all scheduled jobs.
    pub fn list_jobs(&self) -> Vec<ScheduledJobInfo> {
        self.scheduled_jobs
            .iter()
            .map(|(id, scheduled_job)| ScheduledJobInfo {
                id: *id,
                name: scheduled_job.job.name().to_string(),
                enabled: scheduled_job.enabled,
                next_run: scheduled_job.next_run,
                last_run: scheduled_job.last_run,
                run_count: scheduled_job.run_count,
                task_count: scheduled_job.job.tasks.len(),
                status: scheduled_job.job.status().clone(),
                schedule: scheduled_job.schedule.clone(),
            })
            .collect()
    }

    /// Get job details.
    pub fn get_job(&self, job_id: Uuid) -> Option<&ScheduledJob> {
        self.scheduled_jobs.get(&job_id)
    }

    /// Get service names.
    pub fn list_services(&self) -> Vec<String> {
        self.services.keys().cloned().collect()
    }

    /// Get scheduler statistics.
    pub fn stats(&self) -> SchedulerStats {
        let total_jobs = self.scheduled_jobs.len();
        let enabled_jobs = self.scheduled_jobs.values().filter(|j| j.enabled).count();
        let total_runs = self.scheduled_jobs.values().map(|j| j.run_count).sum();

        let next_job = self
            .scheduled_jobs
            .values()
            .filter(|j| j.enabled && j.next_run.is_some())
            .min_by_key(|j| j.next_run.unwrap());

        SchedulerStats {
            total_jobs,
            enabled_jobs,
            total_runs,
            total_services: self.services.len(),
            next_job_name: next_job.map(|j| j.job.name().to_string()),
            next_job_time: next_job.and_then(|j| j.next_run),
        }
    }
}

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

/// Convenience function to start the scheduler.
pub async fn start_scheduler() {
    let mut scheduler = SchedulerOrchestrator::new();
    scheduler.start().await;
}

/// Create a new scheduler instance with custom services.
pub fn create_scheduler_with_services(
    services: Vec<Box<dyn TaskService>>,
) -> SchedulerOrchestrator {
    let mut scheduler = SchedulerOrchestrator::new();

    // Clear default services if custom ones are provided
    if !services.is_empty() {
        scheduler.services.clear();
        for service in services {
            scheduler.register_service(service);
        }
    }

    scheduler
}

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

    #[tokio::test]
    async fn test_scheduler_creation() {
        let scheduler = SchedulerOrchestrator::new();
        assert!(!scheduler.running);
        assert_eq!(scheduler.scheduled_jobs.len(), 0);
        assert!(!scheduler.services.is_empty()); // Should have default services
    }

    #[tokio::test]
    async fn test_add_job() {
        let mut scheduler = SchedulerOrchestrator::new();

        let task = Task::new(
            "Test Task".to_string(),
            "A test task".to_string(),
            "DataBackupService".to_string(),
        );

        let job = Job::new("Test Job".to_string(), "A test job".to_string(), vec![task]);

        let job_id = scheduler
            .add_job(job, Schedule::Interval(Duration::from_secs(3600)))
            .unwrap();

        assert_eq!(scheduler.scheduled_jobs.len(), 1);
        assert!(scheduler.scheduled_jobs.contains_key(&job_id));
    }

    #[tokio::test]
    async fn test_service_registration() {
        let mut scheduler = SchedulerOrchestrator::new();
        let initial_service_count = scheduler.services.len();

        // Register a service with a different name to avoid replacement
        let custom_service = DataBackupService {
            backup_path: "/custom/backup".to_string(),
        };

        // Since we're using the same service type, it will replace the existing one
        // Let's test this behavior explicitly
        scheduler.register_service(Box::new(custom_service));

        // The count should remain the same because we replaced an existing service
        assert_eq!(scheduler.services.len(), initial_service_count);

        // Verify the service was actually replaced by checking the backup path
        let service = scheduler.services.get("DataBackupService").unwrap();
        // We can't directly access the backup_path from the trait object,
        // but we can verify the service exists and has the right name
        assert_eq!(service.name(), "DataBackupService");
    }

    #[tokio::test]
    async fn test_service_registration_new_service() {
        let mut scheduler = SchedulerOrchestrator::new();
        let initial_service_count = scheduler.services.len();

        // Create a new service with a unique name
        #[derive(Debug)]
        struct CustomService;

        #[async_trait]
        impl crate::core::platform::container::task::TaskService for CustomService {
            fn name(&self) -> &str {
                "CustomTestService"
            }

            async fn execute(
                &self,
                _action: &crate::core::base::component::action::Action,
            ) -> Result<Option<serde_json::Value>, crate::core::platform::container::task::TaskError>
            {
                Ok(Some(serde_json::json!({"test": "success"})))
            }

            fn clone_service(&self) -> Box<dyn TaskService> {
                Box::new(CustomService)
            }
        }

        scheduler.register_service(Box::new(CustomService));

        // Now the count should increase by 1
        assert_eq!(scheduler.services.len(), initial_service_count + 1);
        assert!(scheduler.services.contains_key("CustomTestService"));
    }

    #[tokio::test]
    async fn test_service_replacement() {
        let mut scheduler = SchedulerOrchestrator::new();
        let initial_service_count = scheduler.services.len();

        // Register a service with the same name as an existing one
        let replacement_service = DataBackupService {
            backup_path: "/replacement/backup".to_string(),
        };

        scheduler.register_service(Box::new(replacement_service));

        // Count should remain the same (replacement, not addition)
        assert_eq!(scheduler.services.len(), initial_service_count);

        // Service should still exist with the same name
        assert!(scheduler.services.contains_key("DataBackupService"));
    }

    #[tokio::test]
    async fn test_schedule_calculation() {
        // Test interval schedule
        let interval_schedule = Schedule::Interval(Duration::from_secs(3600));
        let next_run = SchedulerOrchestrator::calculate_next_run(&interval_schedule);
        assert!(next_run.is_some());

        // Test one-time schedule in the future
        let future_time = Utc::now() + chrono::Duration::hours(1);
        let once_schedule = Schedule::Once(future_time);
        let next_run = SchedulerOrchestrator::calculate_next_run(&once_schedule);
        assert_eq!(next_run, Some(future_time));

        // Test one-time schedule in the past
        let past_time = Utc::now() - chrono::Duration::hours(1);
        let past_schedule = Schedule::Once(past_time);
        let next_run = SchedulerOrchestrator::calculate_next_run(&past_schedule);
        assert_eq!(next_run, None);
    }

    #[tokio::test]
    async fn test_job_enable_disable() {
        let mut scheduler = SchedulerOrchestrator::new();

        let task = Task::new(
            "Test Task".to_string(),
            "A test task".to_string(),
            "DataBackupService".to_string(),
        );

        let job = Job::new("Test Job".to_string(), "A test job".to_string(), vec![task]);

        let job_id = scheduler
            .add_job(job, Schedule::Interval(Duration::from_secs(3600)))
            .unwrap();

        // Test disable
        assert!(scheduler.disable_job(job_id));
        let scheduled_job = scheduler.scheduled_jobs.get(&job_id).unwrap();
        assert!(!scheduled_job.enabled);
        assert!(scheduled_job.next_run.is_none());

        // Test enable
        assert!(scheduler.enable_job(job_id));
        let scheduled_job = scheduler.scheduled_jobs.get(&job_id).unwrap();
        assert!(scheduled_job.enabled);
        assert!(scheduled_job.next_run.is_some());
    }

    #[tokio::test]
    async fn test_error_variants_display() {
        let service_not_found = SchedulerError::ServiceNotFound("TestService".to_string());
        assert_eq!(
            service_not_found.to_string(),
            "Service not found: TestService"
        );

        let job_not_found = SchedulerError::JobNotFound(Uuid::new_v4());
        assert!(job_not_found.to_string().contains("Job not found"));

        let invalid_schedule = SchedulerError::InvalidSchedule("bad schedule".to_string());
        assert_eq!(
            invalid_schedule.to_string(),
            "Invalid schedule: bad schedule"
        );
    }

    #[tokio::test]
    async fn test_remove_job() {
        let mut scheduler = SchedulerOrchestrator::new();

        let task = Task::new(
            "Test Task".to_string(),
            "A test task".to_string(),
            "DataBackupService".to_string(),
        );

        let job = Job::new("Test Job".to_string(), "A test job".to_string(), vec![task]);

        let job_id = scheduler
            .add_job(job, Schedule::Interval(Duration::from_secs(3600)))
            .unwrap();

        assert_eq!(scheduler.scheduled_jobs.len(), 1);

        // Remove the job
        assert!(scheduler.remove_job(job_id));
        assert_eq!(scheduler.scheduled_jobs.len(), 0);

        // Try to remove again - should return false
        assert!(!scheduler.remove_job(job_id));
    }

    #[tokio::test]
    async fn test_enable_nonexistent_job() {
        let mut scheduler = SchedulerOrchestrator::new();
        let fake_id = Uuid::new_v4();

        assert!(!scheduler.enable_job(fake_id));
    }

    #[tokio::test]
    async fn test_disable_nonexistent_job() {
        let mut scheduler = SchedulerOrchestrator::new();
        let fake_id = Uuid::new_v4();

        assert!(!scheduler.disable_job(fake_id));
    }

    #[tokio::test]
    async fn test_add_job_with_missing_service() {
        let mut scheduler = SchedulerOrchestrator::new();

        let task = Task::new(
            "Test Task".to_string(),
            "A test task".to_string(),
            "NonExistentService".to_string(), // This service doesn't exist
        );

        let job = Job::new("Test Job".to_string(), "A test job".to_string(), vec![task]);

        let result = scheduler.add_job(job, Schedule::Interval(Duration::from_secs(3600)));

        assert!(result.is_err());
        match result {
            Err(SchedulerError::ServiceNotFound(name)) => {
                assert_eq!(name, "NonExistentService");
            }
            _ => panic!("Expected ServiceNotFound error"),
        }
    }

    #[tokio::test]
    async fn test_scheduler_stats() {
        let scheduler = SchedulerOrchestrator::new();
        let stats = scheduler.stats();

        assert_eq!(stats.total_jobs, 0);
        assert_eq!(stats.enabled_jobs, 0);
        assert_eq!(stats.total_runs, 0);
        assert!(stats.total_services > 0); // Should have default services
        assert!(stats.next_job_name.is_none());
        assert!(stats.next_job_time.is_none());
    }

    #[tokio::test]
    async fn test_scheduler_stats_with_jobs() {
        let mut scheduler = SchedulerOrchestrator::new();

        let task = Task::new(
            "Test Task".to_string(),
            "A test task".to_string(),
            "DataBackupService".to_string(),
        );

        let job = Job::new("Test Job".to_string(), "A test job".to_string(), vec![task]);

        scheduler
            .add_job(job, Schedule::Interval(Duration::from_secs(3600)))
            .unwrap();

        let stats = scheduler.stats();
        assert_eq!(stats.total_jobs, 1);
        assert_eq!(stats.enabled_jobs, 1);
        assert!(stats.next_job_name.is_some());
        assert!(stats.next_job_time.is_some());
    }

    #[tokio::test]
    async fn test_scheduled_job_structure() {
        let task = Task::new(
            "Test Task".to_string(),
            "A test task".to_string(),
            "DataBackupService".to_string(),
        );

        let job = Job::new("Test Job".to_string(), "A test job".to_string(), vec![task]);
        let schedule = Schedule::Interval(Duration::from_secs(3600));

        let scheduled_job = ScheduledJob {
            job: job.clone(),
            schedule: schedule.clone(),
            enabled: true,
            next_run: Some(Utc::now()),
            last_run: None,
            run_count: 0,
        };

        assert!(scheduled_job.enabled);
        assert!(scheduled_job.next_run.is_some());
        assert!(scheduled_job.last_run.is_none());
        assert_eq!(scheduled_job.run_count, 0);
    }

    #[tokio::test]
    async fn test_schedule_variants() {
        // Test Interval
        let interval = Schedule::Interval(Duration::from_secs(3600));
        assert!(matches!(interval, Schedule::Interval(_)));

        // Test Daily
        let daily = Schedule::Daily(9, 30); // 9:30 AM
        assert!(matches!(daily, Schedule::Daily(9, 30)));

        // Test Weekly
        let weekly = Schedule::Weekly(1, 10, 0); // Monday 10:00 AM
        assert!(matches!(weekly, Schedule::Weekly(1, 10, 0)));

        // Test Monthly
        let monthly = Schedule::Monthly(15, 14, 30); // 15th of month, 2:30 PM
        assert!(matches!(monthly, Schedule::Monthly(15, 14, 30)));

        // Test Once
        let once_time = Utc::now() + chrono::Duration::hours(1);
        let once = Schedule::Once(once_time);
        assert!(matches!(once, Schedule::Once(_)));

        // Test OnStartup
        let startup = Schedule::OnStartup;
        assert!(matches!(startup, Schedule::OnStartup));
    }

    #[tokio::test]
    async fn test_calculate_next_run_daily() {
        // Daily at 9:00 AM
        let daily_schedule = Schedule::Daily(9, 0);
        let next_run = SchedulerOrchestrator::calculate_next_run(&daily_schedule);
        assert!(next_run.is_some());
    }
}