# Path: app/helpers/orders/data_classes.py
# class Order:
#     """
#     Data class for orders.
#     """
#     amount: float
#     shipping: Shipping
#     status: str
#     items: List[str]
#     id: str = None
# 
# 
#     @staticmethod
#     def deserialize(document):
#         """
#         Helper function for parsing a Firestore document to an Order object.
# 
#         Parameters:
#           document (DocumentSnapshot): A snapshot of Firestore document.
# 
#         Output:
#           An Order object.
#         """
#         data = document.to_dict()
#         if data:
#             return Order(
#                 id=document.id,
#                 amount=data.get('amount'),
#                 shipping=Shipping.deserialize(data.get('shipping')),
#                 status=data.get('status'),
#                 items=data.get('items')
#             )
# 
#         return None


from dataclasses import asdict
from google.cloud import firestore
from .data_classes import Order
import uuid
firestore = firestore.Client()

def add_order(order):
    """
    Helper function for adding an order.

    Parameters:
       order (Order): An Order object.

    Output:
       The ID of the order.
    """

    order_id = uuid.uuid4().hex
    firestore.collection('orders').document(order_id).set(asdict(order))
    return order_id


def get_order(order_id):
    """
    Helper function for getting an order.

    Parameters:
       order_id (str): The ID of the order.

    Output:
       An Order object.
    """

    order_data = firestore.collection('orders').document(order_id).get()

# Path: app/helpers/orders/data_classes.py
# class Order:
#     """
#     Data class for orders.
#     """
#     amount: float
#     shipping: Shipping
#     status: str
#     items: List[str]
#     id: str = None
# 
# 
#     @staticmethod
#     def deserialize(document):
#         """
#         Helper function for parsing a Firestore document to an Order object.
# 
#         Parameters:
#           document (DocumentSnapshot): A snapshot of Firestore document.
# 
#         Output:
#           An Order object.
#         """
#         data = document.to_dict()
#         if data:
#             return Order(
#                 id=document.id,
#                 amount=data.get('amount'),
#                 shipping=Shipping.deserialize(data.get('shipping')),
#                 status=data.get('status'),
#                 items=data.get('items')
#             )
# 
#         return None


# Path: app/helpers/orders/helpers.py
from dataclasses import asdict
from google.cloud import firestore
from .data_classes import Order
import uuid

firestore = firestore.Client()

def add_order(order):
    """
    Helper function for adding an order.

    Parameters:
       order (Order): An Order object.

    Output:
       The ID of the order.
    """

    order_id = uuid.uuid4().hex
    firestore.collection('orders').document(order_id).set(asdict(order))
    return order_id


def get_order(order_id):
    """
    Helper function for getting an order.

    Parameters:
       order_id (str): The ID of the order.

    Output:
       An Order object.
    """

    order_data = firestore.collection('orders').document(order_id).get()

    return Order.deserialize(order_data)