from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.chrome.options import Options
from webdriver_manager.chrome import ChromeDriverManager
from bs4 import BeautifulSoup
import os
import pandas as pd
import time
import random
import json
import logging
from requests.exceptions import RequestException
import mysql.connector

logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')

CHROME_PROFILE_DIR = "/var/www/html/nodeJS/chrome-profile"


chrome_options = Options()
chrome_options.add_argument("--no-sandbox")
chrome_options.add_argument("--disable-dev-shm-usage")
chrome_options.add_argument("--disable-extensions")
chrome_options.add_argument("--no-proxy-server")  
chrome_options.add_argument("--proxy-server='direct://'")
chrome_options.add_argument("--proxy-bypass-list=*")
# chrome_options.add_argument("--user-data-dir=/var/www/html/nodeJS/chrome-profile")
chrome_options.add_argument("--disk-cache-size=0")  
chrome_options.add_argument("--incognito")  
chrome_options.add_argument("--enable-unsafe-swiftshader")
chrome_options.add_argument("--use-gl=swiftshader")
chrome_options.add_argument("--disable-software-rasterizer")
chrome_options.add_argument("--dns-prefetch-disable")  
chrome_options.add_argument("--headless")
chrome_options.add_argument("--disable-gpu")
chrome_options.add_argument("--no-sandbox")
chrome_options.add_argument("--disable-webgl")
chrome_options.add_argument("--use-angle=gl")
chrome_options.add_argument("--enable-unsafe-swiftshader")
# chrome_options.add_argument("--log-level=3")
chrome_options.add_argument(
    "user-agent=Mozilla/5.0 (X11; Linux x86_64) "
    "AppleWebKit/537.36 (KHTML, like Gecko) "
    "Chrome/142.0.0.0 Safari/537.36"
)






#chrome_options.add_argument("--host-resolver-rules=MAP * 0.0.0.0 , EXCLUDE localhost")


driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=chrome_options)

db_config = {
    "host": "104.236.70.14",  
    "user": "integration",     
    "password": "?Q8/{lVK2N08Y<b>k", 
    "database": "Salsify"  
}

try:
    connection = mysql.connector.connect(**db_config)
except mysql.connector.Error as e:
    exit()

query = """
SELECT SL.SKU, SHDUS.`Home Depot ID` as 'LINK'
FROM  Salsify.MainData SL
LEFT JOIN Salsify.Homedepot SHDUS ON SL.SKU = SHDUS.SKU
WHERE (SHDUS.SKU LIKE '%' AND  (SL.Status)='Active' AND SHDUS.`Home Depot ID` IS NOT NULL) OR  (SHDUS.SKU LIKE '%' AND (SL.Status)='Liquidation' AND SHDUS.`Home Depot ID` IS NOT NULL)
"""

try:
    cursor = connection.cursor(dictionary=True)
    cursor.execute(query)
    results = cursor.fetchall()

    df_input = pd.DataFrame(results)
    df_input['Full_URL'] = "https://www.homedepot.com/p/"+df_input['SKU'].astype(str)+"/"+ df_input['LINK'].astype(str)

    urls = df_input['Full_URL'].dropna().tolist()
    additional_data = df_input['SKU'].tolist()

finally:
    if connection.is_connected():
        cursor.close()
        connection.close()

all_data = []

def process_url_with_click(url):
    max_retries = 3 
    for attempt in range(max_retries):
        try:
            time.sleep(random.uniform(5, 8))
            logging.info(f"Обробка URL: {url}")
            driver.get(url)


            time.sleep(5)  
            driver.save_screenshot("test.png")
            pdfObject = {}
            if 'Special Buys' in driver.page_source: 
                pdfObject['Special Buy'] = True
            else:
                pdfObject['Special Buy'] = False
            pdf_links = []
            pdf_links.append(pdfObject);
                    # pdfObject['PDF Link'] = formatted_href
                    # pdfObject['PDF Text'] = text
            return pdf_links

        except Exception as e:
            logging.error(f"Помилка обробки {url} на спробі {attempt + 1}: {e}")
            if attempt < max_retries - 1:
                time.sleep(10)
            else:
                return []

for i, url in enumerate(urls):
    data = process_url_with_click(url)
    if data:
        row = {'Base URL': urls[i], 'SKU': additional_data[i], 'Special Buy' : data[0]['Special Buy']}
        logging.info(f"PDF-дані додано для SKU {additional_data[i]} (URL: {urls[i]})")
        all_data.append(row)
        logging.info(row)

driver.quit()
print(json.dumps(all_data))

# df_output = pd.DataFrame(all_data)
