import 'dart:convert'; class DayPlan { final List colazione; final List pranzo; final List cena; DayPlan({ List? colazione, List? pranzo, List? cena, }) : colazione = colazione ?? [], pranzo = pranzo ?? [], cena = cena ?? []; DayPlan copyWith({ List? colazione, List? pranzo, List? cena, }) => DayPlan( colazione: colazione ?? List.of(this.colazione), pranzo: pranzo ?? List.of(this.pranzo), cena: cena ?? List.of(this.cena), ); List slot(String name) => switch (name) { 'colazione' => colazione, 'pranzo' => pranzo, 'cena' => cena, _ => const [], }; Map toJson() => { 'colazione': colazione, 'pranzo': pranzo, 'cena': cena, }; factory DayPlan.fromJson(Map json) => DayPlan( colazione: List.from(json['colazione'] ?? []), pranzo: List.from(json['pranzo'] ?? []), cena: List.from(json['cena'] ?? []), ); } class MealPlan { final Map days; const MealPlan(this.days); factory MealPlan.empty(List dayIds) => MealPlan({for (final d in dayIds) d: DayPlan()}); MealPlan withUpdatedDay(String dayId, DayPlan updated) => MealPlan({...days, dayId: updated}); bool get hasAnyMeal => days.values.any( (d) => d.colazione.isNotEmpty || d.pranzo.isNotEmpty || d.cena.isNotEmpty, ); List get allItems => days.values .expand((d) => [...d.colazione, ...d.pranzo, ...d.cena]) .toList(); Map toJson() => {for (final e in days.entries) e.key: e.value.toJson()}; factory MealPlan.fromJson(Map json) => MealPlan({ for (final e in json.entries) e.key: DayPlan.fromJson(e.value as Map), }); String toJsonString() => jsonEncode(toJson()); factory MealPlan.fromJsonString(String s) => MealPlan.fromJson(jsonDecode(s) as Map); }