# Path: app/helpers/carts/data_classes.py
# class CartItem:
#     """
#     Data class for items in cart.
#     """
#     item_id: str
#     modify_time: str
#     uid: str
#     document_id: str = None
# 
#     @staticmethod
#     def deserialize(document):
#         """
#         Helper function for parsing a Firestore document to a CartItem object.
# 
#         Parameters:
#            document (DocumentSnapshot): A snapshot of Firestore document.
# 
#         Output:
#            A CartItem object.
#         """
# 
#         data = document.to_dict()
#         if data:
#             return CartItem(
#                 document_id=document.id,
#                 item_id=data.get('item_id'),
#                 modify_time=data.get('modify_time'),
#                 uid=data.get('uid')
#             )
# 
#         return None


from dataclasses import asdict
from google.cloud import firestore
from .data_classes import CartItem
import time
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


"""
A collection of helper functions for cart related operations.
"""





firestore_client = firestore.Client()


def get_cart(uid):
    """
    Helper function for getting all items in a cart.

    Parameters:
       uid (str): The unique ID of an user.

    Output:
       A list of CartItem.
    """

    cart = []
    query_results = firestore_client.collection('carts').where('uid', '==', uid).order_by('modify_time', direction=firestore.Query.DESCENDING).get()
    for result in query_results:

# Path: app/helpers/carts/data_classes.py
# class CartItem:
#     """
#     Data class for items in cart.
#     """
#     item_id: str
#     modify_time: str
#     uid: str
#     document_id: str = None
# 
#     @staticmethod
#     def deserialize(document):
#         """
#         Helper function for parsing a Firestore document to a CartItem object.
# 
#         Parameters:
#            document (DocumentSnapshot): A snapshot of Firestore document.
# 
#         Output:
#            A CartItem object.
#         """
# 
#         data = document.to_dict()
#         if data:
#             return CartItem(
#                 document_id=document.id,
#                 item_id=data.get('item_id'),
#                 modify_time=data.get('modify_time'),
#                 uid=data.get('uid')
#             )
# 
#         return None


# Path: app/helpers/carts/helpers.py
from dataclasses import asdict
from google.cloud import firestore
from .data_classes import CartItem
import time

# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


"""
A collection of helper functions for cart related operations.
"""





firestore_client = firestore.Client()


def get_cart(uid):
    """
    Helper function for getting all items in a cart.

    Parameters:
       uid (str): The unique ID of an user.

    Output:
       A list of CartItem.
    """

    cart = []
    query_results = firestore_client.collection('carts').where('uid', '==', uid).order_by('modify_time', direction=firestore.Query.DESCENDING).get()
    for result in query_results:

        item = CartItem.deserialize(result)