vkteams-bot-cli 0.7.6

High-performance VK Teams Bot API toolkit with CLI and MCP server support
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
use crate::errors::prelude::{CliError, Result as CliResult};
use chrono::{DateTime, Duration, Utc};
use cron::Schedule;
use serde::{Deserialize, Serialize};
use std::cmp::Ordering as CmpOrdering;
use std::collections::{BinaryHeap, HashMap};
use std::path::PathBuf;
use std::str::FromStr;
use std::sync::Arc;
use std::sync::atomic::{AtomicBool, Ordering};
use tempfile::tempdir;
use tokio::sync::{RwLock, Semaphore, mpsc};
use tokio::time::{Duration as TokioDuration, Instant, sleep_until};
use tracing::{debug, error, info};
use uuid::Uuid;
use vkteams_bot::prelude::*;

pub const SCHEDULER_DATA_FILE: &str = "scheduler_tasks.json";
pub const SCHEDULER_PID_FILE: &str = "scheduler_daemon.pid";

/// Daemon status based on PID file
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum DaemonStatus {
    /// Daemon is not running (no PID file)
    NotRunning,
    /// Daemon is running with given PID and start time
    Running { pid: u32, started_at: DateTime<Utc> },
    /// PID file exists but process is not running (stale PID file)
    Stale { pid: u32, started_at: DateTime<Utc> },
    /// Cannot determine status (e.g., invalid PID file)
    Unknown(String),
}

/// Comprehensive daemon status information
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DaemonStatusInfo {
    pub status: DaemonStatus,
    pub total_tasks: usize,
    pub enabled_tasks: usize,
    pub pid_file_path: PathBuf,
}

/// Events that can trigger scheduler wakeup
#[derive(Debug, Clone)]
pub enum SchedulerEvent {
    TaskAdded(String),
    TaskModified(String),
    TaskRemoved(String),
    ForceWakeup,
    Shutdown,
}

/// Wrapper for scheduled tasks in priority queue
#[derive(Debug, Clone)]
struct ScheduledTaskWrapper {
    #[allow(dead_code)] // Used for future task execution logic
    task_id: String,
    next_run_instant: Instant,
}

impl PartialEq for ScheduledTaskWrapper {
    fn eq(&self, other: &Self) -> bool {
        self.next_run_instant == other.next_run_instant
    }
}

impl Eq for ScheduledTaskWrapper {}

impl PartialOrd for ScheduledTaskWrapper {
    fn partial_cmp(&self, other: &Self) -> Option<CmpOrdering> {
        Some(self.cmp(other))
    }
}

impl Ord for ScheduledTaskWrapper {
    fn cmp(&self, other: &Self) -> CmpOrdering {
        // Reverse ordering for min-heap behavior
        other.next_run_instant.cmp(&self.next_run_instant)
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ScheduledTask {
    pub id: String,
    pub task_type: TaskType,
    pub schedule: ScheduleType,
    pub created_at: DateTime<Utc>,
    pub last_run: Option<DateTime<Utc>>,
    pub next_run: DateTime<Utc>,
    pub enabled: bool,
    pub run_count: u64,
    pub max_runs: Option<u64>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum TaskType {
    SendText { chat_id: String, message: String },
    SendFile { chat_id: String, file_path: String },
    SendVoice { chat_id: String, file_path: String },
    SendAction { chat_id: String, action: String },
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum ScheduleType {
    Once(DateTime<Utc>),
    Cron(String),
    Interval {
        duration_seconds: u64,
        start_time: DateTime<Utc>,
    },
}

/// High-performance event-driven scheduler
pub struct Scheduler {
    tasks: Arc<RwLock<HashMap<String, ScheduledTask>>>,
    task_queue: Arc<RwLock<BinaryHeap<ScheduledTaskWrapper>>>,
    data_file: PathBuf,
    pid_file: PathBuf,
    bot: Option<Bot>,
    event_tx: mpsc::UnboundedSender<SchedulerEvent>,
    event_rx: Arc<RwLock<Option<mpsc::UnboundedReceiver<SchedulerEvent>>>>,
    pub shutdown_signal: Arc<AtomicBool>,
    pub max_concurrent_tasks: usize,
    pub task_timeout: TokioDuration,
}

impl Scheduler {
    pub async fn new(data_dir: Option<PathBuf>) -> CliResult<Self> {
        let data_dir = data_dir.unwrap_or_else(|| {
            dirs::data_dir()
                .map(|d| d.join("vkteams-bot-cli"))
                .unwrap_or_else(|| PathBuf::from("."))
        });

        // Ensure the data directory exists
        tokio::fs::create_dir_all(&data_dir)
            .await
            .map_err(|e| CliError::FileError(format!("Could not create data directory: {e}")))?;

        let data_file = data_dir.join(SCHEDULER_DATA_FILE);
        let pid_file = data_dir.join(SCHEDULER_PID_FILE);

        let (event_tx, event_rx) = mpsc::unbounded_channel();

        let mut scheduler = Self {
            tasks: Arc::new(RwLock::new(HashMap::new())),
            task_queue: Arc::new(RwLock::new(BinaryHeap::new())),
            data_file,
            pid_file,
            bot: None,
            event_tx,
            event_rx: Arc::new(RwLock::new(Some(event_rx))),
            shutdown_signal: Arc::new(AtomicBool::new(false)),
            max_concurrent_tasks: 10,
            task_timeout: TokioDuration::from_secs(300), // 5 minutes
        };

        scheduler.load_tasks_async().await?;
        Ok(scheduler)
    }

    pub fn set_bot(&mut self, bot: Bot) {
        self.bot = Some(bot);
    }

    pub fn set_max_concurrent_tasks(&mut self, max: usize) {
        self.max_concurrent_tasks = max;
    }

    pub fn set_task_timeout(&mut self, timeout: TokioDuration) {
        self.task_timeout = timeout;
    }

    pub async fn add_task(
        &mut self,
        task_type: TaskType,
        schedule: ScheduleType,
        max_runs: Option<u64>,
    ) -> CliResult<String> {
        let id = Uuid::new_v4().to_string();
        let now = Utc::now();
        let next_run = self.calculate_next_run(&schedule, None)?;

        let task = ScheduledTask {
            id: id.clone(),
            task_type,
            schedule,
            created_at: now,
            last_run: None,
            next_run,
            enabled: true,
            run_count: 0,
            max_runs,
        };

        {
            let mut tasks = self.tasks.write().await;
            tasks.insert(id.clone(), task.clone());
        }

        self.add_to_queue(task).await;
        self.save_tasks_async().await?;

        // Notify scheduler about new task
        let _ = self.event_tx.send(SchedulerEvent::TaskAdded(id.clone()));

        info!("Added scheduled task with ID: {}", id);
        Ok(id)
    }

    pub async fn remove_task(&mut self, task_id: &str) -> CliResult<()> {
        let removed = {
            let mut tasks = self.tasks.write().await;
            tasks.remove(task_id)
        };

        if removed.is_some() {
            self.rebuild_queue().await;
            self.save_tasks_async().await?;

            // Notify scheduler about task removal
            let _ = self
                .event_tx
                .send(SchedulerEvent::TaskRemoved(task_id.to_string()));

            info!("Removed task: {}", task_id);
            Ok(())
        } else {
            Err(CliError::InputError(format!("Task not found: {task_id}")))
        }
    }

    pub async fn list_tasks(&self) -> Vec<ScheduledTask> {
        let tasks = self.tasks.read().await;
        tasks.values().cloned().collect()
    }

    pub async fn get_task(&self, task_id: &str) -> Option<ScheduledTask> {
        let tasks = self.tasks.read().await;
        tasks.get(task_id).cloned()
    }

    pub async fn enable_task(&mut self, task_id: &str) -> CliResult<()> {
        let mut modified = false;
        {
            let mut tasks = self.tasks.write().await;
            if let Some(task) = tasks.get_mut(task_id) {
                task.enabled = true;
                modified = true;
            }
        }

        if modified {
            self.rebuild_queue().await;
            self.save_tasks_async().await?;

            // Notify scheduler about task modification
            let _ = self
                .event_tx
                .send(SchedulerEvent::TaskModified(task_id.to_string()));

            info!("Enabled task: {}", task_id);
            Ok(())
        } else {
            Err(CliError::InputError(format!("Task not found: {task_id}")))
        }
    }

    pub async fn disable_task(&mut self, task_id: &str) -> CliResult<()> {
        let mut modified = false;
        {
            let mut tasks = self.tasks.write().await;
            if let Some(task) = tasks.get_mut(task_id) {
                task.enabled = false;
                modified = true;
            }
        }

        if modified {
            self.rebuild_queue().await;
            self.save_tasks_async().await?;

            // Notify scheduler about task modification
            let _ = self
                .event_tx
                .send(SchedulerEvent::TaskModified(task_id.to_string()));

            info!("Disabled task: {}", task_id);
            Ok(())
        } else {
            Err(CliError::InputError(format!("Task not found: {task_id}")))
        }
    }

    /// Event-driven reactive scheduler main loop
    pub async fn run_scheduler(&mut self) -> CliResult<()> {
        if self.bot.is_none() {
            return Err(CliError::InputError(
                "Bot not configured for scheduler".to_string(),
            ));
        }

        info!("Starting event-driven scheduler...");

        // Create PID file to indicate daemon is running
        self.create_pid_file().await?;

        let mut event_rx = {
            let mut rx_guard = self.event_rx.write().await;
            rx_guard
                .take()
                .ok_or_else(|| CliError::InputError("Scheduler already running".to_string()))?
        };

        loop {
            let next_wakeup = self.calculate_next_wakeup().await;

            tokio::select! {
                // Handle timer-based wakeups
                _ = sleep_until(next_wakeup) => {
                    if let Err(e) = self.process_due_tasks().await {
                        error!("Error processing due tasks: {}", e);
                    }
                }

                // Handle dynamic events
                event = event_rx.recv() => {
                    match event {
                        Some(SchedulerEvent::TaskAdded(_)) |
                        Some(SchedulerEvent::TaskModified(_)) => {
                            // Recalculate next wakeup time
                            continue;
                        }
                        Some(SchedulerEvent::ForceWakeup) => {
                            if let Err(e) = self.process_due_tasks().await {
                                error!("Error processing forced tasks: {}", e);
                            }
                        }
                        Some(SchedulerEvent::Shutdown) | None => {
                            info!("Scheduler shutting down...");
                            break;
                        }
                        _ => {}
                    }
                }

                // Handle graceful shutdown signal
                _ = self.wait_for_shutdown() => {
                    info!("Received shutdown signal");
                    break;
                }

                // Check for stop signal file every 5 seconds
                _ = tokio::time::sleep(TokioDuration::from_secs(5)) => {
                    if self.check_stop_signal_file().await {
                        info!("Stop signal file detected, shutting down...");
                        self.cleanup_stop_signal_file().await;
                        break;
                    }
                }
            }

            // Clean up completed tasks periodically
            if let Err(e) = self.cleanup_completed_tasks().await {
                error!("Error cleaning up tasks: {}", e);
            }
        }

        // Clean up PID file when daemon stops
        self.cleanup_pid_file().await;
        info!("Scheduler daemon stopped");

        Ok(())
    }

    pub async fn run_task_once(&mut self, task_id: &str) -> CliResult<()> {
        if self.bot.is_none() {
            return Err(CliError::InputError(
                "Bot not configured for scheduler".to_string(),
            ));
        }

        let task_exists = {
            let tasks = self.tasks.read().await;
            tasks.contains_key(task_id)
        };

        if !task_exists {
            return Err(CliError::InputError(format!("Task not found: {task_id}")));
        }

        self.execute_task(task_id).await
    }

    /// Process all tasks that are due for execution
    async fn process_due_tasks(&mut self) -> CliResult<()> {
        let ready_tasks = self.extract_ready_tasks().await;

        if ready_tasks.is_empty() {
            return Ok(());
        }

        info!("Processing {} due tasks", ready_tasks.len());

        // Execute tasks in parallel with timeout and concurrency control
        let semaphore = Arc::new(Semaphore::new(self.max_concurrent_tasks));
        let timeout = self.task_timeout;

        // Execute tasks sequentially to avoid borrow checker issues
        for task_id in ready_tasks {
            let _permit = semaphore
                .acquire()
                .await
                .map_err(|_| CliError::InputError("Semaphore acquire failed".to_string()))?;

            let result = tokio::time::timeout(timeout, self.execute_task(&task_id))
                .await
                .map_err(|_| CliError::InputError(format!("Task {task_id} execution timeout")))?;

            if let Err(e) = result {
                error!("Task execution failed: {}", e);
            }
        }

        Ok(())
    }

    async fn execute_task(&mut self, task_id: &str) -> CliResult<()> {
        let task = {
            let tasks = self.tasks.read().await;
            tasks.get(task_id).cloned()
        };

        let task =
            task.ok_or_else(|| CliError::InputError(format!("Task not found: {task_id}")))?;

        let bot = self.bot.as_ref().unwrap();

        debug!(
            "Executing task: {} ({})",
            task_id,
            task.task_type.description()
        );

        let result: CliResult<()> = match &task.task_type {
            TaskType::SendText { chat_id, message } => {
                let parser =
                    MessageTextParser::new().add(MessageTextFormat::Plain(message.clone()));
                let request =
                    RequestMessagesSendText::new(ChatId::from_borrowed_str(chat_id.as_str()))
                        .set_text(parser)
                        .map_err(|e| {
                            CliError::InputError(format!("Failed to create message: {e}"))
                        })?;
                bot.send_api_request(request)
                    .await
                    .map_err(CliError::ApiError)
                    .map(|_| ())
            }
            TaskType::SendFile { chat_id, file_path } => {
                let request = RequestMessagesSendFile::new((
                    ChatId::from_borrowed_str(chat_id.as_str()),
                    MultipartName::FilePath(file_path.clone()),
                ));
                bot.send_api_request(request)
                    .await
                    .map_err(CliError::ApiError)
                    .map(|_| ())
            }
            TaskType::SendVoice { chat_id, file_path } => {
                let request = RequestMessagesSendVoice::new((
                    ChatId::from_borrowed_str(chat_id.as_str()),
                    MultipartName::FilePath(file_path.clone()),
                ));
                bot.send_api_request(request)
                    .await
                    .map_err(CliError::ApiError)
                    .map(|_| ())
            }
            TaskType::SendAction { chat_id, action } => {
                let chat_action = match action.as_str() {
                    "typing" => ChatActions::Typing,
                    "looking" => ChatActions::Looking,
                    _ => return Err(CliError::InputError(format!("Unknown action: {action}"))),
                };
                let request = RequestChatsSendAction::new((
                    ChatId::from_borrowed_str(chat_id.as_str()),
                    chat_action,
                ));
                bot.send_api_request(request)
                    .await
                    .map_err(CliError::ApiError)
                    .map(|_| ())
            }
        };

        // Update task statistics after execution
        if result.is_ok() {
            info!("Successfully executed task: {}", task_id);
            self.update_task_after_execution(task_id).await?;
        } else {
            error!("Failed to execute task {}: {:?}", task_id, result);
        }

        result
    }

    async fn update_task_after_execution(&mut self, task_id: &str) -> CliResult<()> {
        let now = Utc::now();
        let mut should_save = false;
        let mut task_to_remove = None;

        {
            let mut tasks = self.tasks.write().await;
            if let Some(task) = tasks.get_mut(task_id) {
                task.last_run = Some(now);
                task.run_count += 1;

                // Check if max runs exceeded
                if let Some(max_runs) = task.max_runs
                    && task.run_count >= max_runs
                {
                    info!("Task {} completed all {} runs", task_id, max_runs);
                    task_to_remove = Some(task_id.to_string());
                }

                // Calculate next run time for recurring tasks
                if task_to_remove.is_none() {
                    match task.schedule {
                        ScheduleType::Once(_) => {
                            // One-time task completed
                            task_to_remove = Some(task_id.to_string());
                        }
                        _ => {
                            // Recurring task - calculate next run
                            if let Ok(next_run) = self.calculate_next_run(&task.schedule, Some(now))
                            {
                                task.next_run = next_run;
                            } else {
                                error!("Failed to calculate next run for task {}", task_id);
                                task_to_remove = Some(task_id.to_string());
                            }
                        }
                    }
                }

                should_save = true;
            }
        }

        // Remove completed tasks
        if let Some(task_id_to_remove) = task_to_remove {
            let mut tasks = self.tasks.write().await;
            tasks.remove(&task_id_to_remove);
            info!("Removed completed task: {}", task_id_to_remove);
        }

        if should_save {
            self.rebuild_queue().await;
            self.save_tasks_async().await?;
        }

        Ok(())
    }

    pub async fn extract_ready_tasks(&self) -> Vec<String> {
        let now = Utc::now();
        let mut ready_tasks = Vec::new();

        let tasks = self.tasks.read().await;
        for task in tasks.values() {
            if task.enabled && task.next_run <= now {
                // Check if max runs exceeded
                if let Some(max_runs) = task.max_runs
                    && task.run_count >= max_runs
                {
                    continue;
                }
                ready_tasks.push(task.id.clone());
            }
        }

        ready_tasks
    }

    pub async fn calculate_next_wakeup(&self) -> Instant {
        let now = Instant::now();
        let mut next_wakeup = now + TokioDuration::from_secs(60); // Default: 1 minute

        let tasks = self.tasks.read().await;
        for task in tasks.values() {
            if task.enabled {
                if let Some(max_runs) = task.max_runs
                    && task.run_count >= max_runs
                {
                    continue;
                }

                // Calculate time until this task should run
                let duration_until_run = task.next_run.signed_duration_since(Utc::now());
                if let Ok(tokio_duration) = duration_until_run.to_std() {
                    let task_instant = now + tokio_duration;
                    if task_instant < next_wakeup {
                        next_wakeup = task_instant;
                    }
                }
            }
        }

        next_wakeup
    }

    async fn wait_for_shutdown(&self) {
        while !self.shutdown_signal.load(Ordering::Relaxed) {
            tokio::time::sleep(TokioDuration::from_millis(100)).await;
        }
    }

    pub async fn shutdown(&self) {
        self.shutdown_signal.store(true, Ordering::Relaxed);
        let _ = self.event_tx.send(SchedulerEvent::Shutdown);
    }

    /// Check if stop signal file exists
    async fn check_stop_signal_file(&self) -> bool {
        let temp_dir = std::env::temp_dir();
        let stop_file = temp_dir.join("vkteams_scheduler_stop.signal");
        stop_file.exists()
    }

    /// Remove stop signal file to acknowledge shutdown
    async fn cleanup_stop_signal_file(&self) {
        let temp_dir = std::env::temp_dir();
        let stop_file = temp_dir.join("vkteams_scheduler_stop.signal");
        if let Err(e) = std::fs::remove_file(&stop_file) {
            debug!("Failed to remove stop signal file: {}", e);
        }
    }

    /// Create PID file when daemon starts
    async fn create_pid_file(&self) -> CliResult<()> {
        let pid = std::process::id();
        let pid_content = format!("{}\n{}", pid, Utc::now().to_rfc3339());

        tokio::fs::write(&self.pid_file, pid_content)
            .await
            .map_err(|e| CliError::FileError(format!("Failed to create PID file: {e}")))?;

        info!("Created PID file at: {:?} with PID: {}", self.pid_file, pid);
        Ok(())
    }

    /// Remove PID file when daemon stops
    async fn cleanup_pid_file(&self) {
        if let Err(e) = tokio::fs::remove_file(&self.pid_file).await {
            debug!("Failed to remove PID file: {}", e);
        } else {
            info!("Removed PID file: {:?}", self.pid_file);
        }
    }

    /// Check if daemon is currently running based on PID file
    pub async fn is_daemon_running(&self) -> DaemonStatus {
        if !self.pid_file.exists() {
            return DaemonStatus::NotRunning;
        }

        let pid_content = match tokio::fs::read_to_string(&self.pid_file).await {
            Ok(content) => content,
            Err(_) => return DaemonStatus::NotRunning,
        };

        let lines: Vec<&str> = pid_content.trim().split('\n').collect();
        if lines.len() < 2 {
            return DaemonStatus::Unknown("Invalid PID file format".to_string());
        }

        let pid: u32 = match lines[0].parse() {
            Ok(pid) => pid,
            Err(_) => return DaemonStatus::Unknown("Invalid PID in file".to_string()),
        };

        let started_at = match DateTime::parse_from_rfc3339(lines[1]) {
            Ok(dt) => dt.with_timezone(&Utc),
            Err(_) => return DaemonStatus::Unknown("Invalid timestamp in PID file".to_string()),
        };

        // Check if process with this PID is actually running
        if self.is_process_running(pid) {
            DaemonStatus::Running { pid, started_at }
        } else {
            DaemonStatus::Stale { pid, started_at }
        }
    }

    /// Check if a process with given PID is running
    fn is_process_running(&self, pid: u32) -> bool {
        #[cfg(unix)]
        {
            match std::process::Command::new("kill")
                .arg("-0")
                .arg(pid.to_string())
                .output()
            {
                Ok(output) => output.status.success(),
                Err(_) => false,
            }
        }

        #[cfg(windows)]
        {
            match std::process::Command::new("tasklist")
                .arg("/FI")
                .arg(&format!("PID eq {}", pid))
                .arg("/FO")
                .arg("CSV")
                .output()
            {
                Ok(output) => {
                    let output_str = String::from_utf8_lossy(&output.stdout);
                    output_str.lines().count() > 1 // More than just header line
                }
                Err(_) => false,
            }
        }
    }

    /// Get detailed daemon status information
    pub async fn get_daemon_status(&self) -> DaemonStatusInfo {
        let status = self.is_daemon_running().await;
        let tasks = self.tasks.read().await;
        let total_tasks = tasks.len();
        let enabled_tasks = tasks.values().filter(|t| t.enabled).count();

        DaemonStatusInfo {
            status,
            total_tasks,
            enabled_tasks,
            pid_file_path: self.pid_file.clone(),
        }
    }

    async fn add_to_queue(&self, task: ScheduledTask) {
        if !task.enabled {
            return;
        }

        let duration_until_run = task.next_run.signed_duration_since(Utc::now());
        if let Ok(tokio_duration) = duration_until_run.to_std() {
            let next_run_instant = Instant::now() + tokio_duration;
            let wrapper = ScheduledTaskWrapper {
                task_id: task.id.clone(),
                next_run_instant,
            };

            let mut queue = self.task_queue.write().await;
            queue.push(wrapper);
        }
    }

    pub async fn rebuild_queue(&self) {
        let mut queue = self.task_queue.write().await;
        queue.clear();

        let tasks = self.tasks.read().await;
        for task in tasks.values() {
            if task.enabled {
                let duration_until_run = task.next_run.signed_duration_since(Utc::now());
                if let Ok(tokio_duration) = duration_until_run.to_std() {
                    let next_run_instant = Instant::now() + tokio_duration;
                    let wrapper = ScheduledTaskWrapper {
                        task_id: task.id.clone(),
                        next_run_instant,
                    };
                    queue.push(wrapper);
                }
            }
        }
    }

    async fn cleanup_completed_tasks(&mut self) -> CliResult<()> {
        let mut tasks_to_remove = Vec::new();

        {
            let tasks = self.tasks.read().await;
            for (task_id, task) in tasks.iter() {
                if !task.enabled {
                    match &task.schedule {
                        ScheduleType::Once(_) => {
                            // Remove completed one-time tasks
                            tasks_to_remove.push(task_id.clone());
                        }
                        _ => {
                            // Keep disabled recurring tasks (user might want to re-enable them)
                        }
                    }
                }
            }
        }

        if !tasks_to_remove.is_empty() {
            let mut tasks = self.tasks.write().await;
            for task_id in &tasks_to_remove {
                tasks.remove(task_id);
                debug!("Cleaned up completed task: {}", task_id);
            }

            if !tasks.is_empty() {
                drop(tasks); // Release the lock before async operations
                self.save_tasks_async().await?;
            }
        }

        Ok(())
    }

    async fn load_tasks_async(&mut self) -> CliResult<()> {
        if !self.data_file.exists() {
            debug!("No scheduler data file found, starting with empty task list");
            return Ok(());
        }

        let content = tokio::fs::read_to_string(&self.data_file)
            .await
            .map_err(|e| CliError::FileError(format!("Could not read scheduler data: {e}")))?;

        let tasks: HashMap<String, ScheduledTask> =
            tokio::task::spawn_blocking(move || serde_json::from_str(&content))
                .await
                .map_err(|e| CliError::UnexpectedError(format!("JSON parsing task failed: {e}")))?
                .map_err(|e| {
                    CliError::UnexpectedError(format!("Could not parse scheduler data: {e}"))
                })?;

        {
            let mut task_guard = self.tasks.write().await;
            *task_guard = tasks;
        }

        self.rebuild_queue().await;

        let task_count = {
            let tasks = self.tasks.read().await;
            tasks.len()
        };

        info!("Loaded {} scheduled tasks", task_count);
        Ok(())
    }

    async fn save_tasks_async(&self) -> CliResult<()> {
        let tasks = {
            let tasks = self.tasks.read().await;
            tasks.clone()
        };

        let task_count = tasks.len();
        let data_file = self.data_file.clone();

        let content = tokio::task::spawn_blocking(move || serde_json::to_string_pretty(&tasks))
            .await
            .map_err(|e| CliError::UnexpectedError(format!("JSON serialization task failed: {e}")))?
            .map_err(|e| CliError::UnexpectedError(format!("Could not serialize tasks: {e}")))?;

        tokio::fs::write(&data_file, content)
            .await
            .map_err(|e| CliError::FileError(format!("Could not write scheduler data: {e}")))?;

        debug!("Saved {} tasks to scheduler data file", task_count);
        Ok(())
    }

    fn calculate_next_run(
        &self,
        schedule: &ScheduleType,
        from_time: Option<DateTime<Utc>>,
    ) -> CliResult<DateTime<Utc>> {
        let now = from_time.unwrap_or_else(Utc::now);

        match schedule {
            ScheduleType::Once(time) => Ok(*time),
            ScheduleType::Cron(expr) => {
                let schedule = Schedule::from_str(expr)
                    .map_err(|e| CliError::InputError(format!("Invalid cron expression: {e}")))?;
                schedule
                    .upcoming(Utc)
                    .next()
                    .ok_or_else(|| CliError::InputError("No upcoming cron execution".to_string()))
            }
            ScheduleType::Interval {
                duration_seconds,
                start_time,
            } => {
                let interval = Duration::seconds(*duration_seconds as i64);
                let mut next_run = *start_time;

                while next_run <= now {
                    next_run += interval;
                }

                Ok(next_run)
            }
        }
    }

    #[allow(dead_code)]
    pub async fn create_test_scheduler() -> (Scheduler, tempfile::TempDir) {
        let temp_dir = tempdir().unwrap();
        let mut data_file = PathBuf::from(temp_dir.path());
        data_file.push("scheduler_tasks_test.json");
        let mut pid_file = PathBuf::from(temp_dir.path());
        pid_file.push("scheduler_daemon_test.pid");

        if let Some(parent) = data_file.parent() {
            tokio::fs::create_dir_all(parent).await.unwrap();
        }

        let (event_tx, event_rx) = mpsc::unbounded_channel();

        let scheduler = Scheduler {
            tasks: Arc::new(RwLock::new(HashMap::new())),
            task_queue: Arc::new(RwLock::new(BinaryHeap::new())),
            data_file,
            pid_file,
            bot: None,
            event_tx,
            event_rx: Arc::new(RwLock::new(Some(event_rx))),
            shutdown_signal: Arc::new(AtomicBool::new(false)),
            max_concurrent_tasks: 10,
            task_timeout: TokioDuration::from_secs(300),
        };
        (scheduler, temp_dir)
    }
}

impl TaskType {
    pub fn description(&self) -> String {
        match self {
            TaskType::SendText { chat_id, .. } => format!("Send text to {chat_id}"),
            TaskType::SendFile { chat_id, file_path } => {
                format!("Send file {file_path} to {chat_id}")
            }
            TaskType::SendVoice { chat_id, file_path } => {
                format!("Send voice {file_path} to {chat_id}")
            }
            TaskType::SendAction { chat_id, action } => {
                format!("Send {action} action to {chat_id}")
            }
        }
    }
}

impl ScheduleType {
    pub fn description(&self) -> String {
        match self {
            ScheduleType::Once(time) => format!("Once at {}", time.format("%Y-%m-%d %H:%M:%S UTC")),
            ScheduleType::Cron(expr) => format!("Cron: {expr}"),
            ScheduleType::Interval {
                duration_seconds,
                start_time,
            } => format!(
                "Every {} seconds starting from {}",
                duration_seconds,
                start_time.format("%Y-%m-%d %H:%M:%S UTC")
            ),
        }
    }

    pub fn next_run_time(&self, from_time: DateTime<Utc>) -> CliResult<DateTime<Utc>> {
        match self {
            ScheduleType::Once(time) => Ok(*time),
            ScheduleType::Cron(expr) => {
                let schedule = Schedule::from_str(expr)
                    .map_err(|e| CliError::InputError(format!("Invalid cron expression: {e}")))?;
                schedule
                    .upcoming(Utc)
                    .next()
                    .ok_or_else(|| CliError::InputError("No upcoming cron execution".to_string()))
            }
            ScheduleType::Interval {
                duration_seconds,
                start_time,
            } => {
                let interval = Duration::seconds(*duration_seconds as i64);
                let mut next_run = *start_time;

                while next_run <= from_time {
                    next_run += interval;
                }

                Ok(next_run)
            }
        }
    }
}

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

    #[tokio::test]
    async fn test_scheduler_creation() {
        let (scheduler, _temp_dir) = Scheduler::create_test_scheduler().await;
        assert_eq!(scheduler.list_tasks().await.len(), 0);
    }

    #[tokio::test]
    async fn test_add_and_list_tasks() {
        let (mut scheduler, _temp_dir) = Scheduler::create_test_scheduler().await;

        let task_id = scheduler
            .add_task(
                TaskType::SendText {
                    chat_id: "test_chat".to_string(),
                    message: "test message".to_string(),
                },
                ScheduleType::Once(Utc::now() + Duration::hours(1)),
                None,
            )
            .await
            .unwrap();

        let tasks = scheduler.list_tasks().await;
        assert_eq!(tasks.len(), 1);
        assert_eq!(tasks[0].id, task_id);
    }

    #[tokio::test]
    async fn test_task_enabling_disabling() {
        let (mut scheduler, _temp_dir) = Scheduler::create_test_scheduler().await;

        let task_id = scheduler
            .add_task(
                TaskType::SendText {
                    chat_id: "test_chat".to_string(),
                    message: "test message".to_string(),
                },
                ScheduleType::Once(Utc::now() + Duration::hours(1)),
                None,
            )
            .await
            .unwrap();

        // Disable task
        scheduler.disable_task(&task_id).await.unwrap();
        let task = scheduler.get_task(&task_id).await.unwrap();
        assert!(!task.enabled);

        // Enable task
        scheduler.enable_task(&task_id).await.unwrap();
        let task = scheduler.get_task(&task_id).await.unwrap();
        assert!(task.enabled);
    }

    #[tokio::test]
    async fn test_cron_schedule() {
        let schedule = ScheduleType::Cron("0 0 0 * * *".to_string()); // Daily at midnight (sec min hour day month dayofweek)
        let next_run = schedule.next_run_time(Utc::now()).unwrap();
        assert!(next_run > Utc::now());
    }

    #[tokio::test]
    async fn test_daemon_status_not_running() {
        let (scheduler, _temp_dir) = Scheduler::create_test_scheduler().await;
        let status = scheduler.is_daemon_running().await;
        assert!(matches!(status, DaemonStatus::NotRunning));
    }

    #[tokio::test]
    async fn test_pid_file_creation_and_cleanup() {
        let (scheduler, _temp_dir) = Scheduler::create_test_scheduler().await;

        // Initially no PID file should exist
        assert!(!scheduler.pid_file.exists());

        // Create PID file
        scheduler.create_pid_file().await.unwrap();
        assert!(scheduler.pid_file.exists());

        // Check daemon status shows running
        let status = scheduler.is_daemon_running().await;
        match status {
            DaemonStatus::Running { pid, .. } => {
                assert_eq!(pid, std::process::id());
            }
            _ => panic!("Expected DaemonStatus::Running"),
        }

        // Clean up PID file
        scheduler.cleanup_pid_file().await;
        assert!(!scheduler.pid_file.exists());
    }

    #[tokio::test]
    async fn test_get_daemon_status_info() {
        let (mut scheduler, _temp_dir) = Scheduler::create_test_scheduler().await;

        // Add some test tasks
        scheduler
            .add_task(
                TaskType::SendText {
                    chat_id: "test_chat".to_string(),
                    message: "test message".to_string(),
                },
                ScheduleType::Once(Utc::now() + Duration::hours(1)),
                None,
            )
            .await
            .unwrap();

        let status_info = scheduler.get_daemon_status().await;

        assert_eq!(status_info.total_tasks, 1);
        assert_eq!(status_info.enabled_tasks, 1);
        assert!(matches!(status_info.status, DaemonStatus::NotRunning));
        assert_eq!(status_info.pid_file_path, scheduler.pid_file);
    }

    #[tokio::test]
    async fn test_interval_schedule() {
        let start_time = Utc::now() - Duration::hours(1);
        let schedule = ScheduleType::Interval {
            duration_seconds: 3600, // 1 hour
            start_time,
        };
        let next_run = schedule.next_run_time(Utc::now()).unwrap();
        assert!(next_run > Utc::now());
        assert!(next_run <= Utc::now() + Duration::hours(1));
    }

    #[tokio::test]
    async fn test_task_persistence() {
        let temp_dir = tempdir().unwrap();
        let _temp_dir = tempdir().unwrap();

        {
            let mut scheduler = Scheduler::new(Some(temp_dir.path().to_path_buf()))
                .await
                .unwrap();
            scheduler
                .add_task(
                    TaskType::SendText {
                        chat_id: "test_chat".to_string(),
                        message: "test message".to_string(),
                    },
                    ScheduleType::Once(Utc::now() + Duration::hours(1)),
                    None,
                )
                .await
                .unwrap();
        }

        // Create new scheduler instance
        let scheduler = Scheduler::new(Some(temp_dir.path().to_path_buf()))
            .await
            .unwrap();
        assert_eq!(scheduler.list_tasks().await.len(), 1);
    }
}