o
    Zh&                     @   s2  d dl m Z  d dlmZ d dlZd dlmZ d dlmZ d dlm	Z	 d dl
mZmZ d dlmZ d d	lmZ d d
lmZmZ d dlmZ d dlmZ d dlmZ d dlmZ defddZd dl m Z  d!defddZd"dedefddZdefddZ d dlmZ g ddfdededee dedef
dd Z!dS )#    )datetime)ListN)
PROJECT_ID)db)	API_Error)
apiVersioncreateHeader)Product)Variant)ShopifyGraphQLClientvalidate_shopify_id)getShopifyProductVariants)Shop)
Enterprise)Paramsparamsc              	   C   s   | j d}t|}t|j}td| td|j tdt |j dt d}tj||d}|j	dkr[|
 d	g }td
 |D ]}td|d  d|d  d|d   qC|S td|j d S )NshopIdheaderszshop.urlr   z/admin/api/z/locations.json)r      	locationsu   📍 Available Locations:z- namez | ID: idz | Active: activeu   ❌ Failed to fetch locations: )argsgetr   r   accessTokenprinturlr   requestsstatus_codejsontext)r   r   shopr   r   resr   loc r%   S/var/www/html/riverr-enterprise-integrations-main/V2/functions/Shopify/Locations.pygetShopLocations   s   




(r'   clientc           	      C   s   |s
t | jjj}td| j }|j	r=|
 }|d}|r=d}d|i}| ||}|di di }|r=|S t| |dS )aB  
    Retrieves the fulfillment service for a given shop and validates if the location exists in Shopify.
    If the fulfillment service does not exist, it creates a new one.

    :param shop: ShopifyGraphQLClient instance
    :param shopId: The shop's ID
    :return: Fulfillment service data if valid, otherwise None
    fulfillmentServicesGraphQLr   aV  
            query FulfillmentServiceShow($id: ID!) {
              fulfillmentService(id: $id) {
                    id
                    serviceName
                    callbackUrl
                    location {
                     legacyResourceId
                     id
                    }
              }
            }
            datafulfillmentService)r(   enterpriseName)r   r   r"   enterpriseIdr   r   
collectiondocumentr   existsto_dictexecutecreateFulfillmentService)	r(   r,   reffulfillment_datafulfillment_idquery	variablesresultr+   r%   r%   r&   getFulfillmentServiceO   s   

r:   PODr,   c           	      C   s   | d}dt  d}d}||d}| ||}|di di dg }|r4td	| tt|d
|d d d }td| j	
| |S )z
    Creates a new fulfillment service in Shopify via GraphQL and stores it in Firestore.

    :param shop: Shopify shop data
    z - Fulfillment Servicez+https://riverr-enterprise-integrations-dot-z%.uc.r.appspot.com/shopify/fulfillmenta1  
        mutation fulfillmentServiceCreate($name: String!, $callbackUrl: URL!) {
            fulfillmentServiceCreate(name: $name, callbackUrl: $callbackUrl, trackingSupport: true, inventoryManagement: true) {
                fulfillmentService {
                    id
                    serviceName
                    callbackUrl
                    location {
                        id
                    }
                }
                userErrors {
                    field
                    message
                }
            }
        }
    )r   callbackUrlr*   fulfillmentServiceCreate
userErrorszFULFILLMENT SERVICE ERRORS: i  r+   r)   )r   r2   r   r   r   strr   r.   r/   r   set)	r(   r,   fulfillment_namecallback_urlmutationr8   r9   errorsfulfillment_servicer%   r%   r&   r3   z   s   

r3   c                 C   s@   | j d}t|}t|}t|j}|rt||jdS d S )Nr   )r,   )r   r   r   r   r   r-   r3   r   )r   r   r"   r(   
enterpriser%   r%   r&   createFulfillmentServiceRoute   s   
rG   	productIdvariants
locationIdplatformProductIdc              
   C   s   |st |}|st| dd}td| |s t|j}td| t| |}g }t|D ]\}}d| }	|	d|	 d|d d	| d
 q0dd
| d}
| |
}td| t|D ]&\}}d| }	|di |	i dg }|rtd|d d|  qctd dS )a  
    Updates the fulfillment location of product variants in Shopify using the inventoryAdjustQuantities mutation.

    :param client: Shopify GraphQL client
    :param productId: The product ID
    :param variants: The product variants (optional)
    :param locationId: The location ID to update the inventory
    :param platformProductId: The Shopify platform product ID (optional)
    locationr   zlocationId:-zplatformProductId:-inventoryActivatez	
        z&: inventoryActivate(inventoryItemId: "inventoryItemIdz", locationId: "z") {
          inventoryLevel {
            id
          }
          userErrors {
            field
            message
          }
        }
        z%mutation inventoryActivateMultiple {  z }zRESULT: r*   r>   u$   ❌ ERROR Activating Inventory Item z: u@   ✅ Fulfillment locations updated successfully for all variants!N)r
   getAllr:   r   r   r	   rK   r   	enumerateappendjoinr2   )r(   rH   rI   rJ   rK   shopify_variantsmutation_partsidxvariantaliasrC   r9   rD   r%   r%   r&   updateFulfillmentLocations   s:   







rY   )N)r;   )"r   typingr   r   configs.envr   configs.firebaser   functions.Responser   functions.Shopify.Authr   r   V2.functions.Products.mainr	   V2.functions.Products.Variantsr
   V2.functions.Shopify.mainr   r   V2.functions.Shopify.Productsr   V2.functions.Shops.mainr   V2.functions.Users.mainr   	V2.Paramsr   r'   r:   r?   r3   rG   rY   r%   r%   r%   r&   <module>   s*    9+:.