import 'package:uuid/uuid.dart'; import '../services/database_sync_service.dart'; class SupplementIntake { final int? id; final int supplementId; final DateTime takenAt; final double dosageTaken; // Total dosage amount taken final double unitsTaken; // Number of units taken (can be fractional) final String? notes; // Sync metadata final String syncId; final DateTime lastModified; final RecordSyncStatus syncStatus; final bool isDeleted; SupplementIntake({ this.id, required this.supplementId, required this.takenAt, required this.dosageTaken, required this.unitsTaken, this.notes, String? syncId, DateTime? lastModified, this.syncStatus = RecordSyncStatus.pending, this.isDeleted = false, }) : syncId = syncId ?? const Uuid().v4(), lastModified = lastModified ?? DateTime.now(); Map toMap() { return { 'id': id, 'supplementId': supplementId, 'takenAt': takenAt.toIso8601String(), 'dosageTaken': dosageTaken, 'unitsTaken': unitsTaken, 'notes': notes, 'syncId': syncId, 'lastModified': lastModified.toIso8601String(), 'syncStatus': syncStatus.name, 'isDeleted': isDeleted ? 1 : 0, }; } factory SupplementIntake.fromMap(Map map) { return SupplementIntake( id: map['id'], supplementId: map['supplementId'], takenAt: DateTime.parse(map['takenAt']), dosageTaken: map['dosageTaken'], unitsTaken: (map['unitsTaken'] ?? 1).toDouble(), // Default for backwards compatibility notes: map['notes'], syncId: map['syncId'] ?? const Uuid().v4(), lastModified: map['lastModified'] != null ? DateTime.parse(map['lastModified']) : DateTime.now(), syncStatus: map['syncStatus'] != null ? RecordSyncStatus.values.firstWhere( (e) => e.name == map['syncStatus'], orElse: () => RecordSyncStatus.pending, ) : RecordSyncStatus.pending, isDeleted: (map['isDeleted'] ?? 0) == 1, ); } SupplementIntake copyWith({ int? id, int? supplementId, DateTime? takenAt, double? dosageTaken, double? unitsTaken, String? notes, String? syncId, DateTime? lastModified, RecordSyncStatus? syncStatus, bool? isDeleted, }) { return SupplementIntake( id: id ?? this.id, supplementId: supplementId ?? this.supplementId, takenAt: takenAt ?? this.takenAt, dosageTaken: dosageTaken ?? this.dosageTaken, unitsTaken: unitsTaken ?? this.unitsTaken, notes: notes ?? this.notes, syncId: syncId ?? this.syncId, lastModified: lastModified ?? this.lastModified, syncStatus: syncStatus ?? this.syncStatus, isDeleted: isDeleted ?? this.isDeleted, ); } /// Create a new intake marked as synced SupplementIntake markAsSynced() { return copyWith( syncStatus: RecordSyncStatus.synced, ); } /// Create a new intake marked for deletion SupplementIntake markAsDeleted() { return copyWith( isDeleted: true, lastModified: DateTime.now(), syncStatus: RecordSyncStatus.modified, ); } }