import sys
import csv
import time
import urllib.parse as ul
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException, NoSuchElementException

# 📌 Usage: python export_finder.py "<market to explore>" [max_pages]
if len(sys.argv) < 2:
    print('❌ Usage: python export_finder.py "<market to explore>" [max_pages]')
    sys.exit(1)

market = sys.argv[1].strip()
max_pages = int(sys.argv[2]) if len(sys.argv) > 2 else 8
output_file = f"export_conversion_{market.replace(' ', '_').lower()}.csv"

# 🔎 Query tuned for Conversie / Export (Reddit + Quora + forumuri)
sites_part = '(site:reddit.com OR site:quora.com OR inurl:forum OR inurl:community)'
types_part = '(inurl:comments OR inurl:thread OR inurl:topic OR inurl:discussion)'
intent_part = '(' + ' OR '.join([
    '"how to export"', '"how do I export"', '"export my data"', '"export all"',
    '"export to"', '"export into"', '"export from"', '"export file"',
    '"convert to"', '"convert into"', '"convert from"', '"file format"',
    '"download my data"', '"data export"', '"save my data"', '"backup my data"'
]) + ')'
q = f'"{market}" {sites_part} {types_part} {intent_part}'

# Force English UI, depersonalize a bit, ask for more results per page
base_url = "https://www.google.com/search?hl=en&safe=off&pws=0&num=50&q=" + ul.quote_plus(q)

# 🧭 Setup headless Chrome (slightly stealthier)
options = Options()
# options.add_argument('--headless=new')
options.add_argument('--disable-gpu')
options.add_argument('--no-sandbox')
options.add_argument('--window-size=1920,1080')
options.add_argument('--lang=en-US,en')
options.add_argument("user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36")
options.add_experimental_option('excludeSwitches', ['enable-automation'])
options.add_experimental_option('useAutomationExtension', False)

driver = webdriver.Chrome(options=options)
wait = WebDriverWait(driver, 12)

def click_if_exists(xpaths):
    for xp in xpaths:
        try:
            btn = wait.until(EC.element_to_be_clickable((By.XPATH, xp)))
            btn.click()
            time.sleep(0.8)
            return True
        except Exception:
            continue
    return False

def handle_consent():
    # Handle EU consent in EN/RO and generic variants
    if "consent" in driver.current_url or "Before you continue to Google" in driver.page_source:
        click_if_exists([
            "//button[.//div[contains(.,'Accept all')]]",
            "//button[.//span[contains(.,'Accept all')]]",
            "//button[contains(.,'Accept all')]",
            "//button[contains(.,'I agree')]",
            "//button[contains(.,'Sunt de acord')]",
            "//button[contains(.,'Acceptă tot')]",
            "//button[contains(.,'Accept') and not(contains(.,'Reject'))]"
        ])

def clean_google_redirect(href: str) -> str:
    # Convert https://www.google.com/url?q=<real>&... -> <real>
    try:
        from urllib.parse import urlparse, parse_qs
        parsed = urlparse(href)
        if 'google.' in parsed.netloc and parsed.path == '/url':
            qs = parse_qs(parsed.query)
            if 'q' in qs and qs['q']:
                return qs['q'][0]
        return href
    except Exception:
        return href

def extract_results():
    # Wait until we see at least one organic title <h3> in #search
    try:
        wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, "div#search h3")))
    except TimeoutException:
        return []

    h3s = driver.find_elements(By.CSS_SELECTOR, "div#search h3")
    results = []
    for h3 in h3s:
        try:
            a = h3.find_element(By.XPATH, "./ancestor::a[1]")
            href = clean_google_redirect(a.get_attribute("href"))
            title = h3.text.strip()
            if not title or not href:
                continue
            # keep only reddit/quora/likely-forum links
            if not any(s in href for s in ["reddit.com", "quora.com", "forum", "community"]):
                continue
            results.append((title, href))
        except Exception:
            continue
    return results

print(f"🔍 Searching for: {q}")

rows, seen = [], set()

# Pre-hit Google and accept consent if needed
driver.get("https://www.google.com/ncr")
time.sleep(1.0)
handle_consent()

# 🔁 Paginate: &start=0,10,20,...
for page in range(max_pages):
    start = page * 10
    url = base_url + f"&start={start}"
    print(f"\n📄 Page {page+1}/{max_pages} — {url}")
    driver.get(url)
    time.sleep(0.6)
    handle_consent()

    # Quick block detection
    if "unusual traffic" in driver.page_source.lower():
        print("⚠️ Google is rate-limiting / CAPTCHA. Try slower waits or fewer pages.")
        break

    found = extract_results()
    print(f"  ⮕ Found {len(found)} organic results on this page")

    for title, href in found:
        if href in seen:
            continue
        rows.append({"title": title, "url": href})
        seen.add(href)

driver.quit()

# 💾 Save to CSV
print(f"\n💾 Saving {len(rows)} results to {output_file}")
with open(output_file, "w", newline='', encoding='utf-8') as f:
    writer = csv.DictWriter(f, fieldnames=["title", "url"])
    writer.writeheader()
    writer.writerows(rows)

print("✅ Done!")
