/**
 * News Scout Pipeline — Phase 1c: Reddit JSON API
 *
 * Reddit public JSON API provides hot posts with real engagement metrics.
 * 8 sport subreddits, only external link posts (skip self-posts and reddit media).
 * 7s delay between subreddits (~8.5 req/min, under 10/min limit).
 * 24-hour max age filter, engagement = upvotes + (comments * 0.5).
 */

import { RedditSubredditConfig, ScoutCandidate } from './types';

const SUBREDDITS: RedditSubredditConfig[] = [
  { subreddit: 'nba', sport: 'nba', minUpvotes: 500 },
  { subreddit: 'nfl', sport: 'nfl', minUpvotes: 500 },
  { subreddit: 'soccer', sport: 'soccer', minUpvotes: 500 },
  { subreddit: 'hockey', sport: 'nhl', minUpvotes: 300 },
  { subreddit: 'baseball', sport: 'mlb', minUpvotes: 300 },
  { subreddit: 'MMA', sport: 'mma', minUpvotes: 300 },
  { subreddit: 'CollegeBasketball', sport: 'ncaab', minUpvotes: 200 },
  { subreddit: 'CFB', sport: 'ncaaf', minUpvotes: 200 },
];

const USER_AGENT = 'Rainmaker/2.0 (Sports News Scout; contact: admin@rainmakersports.app)';
const MAX_AGE_HOURS = 24;

// Skip reddit-hosted media
const SKIP_DOMAINS = ['v.redd.it', 'i.redd.it', 'i.imgur.com', 'preview.redd.it', 'gallery.redd.it'];

function sleep(ms: number) {
  return new Promise(r => setTimeout(r, ms));
}

async function fetchSubreddit(config: RedditSubredditConfig): Promise<ScoutCandidate[]> {
  const candidates: ScoutCandidate[] = [];
  const maxAgeMs = MAX_AGE_HOURS * 60 * 60 * 1000;
  const now = Date.now();

  try {
    const url = `https://www.reddit.com/r/${config.subreddit}/hot.json?limit=50&raw_json=1`;
    const controller = new AbortController();
    const timeout = setTimeout(() => controller.abort(), 15000);

    const res = await fetch(url, {
      headers: { 'User-Agent': USER_AGENT },
      signal: controller.signal,
    });
    clearTimeout(timeout);

    if (!res.ok) {
      console.error(`  [reddit-scout] r/${config.subreddit}: HTTP ${res.status}`);
      return [];
    }

    const json: any = await res.json();
    const posts = json?.data?.children || [];

    for (const child of posts) {
      const post = child?.data;
      if (!post) continue;

      // Only external link posts
      if (post.is_self) continue;

      const postUrl = (post.url || '').trim();
      if (!postUrl) continue;

      // Skip reddit-hosted media
      try {
        const hostname = new URL(postUrl).hostname;
        if (SKIP_DOMAINS.some(d => hostname.includes(d))) continue;
      } catch {
        continue;
      }

      // Check upvote threshold
      const upvotes = post.ups || 0;
      if (upvotes < config.minUpvotes) continue;

      // Check age
      const createdAt = new Date((post.created_utc || 0) * 1000);
      if (now - createdAt.getTime() > maxAgeMs) continue;

      const title = (post.title || '').trim();
      if (!title) continue;

      const comments = post.num_comments || 0;
      const engagementScore = upvotes + Math.round(comments * 0.5);

      candidates.push({
        guid: `reddit:${post.id}`,
        title,
        url: postUrl,
        source: 'reddit',
        sourceDisplay: `r/${config.subreddit}`,
        sport: config.sport,
        description: title,
        publishedAt: createdAt,
        sourceType: 'reddit',
        engagementScore,
        isCurated: false,
        isBreaking: false,
        isFeatured: false,
      });
    }
  } catch (err: any) {
    console.error(`  [reddit-scout] r/${config.subreddit} failed: ${err.message}`);
  }

  return candidates;
}

export async function runRedditScout(): Promise<ScoutCandidate[]> {
  const candidates: ScoutCandidate[] = [];
  const seenUrls = new Set<string>();

  for (let i = 0; i < SUBREDDITS.length; i++) {
    const config = SUBREDDITS[i];
    console.log(`  [reddit-scout] Fetching r/${config.subreddit}...`);

    const posts = await fetchSubreddit(config);

    // Dedupe by URL across subreddits
    for (const post of posts) {
      if (!seenUrls.has(post.url)) {
        seenUrls.add(post.url);
        candidates.push(post);
      }
    }

    // 7s delay between subreddits
    if (i < SUBREDDITS.length - 1) {
      await sleep(7000);
    }
  }

  console.log(`[reddit-scout] Found ${candidates.length} link posts from ${SUBREDDITS.length} subreddits`);
  return candidates;
}
