import csv
import time
import sys
import os
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options

# ✅ Input
if len(sys.argv) < 2:
    print("Usage: python scrape_reddit_post_details.py <input_csv_file>")
    sys.exit(1)

input_csv = sys.argv[1]
subreddit = input_csv.split("_")[1]
comments_csv = f"r_{subreddit}_comments.csv"

# ✅ Load posts
with open(input_csv, "r", encoding="utf-8") as f:
    reader = csv.DictReader(f)
    posts = list(reader)

# ✅ Setup browser
options = Options()
options.add_argument("--headless")
options.add_argument("--disable-gpu")
options.add_argument("--no-sandbox")
options.add_argument("--window-size=1920x1080")
options.add_argument("user-agent=Mozilla/5.0")
driver = webdriver.Chrome(options=options)

# ✅ Resumable tracking helpers
def save_posts(posts, file):
    with open(file, "w", newline="", encoding="utf-8") as f:
        writer = csv.DictWriter(f, fieldnames=list(posts[0].keys()))
        writer.writeheader()
        writer.writerows(posts)

def append_comments(comments, file):
    file_exists = os.path.isfile(file)
    with open(file, "a", newline="", encoding="utf-8") as f:
        writer = csv.DictWriter(f, fieldnames=["post_url", "author", "comment"])
        if not file_exists:
            writer.writeheader()
        writer.writerows(comments)

# 🔁 Scrape loop
for idx, post in enumerate(posts):
    url = post["url"]

    if post.get("scraped_comments") == "True":
        print(f"[{idx+1}/{len(posts)}] Skipping (already scraped): {url}")
        continue

    print(f"[{idx+1}/{len(posts)}] Scraping comments from: {url}")
    driver.get(url)
    time.sleep(4)

    comments = []
    try:
        comment_blocks = driver.find_elements(By.CSS_SELECTOR, "shreddit-comment")
        for block in comment_blocks:
            try:
                author = block.get_attribute("author") or "unknown"
                text = block.text.strip()
                if text:
                    comments.append({
                        "post_url": url,
                        "author": author,
                        "comment": text
                    })
            except:
                continue
    except Exception as e:
        print(f"  ⚠️ Error loading comments: {e}")
        continue

    # ✅ Save comments immediately
    append_comments(comments, comments_csv)
    print(f"  💾 Saved {len(comments)} comments")

    # ✅ Update post to mark as done
    post["scraped_comments"] = "True"
    save_posts(posts, input_csv)
    time.sleep(2)

driver.quit()

print("\n✅ DONE: All available comments scraped.")
