func (r *ChoreRepository) UndoChoreAction(c context.Context, choreID int, historyID int, circleID int, previousAssignedTo *int, previousDueDate *time.Time) error {
	nextVersion, err := r.nextSyncVersion(c, circleID)
	if err != nil {
		return err
	}
	return r.db.WithContext(c).Transaction(func(tx *gorm.DB) error {
		// Get the history entry to undo
		var historyToUndo chModel.ChoreHistory
		if err := tx.First(&historyToUndo, historyID).Error; err != nil {
			return err
		}

		// Prepare chore updates
		choreUpdates := map[string]interface{}{
			"status":       chModel.ChoreStatusNoStatus,
			"sync_version": nextVersion,
		}

		// Build list of fields to explicitly update (including nullable fields)
		selectFields := []string{"status", "sync_version"}

		// Restore previous assignee
		if previousAssignedTo != nil {
			choreUpdates["assigned_to"] = *previousAssignedTo
		} else {
			choreUpdates["assigned_to"] = nil
		}
		selectFields = append(selectFields, "assigned_to")

		// Restore previous due date; always reactivate since the chore was
		// active before completion (e.g. "once" chores have no due date but
		// must be unarchived on undo).
		if previousDueDate != nil {
			choreUpdates["next_due_date"] = *previousDueDate
		} else {
			choreUpdates["next_due_date"] = nil
		}
		choreUpdates["is_active"] = true
		selectFields = append(selectFields, "next_due_date", "is_active")

		// Update the chore with explicit field selection to handle nil values
		if err := tx.Model(&chModel.Chore{}).Where("id = ?", choreID).Select(selectFields).Updates(choreUpdates).Error; err != nil {
			return err
		}

		// Remove points if they were added during completion/approval
		if historyToUndo.Points != nil && *historyToUndo.Points > 0 &&
			(historyToUndo.Status == chModel.ChoreHistoryStatusCompleted) {
			// Get the chore to find circle ID
			var chore chModel.Chore
			if err := tx.Select("circle_id").First(&chore, choreID).Error; err != nil {
				return err
			}
			// Subtract points from user
			if err := tx.Model(&cModel.UserCircle{}).
				Where("user_id = ? AND circle_id = ?", historyToUndo.CompletedBy, chore.CircleID).
				Update("points", gorm.Expr("points - ?", *historyToUndo.Points)).Error; err != nil {
				return err
			}
		}

		// Delete the history entry being undone
		if err := tx.Delete(&chModel.ChoreHistory{}, historyID).Error; err != nil {
			return err
		}

		// Delete associated time sessions for this history entry
		if err := tx.Where("chore_history_id = ?", historyID).Delete(&chModel.TimeSession{}).Error; err != nil {
			return err
		}

		return nil
	})
}
