import ContentRow from '../ui/ContentRow'
import { mapTmdbToJf } from '../../lib/tmdb-mapping'

interface Props {
  recommendations: any[]
  similar: any[]
  libraryMap?: Map<string, { id: string; name: string; type: string }>
}

export default function DiscoveryRows({ recommendations, similar, libraryMap }: Props) {
  // Combined pool for "in library" - dedup by tmdb id
  const seen = new Set<string>()
  const pool = [...recommendations, ...similar].filter(m => {
    if (seen.has(String(m.id))) return false
    seen.add(String(m.id))
    return true
  })
  const inLibrary = libraryMap
    ? pool.filter(m => libraryMap.has(String(m.id)))
    : []

  return (
    <>
      {inLibrary.length > 0 && (
        <div className="mt-8">
          <ContentRow
            title="In your library"
            subtitle="Related titles you already own"
            items={mapTmdbToJf(inLibrary, libraryMap)}
          />
        </div>
      )}
      {recommendations.length > 0 && (
        <div className="mt-2">
          <ContentRow
            title="You might like"
            subtitle="Recommendations from TMDB"
            items={mapTmdbToJf(recommendations, libraryMap)}
          />
        </div>
      )}
      {similar.length > 0 && (
        <div className="mt-2">
          <ContentRow
            title="More like this"
            subtitle="Algorithmically similar"
            items={mapTmdbToJf(similar, libraryMap)}
          />
        </div>
      )}
    </>
  )
}
