zinzen 0.3.0

Algorithm for auto-scheduling time-constrained tasks on a timeline
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
use std::cmp::{min, PartialEq};
use std::collections::{BTreeMap, HashSet};
use std::fmt::{Debug, Formatter};
use std::ops::{Add, Sub};
use std::rc::Rc;

use chrono::{Duration, NaiveDateTime, Weekday};
use serde::{Deserialize, Serialize};

use crate::models::activity::ActivityStatus::{BestEffort, Impossible, Scheduled};
use crate::models::activity::ActivityType::TopUpWeekBudget;
use crate::models::budget::TimeBudgetType::{Period, Week};
use crate::models::calendar_interval::CalIntStatus::Claimable;
use crate::models::calendar_interval::{CalIntStatus, CalendarInterval};
use crate::models::interval::Interval;
use crate::models::time_grid;
use crate::technical::error::SchedulerError;

use super::activity::{Activity, ActivityStatus};
use super::budget::{get_time_budgets_from, CalendarBudget};
use super::goal::{Budget, BudgetPeriod, Goal};
use super::task::{DayTasks, FinalTasks, Task};

#[derive(Debug, Serialize, Deserialize, Clone)]
#[serde(rename_all = "camelCase")]
pub struct ImpossibleActivity {
    pub id: String,
    pub minutes_missing: usize,
    pub period_start_date_time: NaiveDateTime,
    pub period_end_date_time: Option<NaiveDateTime>,
}

pub struct Calendar {
    pub start_date_time: NaiveDateTime,
    pub end_date_time: NaiveDateTime,
    pub impossible_activities: Vec<ImpossibleActivity>,
    pub budgets: Vec<CalendarBudget>,
    pub intervals: Vec<CalendarInterval>,
    registered_act_index: usize,
}

impl Calendar {
    pub(crate) fn get_datetime_of(&self, index: usize) -> NaiveDateTime {
        time_grid::datetime_at(self.start_date_time, index)
    }
}

impl Calendar {
    pub(crate) fn unregister(&mut self, interval: &Interval, act_index: usize) {
        for cal_int in &mut self.intervals {
            //occupied interval could be using multiple cal_ints
            let is_overlapping =
                interval.start < cal_int.interval.end && cal_int.interval.start < interval.end;
            if is_overlapping {
                match cal_int.status {
                    Claimable(ref mut claims) => {
                        claims.remove(&act_index);
                    }
                    CalIntStatus::Occupied(_, _) => {
                        //do nothing, there is no claim to unregister
                    }
                }
            }
        }
    }
}

impl Calendar {
    pub(crate) fn register_activities(&mut self, activities: &[Activity]) {
        for (act_index, activity) in activities.iter().enumerate() {
            if act_index < self.registered_act_index {
                continue;
            }

            for interval in &activity.compatible_intervals {
                crate::log_debug!(
                    "Registering activity {} with act_index {}",
                    activity.title,
                    act_index
                );
                self.register(interval, act_index);
            }
            if activities[act_index].status != BestEffort {
                self.registered_act_index += 1;
            }
        }
    }
}

impl Calendar {
    pub(crate) fn reduce_budgets_for(
        &mut self,
        goal_id: &str,
        cal_index_start: usize,
        cal_index_end: usize,
    ) {
        let calendar_start = self.start_date_time;
        for budget in &mut self.budgets {
            budget.reduce_for_(calendar_start, goal_id, cal_index_start, cal_index_end);
        }
    }
}

impl Calendar {
    pub(crate) fn update_compatible_intervals(&self, activity: &mut Activity) {
        //check to see if still ok according to budgets
        if activity.status == Scheduled
            || activity.status == Impossible
            || activity.status == ActivityStatus::Processed
            || activity.compatible_intervals.is_empty()
        {
            //return - no need to update compatible_ints
            return;
        }

        //check if max_week / max_period reached
        for budget in &self.budgets {
            if budget.applies_to(&activity.goal_id) {
                for time_budget in &budget.time_budgets {
                    if time_budget.time_budget_type == Week
                        && time_budget.max_scheduled == time_budget.scheduled
                    {
                        activity.reset_compatible_intervals();
                        return;
                    }
                    if time_budget.time_budget_type == Period
                        && time_budget.max_scheduled == time_budget.scheduled
                    {
                        // Period is full for this week: remove contiguous window
                        // intervals on matching days (not one slot at a time).
                        let Some(period_index) = time_budget.period_index else {
                            continue;
                        };
                        let period = &budget.periods[period_index];
                        let mut day_start = time_budget.calendar_start_index;
                        while day_start < time_budget.calendar_end_index {
                            let day_end = day_start + time_grid::SLOTS_PER_DAY;
                            let weekday = time_grid::weekday_at(self.start_date_time, day_start);
                            if period.on_days.contains(&weekday) {
                                let after = period.window.after_time;
                                let before = period.window.before_time;
                                if after < before {
                                    activity.remove_interval(&Interval {
                                        start: day_start + after,
                                        end: day_start + before,
                                    });
                                } else {
                                    activity.remove_interval(&Interval {
                                        start: day_start + after,
                                        end: day_end,
                                    });
                                    activity.remove_interval(&Interval {
                                        start: day_start,
                                        end: day_start + before,
                                    });
                                }
                            }
                            day_start = day_end;
                        }
                    }
                }
            }
        }

        let mut intervals_that_cant_fit_in_budget: Vec<Interval> = vec![];
        for act_int in &activity.compatible_intervals {
            for hour_index in act_int.start..act_int.end - activity.min_block_size {
                for budget in &self.budgets {
                    if !budget.applies_to(&activity.goal_id) {
                        continue;
                    }
                    for time_budget in &budget.time_budgets {
                        let mut slots_toward_budget = 0usize;
                        for offset in 0..activity.min_block_size {
                            if budget.slot_counts_toward(
                                self.start_date_time,
                                time_budget,
                                hour_index + offset,
                            ) {
                                slots_toward_budget += 1;
                            }
                        }
                        if slots_toward_budget == 0 {
                            continue;
                        }
                        let budget_left = time_budget.max_scheduled - time_budget.scheduled;
                        if slots_toward_budget > budget_left {
                            intervals_that_cant_fit_in_budget.push(Interval {
                                start: hour_index,
                                end: hour_index + 1,
                            });
                        }
                    }
                }
            }
        }
        if !intervals_that_cant_fit_in_budget.is_empty() {
            crate::log_dbg!(&intervals_that_cant_fit_in_budget);
            for interval in intervals_that_cant_fit_in_budget {
                crate::log_debug!(
                    "Removing interval {}-{} from activity{}",
                    interval.start,
                    interval.end,
                    activity.title
                );
                activity.remove_interval(&interval);
            }
        }
    }
}
impl PartialEq<&Interval> for CalendarInterval {
    fn eq(&self, other: &&Interval) -> bool {
        if self.interval.start == other.start && self.interval.end == other.end {
            return true;
        }
        false
    }
}

impl PartialEq<Interval> for CalendarInterval {
    fn eq(&self, other: &Interval) -> bool {
        if self.interval.start == other.start && self.interval.end == other.end {
            return true;
        }
        false
    }
}

impl Calendar {
    pub(crate) fn occupy(
        &mut self,
        interval: &Interval,
        act_index: usize,
        activities: &mut [Activity],
    ) {
        let mut impacted_act_indexes: HashSet<usize> = HashSet::new();
        for cal_interval in &mut self.intervals {
            let is_overlapping = interval.start < cal_interval.interval.end
                && cal_interval.interval.start < interval.end;
            if is_overlapping {
                #[cfg(debug_assertions)]
                assert!(
                    cal_interval.interval.end <= interval.end && cal_interval.interval.start >= interval.start,
                    "Assumption broken: If cal_interval and interval overlap, cal_interval should always be equal or subset of occupied interval."
                );

                if let Claimable(claims) = &mut cal_interval.status {
                    for act_index_in_claim in claims.iter() {
                        activities[*act_index_in_claim].remove_interval(interval); //will reset flex if incompatible intervals are generated
                        if *act_index_in_claim != act_index {
                            impacted_act_indexes.insert(*act_index_in_claim);
                        }
                    }
                }
                cal_interval.status =
                    CalIntStatus::Occupied(act_index, activities[act_index].goal_id.clone());
            } else if let Claimable(claims) = &mut cal_interval.status {
                if claims.contains(&act_index) && activities[act_index].status == Scheduled {
                    claims.remove(&act_index);
                    for act_index_in_claim in claims.iter() {
                        activities[*act_index_in_claim].flex_reset();
                    }
                }
            }
        }
        for act_index_impacted in &impacted_act_indexes {
            for incompatible_int in &activities[*act_index_impacted].incompatible_intervals {
                self.register(incompatible_int, *act_index_impacted);
                self.unregister(incompatible_int, *act_index_impacted);
            }
            activities[*act_index_impacted].incompatible_intervals = vec![];
        }
    }
}

impl PartialEq<Rc<Activity>> for Activity {
    fn eq(&self, other: &Rc<Activity>) -> bool {
        self.goal_id == other.goal_id
    }
}

impl PartialEq for Activity {
    fn eq(&self, other: &Activity) -> bool {
        self.goal_id == other.goal_id
    }
}

impl Calendar {
    pub(crate) fn register(&mut self, interval: &Interval, act_index: usize) {
        // Allocation-light (ADR-0001 / plan P0-2): move each interval instead
        // of deep-cloning the whole vector twice. Non-overlapping intervals
        // (the vast majority) are moved untouched; only the overlapped ones are
        // split into up to three pieces. The produced sequence is identical to
        // the previous clone-based implementation.
        let old = std::mem::take(&mut self.intervals);
        let mut result: Vec<CalendarInterval> = Vec::with_capacity(old.len() + 2);

        for cal_interval in old {
            let is_overlapping = interval.start < cal_interval.interval.end
                && cal_interval.interval.start < interval.end;
            if !is_overlapping {
                result.push(cal_interval);
                continue;
            }

            let cal_start = cal_interval.interval.start;
            let cal_end = cal_interval.interval.end;
            let status = cal_interval.status;
            let empty_begin = interval.start > cal_start;
            let empty_end = interval.end < cal_end;

            // leftover before the overlap (unclaimed)
            if empty_begin {
                result.push(CalendarInterval {
                    interval: Interval {
                        start: cal_start,
                        end: interval.start,
                    },
                    status: status.clone(),
                });
            }

            // the overlapping middle, claimed by this activity
            let overlap_start = if empty_begin {
                interval.start
            } else {
                cal_start
            };
            let overlap_end = if empty_end { interval.end } else { cal_end };
            let mut claimed = CalendarInterval {
                interval: Interval {
                    start: overlap_start,
                    end: overlap_end,
                },
                status: status.clone(),
            };
            claimed.claim_by(act_index);
            result.push(claimed);

            // leftover after the overlap (unclaimed)
            if empty_end {
                result.push(CalendarInterval {
                    interval: Interval {
                        start: interval.end,
                        end: cal_end,
                    },
                    status,
                });
            }
        }

        self.intervals = result;
    }
}

impl PartialEq for CalIntStatus {
    fn eq(&self, other: &Self) -> bool {
        match self {
            Claimable(_) => match other {
                Claimable(_) => return true,
                CalIntStatus::Occupied(_, _) => {}
            },
            CalIntStatus::Occupied(_, goal_id) => match other {
                Claimable(_) => {}
                CalIntStatus::Occupied(_, goal_id2) => {
                    if goal_id.eq(goal_id2) {
                        return true;
                    }
                }
            },
        }
        false
    }
}

impl Calendar {
    pub fn new(start_date_time: NaiveDateTime, end_date_time: NaiveDateTime) -> Self {
        let span_minutes = (end_date_time - start_date_time).num_minutes();
        crate::log_debug!(
            "Calendar span {:?} minutes, from {:?} to {:?}",
            span_minutes,
            start_date_time,
            end_date_time,
        );
        // one buffer day at the front and one at the back
        let span_slots = (span_minutes / time_grid::SLOT_MINUTES) as usize;
        let extended_calendar_slots = 2 * time_grid::BUFFER_SLOTS + span_slots;
        let intervals = vec![CalendarInterval {
            //fragment the first day already so it can be split off easily when printing calendar
            interval: Interval {
                start: 0,
                end: extended_calendar_slots,
            },
            status: Claimable(HashSet::new()),
        }];

        Self {
            start_date_time,
            end_date_time,
            impossible_activities: vec![],
            budgets: vec![],
            intervals,
            registered_act_index: 0,
        }
    }

    pub(crate) fn hours(&self) -> usize {
        self.intervals
            .last()
            .expect("when calling hours there should be at least one interval in calendar.")
            .interval
            .end
    }
    pub fn get_week_day_of(&self, index_to_test: usize) -> Weekday {
        #[cfg(debug_assertions)]
        assert!(index_to_test < self.hours(),
                "Can't request weekday for index {:?} outside of calendar capacity {:?}\nIndexes start at 0.\n",
                index_to_test,
                self.hours()
        );
        time_grid::weekday_at(self.start_date_time, index_to_test)
    }

    pub fn is_participating_in_a_budget(&self, goal_id: &str) -> bool {
        for budget in &self.budgets {
            if budget.applies_to(goal_id) {
                return true;
            }
        }
        false
    }

    pub fn get_index_of(&self, date_time: NaiveDateTime) -> usize {
        if date_time < self.start_date_time.sub(Duration::days(1))
            || date_time > self.end_date_time.add(Duration::days(1))
        {
            // TODO: Fix magic number offset everywhere in code
            panic!(
                "can't request an index more than 1 day outside of calendar bounds for date {:?}\nCalendar starts at {:?} and ends at {:?}", date_time, self.start_date_time, self.end_date_time
            )
        }
        time_grid::index_at(self.start_date_time, date_time)
    }
    pub fn print_new(&mut self, activities: &Vec<Activity>) -> FinalTasks {
        crate::log_debug!("Printing new calendar:");
        crate::log_dbg!(&self);
        crate::log_debug!("Now consolidating intervals and splitting on day boundaries...");
        consolidate_intervals_on_goal_id(&mut self.intervals);
        split_intervals_on_day_boundaries(&mut self.intervals);
        crate::log_dbg!(&self);
        let mut scheduled: Vec<DayTasks> = transform_intervals_to_day_tasks(
            self.intervals.clone(),
            activities,
            self.start_date_time,
        );

        FinalTasks {
            scheduled: scheduled.drain(1..scheduled.len() - 1).collect::<Vec<_>>(), //skip the first leading 24 hours, and last trailing 24 hours
            impossible: self.impossible_activities.clone(),
        }
    }

    pub fn add_budgets_from(
        &mut self,
        goal_map: &BTreeMap<String, Goal>,
        input_budgets: &[Budget],
    ) -> Result<(), SchedulerError> {
        crate::log_debug!("Adding budgets (not activities) to calendar...");

        let mut seen_ids = HashSet::new();
        for budget in input_budgets {
            if !seen_ids.insert(budget.id.clone()) {
                return Err(SchedulerError::DuplicateBudgetId {
                    budget_id: budget.id.clone(),
                });
            }
            if budget.periods.is_empty() {
                return Err(SchedulerError::BudgetMissingPeriods {
                    budget_id: budget.id.clone(),
                });
            }
            if budget.min_per_week > budget.max_per_week {
                return Err(SchedulerError::InvalidBudgetBounds {
                    budget_id: budget.id.clone(),
                    what: "minPerWeek > maxPerWeek",
                });
            }
            for period in &budget.periods {
                if period.min_for_period > period.max_for_period {
                    return Err(SchedulerError::InvalidBudgetBounds {
                        budget_id: budget.id.clone(),
                        what: "minForPeriod > maxForPeriod",
                    });
                }
                if period.on_days.is_empty() {
                    return Err(SchedulerError::InvalidBudgetBounds {
                        budget_id: budget.id.clone(),
                        what: "period.onDays is empty",
                    });
                }
            }
            #[cfg(debug_assertions)]
            if budget.soft_period_mins_exceed_week_max() {
                crate::log_debug!(
                    "Soft check: sum of period mins exceeds maxPerWeek for budget {}",
                    budget.id
                );
            }
        }

        for goal in goal_map.values() {
            if let Some(budget_id) = &goal.budget_id {
                if !input_budgets.iter().any(|b| b.id == *budget_id) {
                    return Err(SchedulerError::UnknownBudgetId {
                        goal_id: goal.id.clone(),
                        budget_id: budget_id.clone(),
                    });
                }
            }
        }

        for budget in input_budgets {
            let mut participating_goals: Vec<String> = goal_map
                .values()
                .filter(|g| g.budget_id.as_deref() == Some(budget.id.as_str()))
                .map(|g| g.id.clone())
                .collect();
            participating_goals.sort();

            self.budgets.push(CalendarBudget {
                budget_id: budget.id.clone(),
                title: budget.title.clone().unwrap_or_else(|| budget.id.clone()),
                participating_goals,
                time_budgets: get_time_budgets_from(self, budget),
                periods: budget.periods.clone(),
            });
        }
        Ok(())
    }

    pub fn log_impossible_activities(&mut self, activities: &Vec<Activity>) {
        for budget in &self.budgets {
            for time_budget in &budget.time_budgets {
                if time_budget.time_budget_type != Period {
                    continue;
                }
                if time_budget.scheduled < time_budget.min_scheduled
                    && time_budget.calendar_end_index < self.get_index_of(self.end_date_time)
                {
                    self.impossible_activities.push(ImpossibleActivity {
                        id: budget.budget_id.clone(),
                        minutes_missing: time_grid::slots_to_minutes(
                            time_budget.min_scheduled - time_budget.scheduled,
                        ),
                        period_start_date_time: self.start_date_time
                            + time_grid::slot_span(time_budget.calendar_start_index as i64),
                        period_end_date_time: Some(
                            self.start_date_time
                                + time_grid::slot_span(time_budget.calendar_end_index as i64),
                        ),
                    });
                }
            }
            // Week under-min: report when scheduled fell short of minPerWeek.
            for time_budget in &budget.time_budgets {
                if time_budget.time_budget_type != Week {
                    continue;
                }
                if time_budget.scheduled < time_budget.min_scheduled
                    && time_budget.calendar_end_index <= self.get_index_of(self.end_date_time)
                {
                    self.impossible_activities.push(ImpossibleActivity {
                        id: budget.budget_id.clone(),
                        minutes_missing: time_grid::slots_to_minutes(
                            time_budget.min_scheduled - time_budget.scheduled,
                        ),
                        period_start_date_time: self.start_date_time
                            + time_grid::slot_span(time_budget.calendar_start_index as i64),
                        period_end_date_time: Some(
                            self.start_date_time
                                + time_grid::slot_span(time_budget.calendar_end_index as i64),
                        ),
                    });
                }
            }
        }
        for activity in activities {
            if activity.status == Impossible
                && activity.deadline.is_some()
                && activity.activity_type != TopUpWeekBudget
                && activity.deadline.unwrap() <= self.end_date_time
            // exempt activities that run over edge of calendar
            {
                self.impossible_activities.push(ImpossibleActivity {
                    id: activity.goal_id.clone(),
                    minutes_missing: time_grid::slots_to_minutes(activity.duration_left),
                    period_start_date_time: activity.start,
                    period_end_date_time: activity.deadline,
                });
            }
        }
    }

    pub(crate) fn get_periods_for(&self, id: &str) -> Option<&[BudgetPeriod]> {
        for budget in &self.budgets {
            if budget.applies_to(id) {
                return Some(&budget.periods);
            }
        }
        None
    }
}

impl Debug for Calendar {
    fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
        writeln!(f)?;
        writeln!(
            f,
            "{:?} impossible activities",
            self.impossible_activities.len()
        )?;
        for budget in &self.budgets {
            writeln!(f, "{:?}", &budget)?;
        }
        for interval in &self.intervals {
            writeln!(f, "{:?}", &interval)?;
        }
        Ok(())
    }
}

fn consolidate_intervals_on_goal_id(cal_ints: &mut Vec<CalendarInterval>) {
    if cal_ints.is_empty() {
        return;
    }

    let mut write_index = 0;
    for read_index in 1..cal_ints.len() {
        if cal_ints[read_index].status == cal_ints[write_index].status {
            // Extend the current interval
            cal_ints[write_index].interval.end = cal_ints[read_index].interval.end;
        } else {
            // Move to the next slot and copy the new interval
            write_index += 1;
            cal_ints[write_index] = cal_ints[read_index].clone();
        }
    }

    // Truncate the vector to remove any unused elements
    cal_ints.truncate(write_index + 1);
}

fn split_intervals_on_day_boundaries(intervals: &mut Vec<CalendarInterval>) {
    let mut i = 0;
    while i < intervals.len() {
        let start = intervals[i].interval.start;
        let end = intervals[i].interval.end;
        let first_multiple = start.div_ceil(time_grid::SLOTS_PER_DAY) * time_grid::SLOTS_PER_DAY;

        if first_multiple < end {
            // Split needed
            let mut new_intervals = Vec::new();

            // First part (if exists)
            if start < first_multiple {
                new_intervals.push(CalendarInterval {
                    interval: Interval {
                        start,
                        end: first_multiple,
                    },
                    status: intervals[i].status.clone(),
                });
            }

            // Middle parts
            let mut current = first_multiple;
            while current + time_grid::SLOTS_PER_DAY < end {
                new_intervals.push(CalendarInterval {
                    interval: Interval {
                        start: current,
                        end: current + time_grid::SLOTS_PER_DAY,
                    },
                    status: intervals[i].status.clone(),
                });
                current += time_grid::SLOTS_PER_DAY;
            }

            // Last part
            new_intervals.push(CalendarInterval {
                interval: Interval {
                    start: current,
                    end,
                },
                status: intervals[i].status.clone(),
            });

            // Replace the original interval with the new splits
            intervals.splice(i..=i, new_intervals.clone());
            i += new_intervals.len();
        } else {
            i += 1;
        }
    }
}

fn transform_intervals_to_day_tasks(
    intervals: Vec<CalendarInterval>,
    activities: &Vec<Activity>,
    calendar_start: NaiveDateTime,
) -> Vec<DayTasks> {
    let mut task_counter: usize = 0;
    let mut day_tasks: Vec<DayTasks> = Vec::new();
    let mut current_day = 0;
    let mut current_day_tasks = Vec::new();

    #[allow(clippy::explicit_counter_loop)]
    for interval in intervals {
        let day_start =
            (interval.interval.start / time_grid::SLOTS_PER_DAY) * time_grid::SLOTS_PER_DAY;
        let day_end = day_start + time_grid::SLOTS_PER_DAY;

        if day_start > current_day {
            // Start a new day
            if !current_day_tasks.is_empty() {
                day_tasks.push(DayTasks {
                    day: (calendar_start
                        + time_grid::slot_span(
                            current_day as i64 - time_grid::BUFFER_SLOTS as i64,
                        ))
                    .into(),
                    tasks: current_day_tasks,
                });
            }
            current_day = day_start;
            current_day_tasks = Vec::new();
        }

        let start = interval.interval.start % time_grid::SLOTS_PER_DAY;
        let end = min(interval.interval.end, day_end) % time_grid::SLOTS_PER_DAY;
        let duration = if end > start {
            end - start
        } else {
            (time_grid::SLOTS_PER_DAY - start) + end
        };
        let task = Task {
            taskid: task_counter,
            goalid: match interval.status {
                CalIntStatus::Occupied(.., ref goal_id) => goal_id.clone(),
                Claimable(_) => "free".to_string(),
            },
            title: match interval.status {
                CalIntStatus::Occupied(act_index, ..) => activities[act_index].title.clone(),
                Claimable(_) => "free".to_string(),
            },
            duration: time_grid::slots_to_minutes(duration),
            start: calendar_start
                + time_grid::slot_span(
                    day_start as i64 + start as i64 - time_grid::BUFFER_SLOTS as i64,
                ),
            deadline: calendar_start
                + time_grid::slot_span(
                    day_start as i64 + start as i64 + duration as i64
                        - time_grid::BUFFER_SLOTS as i64,
                ),
        };

        if day_start > 0 {
            //don't increment for first (leading) day - as that will be removed anyway
            task_counter += 1;
        }

        current_day_tasks.push(task);

        if interval.interval.end > day_end {
            // Interval spans multiple days, create a new interval for the next day
            let remaining_interval = CalendarInterval {
                interval: Interval {
                    start: day_end,
                    end: interval.interval.end,
                },
                status: interval.status,
            };
            // Recursively process the remaining interval
            day_tasks.extend(transform_intervals_to_day_tasks(
                vec![remaining_interval],
                activities,
                calendar_start,
            ));
        }
    }

    // Add the last day's tasks
    if !current_day_tasks.is_empty() {
        day_tasks.push(DayTasks {
            day: (calendar_start + time_grid::slot_span(current_day as i64)).into(),
            tasks: current_day_tasks,
        });
    }

    day_tasks
}