import sys
import csv
import time
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.service import Service

# 📌 Parse command-line arguments
if len(sys.argv) < 2:
    print("❌ Usage: python reddit.py <subreddit-name>")
    sys.exit(1)

subreddit = sys.argv[1].lower().strip()
url = f"https://www.reddit.com/r/{subreddit}/top/?t=year"
output_file = f"r_{subreddit}_infinite_scroll.csv"

# 🧭 Setup headless Chrome with auto driver management
options = Options()
# options.add_argument('--headless')
options.add_argument('--disable-gpu')
options.add_argument('--no-sandbox')
options.add_argument('--window-size=1920,1080')
options.add_argument("user-agent=Mozilla/5.0")
options.add_argument("--user-data-dir=reddit_profile")
options.add_argument("--profile-directory=Default")
options.add_argument("--disable-blink-features=AutomationControlled")
options.add_argument(
    "--user-data-dir=/Users/bogdansandulescu/selenium_reddit_profile"
)
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option('useAutomationExtension', False)

driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=options)
print(f"🔍 Scraping r/{subreddit} from {url}")
driver.get(url)
time.sleep(5)

# 🌀 Scroll config
scroll_pause = 10
max_scrolls = 100
max_no_growth = 3   # ✅ STOP dacă nu apar postări noi

seen_urls = set()
scraped = []

no_growth = 0
last_count = 0

# 🔁 Infinite scroll loop
for scroll in range(max_scrolls):
    print(f"🔽 Scroll {scroll+1}/{max_scrolls}")
    driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
    time.sleep(scroll_pause)

    posts = driver.find_elements(By.CSS_SELECTOR, "article.w-full.m-0")
    print(f"  ⮕ Found {len(posts)} posts")

    for post in posts:
        try:
            title_elem = post.find_element(By.CSS_SELECTOR, "a[id^='post-title']")
            title = title_elem.text.strip()
            post_url = title_elem.get_attribute("href")

            if post_url in seen_urls:
                continue

            try:
                body_elem = post.find_element(By.CSS_SELECTOR, "div.feed-card-text-preview")
                body = body_elem.text.strip()
            except:
                body = ""

            scraped.append({
                "title": title,
                "url": post_url,
                "body": body
            })
            seen_urls.add(post_url)

        except Exception:
            continue

    current_count = len(scraped)

    if current_count == last_count:
        no_growth += 1
        print(f"⚠️ No new posts ({no_growth}/{max_no_growth})")
    else:
        print(f"✅ New posts: +{current_count - last_count}")
        last_count = current_count
        no_growth = 0

    if no_growth >= max_no_growth:
        print("🛑 No more new posts → stopping scroll")
        break

driver.quit()

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

print("✅ Done!")
