import type { Article } from '@/types/nostr'
import { ArticleCard } from './ArticleCard'
import { t } from '@/lib/i18n'

interface ArticlesListProps {
  articles: Article[]
  allArticles: Article[]
  loading: boolean
  error: string | null
  onUnlock: (article: Article) => void
  unlockedArticles: Set<string>
}

function LoadingState(): React.ReactElement {
  // Use generic loading message at startup, then specific message once we know what we're loading
  return (
    <div className="text-center py-12">
      <p className="text-cyber-accent/70">{t('common.loading.articles')}</p>
    </div>
  )
}

function ErrorState({ message }: { message: string }): React.ReactElement {
  return (
    <div className="bg-red-900/20 border border-red-500/50 rounded-lg p-4 mb-4">
      <p className="text-red-400">{message}</p>
    </div>
  )
}

function EmptyState({ hasAny }: { hasAny: boolean }): React.ReactElement {
  return (
    <div className="text-center py-12">
      <p className="text-cyber-accent/70">
        {hasAny ? t('common.empty.articles.filtered') : t('common.empty.articles')}
      </p>
    </div>
  )
}

export function ArticlesList({
  articles,
  allArticles,
  loading,
  error,
  onUnlock,
  unlockedArticles,
}: ArticlesListProps): React.ReactElement {
  if (loading) {
    return <LoadingState />
  }
  if (error) {
    return <ErrorState message={error} />
  }
  if (articles.length === 0) {
    return <EmptyState hasAny={allArticles.length > 0} />
  }

  return (
    <>
      <div className="mb-4 text-sm text-cyber-accent/70">
        Showing {articles.length} of {allArticles.length} article{allArticles.length !== 1 ? 's' : ''}
      </div>
      <div className="space-y-6">
        {articles.map((article) => (
          <ArticleCard
            key={article.id}
            article={{ ...article, paid: unlockedArticles.has(article.id) || article.paid }}
            onUnlock={onUnlock}
          />
        ))}
      </div>
    </>
  )
}
