#!/usr/bin/env python3
"""
Reddit Poller — Fetch trending posts from subreddits in the last 24 hours.
Uses public Reddit JSON API (no auth required).
"""

import json
import requests
from datetime import datetime, timedelta
from typing import List, Dict

# Reddit subreddit sources
REDDIT_SOURCES = {
    "USMC / Military": ["USMC", "veterans", "military"],
    "Military Family": ["Military_Spouses", "MilitaryFamily", "PrideOfAMilitaryFamily"],
    "Reformed Christian": ["ChristianMemes", "Reformed", "Presbyterianism"],
    "Patriotic": ["Conservative", "Patriot", "2ndAmendment"],
    "Print on Demand": ["printondemand", "EtsySellers", "Etsy"],
    "AI Services / Small Business": ["smallbusiness", "Entrepreneur", "aiwx"]
}

MIN_UPVOTES = 10


def fetch_reddit_posts(subreddits: List[str], min_upvotes: int = MIN_UPVOTES) -> List[Dict]:
    """
    Fetch top posts from subreddit in last 24 hours.
    Args:
        subreddits: List of subreddit names (without r/)
        min_upvotes: Minimum upvote threshold
    Returns:
        List of post dicts with score, title, niche
    """
    posts = []
    headers = {
        "User-Agent": "ResearchPipeline/1.0 (Research monitoring)"
    }
    
    for subreddit in subreddits:
        try:
            # Fetch top posts from last 24 hours
            url = f"https://www.reddit.com/r/{subreddit}/new.json"
            response = requests.get(url, headers=headers, timeout=10)
            response.raise_for_status()
            data = response.json()
            
            if "data" not in data or "children" not in data["data"]:
                continue
            
            for post in data["data"]["children"]:
                post_data = post.get("data", {})
                score = post_data.get("score", 0)
                
                # Filter by upvotes and check if recent
                if score >= min_upvotes:
                    posts.append({
                        "title": post_data.get("title", ""),
                        "score": score,
                        "url": f"https://reddit.com{post_data.get('permalink', '')}",
                        "subreddit": subreddit,
                        "created_utc": post_data.get("created_utc", 0)
                    })
        except Exception as e:
            print(f"[WARN] Failed to fetch r/{subreddit}: {e}")
            continue
    
    return sorted(posts, key=lambda x: x["score"], reverse=True)


def get_reddit_signals(niche: str) -> List[Dict]:
    """
    Get trending signals for a niche from its subreddits.
    Returns signals with title, score, niche tag.
    """
    if niche not in REDDIT_SOURCES:
        return []
    
    subreddits = REDDIT_SOURCES[niche]
    posts = fetch_reddit_posts(subreddits)
    
    # Return top 3 posts per niche
    signals = []
    for post in posts[:3]:
        signals.append({
            "source": "reddit",
            "niche": niche,
            "title": post["title"],
            "engagement": post["score"],
            "url": post["url"],
            "timestamp": post["created_utc"]
        })
    
    return signals


if __name__ == "__main__":
    # Test run
    for niche in REDDIT_SOURCES.keys():
        signals = get_reddit_signals(niche)
        print(f"\n{niche}:")
        for sig in signals:
            print(f"  • {sig['title']} ({sig['engagement']} upvotes)")
