    pub async fn update(&self, id: &ObjectId, updates: UpdateMedicationRequest) -> Result<Option<Medication>, Box<dyn std::error::Error>> {
        let mut update_doc = doc! {};
        
        if let Some(name) = updates.name {
            update_doc.insert("medicationData.name", name);
        }
        if let Some(dosage) = updates.dosage {
            update_doc.insert("medicationData.dosage", dosage);
        }
        if let Some(frequency) = updates.frequency {
            update_doc.insert("medicationData.frequency", frequency);
        }
        if let Some(route) = updates.route {
            update_doc.insert("medicationData.route", route);
        }
        if let Some(reason) = updates.reason {
            update_doc.insert("medicationData.reason", reason);
        }
        if let Some(instructions) = updates.instructions {
            update_doc.insert("medicationData.instructions", instructions);
        }
        if let Some(side_effects) = updates.side_effects {
            update_doc.insert("medicationData.sideEffects", side_effects);
        }
        if let Some(prescribed_by) = updates.prescribed_by {
            update_doc.insert("medicationData.prescribedBy", prescribed_by);
        }
        if let Some(prescribed_date) = updates.prescribed_date {
            update_doc.insert("medicationData.prescribedDate", prescribed_date);
        }
        if let Some(start_date) = updates.start_date {
            update_doc.insert("medicationData.startDate", start_date);
        }
        if let Some(end_date) = updates.end_date {
            update_doc.insert("medicationData.endDate", end_date);
        }
        if let Some(notes) = updates.notes {
            update_doc.insert("medicationData.notes", notes);
        }
        if let Some(tags) = updates.tags {
            update_doc.insert("medicationData.tags", tags);
        }
        if let Some(reminder_times) = updates.reminder_times {
            update_doc.insert("reminderTimes", reminder_times);
        }
        if let Some(pill_identification) = updates.pill_identification {
            // Convert PillIdentification to Bson using to_bson
            let pill_bson = mongodb::bson::to_bson(&pill_identification)?;
            update_doc.insert("pillIdentification", pill_bson);
        }
        
        update_doc.insert("updatedAt", mongodb::bson::DateTime::now());
        
        let filter = doc! { "_id": id };
        let medication = self.collection.find_one_and_update(filter, doc! { "$set": update_doc }, None).await?;
        Ok(medication)
    }
