import json
import traceback
from datetime import datetime

import requests

from configs.env import PROJECT_ID
from configs.firebase import SERVER_TIMESTAMP, Increment, db
from functions.LastUpdated import ordersLastUpdated, saveOrdersLastUpdated
from functions.Orders import (convertOrder, convertOrderItem,
                              convertShippingAddress, saveOrder)
from functions.Response import API_Error, saveError
from functions.Shopify.Auth import apiVersion, createHeader
from functions.Shops import getShopById, getShops, updateShop
from functions.Webhooks.SaveWebhook import saveWebhook
from V2.functions.Shops.main import Shop
from V2.Params import Params


def updateAllOrders(params={}):
    shopId = params.get("shopId")
    if not shopId:
        shops = getShops(platformId="2", credentials= True)
        res = [updateShopOrders(shop) for shop in shops if not shop.get("disabled")]
        return str(res)
    shop = getShopById(shopId, True)
    return str(updateShopOrders(shop))


def updateShopOrders(shop:dict):
    if shop:
        uid = shop.get('uid')
        shopId  = shop.get('id')
        platformId = shop.get('platformId')
        accessToken = shop.get('accessToken')
        enterpriseId = shop.get('enterpriseId')
        url = shop.get('url')
        headers = createHeader(accessToken)
        lastUpdate = ordersLastUpdated(shopId)
        ordersUrl = f"{url}/admin/api/{apiVersion}/orders.json"
        nowTimestamp = int(datetime.now().timestamp())
        updated_at_min= datetime.fromtimestamp(lastUpdate.get("timestamp")).isoformat()
        try:
            res = requests.get(ordersUrl, headers=headers, params=dict(created_at_min=updated_at_min,status="any",financial_status="paid",limit=250))
            if res.status_code != 200:
                if lastUpdate.get("failCount", 0)>=3: updateShop(shopId, disabled=True)
                saveOrdersLastUpdated(
                        shopId=shopId,
                        count=0,
                        orderIds=[],
                        cancelledOrders=[],
                        timestamp=lastUpdate.get("timestamp"),
                        failed=True,
                        failCount=Increment(1)
                    )
                print(res.text)
                return res.json()
            
            cancelledOrdersRequest = requests.get(ordersUrl, headers=headers, params=dict(created_at_min=updated_at_min,status="any",financial_status="refunded" ,limit=250))
            authorizedOrdersRequest = requests.get(ordersUrl, headers=headers, params=dict(created_at_min=updated_at_min,status="any",financial_status="authorized" ,limit=200))
            orders =  []
            updates = []
            if cancelledOrdersRequest.status_code == 200: orders.extend(cancelledOrdersRequest.json().get('orders'))
            if authorizedOrdersRequest.status_code == 200: orders.extend(authorizedOrdersRequest.json().get('orders'))
            endpoint = None
            if res.status_code == 200:
                orders.extend(res.json().get('orders', []))
                Link:str|None = res.headers.get("Link")
                if Link:
                    nextPage = Link.endswith('rel="next"')
                    while Link and nextPage and len(orders)<500:
                        endpoint = Link.replace("<", "").replace('>; rel="next"', "")
                        request = requests.get(endpoint, headers=headers)
                        if request.status_code == 200:
                            data = request.json()
                            orders.extend(data.get('orders', []))
                            Link = request.headers.get("Link")
                            nextPage = Link.endswith('rel="next"')
                        else:
                            print(request.text)
                            saveError(uid, "Shopify/Orders/updateShopOrders", res.text,data= dict(
                                    shopId=shopId,
                                    lastUpdate=lastUpdate,updated_at_min=updated_at_min
                                ))
                            break
                # def sorter(o): return datetime.fromisoformat(o.get('created_at')).timestamp()
                # orders.sort(key=sorter)
                # if len(orders)>0: nowTimestamp = int(datetime.fromisoformat(orders[-1].get('created_at')).timestamp())
                for order in orders:
                    # print(order.get("order_number"), order)
                    # if order.get("order_number") != "1019": continue
                    ignoredItems = []
                    shipping_address = order.get('shipping_address', {})
                    source_name = order.get('source_name')
                    tags = []
                    if source_name in ["4919179", "2329312", "4555605"]: source_name = f"{shop.get('name')} - tiktok"
                    tags.append(source_name)
                    convertedOrderItems = []
                    if shipping_address:
                        items = order.get('line_items', [])
                        convertededOrder = convertOrder(
                            uid=uid,
                            platformId=platformId,
                            shopId= shopId,
                            enterpriseId=enterpriseId,
                            shippingAddress=convertShippingAddress(
                                name = shipping_address.get('name'), 
                                address1= shipping_address.get('address1'),
                                address2= shipping_address.get('address2'),
                                city= shipping_address.get('city'),
                                state= shipping_address.get('province'),
                                zip= shipping_address.get('zip'),
                                country= shipping_address.get('country'),
                                email= order.get('email'),
                                phone= order.get('phone'),
                                company= shipping_address.get('company', ''),  # Added company field
                            ),
                            cancelled =bool(order.get("cancelled_at")),
                            shipped= order.get('fulfillment_status') == "fulfilled",
                            createdAt= datetime.fromisoformat(order.get('created_at')),
                            updatedAt= datetime.fromisoformat(order.get('updated_at')),
                            currencyCode= order.get('currency'),
                            platformOrderId= order.get('id'),
                            grandTotal= order.get('total_price'),
                            shippingCost= order.get('total_shipping_price_set', {}).get('shop_money', {}).get('amount', 0),
                            totalDiscount= order.get('total_discounts'),
                            totalPrice= order.get('total_price'),
                            totalTax= order.get('total_tax'),
                            orderNumber = str(order.get('order_number')),
                            shopName = shop.get('name'),
                            metadata=dict(
                                tags=tags,
                                vendor = ", ".join([item.get("vendor") for item in items if item.get("vendor")])
                            )
                        )
                        props = {str(item.get("id")):{prop.get("name"):prop.get("value") for prop in item.get('properties', [])} for item in items}
                        variantMappings = {}
                        blankVariants = {}
                        decorationMethods = {}
                        placementsData = {}
                        if props:
                            for key, value in props.items():
                                _placementsDocumentId = value.get("_placementsDocumentId")
                                if _placementsDocumentId:
                                    data = db.collection("shopifyCartPlacements").document(_placementsDocumentId).get().to_dict()
                                    if data:
                                        props[key] = data
                                        placementsData[key] = data
                                        orderType = data.get("type")
                                        if orderType == "nester":
                                            blankProductId = "7DUPlJbSmyNByC5syiEj"
                                            variations = data.get("variations")
                                            variants = data.get("variants")
                                            blankVariants.update({str(variant.get("platformVariantId")):"7DUPlJbSmyNByC5syiEj0" for variant in variants})
                                            variantId = next((str(v.get("variant_id")) for v in items if str(v.get("id")) == key), None)
                                            variant = next((v for v in variants if str(v.get("platformVariantId")) == variantId), None)
                                            variation = next((v for v in variations if v.get("varId") == variant.get("varId")), None)
                                            images = [
                                                    dict(
                                                        placement = "1",
                                                        name=variation.get("file").get("name"),
                                                        url=variation.get("file").get("url"),
                                                        type="printing",
                                                        uid=variation.get("file").get("uid"),
                                                        id=variation.get("file").get('id')
                                                    )
                                                ]
                                            variantMapping = dict(
                                                id=str(key),
                                                blankProductId = blankProductId,
                                                # blankVariantId = "7DUPlJbSmyNByC5syiEj0",
                                                images = images,
                                                ignored=False,
                                                placements = [i.get("placement") for i in images],
                                                mapped="mapped",
                                                productId=str(data.get("parentProductId")),
                                            )
                                            variantMappings[key] = variantMapping
                                        else:
                                            convertedOrderItems = convertCustomizerItems(
                                                order, 
                                                placementsData,
                                                Shop.from_dict(shop))
                                            # print("convertedOrderItems",convertedOrderItems )
                                            # placementDetails= dict(data.get("placementDetails"))
                                            # blankProductId = str(list(placementDetails.keys())[0])
                                            # printFiles= placementDetails.get(blankProductId, {}).get("canvasImages", {}).get("default", {}).items()
                                            # blankVariants.update({str(variant.get("platformVariantId")):(variant.get("blankVariantId") if variant.get("blankVariantId")  else variant.get("id") )for variant in data.get("variants", [])})
                                            # images = [
                                            #         dict(placement = str(k),**v)
                                            #         for k,v in printFiles
                                            #     ]
                                            # variantMapping = dict(
                                            #     id=str(key),
                                            #     blankProductId = blankProductId,
                                            #     # blankVariants=blankVariants,
                                            #     # blankVariantId = next(variant.get("blankVariantId") for variant in data.get("variants") if variant.get("platformVariantId") == key),
                                            #     images = images,
                                            #     ignored=False,
                                            #     placements = [i.get("placement") for i in images],
                                            #     mapped="mapped",
                                            #     productId=str(data.get("parentProductId")),
                                            # )
                                            # variantMappings[key] = variantMapping
                                            # decorationMethods[key] = data.get("product", {}).get("decorationType")
                                        # blankVariants.update({key: data.get("variants")[0].get("id")})
                                        # print(variantMapping, blankVariants)
                                        # data['blankVariants'] = blankVariants
                                        # props[key] = dict(variantMapping, placementsData = data)

                        # print(blankVariants)
                        # print([item.get("variant_id") for item in items])
                        orderIgnored = False
                        vendors = ["MSP", "xyz", "999","MSS"]
                        if shop.get("platformShopId") in ["60010627239", "66592309433"]: 
                            for item in items:
                                if item.get("vendor") not in vendors: ignoredItems.append(item.get("id"))
                                if str(item.get("product_id")) == "4683530141765": tags.append("Skip the line")
                                if item.get("tip_payment_gateway"): ignoredItems.append(item.get("id"))
                            anyMSP = any(item.get("vendor") in vendors for item in items)
                            if not anyMSP: orderIgnored = True
                        if orderIgnored: continue
                        if len(convertedOrderItems) == 0: convertedOrderItems = [convertOrderItem(
                                uid = uid,
                                platformId = platformId,
                                enterpriseId=enterpriseId,
                                platformOrderItemId= item.get('id'),
                                platformOrderId = order.get('id'),
                                shopId=shopId,
                                cancelled=bool(order.get("cancelled_at")),
                                createdAt= datetime.fromisoformat(order.get('created_at')),
                                updatedAt= datetime.fromisoformat(order.get('updated_at')),
                                name= item.get('name'),
                                platformProductId= item.get('product_id'),
                                price= item.get('price'),
                                quantity= item.get('quantity'),
                                shipped= order.get('fulfillment_status') == "fulfilled",
                                variantId= item.get('variant_id'),
                                # variantProperties={p.get("name"):p.get("value") for p in item.get('properties', [])},
                                placementsData = props.get(str(item.get('id')), {}),
                                productMapping = dict(
                                    printOnDemand=True, 
                                    ignored=False,
                                    decorationType=decorationMethods.get(str(item.get("id"))),
                                    ) if props.get(str(item.get('id')), {}) and  blankVariants.get(str(item.get("variant_id"))) else None,
                                variantProperties=[
                                    dict(
                                        name = p.get("name"),
                                        value = p.get("value")
                                    ) for p in item.get("properties", [])
                                ],
                                variantMapping=dict(blankVariantId = blankVariants.get(str(item.get("variant_id"))),**variantMappings.get(str(item.get('id')), {})) if props.get(str(item.get('id'))) and blankVariants.get(str(item.get("variant_id"))) else {},
                                ignored=item.get("id") in ignoredItems,
                                metadata=dict(vendor = item.get("vendor"))
                            ) for item in items
                            ]
                        updates.append(saveOrder(convertededOrder, convertedOrderItems))
                orderIds = [order.get('id') for order in orders]
                # authorizedOrders = [order.get("id") for order in authorizedOrdersRequest.json().get('orders')]
                cancelledOrders = [order.get("id") for order in cancelledOrdersRequest.json().get('orders')]
                saveOrdersLastUpdated(
                        shopId = shopId, count= len(updates),
                        orderIds= orderIds,
                        # authorizedOrders=authorizedOrders,
                        cancelledOrders=cancelledOrders,
                        timestamp=nowTimestamp,
                        failed=False,
                        failCount=0
                    )
            else:
                # print(res.text)
                saveError(uid, "Shopify/Orders/updateShopOrders", res.text,data=dict(
                    shopId=shopId,
                    lastUpdate=lastUpdate,updated_at_min=updated_at_min
                ))
            return f"Orders updated => {len(updates)}" 
        except Exception as e:
            traceback.print_exc()
            saveError(uid, "Shopify/Orders/updateShopOrders", str(e),data =  dict(
                shopId=shopId,
                lastUpdate=lastUpdate
            ))
    return "No Updates"
            
def fulfillmentRequest(data:dict):
    kind = data.get('kind')
    return dict(kind=kind)

def getAssignedOrders(shop):
    url = f"{shop.get('url')}/admin/api/{apiVersion}/assigned_fulfillment_orders.json"
    headers = createHeader(shop.get("accessToken"))
    res = requests.get(url,
        headers=headers,params=dict(assignment_status="fulfillment_requested"))
    print(res.url,res.text, res.status_code)

def orderWebhook(data:dict):
    shopId = data.get('shopId')
    currentUser = data.get('currentUser')
    uid = currentUser.get('uid')
    shop = getShopById(shopId, True)
    accessToken = shop.get('accessToken')
    url = shop.get('url')
    headers = createHeader(accessToken)
    webhooksUrl = f"{url}/admin/api/{apiVersion}/webhooks.json"
    webhook = json.dumps(dict(webhook=dict( 
                topic="orders/edited",
                address=f"https://riverr-enterprise-integrations-dot-{PROJECT_ID}.uc.r.appspot.com/shopify/webhooks/{shopId}/orders",
                format="json"
        )))
    res = requests.post(webhooksUrl,
            headers=headers,
            data=webhook,
            json=webhook
        )
    if res.status_code == 201:
        webhook = res.json().get('webhook')
        saveWebhook(uid, shopId,
         webhook.get('id'), webhook.get('topic'), webhook.get('address'),
            topic=webhook.get('topic'),
            createdAt=SERVER_TIMESTAMP,
            updatedAt=SERVER_TIMESTAMP
        )
        return res.json()
    else:
        print(res.json())
        saveError(uid, "Shopify/Orders/orderWebhook", res.text,data=dict(shopId=shopId))
        return None

def ordersWebhookEvent(data:dict):
    order = data.get('order_edit')
    id = data.get("id")
    orderId = order.get('order_id')
    shop = getShopById(id, True)
    uid = shop.get('uid')
    platformId = shop.get('platformId')
    enterpriseId = shop.get('enterpriseId')
    headers = createHeader(shop.get('accessToken'))
    res = requests.get(f"{shop.get('url')}/admin/api/{apiVersion}/orders/{orderId}.json", headers=headers)
    if res.status_code == 200:
        order = res.json().get('order')
        shipping_address = order.get('shipping_address', {})
        source_name = order.get('source_name')
        if source_name in ["4919179", "2329312", "4555605"]: source_name = f"{shop.get('name')} - tiktok"
        convertededOrder = convertOrder(
            uid=uid,
            platformId=platformId,
            shopId= id,
            enterpriseId=enterpriseId,
            shippingAddress=convertShippingAddress(
                name = shipping_address.get('name'), 
                address1= shipping_address.get('address1'),
                address2= shipping_address.get('address2'),
                city= shipping_address.get('city'),
                state= shipping_address.get('province'),
                zip= shipping_address.get('zip'),
                country= shipping_address.get('country'),
                email= order.get('email'),
                phone= order.get('phone'),
                company= shipping_address.get('company', ''),  # Added company field
            ),
            cancelled =bool(order.get("cancelled_at")),
            shipped= order.get('fulfillment_status') == "fulfilled",
            createdAt= datetime.fromisoformat(order.get('created_at')),
            updatedAt= datetime.fromisoformat(order.get('updated_at')),
            currencyCode= order.get('currency'),
            platformOrderId= order.get('id'),
            grandTotal= order.get('total_price'),
            shippingCost= order.get('total_shipping_price_set', {}).get('shop_money', {}).get('amount', 0),
            totalDiscount= order.get('total_discounts'),
            totalPrice= order.get('total_price'),
            totalTax= order.get('total_tax'),
            orderNumber = str(order.get('order_number')),
            metadata=dict(
                tags=[source_name],
            ),
            shopName=shop.get('name')
        )
        items = order.get('line_items', [])
        properties = order.get('properties', [])
        # print(properties)
        convertedOrderItems = [convertOrderItem(
                uid = uid,
                platformId = platformId,
                enterpriseId=enterpriseId,
                platformOrderItemId= item.get('id'),
                platformOrderId = order.get('id'),
                shopId=id,
                cancelled=bool(order.get("cancelled_at")),
                createdAt= datetime.fromisoformat(order.get('created_at')),
                updatedAt= datetime.fromisoformat(order.get('updated_at')),
                name= item.get('name'),
                platformProductId= item.get('product_id'),
                price= item.get('price'),
                quantity= item.get('quantity'),
                shipped= item.get('fulfillment_status') == 'fulfilled',
                variantId= item.get('variant_id'),
                variantProperties=[
                            dict(
                                name = p.get("name"),
                                value = p.get("value")
                            ) for p in item.get("properties", [])
                    ],
                 metadata=dict(
                                tags=[source_name],
                    )
            ) for item in items
            ]
        res = saveOrder(convertededOrder, convertedOrderItems, True)
        return convertededOrder
    else:
        print(res.json())
        return order
    

def updateSingleOrder(shop:Shop, platformOrderId, readd=False):
        uid = shop.uid
        shopId  = shop.id
        platformId = shop.platformId
        accessToken = shop.accessToken
        enterpriseId = shop.enterpriseId
        url = shop.url
        headers = createHeader(accessToken)
        ordersUrl = f"{url}/admin/api/{apiVersion}/orders/{platformOrderId}.json"
        res = requests.get(ordersUrl, headers=headers)
        if res.status_code == 200:
            order = res.json().get('order')
            shipping_address = order.get('shipping_address', {})
            source_name = order.get('source_name')
            if source_name in ["4919179", "2329312", "4555605"]: source_name = f"{shop.name} - tiktok"
            if shipping_address:
                convertededOrder = convertOrder(
                    uid=uid,
                    platformId=platformId,
                    shopId= shopId,
                    enterpriseId=enterpriseId,
                    shippingAddress=convertShippingAddress(
                        name = shipping_address.get('name'), 
                        address1= shipping_address.get('address1'),
                        address2= shipping_address.get('address2'),
                        city= shipping_address.get('city'),
                        state= shipping_address.get('province'),
                        zip= shipping_address.get('zip'),
                        country= shipping_address.get('country'),
                        email= order.get('email'),
                        phone= order.get('phone'),
                        company= shipping_address.get('company', ''),  # Added company field
                    ),
                    cancelled =bool(order.get("cancelled_at")),
                    shipped= order.get('fulfillment_status') == "fulfilled",
                    createdAt= datetime.fromisoformat(order.get('created_at')),
                    updatedAt= datetime.fromisoformat(order.get('updated_at')),
                    currencyCode= order.get('currency'),
                    platformOrderId= order.get('id'),
                    grandTotal= order.get('total_price'),
                    shippingCost= order.get('total_shipping_price_set', {}).get('shop_money', {}).get('amount', 0),
                    totalDiscount= order.get('total_discounts'),
                    totalPrice= order.get('total_price'),
                    totalTax= order.get('total_tax'),
                    orderNumber = str(order.get('order_number')),
                    shopName = shop.name,
                    metadata=dict(tags=[source_name])
                )
                items = order.get('line_items', [])
                props = {str(item.get("id")):{prop.get("name"):prop.get("value") for prop in item.get('properties', [])} for item in items}
                variantMappings = {}
                blankVariants = {}
                decorationMethods = {}
                convertedOrderItems = []
                if props:
                    placementDocuments = {}
                    for key, value in props.items():
                        _placementsDocumentId = value.get("_placementsDocumentId")
                        if _placementsDocumentId:
                            data = db.collection("shopifyCartPlacements").document(_placementsDocumentId).get().to_dict()
                            if data: placementDocuments[key] = data
                    if placementDocuments: convertedOrderItems = convertCustomizerItems(order, placementDocuments, shop)
                if not len(convertedOrderItems):
                    convertedOrderItems = [convertOrderItem(
                        uid = uid,
                        platformId = platformId,
                        enterpriseId=enterpriseId,
                        platformOrderItemId= item.get('id'),
                        platformOrderId = order.get('id'),
                        shopId=shopId,
                        cancelled=bool(order.get("cancelled_at")),
                        createdAt= datetime.fromisoformat(order.get('created_at')),
                        updatedAt= datetime.fromisoformat(order.get('updated_at')),
                        name= item.get('name'),
                        platformProductId= item.get('product_id'),
                        price= item.get('price'),
                        quantity= item.get('quantity'),
                        shipped= item.get('fulfillment_status') == 'fulfilled',
                        variantId= item.get('variant_id'),
                        # variantProperties={p.get("name"):p.get("value") for p in item.get('properties', [])},
                        placementsData = props.get(str(item.get('id')), {}),
                        productMapping = dict(
                                    printOnDemand=True, 
                                    ignored=False,
                                    decorationType=decorationMethods.get(str(item.get("id"))),
                                    ) if props.get(str(item.get('id')), {}) and  blankVariants.get(str(item.get("variant_id"))) else None,
                        variantMapping=dict(
                            blankVariantId = blankVariants.get(str(item.get("variant_id"))),
                            images=[],
                            placements=[],
                            **variantMappings.get(str(item.get('id')), {})) if props.get(str(item.get('id'))) and blankVariants.get(str(item.get("variant_id"))) else {},
                        variantProperties=[
                            dict(
                                name = p.get("name"),
                                value = p.get("value")
                            ) for p in item.get("properties", [])
                        ],
                        metadata=dict(
                            tags = [source_name]
                        )
                    ) for item in items
                    ]
                saveOrder(convertededOrder, convertedOrderItems, True, readd)
            return f"Orders updated => {platformOrderId}" 
        else: 
            print(res.request.url, res.text, res.status_code)


def convertCustomizerItems(order:dict, placementsData:dict, shop:Shop):
    items = order.get("line_items", [])
    convertedOrderItems = []
    source_name = order.get('source_name')
    if source_name in ["4919179", "2329312", "4555605"]: source_name = f"{shop.name} - tiktok"
    for item in items:
        data = placementsData.get(str(item.get("id")))
        if not data: return []
        placementDetails= dict(data.get("placementDetails"))
        blankProductId = str(list(placementDetails.keys())[0])
        variantType = placementDetails.get(blankProductId, {}).get("variantType")
        if not variantType: variantType = "color"
        canvasImages = placementDetails.get(blankProductId, {}).get("canvasImages", {})
        printImages = []
        for key, canvasImage in canvasImages.items():
            for placement, printImage in canvasImage.items():
                printImages.append(
                        dict(
                            id = printImage.get("id"),
                            placement = placement,
                            # variantTypeValue = key,
                            # variantType = variantType,
                            url = printImage.get("url"),
                            name = printImage.get("name"),
                            type="printing",
                            **{variantType: key}
                        )
                )
        mockImages = []
        previewImages = placementDetails.get(blankProductId, {}).get("previewImages", {})
        for key, placementPreviews in previewImages.items():
            for placement, img in placementPreviews.items():
                mockImages.append(
                    dict(
                        id = img.get("id"),
                        url = img.get("url"),
                        name = img.get("name"),
                        placement = placement,
                        type="preview",
                        **{variantType: key}
                    )
                )
        # print("mockImages", mockImages)
        variants = data.get("variants")
        variations = data.get("variations")
        variantType = placementDetails.get(blankProductId, {}).get("variantType", "color")
        variantId = item.get("variant_id")
        variant = next((v for v in variants if str(v.get("platformVariantId")) == str(variantId)), None)
        if not variant: return []
        variation = next((v for v in variations if str(v.get("varId")) == str(variant.get("varId"))), None)
        thisItemPrintImages = [i for i in printImages if i.get(variantType) == variation.get(variantType)]
        if len(thisItemPrintImages) == 0:  thisItemPrintImages = [i for i in printImages if i.get(variantType) == "default"]
        thisItemMockImages = [i for i in mockImages if i.get(variantType) == variation.get(variantType)]
        if len(thisItemMockImages) == 0:  thisItemMockImages = [i for i in mockImages if i.get(variantType) == "default"]
        blankVariantId = variant.get("blankVariantId") if variant.get("blankVariantId")  else variant.get("id")
        convertedOrderItem = convertOrderItem(
                            uid = shop.uid,
                            platformId = "2",
                            enterpriseId=shop.enterpriseId,
                            platformOrderItemId= item.get('id'),
                            platformOrderId = order.get('id'),
                            shopId=shop.id,
                            cancelled=bool(order.get("cancelled_at")),
                            createdAt= datetime.fromisoformat(order.get('created_at')),
                            updatedAt= datetime.fromisoformat(order.get('updated_at')),
                            name= item.get('name'),
                            platformProductId= item.get('product_id'),
                            price= item.get('price'),
                            quantity= item.get('quantity'),
                            shipped= item.get('fulfillment_status') == 'fulfilled',
                            variantId= item.get('variant_id'),
                            images=thisItemMockImages,
                            rewriteImages=False,
                            productMapping = dict(
                                        printOnDemand=True, 
                                        ignored=False,
                                        decorationType=data.get("product").get("decorationType"),
                                    ),
                            variantMapping=dict(
                                blankProductId=blankProductId,
                                blankVariantId = blankVariantId,
                                images=thisItemPrintImages,
                                placements=[i.get("placement") for i in thisItemPrintImages],
                            variantProperties=[
                                dict(
                                    name = p.get("name"),
                                    value = p.get("value")
                                ) for p in item.get("properties", [])
                            ],
                            metadata=dict(
                                tags = [source_name]
                        )
                ))
        convertedOrderItems.append(convertedOrderItem)
    return convertedOrderItems



# import concurrent.futures
# import json

# from google.cloud import pubsub_v1


def acceptPubsubRequest(params={}):
    shop = params.get("shop")
    print(shop)
    return updateShopOrders(shop)

# # Initialize Pub/Sub client
# def publishShopifyOrdersUpdate(params={}):
#     publisher = pubsub_v1.PublisherClient()
#     topic_path = publisher.topic_path('riverr-enterprise-user', 'shopify-order-updates')

#     shops = getShops(platformId="2", credentials= True)

# # Function to publish messages asynchronously
#     def publish_shop_order_update(shop):
#         # Publish the message asynchronously
#         future = publisher.publish(topic_path, json.dumps(shop).encode('utf-8'))
#         return future

#     # Use ThreadPoolExecutor to publish in parallel
#     with concurrent.futures.ThreadPoolExecutor() as executor:
#         # Submit tasks for each shop
#         futures = [executor.submit(publish_shop_order_update, shop) for shop in shops]

#         # Optionally wait for all futures to complete
#         # for future in concurrent.futures.as_completed(futures):
#         #     try:
#         #         result = future.result()  # Optionally handle result or exceptions
#         #     except Exception as e:
#         #         print(f"An error occurred: {e}")
