/**
 * Watched-source sync (Pinchflat-style). Re-indexes
 * every watched Source and reports the videos that are new since its last index.
 * A YouTube RSS feed is used as a cheap pre-check so a source with no new uploads
 * is skipped before the (more expensive) full `yt-dlp` re-index.
 */

import { listSources, listMediaItems } from './sources'
import { indexSource } from './indexer'
import { parseRssVideoIds, isYouTubeFeedUrl } from './core/indexerCore'
import { FEED_FETCH_TIMEOUT_MS } from './constants'
import type { IndexProgress, MediaItem, SyncResult } from '@shared/ipc'

/** Fetch a YouTube RSS feed and return its recent video ids. Throws on failure. */
async function fetchFeedIds(feedUrl: string): Promise<string[]> {
  // Only ever fetch a genuine YouTube feed — refuse an arbitrary host that a
  // corrupted sources.json might carry (SSRF guard, audit T7). A throw here is
  // caught by the caller, which then falls back to a full yt-dlp re-index.
  if (!isYouTubeFeedUrl(feedUrl)) throw new Error('Refusing to fetch a non-YouTube feed URL.')
  const res = await fetch(feedUrl, { signal: AbortSignal.timeout(FEED_FETCH_TIMEOUT_MS) })
  if (!res.ok) throw new Error(`feed responded ${res.status}`)
  return parseRssVideoIds(await res.text())
}

/**
 * Re-index every watched source and collect the videos new since the last index.
 * RSS pre-check: if the feed shows only ids the source already knows, the full
 * re-index is skipped. `onProgress` is forwarded from the underlying indexSource.
 */
export async function syncWatchedSources(
  onProgress: (p: IndexProgress) => void
): Promise<SyncResult> {
  try {
    const watched = listSources().filter((s) => s.watched)
    const newItems: MediaItem[] = []
    for (const src of watched) {
      // Cheap freshness check — skip the full re-index when nothing is new.
      if (src.feedUrl) {
        const known = new Set(listMediaItems(src.id).map((m) => m.videoId))
        const recent = await fetchFeedIds(src.feedUrl).catch(() => null)
        if (recent && recent.length > 0 && recent.every((id) => known.has(id))) continue
      }
      const before = new Set(listMediaItems(src.id).map((m) => m.videoId))
      const res = await indexSource(onProgress, src.url)
      if (res.ok) {
        for (const it of listMediaItems(src.id)) {
          if (!before.has(it.videoId)) newItems.push(it)
        }
      }
    }
    return { ok: true, newItems }
  } catch (e) {
    return { ok: false, newItems: [], error: e instanceof Error ? e.message : String(e) }
  }
}
