import requests
import json
import pandas as pd
from datetime import datetime
import random

LIST_OF_VGU = 'vgu.csv'
ERROR_NOT_LOADED_URLS = 'Not Available'
RESULT_FILE_NAME = 'url_wf_'


def get_pdf_info_json(vgu: str):
    URL = "https://www.wayfair.com/graphql?hash=24e17b1e9c2cffd32c2cab2189a5e917%2316dd774cea9d3bd2c887f99be034a1de"
    HEADER = {
        "use-web-hash": "true",    
        "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36 Edg/130.0.0.0",  
    }
    payload = {
        "variables": {
            "sku": str(vgu),
            "selectedOptionIds": ["75220470"],
            "withLegacySpecificationsData": False
        }
    }
    response = requests.post(url=URL, json=payload, headers=HEADER)
    if response.status_code == 200:
        data = json.loads(response.text)
        result = {
            "PDF Link": [doc["url"] for doc in data["data"]["product"]["documents"]],
            "PDF Text": [doc["name"] for doc in data["data"]["product"]["documents"]],
            "sku": data["data"]["product"]["manufacturerPartNumber"]["partNumbers"][0],
            "vgu": vgu

        }
        return result
    else:
        # print(response.status_code)
        # print(response.headers)
        result = {
            "documentUrls": ERROR_NOT_LOADED_URLS,
            "sku": ERROR_NOT_LOADED_URLS,
            "vgu": vgu
        }


# vgu_to_search = []
print(get_pdf_info_json('VGU1000'))
# amount_of_rows = vgu_to_search.shape[0]
# master_df = pd.DataFrame()

# for index, row in vgu_to_search.iterrows():
#     # print(f'Working on {row['VGU']} {index+1}/{amount_of_rows}')
#     data = get_pdf_info_json(row['VGU'])
#     temp_df = pd.DataFrame({
#         'VGU': [data['vgu']],
#         'SKU': [data['sku']]
#     })
#     if len(data['documentUrls']) < 1:
#             temp_df['URL 1'] = 'No PDF attached'
#     for i in range(len(data['documentUrls'])):
#         temp_df[f'URL {i+1}'] = data['documentUrls'][i]
#     master_df = pd.concat([master_df, temp_df], ignore_index=True)
#     #sleep_time = random.randint(3, 7)


# current_datetime = datetime.now().strftime("%Y%m%d_%H%M%S")
# master_df.to_csv(f'{RESULT_FILE_NAME}{current_datetime}.csv', index=False)