Files
satiea/app/src/main/java/com/example/data/model/MealPlanModels.kt
T

360 lines
12 KiB
Kotlin

package com.example.data.model
import com.example.domain.IngredientMatcher
import java.time.LocalDate
import java.time.format.DateTimeFormatter
import java.util.Locale
import java.util.UUID
enum class ExpirationStatus {
EXPIRED,
URGENT,
SOON,
FRESH,
NONE
}
object ExpirationDefaults {
/**
* Suggests a realistic shelf-life in days based on category and storage location,
* or null for long-shelf-life pantry/frozen items where no short-term alert is needed.
*/
fun suggestedShelfLifeDays(category: FoodCategory, location: StorageLocation): Long? {
return when (location) {
StorageLocation.FRIDGE -> when (category) {
FoodCategory.PROTEIN -> 3L
FoodCategory.VEGETABLE -> 5L
FoodCategory.FRUIT -> 5L
FoodCategory.DAIRY -> 7L
FoodCategory.CHEESE -> 14L
FoodCategory.BREAD -> 4L
FoodCategory.STARCH -> 4L
FoodCategory.OTHER -> 5L
FoodCategory.FAT_SEASONING,
FoodCategory.NUTS,
FoodCategory.PLEASURE -> 14L
}
StorageLocation.PANTRY -> when (category) {
FoodCategory.BREAD -> 3L
FoodCategory.FRUIT -> 5L
FoodCategory.VEGETABLE -> 7L
else -> null
}
StorageLocation.FREEZER -> null
}
}
fun suggestedExpirationEpochDay(
category: FoodCategory,
location: StorageLocation,
today: LocalDate = LocalDate.now()
): Long? {
val days = suggestedShelfLifeDays(category, location) ?: return null
return today.plusDays(days).toEpochDay()
}
}
enum class StorageLocation(val displayName: String) {
FRIDGE("Frigo"),
FREEZER("Congélateur"),
PANTRY("Placard");
companion object {
fun fromString(raw: String?): StorageLocation {
if (raw.isNullOrBlank()) return PANTRY
val trimmed = raw.trim()
return entries.firstOrNull {
it.name.equals(trimmed, ignoreCase = true) ||
it.displayName.equals(trimmed, ignoreCase = true)
} ?: PANTRY
}
fun defaultForCategory(category: FoodCategory): StorageLocation {
return when (category) {
FoodCategory.PROTEIN,
FoodCategory.VEGETABLE,
FoodCategory.FRUIT,
FoodCategory.CHEESE,
FoodCategory.DAIRY -> FRIDGE
FoodCategory.STARCH,
FoodCategory.BREAD,
FoodCategory.NUTS,
FoodCategory.FAT_SEASONING,
FoodCategory.PLEASURE,
FoodCategory.OTHER -> PANTRY
}
}
}
}
data class PlannedMealEntry(
val id: String = "",
val dateEpochDay: Long,
val mealType: MealType,
val recipeId: String? = null,
val customTitle: String = "",
val servings: Double = 1.0,
val itemsSerialized: List<String> = emptyList(),
val isConsumed: Boolean = false,
val updatedAt: Long = System.currentTimeMillis()
) {
fun parsedItems(): List<MealFoodItem> {
return itemsSerialized.mapNotNull { MealFoodItem.deserialize(it) }
}
fun resolvedTitle(recipesById: Map<String, RecipeEntry>): String {
val recipeTitle = recipeId?.let { recipesById[it]?.title }
return when {
!recipeTitle.isNullOrBlank() -> recipeTitle
customTitle.isNotBlank() -> customTitle
itemsSerialized.isNotEmpty() -> parsedItems().joinToString(", ") { it.name }
else -> mealType.displayName
}
}
fun resolvedFoodItems(recipesById: Map<String, RecipeEntry>): List<MealFoodItem> {
val recipe = recipeId?.let { recipesById[it] }
return if (recipe != null) {
recipe.scaledIngredients(servings.coerceAtLeast(0.5))
} else {
parsedItems()
}
}
}
data class InventoryItem(
val id: String = "",
val name: String,
val normalizedName: String = IngredientMatcher.normalize(name),
val quantity: Double,
val unit: FoodUnit = FoodUnit.G,
val category: FoodCategory = FoodCategory.OTHER,
val location: StorageLocation = StorageLocation.PANTRY,
val expirationDateEpochDay: Long? = null,
val updatedAt: Long = System.currentTimeMillis()
) {
fun formatQuantity(): String = IngredientMatcher.formatQuantity(quantity, unit)
fun daysUntilExpiration(today: LocalDate = LocalDate.now()): Long? {
val exp = expirationDateEpochDay ?: return null
return exp - today.toEpochDay()
}
fun expirationStatus(
alertThresholdDays: Int = 3,
today: LocalDate = LocalDate.now()
): ExpirationStatus {
val days = daysUntilExpiration(today) ?: return ExpirationStatus.NONE
val safeThreshold = alertThresholdDays.coerceIn(1, 7).toLong()
return when {
days < 0L -> ExpirationStatus.EXPIRED
days <= 1L -> ExpirationStatus.URGENT
days <= safeThreshold -> ExpirationStatus.SOON
else -> ExpirationStatus.FRESH
}
}
fun formatExpirationBadge(today: LocalDate = LocalDate.now()): String? {
val exp = expirationDateEpochDay ?: return null
val days = exp - today.toEpochDay()
return when {
days < -1L -> "Périmé (${-days} j)"
days == -1L -> "Périmé hier"
days == 0L -> "Aujourd'hui"
days == 1L -> "Demain"
days <= 7L -> "Dans $days j"
else -> {
val fmt = DateTimeFormatter.ofPattern("d MMM", Locale.FRENCH)
LocalDate.ofEpochDay(exp).format(fmt)
}
}
}
}
data class ManualShoppingItem(
val id: String = "",
val name: String,
val quantity: Double? = null,
val unit: FoodUnit = FoodUnit.PORTION,
val category: FoodCategory = FoodCategory.OTHER,
val isChecked: Boolean = false,
val updatedAt: Long = System.currentTimeMillis()
) {
fun formatQuantity(): String? = quantity?.takeIf { it > 0.0 }?.let {
IngredientMatcher.formatQuantity(it, unit)
}
}
enum class ShoppingItemSource {
FROM_PLAN,
MANUAL,
BOTH
}
data class ShoppingListItem(
val key: String,
val name: String,
val normalizedName: String,
val missingQuantity: Double?,
val requiredQuantity: Double?,
val inStockQuantity: Double,
val unit: FoodUnit,
val category: FoodCategory,
val source: ShoppingItemSource,
val manualItemId: String? = null,
val isChecked: Boolean = false,
val relatedMealsCount: Int = 0
) {
fun formatMissingQuantity(): String? = missingQuantity?.takeIf { it > 0.0 }?.let {
IngredientMatcher.formatQuantity(it, unit)
}
fun formatInStockQuantity(): String? = inStockQuantity.takeIf { it > 0.0 }?.let {
IngredientMatcher.formatQuantity(it, unit)
}
}
data class ScannedReceiptItem(
val id: String = UUID.randomUUID().toString().take(8),
val rawLabel: String,
val name: String,
val quantity: Double,
val unit: FoodUnit = FoodUnit.PORTION,
val category: FoodCategory = FoodCategory.OTHER,
val location: StorageLocation = StorageLocation.PANTRY,
val expirationDateEpochDay: Long? = null,
val selected: Boolean = true
)
data class ScannedBarcodeItem(
val id: String = "bc_${UUID.randomUUID().toString().take(8)}",
val barcode: String,
val name: String,
val brand: String? = null,
val unitQuantity: Double = 1.0,
val packCount: Int = 1,
val unit: FoodUnit = FoodUnit.PORTION,
val category: FoodCategory = FoodCategory.OTHER,
val location: StorageLocation = StorageLocation.PANTRY,
val expirationDateEpochDay: Long? = null,
val imageUrl: String? = null,
val isFoundInCatalog: Boolean = true,
val isLoading: Boolean = false
) {
val totalQuantity: Double
get() = unitQuantity * packCount.coerceAtLeast(1)
fun formatTotalQuantity(): String = IngredientMatcher.formatQuantity(totalQuantity, unit)
fun formatPackageQuantity(): String = IngredientMatcher.formatQuantity(unitQuantity, unit)
}
data class StockDeductionProposal(
val inventoryItem: InventoryItem,
val deductAmount: Double,
val remainingAfter: Double,
val selected: Boolean = true
)
sealed interface ReceiptScanUiState {
data object Idle : ReceiptScanUiState
data object Scanning : ReceiptScanUiState
data class Preview(val items: List<ScannedReceiptItem>) : ReceiptScanUiState
data class Error(val message: String) : ReceiptScanUiState
}
enum class PredictionMealScope(
val displayName: String,
val mealTypes: List<MealType>
) {
MAIN_MEALS("Déjeuner & Dîner", listOf(MealType.LUNCH, MealType.DINNER)),
ALL_MEALS("Tous les moments", listOf(MealType.BREAKFAST, MealType.LUNCH, MealType.SNACK, MealType.DINNER))
}
data class PredictedMealProposal(
val id: String = "pred_${UUID.randomUUID().toString().take(8)}",
val dateEpochDay: Long,
val mealType: MealType,
val recipeId: String? = null,
val title: String,
val servings: Double = 1.0,
val items: List<MealFoodItem> = emptyList(),
val reason: String = "",
val selected: Boolean = true
) {
fun toPlannedMealEntry(): PlannedMealEntry {
return PlannedMealEntry(
id = "",
dateEpochDay = dateEpochDay,
mealType = mealType,
recipeId = recipeId,
customTitle = title.trim(),
servings = servings.coerceAtLeast(0.5),
itemsSerialized = if (recipeId.isNullOrBlank()) {
items.map { it.serialize() }
} else {
emptyList()
},
isConsumed = false
)
}
}
sealed interface WeekPredictionUiState {
data object Idle : WeekPredictionUiState
data object Loading : WeekPredictionUiState
data class Preview(
val proposals: List<PredictedMealProposal>,
val scope: PredictionMealScope,
val defaultServings: Double
) : WeekPredictionUiState
data class Error(
val message: String,
val scope: PredictionMealScope,
val defaultServings: Double
) : WeekPredictionUiState
}
data class MenuAdaptationProposal(
val id: String = "adapt_${UUID.randomUUID().toString().take(8)}",
val dateEpochDay: Long,
val mealType: MealType,
val existingPlannedMealId: String? = null,
val existingMealTitle: String? = null,
val recipeId: String? = null,
val title: String,
val servings: Double = 1.0,
val items: List<MealFoodItem> = emptyList(),
val usedExpiringNames: List<String> = emptyList(),
val reason: String = "",
val selected: Boolean = true
) {
fun toPlannedMealEntry(): PlannedMealEntry {
return PlannedMealEntry(
id = existingPlannedMealId.orEmpty(),
dateEpochDay = dateEpochDay,
mealType = mealType,
recipeId = recipeId,
customTitle = title.trim(),
servings = servings.coerceAtLeast(0.5),
itemsSerialized = if (recipeId.isNullOrBlank()) {
items.map { it.serialize() }
} else {
emptyList()
},
isConsumed = false
)
}
}
sealed interface MenuAdaptationUiState {
data object Idle : MenuAdaptationUiState
data object Loading : MenuAdaptationUiState
data class Preview(
val proposals: List<MenuAdaptationProposal>,
val expiringItems: List<InventoryItem>
) : MenuAdaptationUiState
data class Error(val message: String) : MenuAdaptationUiState
}