package com.example.data.local import androidx.room.Entity import androidx.room.PrimaryKey import com.example.data.model.AppNotification import com.example.data.model.CustomerUser import com.example.data.model.DeliveryRider import com.example.data.model.FoodItem import com.example.data.model.Order import com.example.data.model.OrderItemSummary import com.example.data.model.OrderStatus import com.example.data.model.RiderStatus import com.example.data.model.FoodReview @Entity(tableName = "food_items") data class FoodItemEntity( @PrimaryKey(autoGenerate = true) val id: Long = 0, val name: String, val category: String, val price: Double, val originalPrice: Double, val description: String, val isVeg: Boolean, val isBestSeller: Boolean, val imageUri: String, val isAvailable: Boolean, val rating: Float, val reviewCount: Int = 12, val preparationTimeMins: Int ) { fun toDomain() = FoodItem( id = id, name = name, category = category, price = price, originalPrice = originalPrice, description = description, isVeg = isVeg, isBestSeller = isBestSeller, imageUri = imageUri, isAvailable = isAvailable, rating = rating, reviewCount = reviewCount, preparationTimeMins = preparationTimeMins ) companion object { fun fromDomain(item: FoodItem) = FoodItemEntity( id = item.id, name = item.name, category = item.category, price = item.price, originalPrice = item.originalPrice, description = item.description, isVeg = item.isVeg, isBestSeller = item.isBestSeller, imageUri = item.imageUri, isAvailable = item.isAvailable, rating = item.rating, reviewCount = item.reviewCount, preparationTimeMins = item.preparationTimeMins ) } } @Entity(tableName = "food_reviews") data class FoodReviewEntity( @PrimaryKey(autoGenerate = true) val id: Long = 0, val foodItemId: Long, val foodName: String, val customerName: String, val customerPhone: String, val rating: Float, val reviewText: String, val timestamp: Long ) { fun toDomain() = FoodReview( id = id, foodItemId = foodItemId, foodName = foodName, customerName = customerName, customerPhone = customerPhone, rating = rating, reviewText = reviewText, timestamp = timestamp ) companion object { fun fromDomain(review: FoodReview) = FoodReviewEntity( id = review.id, foodItemId = review.foodItemId, foodName = review.foodName, customerName = review.customerName, customerPhone = review.customerPhone, rating = review.rating, reviewText = review.reviewText, timestamp = review.timestamp ) } } @Entity(tableName = "orders") data class OrderEntity( @PrimaryKey val orderId: String, val customerName: String, val customerPhone: String, val deliveryAddress: String, val itemsSummary: String, val itemTotal: Double, val deliveryFee: Double, val discount: Double, val grandTotal: Double, val paymentMethod: String, val paymentStatus: String, val upiTransactionId: String, val orderStatus: String, val assignedRiderName: String = "", val assignedRiderPhone: String = "", val assignedRiderVehicle: String = "", val timestamp: Long ) { fun toDomain(): Order { val itemsList = if (itemsSummary.isBlank()) emptyList() else { itemsSummary.split(";;").mapNotNull { part -> val tokens = part.split("|") if (tokens.size >= 4) { OrderItemSummary( foodName = tokens[0], quantity = tokens[1].toIntOrNull() ?: 1, price = tokens[2].toDoubleOrNull() ?: 0.0, isVeg = tokens[3].toBoolean() ) } else null } } val status = try { OrderStatus.valueOf(orderStatus) } catch (_: Exception) { OrderStatus.PLACED } return Order( orderId = orderId, customerName = customerName, customerPhone = customerPhone, deliveryAddress = deliveryAddress, items = itemsList, itemTotal = itemTotal, deliveryFee = deliveryFee, discount = discount, grandTotal = grandTotal, paymentMethod = paymentMethod, paymentStatus = paymentStatus, upiTransactionId = upiTransactionId, orderStatus = status, assignedRiderName = assignedRiderName, assignedRiderPhone = assignedRiderPhone, assignedRiderVehicle = assignedRiderVehicle, timestamp = timestamp ) } companion object { fun fromDomain(order: Order): OrderEntity { val summary = order.items.joinToString(";;") { "${it.foodName}|${it.quantity}|${it.price}|${it.isVeg}" } return OrderEntity( orderId = order.orderId, customerName = order.customerName, customerPhone = order.customerPhone, deliveryAddress = order.deliveryAddress, itemsSummary = summary, itemTotal = order.itemTotal, deliveryFee = order.deliveryFee, discount = order.discount, grandTotal = order.grandTotal, paymentMethod = order.paymentMethod, paymentStatus = order.paymentStatus, upiTransactionId = order.upiTransactionId, orderStatus = order.orderStatus.name, assignedRiderName = order.assignedRiderName, assignedRiderPhone = order.assignedRiderPhone, assignedRiderVehicle = order.assignedRiderVehicle, timestamp = order.timestamp ) } } } @Entity(tableName = "notifications") data class NotificationEntity( @PrimaryKey(autoGenerate = true) val id: Long = 0, val title: String, val message: String, val timestamp: Long, val isRead: Boolean, val type: String ) { fun toDomain() = AppNotification( id = id, title = title, message = message, timestamp = timestamp, isRead = isRead, type = type ) companion object { fun fromDomain(notif: AppNotification) = NotificationEntity( id = notif.id, title = notif.title, message = notif.message, timestamp = notif.timestamp, isRead = notif.isRead, type = notif.type ) } } @Entity(tableName = "delivery_riders") data class DeliveryRiderEntity( @PrimaryKey(autoGenerate = true) val id: Long = 0, val name: String, val phone: String, val vehicleNumber: String, val status: String, val totalDeliveries: Int, val rating: Float, val currentOrderId: String ) { fun toDomain() = DeliveryRider( id = id, name = name, phone = phone, vehicleNumber = vehicleNumber, status = try { RiderStatus.valueOf(status) } catch (_: Exception) { RiderStatus.AVAILABLE }, totalDeliveries = totalDeliveries, rating = rating, currentOrderId = currentOrderId ) companion object { fun fromDomain(rider: DeliveryRider) = DeliveryRiderEntity( id = rider.id, name = rider.name, phone = rider.phone, vehicleNumber = rider.vehicleNumber, status = rider.status.name, totalDeliveries = rider.totalDeliveries, rating = rider.rating, currentOrderId = rider.currentOrderId ) } } @Entity(tableName = "users") data class UserEntity( @PrimaryKey val phone: String, // 10-digit normalized phone number val name: String, val email: String, val alternatePhone: String = "", val houseNo: String = "", val streetArea: String = "", val landmark: String = "", val zone: String = "Kumarghat", val pincode: String = "799290", val fullAddress: String, val dietaryPreference: String = "Both", val dateOfBirth: String = "", val deliveryNote: String = "", val walletBalance: Double = 0.0, val profileImageUri: String = "", val password: String = "", val createdAt: Long = System.currentTimeMillis() ) { fun toDomain(isLoggedIn: Boolean = true) = CustomerUser( id = phone, name = name, phone = if (phone.startsWith("+91")) phone else "+91 $phone", email = email, alternatePhone = alternatePhone, houseNo = houseNo, streetArea = streetArea, landmark = landmark, zone = zone, pincode = pincode, address = fullAddress, dietaryPreference = dietaryPreference, dateOfBirth = dateOfBirth, deliveryNote = deliveryNote, walletBalance = walletBalance, profileImageUri = profileImageUri, isLoggedIn = isLoggedIn, password = password ) companion object { fun fromDomain(user: CustomerUser): UserEntity { val digits = user.phone.filter { it.isDigit() }.let { if (it.length > 10) it.takeLast(10) else it } return UserEntity( phone = digits, name = user.name, email = user.email, alternatePhone = user.alternatePhone, houseNo = user.houseNo, streetArea = user.streetArea, landmark = user.landmark, zone = user.zone, pincode = user.pincode, fullAddress = user.address, dietaryPreference = user.dietaryPreference, dateOfBirth = user.dateOfBirth, deliveryNote = user.deliveryNote, walletBalance = user.walletBalance, profileImageUri = user.profileImageUri, password = user.password ) } } } @Entity(tableName = "cart_items") data class CartItemEntity( @PrimaryKey(autoGenerate = true) val id: Long = 0, val foodId: Long, val name: String, val price: Double, val quantity: Int, val isVeg: Boolean, val imageUri: String )