def get_customer(customer_id):
    res = spcall('get_single_customer', (customer_id))

    if len(res) == 0:
        return jsonify({'status': 'ok', 'message': 'No entries found', 'entries': [], 'count': '0'})
    else:
        recs = []
        for r in res:
            recs.append({
                "id": customer_id,
                "first_name": r[0],
                "last_name": r[1],
                "address": r[2],
                "city": r[3],
                "state": r[4],
                "postal_code": r[5],
                "country": r[6],
                "phone": r[7],
                "email": r[8],
                "user_id": r[9],
                "billing_address": r[10],
                "shipping_address": r[11],
                "date_created": r[12]
            })
            return jsonify({'status': 'ok', 'entries': recs, 'count': len(recs)})
def get_all_customers():
    res = spcall('get_all_customers', ())

    if 'Error' in str(res[0][0]):
        return jsonify({'status': 'error', 'message': res[0][0]})

    recs = []
    for r in res:
        recs.append({
            "id": r[0],
            "first_name": r[1],
            "last_name": r[2],
            "address": r[3],
            "city": r[4],
            "state": r[5],
            "postal_code": r[6],
            "country": r[7],
            "phone": r[8],
            "email": r[9],
            "user_id": r[10],
            "billing_address": r[11],
            "shipping_address": r[12],
            "date_created": r[13]
        })
        return jsonify({'status': 'ok', 'entries': recs, 'count': len(recs)})
def delete_product_category(id):
    res = spcall("delete_product_category", (id,), True)
    if 'Error' in res[0][0]:
        return jsonify({'status': 'error', 'message': res[0][0]})

    return jsonify({'status': 'ok', 'message': res[0][0]})

    """ Todo: This route should be protected """
def get_wishlist_items():
    res = spcall('get_wishlist_items', ())

    if 'Error' in str(res[0][0]):
        return jsonify({'status': 'error', 'message': res[0][0]})

    recs = []
    for r in res:
        recs.append({"wishlist_item_id": r[0], "wishlist_id": r[1], "item_id": r[2], "time_stamp": r[3]})
    return jsonify({'status': 'ok', 'entries': recs, 'count': len(recs)})
def get_wishlist_item(wishlist_item_id):
    res = spcall('get_wishlist_item', (wishlist_item_id,))

    entries = []
    if len(res) == 0:
        return jsonify({"status": "ok", "message": "No entries found", "entries": [], "count": "0"})
    else:
        row = res[0]
        entries.append({"wishlist_item_id": r[0], "wishlist_id": r[1], "item_id": r[2], "time_stamp": r[3]})
        return jsonify({"status": "ok", "message": "ok", "entries": entries, "count": len(entries)})
def new_wishlist():
    data = json.loads(request.data)

    response = spcall('new_wishlist', (
        data['wishlist_id'],data['wishlist_name']), True)

    if 'Error' in response[0][0]:
        return jsonify({'status': 'error', 'message': response[0][0]})

    return jsonify({'status': 'ok', 'status_code': '200', 'message': response[0][0]}), 200
def get_all_wishlists():
    res = spcall('get_wishlists', ())

    if 'Error' in str(res[0][0]):
        return jsonify({'status': 'error', 'message': res[0][0]})

    recs = []
    for r in res:
        recs.append({"wishlist_id": (r[0]), "wishlist_name": (r[1])})
    return jsonify({'status': 'ok', 'entries': recs, 'count': len(recs)}), 200
def get_wishlist(wishlist_id):
    res = spcall('get_wishlist', (wishlist_id,))

    entries = []
    if len(res) == 0:
        return jsonify({"status": "ok", "message": "No entries found", "entries": [], "count": "0"})
    else:
        row = res[0]
        entries.append({"wishlist_id": row[0], "wishlist_name": row[1]})
        return jsonify({"status": "ok", "message": "ok", "entries": entries, "count": len(entries)})
def get_product_category(product_category_id):
    res = spcall('get_product_category_id', (product_category_id))

    if 'Error' in res[0][0]:
        return jsonify({'status': 'error', 'message': res[0][0]})

    r = res[0]
    return jsonify(
        {"id": str(product_category_id), "name": str(r[0]), "description": str(r[1]), "main_image": str(r[2]),
         "parent_category_id": str(r[3]), "is_active": str(r[4])})
def new_wishlist_item():
    json = request.json
    wishlist_item_id = json['wishlist_item_id']
    wishlist_id = json['wishlist_id']
    item_id = json['item_id']
    time_stamp = json['time_stamp']
    res = spcall('new_wishlist_item', (wishlist_item_id, wishlist_id, product_id, time_stamp), True)

    if 'Error' in res[0][0]:
        return jsonify({'status': 'error', 'message': res[0][0]})
    return jsonify({'status': 'ok', 'message': res[0][0]})
def get_all_suppliers():
    res = spcall('get_suppliers', ())

    if 'Error' in str(res[0][0]):
        return jsonify({'status': 'error', 'message': res[0][0]})

    recs = []
    for r in res:
        recs.append(
            {"supplier_id": str(r[0]), "name": str(r[1]), "address": str(r[2]), "phone": str(r[3]), "fax": str(r[4]),
             "email": str(r[5]), "is_active": str(r[6])})
    return jsonify({'status': 'ok', 'entries': recs, 'count': len(recs)})
def wishlists_upsert(wishlist_id=None):
    data = json.loads(request.data)

    response = spcall("wishlists_upsert", (wishlist_id, data["wishlist_name"]), True)

    json_dict = build_json(response)

    status_code = 200
    if not wishlist_id:
        status_code = 201

    return jsonify(json_dict), status_code
def wishlist_items_upsert(wishlist_id, item_id=None):
    data = json.loads(request.data)

    response = spcall("wishlist_items_upsert", (data["wishlist_id"], data["item_id"], data["time_stamp"]), True)

    json_dict = build_json(response)

    status_code = 200
    if not item_id:
        status_code = 201

    return jsonify(json_dict), status_code
def options_upsert(option_group_id, option_id=None):
    data = json.loads(request.data)
    response = spcall('options_upsert', (
        option_id,
        option_group_id,
        data['option_value'],), True)
    json_dict = build_json(response)

    status_code = 200
    if not option_id:
        status_code = 201

    return jsonify(json_dict), status_code
def new_user():
    data = json.loads(request.data)
    response = spcall('new_user', (
        data['username'],
        data['email'],
        data['password'],
        data['date_created'],
        data['is_admin'],), True)

    if 'Error' in response[0][0]:
        return jsonify({'status': 'error', 'message': response[0][0]})

    return jsonify({'status': 'ok', 'message': response[0][0]}), 201
def get_cart(cart_id):
    res = spcall('get_cart', (cart_id,))

    if len(res) == 0:
        return jsonify({"status": "ok", "message": "No entries found", "entries": [], "count": "0"})
    else:
        recs = []
        for r in res:
            recs.append({"session_id": r[0],
                         "date_created": str(r[1]),
                         "customer_id": r[2],
                         "is_active": r[3]})

            return jsonify({'status': 'ok', 'entries': recs, 'count': len(recs)})
def locations_upsert(location_id=None):
    data = json.loads(request.data)

    response = spcall('locations_upsert', (
        location_id,
        data['location_name'],), True)

    json_dict = build_json(response)

    status_code = 200
    if not location_id:
        status_code = 201

    return jsonify(json_dict), status_code
def get_supplier(supplier_id):
    response = spcall('get_supplier', (supplier_id,))
    entries = []
    if len(response) == 0:
        return jsonify({"status": "ok", "message": "No entries found", "entries": [], "count": "0"})
    else:
        row = response[0]
        entries.append({"supplier_id": row[0],
                        "name": row[1],
                        "address": row[2],
                        "phone": row[3],
                        "fax": row[4],
                        "email": row[5]})
        return jsonify({"status": "ok", "message": "ok", "entries": entries, "count": len(entries)})
def new_cart_item(cart_id):
    data = json.loads(request.data)

    response = spcall('new_cart_item', (
        data['id'],
        cart_id,
        data['product_id'],
        data['quantity'],
        data['time_stamp'],), True)

    if 'Error' in response[0][0]:
        return jsonify({'status': 'error', 'message': response[0][0]})

    return jsonify({'status': 'ok', 'message': response[0][0]}), 200
def get_cart_item(cart_id, cart_item_id):
    res = spcall('get_cart_item', (cart_item_id, cart_id,))

    if len(res) == 0:
        return jsonify({"status": "ok", "message": "No entries found", "entries": [], "count": "0"})
    else:
        recs = []
        for r in res:
            recs.append({"cart_id": r[0],
                         "product_id": r[1],
                         "quantity": r[2],
                         "time_stamp": str(r[3])})

            return jsonify({'status': 'ok', 'entries': recs, 'count': len(recs)})
def new_cart():
    data = json.loads(request.data)

    response = spcall('new_cart', (
        data['id'],
        data['session_id'],
        data['date_created'],
        data['customer_id'],
        data['is_active'],), True)

    if 'Error' in response[0][0]:
        return jsonify({'status': 'error', 'message': response[0][0]})

    return jsonify({'status': 'ok', 'message': response[0][0]}), 200
def attributes_upsert(attribute_id=None):
    data = json.loads(request.data)

    response = spcall('attributes_upsert', (
        attribute_id,
        data['attribute_name'],
        data['validation'],), True)

    json_dict = build_json(response)

    status_code = 200
    if not attribute_id:
        status_code = 201

    return jsonify(json_dict), status_code
def item_attributes_upsert(item_id, attribute_id=None):
    data = json.loads(request.data)

    response = spcall('item_attributes_upsert', (
        data['attribute_id'],
        data['item_id'],
        data['attribute_value'],), True)

    json_dict = build_json(response)

    status_code = 200
    if not attribute_id:
        status_code = 201

    return jsonify(json_dict), status_code
def new_order_item(order_id):
    data = json.loads(request.data)

    response = spcall('new_order_item', (
        data['id'],
        order_id,
        data['item_id'],
        data['unit_price'],
        data['discount'],
        data['quantity'],), True)

    if 'Error' in response[0][0]:
        return jsonify({'status': 'error', 'message': response[0][0]})

    return jsonify({'status': 'ok', 'message': response[0][0]}), 200
def get_cart_items(cart_id):
    res = spcall('get_cart_items', (cart_id,))

    if 'Error' in str(res[0][0]):
        return jsonify({'status': 'error', 'message': res[0][0]})

    recs = []
    for r in res:
        recs.append({"id": r[0],
                     "cart_id": r[1],
                     "product_id": r[2],
                     "quantity": r[3],
                     "time_stamp": r[4]})

        return jsonify({'status': 'ok', 'entries': recs, 'count': len(recs)})
def get_order(order_id):
    response = spcall('get_order_id', (order_id,))
    entries = []
    if len(response) == 0:
        return jsonify({"status": "ok", "message": "No entries found", "entries": [], "count": "0"})
    else:
        r = response[0]
        entries.append({"customer_id": r[0],
                        "payment_id": r[1],
                        "transaction_date": str(r[2]),
                        "shipping_date": str(r[3]),
                        "time_stamp": str(r[4]),
                        "transaction_status": str(r[5]),
                        "total": r[6]})
        return jsonify({"status": "ok", "message": "ok", "entries": entries, "count": len(entries)})
def get_all_users():
    res = spcall('get_users', ())

    if 'Error' in str(res[0][0]):
        return jsonify({'status': 'error', 'message': res[0][0]})

    recs = []
    for r in res:
        recs.append({"id": r[0],
                     "username": r[1],
                     "password": r[2],
                     "email": r[3],
                     "is_admin": r[4]})

        return jsonify({'status': 'ok', 'entries': recs, 'count': len(recs)})
def new_product_category():
    id = request.form['inputID']
    name = request.form['inputName']
    description = request.form['inputDescription']
    main_image = request.form['inputMainImage']
    parent_category_id = request.form['inputParentCategoryId']
    is_active = False

    res = spcall('new_product_category', (
        id, name, description, main_image, parent_category_id, is_active), True)

    if 'Error' in res[0][0]:
        return jsonify({'status': 'error', 'message': res[0][0]})

    return jsonify({'status': 'ok', 'message': res[0][0]})
def get_all_orders():
    """
    Retrieve All Orders
    """

    res = spcall('get_orders', ())

    if 'Error' in str(res[0][0]):
        return jsonify({'status': 'error', 'message': res[0][0]})

    recs = []
    for r in res:
        recs.append({"id": str(r[0]), "customer_id": str(r[1]), "payment_id": str(r[2]), "transaction_date": str(r[3]),
                     "shipping_date": str(r[4]),
                     "time_stamp": str(r[5]), "transaction_status": str(r[6]), "total": str(r[7])})
    return jsonify({'status': 'ok', 'entries': recs, 'count': len(recs)})
def get_order_items(order_id):
    """
    Retrieve All Order_Details
    """

    res = spcall('get_order_items', ())

    if 'Error' in str(res[0][0]):
        return jsonify({'status': 'error', 'message': res[0][0]})

    recs = []
    for r in res:
        recs.append({"id": str(r[0]), order_id: str(r[1]), "item_id": str(r[2]), "unit_price": str(r[3]),
                     "discount": str(r[4]),
                     "quantity": str(r[5])})
    return jsonify({'status': 'ok', 'entries': recs, 'count': len(recs)})