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
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
/*
Orchestration Use Cases

Application-layer orchestration module. Relocated from
`core/platform/manager/orchestrator.rs`, `listener_service.rs`, and `scheduler.rs`.

This module exposes:
- `Orchestrator` — coordinates jobs, workflows, events, and content processing
- `listener` sub-module — `ListenerOrchestrator` (renamed from `ListenerService`)
- `scheduler` sub-module — `SchedulerOrchestrator` (renamed from `Scheduler`)
- `types` sub-module — coordination error/stats/trait types
- `OrchestrationContext` re-exported from paladin-core for callers
*/

pub mod listener;
pub mod scheduler;
pub mod types;

pub use crate::core::platform::container::orchestration_context::OrchestrationContext;
pub use listener::{EventListener, ListenerConfig, ListenerService, ListenerStats};
pub use scheduler::{Schedule, Scheduler, SchedulerStats};
pub use types::{
    ContentAnalysisType, ContentProcessingResult, ContentProcessor, DefaultContentProcessor,
    ListenerError, OrchestratorError, OrchestratorStats, SchedulerError,
};

use crate::application::use_cases::queue_orchestrator::QueueService;
use crate::core::base::component::action::{Action, ActionPriority};
use crate::core::base::component::event::Event;
use crate::core::base::entity::message::{Location, Message, MessagePriority};
use crate::core::platform::container::content::ContentItem;
use crate::core::platform::container::job::Job;
use crate::core::platform::container::queue_item::QueueItem;
use crate::core::platform::container::task::{Task, TaskError, TaskService};
use crate::core::platform::container::trigger::{Trigger, TriggerCondition};
use crate::core::platform::container::workflow::{
    Workflow, WorkflowExecutionOrder, WorkflowListener,
};
use chrono::Utc;
use listener::ListenerOrchestrator;
use scheduler::SchedulerOrchestrator;
use std::collections::HashMap;
use std::sync::Arc;
use tokio::sync::{Mutex, RwLock};
use uuid::Uuid;

/// Main Orchestrator service.
pub struct Orchestrator {
    scheduler: Arc<Mutex<SchedulerOrchestrator>>,
    queue_service: Arc<QueueService>,
    listener_service: Arc<ListenerOrchestrator>,
    task_services: Arc<RwLock<HashMap<String, Box<dyn TaskService>>>>,
    workflows: Arc<RwLock<HashMap<Uuid, Workflow>>>,
    active_sessions: Arc<RwLock<HashMap<Uuid, OrchestrationContext>>>,
    content_processors: Arc<RwLock<HashMap<String, Box<dyn ContentProcessor>>>>,
}

impl Orchestrator {
    /// Create a new Orchestrator.
    pub fn new() -> Self {
        Self {
            scheduler: Arc::new(Mutex::new(SchedulerOrchestrator::new())),
            queue_service: Arc::new(QueueService::new()),
            listener_service: Arc::new(ListenerOrchestrator::new()),
            task_services: Arc::new(RwLock::new(HashMap::new())),
            workflows: Arc::new(RwLock::new(HashMap::new())),
            active_sessions: Arc::new(RwLock::new(HashMap::new())),
            content_processors: Arc::new(RwLock::new(HashMap::new())),
        }
    }

    /// Start the orchestrator and all its services.
    pub async fn start(&self) -> Result<(), OrchestratorError> {
        // Initialize default services
        self.initialize_default_services().await?;

        // Start scheduler in background
        let scheduler_clone = Arc::clone(&self.scheduler);
        tokio::spawn(async move {
            let mut scheduler = scheduler_clone.lock().await;
            scheduler.start().await;
        });

        println!("Orchestrator started successfully");
        Ok(())
    }

    /// Register a task service.
    pub async fn register_task_service(
        &self,
        service: Box<dyn TaskService>,
    ) -> Result<(), OrchestratorError> {
        let service_name = service.name().to_string();

        // Register with scheduler using the new clone_service method
        {
            let mut scheduler = self.scheduler.lock().await;
            scheduler.register_service(service.clone_service());
        }

        // Register with orchestrator
        {
            let mut services = self.task_services.write().await;
            services.insert(service_name.clone(), service);
        }

        println!("Registered task service: {}", service_name);
        Ok(())
    }

    /// Register an event listener.
    pub async fn register_event_listener(
        &self,
        listener: Box<dyn EventListener>,
    ) -> Result<(), OrchestratorError> {
        self.listener_service
            .register_listener(listener)
            .await
            .map_err(OrchestratorError::ListenerError)?;
        Ok(())
    }

    /// Register a content processor.
    pub async fn register_content_processor(
        &self,
        processor: Box<dyn ContentProcessor>,
    ) -> Result<(), OrchestratorError> {
        let processor_name = processor.name().to_string();
        let mut processors = self.content_processors.write().await;
        processors.insert(processor_name.clone(), processor);
        println!("Registered content processor: {}", processor_name);
        Ok(())
    }

    /// Create and execute a simple job immediately.
    pub async fn execute_job(
        &self,
        job: Job,
        context: OrchestrationContext,
    ) -> Result<Uuid, OrchestratorError> {
        let job_id = job.id();

        // Start orchestration session
        self.start_session(context.clone()).await?;

        // Execute job with registered services - collect cloned services
        let services: HashMap<String, Box<dyn TaskService>> = {
            let services_guard = self.task_services.read().await;
            services_guard
                .iter()
                .map(|(k, v)| (k.clone(), v.clone_service()))
                .collect()
        };

        let mut job_clone = job.clone();
        match job_clone.execute(&services).await {
            Ok(_) => {
                println!("Job '{}' executed successfully", job_clone.name());
                self.end_session(context.session_id).await?;
                Ok(job_id)
            }
            Err(e) => {
                println!("Job '{}' execution failed: {}", job_clone.name(), e);
                self.end_session(context.session_id).await?;
                Err(OrchestratorError::JobError(e))
            }
        }
    }

    /// Schedule a job for recurring execution.
    pub async fn schedule_job(
        &self,
        job: Job,
        schedule: Schedule,
        context: OrchestrationContext,
    ) -> Result<Uuid, OrchestratorError> {
        let job_id = job.id();

        // Start orchestration session
        self.start_session(context).await?;

        // Add to scheduler
        {
            let mut scheduler = self.scheduler.lock().await;
            scheduler
                .add_job(job, schedule)
                .map_err(OrchestratorError::SchedulerError)?;
        }

        println!("Job scheduled with ID: {}", job_id);
        Ok(job_id)
    }

    /// Queue a job for asynchronous execution.
    pub async fn queue_job(
        &self,
        job: Job,
        queue_name: &str,
        context: OrchestrationContext,
    ) -> Result<Uuid, OrchestratorError> {
        // Convert job to queue item
        let message = Message::with_priority(
            Location::system("orchestrator"),
            Location::service("job_processor"),
            job.clone(),
            match job.action.priority {
                ActionPriority::Low => MessagePriority::Low,
                ActionPriority::Normal => MessagePriority::Normal,
                ActionPriority::High => MessagePriority::High,
                ActionPriority::Critical => MessagePriority::Critical,
            },
        );

        let queue_item = QueueItem::new(queue_name.to_string(), message, None);
        let item_id = self
            .queue_service
            .enqueue(queue_name, queue_item)
            .await
            .map_err(OrchestratorError::QueueError)?;

        // Start orchestration session
        self.start_session(context).await?;

        println!("Job queued with item ID: {}", item_id);
        Ok(item_id)
    }

    /// Process content through a content analysis workflow.
    pub async fn process_content(
        &self,
        content: ContentItem,
        processor_name: &str,
        context: OrchestrationContext,
    ) -> Result<ContentProcessingResult, OrchestratorError> {
        // Get content processor
        let processor = {
            let processors = self.content_processors.read().await;
            processors
                .get(processor_name)
                .ok_or_else(|| OrchestratorError::ProcessorNotFound(processor_name.to_string()))?
                .clone_box()?
        };

        // Start orchestration session
        self.start_session(context.clone()).await?;

        // Process content
        let result = processor.process_content(content, context.clone()).await?;

        // End session
        self.end_session(context.session_id).await?;

        Ok(result)
    }

    /// Create and register a complete workflow.
    pub async fn create_workflow(&self, mut workflow: Workflow) -> Result<Uuid, OrchestratorError> {
        let workflow_id = workflow.id;

        // Create queues for the workflow (skip if empty or if creation fails)
        for workflow_queue in &workflow.queues {
            match self
                .queue_service
                .create_queue(
                    workflow_queue.name.clone(),
                    Some(workflow_queue.config.clone()),
                )
                .await
            {
                Ok(_) => println!("Created queue: {}", workflow_queue.name),
                Err(e) => {
                    println!(
                        "Warning: Failed to create queue {}: {:?}",
                        workflow_queue.name, e
                    );
                    // Continue with workflow creation even if queue creation fails
                }
            }
        }

        // Register listeners for the workflow
        for workflow_listener in &workflow.listeners {
            let listener = self
                .create_workflow_listener(workflow_listener.clone(), workflow_id)
                .await?;
            if let Err(e) = self.listener_service.register_listener(listener).await {
                println!(
                    "Warning: Failed to register listener {}: {:?}",
                    workflow_listener.name, e
                );
                // Continue with workflow creation even if listener registration fails
            }
        }

        // Schedule or queue jobs based on execution order
        match &workflow.execution_order {
            WorkflowExecutionOrder::Sequential => {
                // For testing, just store the workflow without complex scheduling
                println!(
                    "Sequential execution order configured for workflow {}",
                    workflow_id
                );
            }
            WorkflowExecutionOrder::Parallel => {
                // For testing, just store the workflow without complex scheduling
                println!(
                    "Parallel execution order configured for workflow {}",
                    workflow_id
                );
            }
            WorkflowExecutionOrder::EventDriven => {
                // Jobs are triggered by events through listeners
                println!(
                    "Event-driven execution order configured for workflow {}",
                    workflow_id
                );
            }
            WorkflowExecutionOrder::Custom(stages) => {
                // Complex multi-stage execution
                println!(
                    "Custom execution order with {} stages configured for workflow {}",
                    stages.len(),
                    workflow_id
                );
            }
        }

        // Store workflow
        workflow.updated_at = Utc::now();
        let mut workflows = self.workflows.write().await;
        workflows.insert(workflow_id, workflow);

        println!(
            "Workflow '{}' created with ID: {}",
            workflows.get(&workflow_id).unwrap().name,
            workflow_id
        );
        Ok(workflow_id)
    }

    /// Process an event through the listener system.
    pub async fn process_event(&self, event: Event) -> Result<Vec<Uuid>, OrchestratorError> {
        let trigger_ids = self
            .listener_service
            .process_event(event)
            .await
            .map_err(OrchestratorError::ListenerError)?;

        // Process created triggers
        for trigger_id in &trigger_ids {
            if let Some(trigger) = self.listener_service.get_next_trigger().await {
                println!(
                    "Processing trigger {} for trigger ID {}",
                    trigger.name, trigger_id
                );
                self.execute_trigger(trigger).await?;
            }
        }

        Ok(trigger_ids)
    }

    /// Execute a trigger (convert to job and execute).
    async fn execute_trigger(&self, trigger: Trigger) -> Result<(), OrchestratorError> {
        // Check if this is a workflow trigger with a specific target job
        let workflows = self.workflows.read().await;
        let mut target_job: Option<Job> = None;

        // Find the workflow and target job if specified
        for workflow in workflows.values() {
            for listener in &workflow.listeners {
                if listener.name == trigger.source
                    && let Some(target_job_id) = listener.target_job_id
                {
                    // Find the specific job in the workflow
                    if let Some(job) = workflow.jobs.iter().find(|j| j.id() == target_job_id) {
                        target_job = Some(job.clone());
                        break;
                    }
                }
            }
            if target_job.is_some() {
                break;
            }
        }

        // Use the target job if found, otherwise create a generic job from the trigger
        let job = if let Some(job) = target_job {
            job
        } else {
            // Create a job from the trigger's action (existing behavior)
            let task = Task::new(
                trigger.action.name.clone(),
                trigger.action.description.clone(),
                trigger.target.clone(),
            );

            Job::new(
                format!("Triggered Job: {}", trigger.name),
                format!("Job created from trigger: {}", trigger.description),
                vec![task],
            )
        };

        let context = OrchestrationContext::new(
            trigger.source.clone(),
            "production".to_string(), // Could be configurable
        );

        // Execute the job
        self.execute_job(job, context).await?;

        Ok(())
    }

    /// Create a content analysis workflow.
    pub async fn create_content_analysis_workflow(
        &self,
        content_items: Vec<ContentItem>,
        analysis_type: ContentAnalysisType,
        context: OrchestrationContext,
    ) -> Result<Uuid, OrchestratorError> {
        if content_items.is_empty() {
            return Err(OrchestratorError::ConfigurationError(
                "No content items provided".to_string(),
            ));
        }

        let workflow_id = Uuid::new_v4();

        // Create tasks for content analysis
        let mut jobs = Vec::new();

        for (i, content_item) in content_items.iter().enumerate() {
            let content_task = self
                .create_content_analysis_task(content_item.clone(), analysis_type.clone(), i)
                .await?;

            let job = Job::new(
                format!("Content Analysis Job {}", i + 1),
                format!("Analyze content item: {}", content_item.uuid()),
                vec![content_task],
            )
            .with_priority(ActionPriority::Normal);

            jobs.push(job);
        }

        // Create workflow without queues to avoid FileNotFound errors
        let workflow = Workflow {
            id: workflow_id,
            name: format!("Content Analysis Workflow - {}", analysis_type.name()),
            description: format!(
                "Analyze {} content items using {}",
                content_items.len(),
                analysis_type.name()
            ),
            jobs,
            listeners: vec![],
            queues: vec![], // Remove queue creation for now
            execution_order: WorkflowExecutionOrder::Parallel,
            context: context.clone(),
            created_at: Utc::now(),
            updated_at: Utc::now(),
        };

        // Store workflow directly instead of calling create_workflow
        // to avoid queue creation issues
        let mut workflows = self.workflows.write().await;
        workflows.insert(workflow_id, workflow);

        println!("Content analysis workflow created with ID: {}", workflow_id);

        Ok(workflow_id)
    }

    /// Start an orchestration session.
    async fn start_session(&self, context: OrchestrationContext) -> Result<(), OrchestratorError> {
        let mut sessions = self.active_sessions.write().await;
        sessions.insert(context.session_id, context.clone());
        println!("Started orchestration session: {}", context.session_id);
        Ok(())
    }

    /// End an orchestration session.
    async fn end_session(&self, session_id: Uuid) -> Result<(), OrchestratorError> {
        let mut sessions = self.active_sessions.write().await;
        if let Some(context) = sessions.remove(&session_id) {
            let duration = Utc::now().timestamp() - context.started_at.timestamp();
            println!(
                "Ended orchestration session: {} (duration: {}s)",
                session_id, duration
            );
        }
        Ok(())
    }

    /// Get orchestrator statistics.
    pub async fn get_stats(&self) -> OrchestratorStats {
        let scheduler_stats = {
            let scheduler = self.scheduler.lock().await;
            scheduler.stats()
        };

        let queue_stats = self.queue_service.get_all_stats().await;
        let listener_stats = self.listener_service.get_all_stats().await;

        let workflows_count = {
            let workflows = self.workflows.read().await;
            workflows.len()
        };

        let active_sessions_count = {
            let sessions = self.active_sessions.read().await;
            sessions.len()
        };

        let services_count = {
            let services = self.task_services.read().await;
            services.len()
        };

        let processors_count = {
            let processors = self.content_processors.read().await;
            processors.len()
        };

        OrchestratorStats {
            active_sessions: active_sessions_count,
            total_workflows: workflows_count,
            total_services: services_count,
            total_processors: processors_count,
            scheduler_stats,
            queue_stats,
            listener_stats,
        }
    }

    /// Initialize default services.
    async fn initialize_default_services(&self) -> Result<(), OrchestratorError> {
        // Register default task services
        self.register_task_service(Box::new(
            crate::core::platform::container::task::DataBackupService {
                backup_path: "/var/backups".to_string(),
            },
        ))
        .await?;

        self.register_task_service(Box::new(
            crate::core::platform::container::task::ContentIndexingService {
                index_name: "main_index".to_string(),
            },
        ))
        .await?;

        self.register_task_service(Box::new(
            crate::core::platform::container::task::EmailNotificationService {
                smtp_server: "localhost:587".to_string(),
            },
        ))
        .await?;

        // Register default content processors
        self.register_content_processor(Box::new(DefaultContentProcessor))
            .await?;

        Ok(())
    }

    /// Create a workflow listener.
    async fn create_workflow_listener(
        &self,
        workflow_listener: WorkflowListener,
        workflow_id: Uuid,
    ) -> Result<Box<dyn EventListener>, OrchestratorError> {
        Ok(Box::new(WorkflowEventListener {
            name: workflow_listener.name,
            conditions: workflow_listener.conditions,
            target_job_id: workflow_listener.target_job_id,
            target_queue: workflow_listener.target_queue,
            workflow_id,
            config: ListenerConfig::default(),
        }))
    }

    /// Create a content analysis task.
    async fn create_content_analysis_task(
        &self,
        content_item: ContentItem,
        analysis_type: ContentAnalysisType,
        index: usize,
    ) -> Result<Task, OrchestratorError> {
        let mut task = Task::new(
            format!("Content Analysis Task {}", index + 1),
            format!("Analyze content using {}", analysis_type.name()),
            "ContentAnalysisService".to_string(),
        );

        // Add content item and analysis type as arguments
        task.action
            .add_argument("content_item".to_string(), content_item)
            .map_err(|e| OrchestratorError::TaskError(TaskError::ActionError(e)))?;

        task.action
            .add_argument("analysis_type".to_string(), analysis_type)
            .map_err(|e| OrchestratorError::TaskError(TaskError::ActionError(e)))?;

        Ok(task)
    }
}

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

/// Workflow event listener implementation.
struct WorkflowEventListener {
    name: String,
    conditions: Vec<TriggerCondition>,
    target_job_id: Option<Uuid>,
    target_queue: Option<String>,
    workflow_id: Uuid,
    config: ListenerConfig,
}

use async_trait::async_trait;

#[async_trait]
impl EventListener for WorkflowEventListener {
    fn name(&self) -> &str {
        &self.name
    }

    fn description(&self) -> &str {
        "Workflow event listener"
    }

    fn conditions(&self) -> &[TriggerCondition] {
        &self.conditions
    }

    async fn should_process(&self, event: &Event) -> bool {
        // Simple pattern matching for demo
        self.conditions.iter().any(|condition| {
            condition.event_type_pattern == "*"
                || condition.event_type_pattern == event.event_type
                || event
                    .event_type
                    .contains(&condition.event_type_pattern.replace('*', ""))
        })
    }

    async fn create_trigger(&self, event: Event) -> Result<Trigger, ListenerError> {
        let mut action = Action::new(
            format!("Workflow Action: {}", self.name),
            format!(
                "Action triggered by workflow listener for event: {}",
                event.event_type
            ),
            self.name.clone(),
            self.target_queue
                .clone()
                .unwrap_or_else(|| "default_queue".to_string()),
        );

        // Add target job ID to action metadata if specified
        if let Some(job_id) = self.target_job_id {
            action
                .add_argument("target_job_id".to_string(), job_id)
                .map_err(|e| ListenerError::TriggerCreationFailed(e.to_string()))?;
        }

        let condition = self
            .conditions
            .first()
            .ok_or_else(|| {
                ListenerError::InvalidConfiguration("No conditions defined".to_string())
            })?
            .clone();

        Ok(Trigger::new(
            format!("Workflow Trigger: {}", self.name),
            format!(
                "Trigger for workflow {} from event {}",
                self.workflow_id, event.event_type
            ),
            self.name.clone(),
            self.target_queue
                .clone()
                .unwrap_or_else(|| "default_service".to_string()),
            event,
            action,
            condition,
        ))
    }

    fn config(&self) -> &ListenerConfig {
        &self.config
    }

    fn update_config(&mut self, config: ListenerConfig) {
        self.config = config;
    }

    async fn health_check(&self) -> Result<bool, ListenerError> {
        Ok(true)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::application::use_cases::queue_orchestrator::QueueError;
    use crate::core::platform::container::content::{ContentType, TextContent};

    #[tokio::test]
    async fn test_orchestrator_creation() {
        let orchestrator = Orchestrator::new();

        let stats = orchestrator.get_stats().await;
        assert_eq!(stats.active_sessions, 0);
        assert_eq!(stats.total_workflows, 0);
    }

    #[tokio::test]
    async fn test_orchestrator_default() {
        let orchestrator = Orchestrator::default();
        let stats = orchestrator.get_stats().await;
        assert_eq!(stats.active_sessions, 0);
    }

    #[tokio::test]
    async fn test_orchestration_context_creation() {
        let context = OrchestrationContext::new("test_user".to_string(), "dev".to_string());

        assert_eq!(context.initiator, "test_user");
        assert_eq!(context.environment, "dev");
        assert!(context.correlation_id.is_none());
        assert!(context.metadata.is_empty());
        assert!(context.started_at <= Utc::now());
    }

    #[tokio::test]
    async fn test_orchestration_context_with_correlation() {
        let correlation_id = Uuid::new_v4();
        let context = OrchestrationContext::new("test_user".to_string(), "staging".to_string())
            .with_correlation(correlation_id);

        assert_eq!(context.correlation_id, Some(correlation_id));
    }

    #[tokio::test]
    async fn test_orchestration_context_add_metadata() {
        let mut context = OrchestrationContext::new("test_user".to_string(), "prod".to_string());

        let result = context.add_metadata("key1".to_string(), "value1");
        assert!(result.is_ok());

        assert_eq!(context.metadata.len(), 1);
        assert!(context.metadata.contains_key("key1"));
    }

    #[tokio::test]
    async fn test_orchestration_context_add_complex_metadata() {
        let mut context = OrchestrationContext::new("test_user".to_string(), "test".to_string());

        let complex_data = serde_json::json!({
            "nested": {
                "field": 123,
                "array": [1, 2, 3]
            }
        });

        let result = context.add_metadata("complex".to_string(), complex_data);
        assert!(result.is_ok());
        assert!(context.metadata.contains_key("complex"));
    }

    #[tokio::test]
    async fn test_error_variants_display() {
        let scheduler_err =
            OrchestratorError::SchedulerError(SchedulerError::JobNotFound(Uuid::new_v4()));
        assert!(scheduler_err.to_string().contains("Scheduler error"));

        let queue_err =
            OrchestratorError::QueueError(QueueError::QueueNotFound("test_queue".to_string()));
        assert!(queue_err.to_string().contains("Queue error"));

        let listener_err = OrchestratorError::ListenerError(ListenerError::ListenerNotFound(
            "test_listener".to_string(),
        ));
        assert!(listener_err.to_string().contains("Listener error"));

        let processor_err = OrchestratorError::ProcessorNotFound("test_processor".to_string());
        assert_eq!(
            processor_err.to_string(),
            "Processor not found: test_processor"
        );

        let workflow_err = OrchestratorError::WorkflowNotFound(Uuid::new_v4());
        assert!(workflow_err.to_string().contains("Workflow not found"));

        let session_err = OrchestratorError::SessionNotFound(Uuid::new_v4());
        assert!(session_err.to_string().contains("Session not found"));

        let serialization_err = OrchestratorError::SerializationError("invalid json".to_string());
        assert_eq!(
            serialization_err.to_string(),
            "Serialization error: invalid json"
        );

        let config_err = OrchestratorError::ConfigurationError("bad config".to_string());
        assert_eq!(config_err.to_string(), "Configuration error: bad config");

        let service_err = OrchestratorError::ServiceError("service down".to_string());
        assert_eq!(service_err.to_string(), "Service error: service down");
    }

    #[tokio::test]
    async fn test_orchestrator_service_registration() {
        let orchestrator = Orchestrator::new();

        let service = crate::core::platform::container::task::DataBackupService {
            backup_path: "/test/backup".to_string(),
        };

        let result = orchestrator.register_task_service(Box::new(service)).await;
        assert!(result.is_ok());

        let stats = orchestrator.get_stats().await;
        assert_eq!(stats.total_services, 1);
    }

    #[tokio::test]
    async fn test_start_and_end_session() {
        let orchestrator = Orchestrator::new();

        let context = OrchestrationContext::new("test_user".to_string(), "test".to_string());
        let session_id = context.session_id;

        // Start session
        let start_result = orchestrator.start_session(context).await;
        assert!(start_result.is_ok());

        let stats = orchestrator.get_stats().await;
        assert_eq!(stats.active_sessions, 1);

        // End session
        let end_result = orchestrator.end_session(session_id).await;
        assert!(end_result.is_ok());

        let stats = orchestrator.get_stats().await;
        assert_eq!(stats.active_sessions, 0);
    }

    #[tokio::test]
    async fn test_end_nonexistent_session() {
        let orchestrator = Orchestrator::new();

        let fake_session_id = Uuid::new_v4();
        let result = orchestrator.end_session(fake_session_id).await;

        // end_session returns Ok even if session doesn't exist (idempotent)
        assert!(result.is_ok());
    }

    #[tokio::test]
    async fn test_content_processing() {
        let orchestrator = Orchestrator::new();
        orchestrator.start().await.unwrap();

        // Create test content
        let text_content = TextContent::new(None, Some("Test content".to_string())).unwrap();
        let content_item = ContentItem::new(ContentType::Text(text_content)).unwrap();

        let context = OrchestrationContext::new("test_user".to_string(), "test".to_string());

        let result = orchestrator
            .process_content(content_item, "DefaultContentProcessor", context)
            .await;

        assert!(result.is_ok());
        let processing_result = result.unwrap();
        assert!(processing_result.success);
        assert!(processing_result.result_data.is_some());
        assert!(processing_result.error.is_none());
        assert_eq!(processing_result.processor_name, "DefaultContentProcessor");
    }

    #[tokio::test]
    async fn test_content_processing_with_nonexistent_processor() {
        let orchestrator = Orchestrator::new();
        orchestrator.start().await.unwrap();

        let text_content = TextContent::new(None, Some("Test".to_string())).unwrap();
        let content_item = ContentItem::new(ContentType::Text(text_content)).unwrap();
        let context = OrchestrationContext::new("test_user".to_string(), "test".to_string());

        let result = orchestrator
            .process_content(content_item, "NonExistentProcessor", context)
            .await;

        assert!(result.is_err());
        match result {
            Err(OrchestratorError::ProcessorNotFound(name)) => {
                assert_eq!(name, "NonExistentProcessor");
            }
            _ => panic!("Expected ProcessorNotFound error"),
        }
    }

    #[tokio::test]
    async fn test_job_execution() {
        let orchestrator = Orchestrator::new();
        orchestrator.start().await.unwrap();

        // Create a simple job
        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 context = OrchestrationContext::new("test_user".to_string(), "test".to_string());

        let result = orchestrator.execute_job(job, context).await;
        assert!(result.is_ok());
    }

    #[tokio::test]
    async fn test_schedule_job() {
        let orchestrator = Orchestrator::new();
        orchestrator.start().await.unwrap();

        let task = Task::new(
            "Scheduled Task".to_string(),
            "A scheduled task".to_string(),
            "DataBackupService".to_string(),
        );

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

        // Use Interval schedule instead of cron
        let schedule = Schedule::Interval(std::time::Duration::from_secs(3600));
        let context = OrchestrationContext::new("test_user".to_string(), "test".to_string());

        let result = orchestrator.schedule_job(job, schedule, context).await;
        assert!(result.is_ok());
    }

    #[tokio::test]
    async fn test_queue_job() {
        let orchestrator = Orchestrator::new();
        orchestrator.start().await.unwrap();

        let task = Task::new(
            "Queued Task".to_string(),
            "A queued task".to_string(),
            "DataBackupService".to_string(),
        );

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

        let context = OrchestrationContext::new("test_user".to_string(), "test".to_string());

        // Create the queue first
        let _ = orchestrator
            .queue_service
            .create_queue("test_queue".to_string(), None)
            .await;

        let result = orchestrator.queue_job(job, "test_queue", context).await;
        assert!(result.is_ok());
    }

    #[tokio::test]
    async fn test_content_analysis_workflow() {
        let orchestrator = Arc::new(Orchestrator::new());
        orchestrator.start().await.unwrap();

        // Create test content without URL to avoid file system access
        let text_content = TextContent::new(
            None, // Remove URL to avoid file system operations
            Some("This is test content for analysis".to_string()),
        )
        .unwrap();
        let content_item = ContentItem::new(ContentType::Text(text_content)).unwrap();

        let context = OrchestrationContext::new("test_service".to_string(), "test".to_string());

        let result = orchestrator
            .create_content_analysis_workflow(
                vec![content_item],
                ContentAnalysisType::LanguageDetection,
                context,
            )
            .await;

        // Better error handling with detailed error information
        match result {
            Ok(workflow_id) => {
                assert!(workflow_id != Uuid::nil());

                // Verify workflow was created
                let workflows = orchestrator.workflows.read().await;
                assert!(workflows.contains_key(&workflow_id));

                let workflow = workflows.get(&workflow_id).unwrap();
                assert!(!workflow.jobs.is_empty());
                assert!(workflow.name.contains("Content Analysis Workflow"));
            }
            Err(e) => {
                panic!("Workflow creation failed with error: {:?}", e);
            }
        }
    }

    #[tokio::test]
    async fn test_event_processing() {
        let orchestrator = Orchestrator::new();
        orchestrator.start().await.unwrap();

        let event = Event::new(
            "test_event".to_string(),
            serde_json::json!({"test": "data"}),
            "test_source".to_string(),
        );

        let result = orchestrator.process_event(event).await;
        assert!(result.is_ok());
    }

    #[tokio::test]
    async fn test_multiple_sessions() {
        let orchestrator = Orchestrator::new();

        // Start multiple sessions
        let context1 = OrchestrationContext::new("user1".to_string(), "dev".to_string());
        let context2 = OrchestrationContext::new("user2".to_string(), "prod".to_string());
        let context3 = OrchestrationContext::new("user3".to_string(), "staging".to_string());

        orchestrator.start_session(context1.clone()).await.unwrap();
        orchestrator.start_session(context2.clone()).await.unwrap();
        orchestrator.start_session(context3.clone()).await.unwrap();

        let stats = orchestrator.get_stats().await;
        assert_eq!(stats.active_sessions, 3);

        // End one session
        orchestrator.end_session(context1.session_id).await.unwrap();

        let stats = orchestrator.get_stats().await;
        assert_eq!(stats.active_sessions, 2);
    }

    #[tokio::test]
    async fn test_workflow_creation() {
        let orchestrator = Orchestrator::new();
        orchestrator.start().await.unwrap();

        // Create workflow manually since there's no Workflow::new
        let context = OrchestrationContext::new("test_user".to_string(), "test".to_string());
        let workflow = Workflow {
            id: Uuid::new_v4(),
            name: "Test Workflow".to_string(),
            description: "A test workflow".to_string(),
            jobs: Vec::new(),
            listeners: Vec::new(),
            queues: Vec::new(),
            execution_order: WorkflowExecutionOrder::Sequential,
            context,
            created_at: Utc::now(),
            updated_at: Utc::now(),
        };

        let result = orchestrator.create_workflow(workflow).await;
        assert!(result.is_ok());

        let stats = orchestrator.get_stats().await;
        assert_eq!(stats.total_workflows, 1);
    }

    #[tokio::test]
    async fn test_default_content_processor() {
        let processor = DefaultContentProcessor;
        assert_eq!(processor.name(), "DefaultContentProcessor");

        let text_content = TextContent::new(None, Some("Test content".to_string())).unwrap();
        let content_item = ContentItem::new(ContentType::Text(text_content)).unwrap();
        let context = OrchestrationContext::new("test_user".to_string(), "test".to_string());

        let result = processor.process_content(content_item, context).await;
        assert!(result.is_ok());

        let processing_result = result.unwrap();
        assert!(processing_result.success);
        assert_eq!(processing_result.processor_name, "DefaultContentProcessor");
    }

    #[tokio::test]
    async fn test_content_processor_clone() {
        let processor = DefaultContentProcessor;
        let cloned = processor.clone_box();
        assert!(cloned.is_ok());

        let cloned_processor = cloned.unwrap();
        assert_eq!(cloned_processor.name(), "DefaultContentProcessor");
    }

    #[tokio::test]
    async fn test_orchestrator_stats_structure() {
        let orchestrator = Orchestrator::new();
        orchestrator.start().await.unwrap(); // Start to register default processor and services

        let stats = orchestrator.get_stats().await;

        assert_eq!(stats.active_sessions, 0);
        assert_eq!(stats.total_workflows, 0);
        assert_eq!(stats.total_services, 3); // DataBackupService, ContentIndexingService, EmailNotificationService
        assert_eq!(stats.total_processors, 1); // DefaultContentProcessor registered on start
        assert!(stats.queue_stats.is_empty());
        assert!(stats.listener_stats.is_empty());
    }

    #[tokio::test]
    async fn test_content_processing_result_structure() {
        let result = ContentProcessingResult {
            content_id: Uuid::new_v4(),
            processor_name: "TestProcessor".to_string(),
            processing_time_ms: 150,
            success: true,
            result_data: Some(serde_json::json!({"test": "data"})),
            error: None,
            metadata: HashMap::new(),
        };

        assert_eq!(result.processor_name, "TestProcessor");
        assert_eq!(result.processing_time_ms, 150);
        assert!(result.success);
        assert!(result.result_data.is_some());
        assert!(result.error.is_none());
        assert!(result.metadata.is_empty());
    }
}