import sys
import csv
import time
from playwright.sync_api import sync_playwright

# 📌 Parse command-line arguments
if len(sys.argv) < 2:
    print("❌ Usage: python reddit_playwright.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"

print(f"🔍 Scraping r/{subreddit} from {url}")

# 🌀 Scroll config (identic ca intenție)
scroll_pause = 8
max_scrolls = 100
max_no_growth = 3

scraped = []
seen_urls = set()

no_growth = 0
last_count = 0


with sync_playwright() as p:
    # ✅ Persistent context (profil real)
    browser = p.chromium.launch_persistent_context(
        user_data_dir="playwright_reddit_profile",
        headless=False,
        viewport={"width": 1920, "height": 1080},
        user_agent="Mozilla/5.0"
    )

    page = browser.new_page()
    page.goto(url, timeout=60000)
    time.sleep(5)

    # 🔁 Infinite scroll loop
    for scroll in range(max_scrolls):
        print(f"🔽 Scroll {scroll+1}/{max_scrolls}")

        page.evaluate(
            "window.scrollTo(0, document.body.scrollHeight);"
        )
        time.sleep(scroll_pause)

        posts = page.query_selector_all("article")

        for post in posts:
            try:
                title_elem = post.query_selector("a[id^='post-title']")
                if not title_elem:
                    continue

                post_url = title_elem.get_attribute("href")
                title = title_elem.inner_text().strip()

                if post_url in seen_urls:
                    continue

                body_elem = post.query_selector("div[data-adclicklocation='media']")
                body = body_elem.inner_text().strip() if body_elem else ""

                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

    browser.close()

# 💾 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!")
