Skip to main content

meal_planner/
application.rs

1//! Application context for AI-assisted meal planning.
2use crate::{
3    FamilyMember, MealArrangement, MealPlannerError, MealPlannerStore, MealType, Recipe,
4    SchoolMenuEntry, WeeklyPlan,
5};
6use chrono::{Datelike, Days, NaiveDate, Weekday};
7use serde::Serialize;
8
9#[derive(Debug, Clone, Serialize)]
10pub struct PlanningParticipation {
11    pub date: NaiveDate,
12    pub meal_type: MealType,
13    pub member_id: i64,
14    pub member_name: String,
15    pub arrangement: Option<MealArrangement>,
16    pub source: Option<String>,
17}
18#[derive(Debug, Clone, Serialize)]
19pub struct PlanningContext {
20    pub week_start: NaiveDate,
21    pub week_end: NaiveDate,
22    pub members: Vec<FamilyMember>,
23    pub participation: Vec<PlanningParticipation>,
24    pub recipes: Vec<Recipe>,
25    pub school_menus: Vec<SchoolMenuEntry>,
26    pub current_plan: Option<WeeklyPlan>,
27}
28#[derive(Clone)]
29pub struct MealPlannerApplication {
30    store: MealPlannerStore,
31}
32impl MealPlannerApplication {
33    pub fn new(store: MealPlannerStore) -> Self {
34        Self { store }
35    }
36    pub fn planning_context(
37        &self,
38        owner_id: &str,
39        families: &[String],
40        week_start: NaiveDate,
41    ) -> Result<PlanningContext, MealPlannerError> {
42        if week_start.weekday() != Weekday::Mon {
43            return Err(MealPlannerError::InvalidInput(
44                "week_start must be a Monday".into(),
45            ));
46        }
47        let family_id = families.first().map(String::as_str).unwrap_or("legacy");
48        let week_end = week_start.checked_add_days(Days::new(6)).ok_or_else(|| {
49            MealPlannerError::InvalidInput("week_start is outside the supported date range".into())
50        })?;
51        let participation = self
52            .store
53            .resolve_meal_week_for_family(family_id, week_start)?
54            .into_iter()
55            .map(|entry| {
56                let (arrangement, source) = match entry.status {
57                    crate::ParticipationStatus::Unconfigured => (None, None),
58                    crate::ParticipationStatus::Configured {
59                        arrangement,
60                        source,
61                    } => {
62                        let source = match source {
63                            crate::ParticipationSource::RecurringRule => "recurring_rule",
64                            crate::ParticipationSource::DateException => "date_exception",
65                        };
66                        (Some(arrangement), Some(source.to_owned()))
67                    }
68                };
69                PlanningParticipation {
70                    date: entry.date,
71                    meal_type: entry.meal_type,
72                    member_id: entry.member.id,
73                    member_name: entry.member.name,
74                    arrangement,
75                    source,
76                }
77            })
78            .collect();
79        Ok(PlanningContext {
80            week_start,
81            week_end,
82            members: self.store.list_family_members_for_family(family_id)?,
83            participation,
84            recipes: self.store.list_recipes(owner_id, families, None)?,
85            school_menus: self
86                .store
87                .list_school_menus_for_family(family_id, week_start, week_end)?,
88            current_plan: self
89                .store
90                .get_weekly_plan_for_family(family_id, week_start)?,
91        })
92    }
93}