import type { Article } from '@/types/nostr'
import { AuthorCard } from './AuthorCard'
import { ErrorState, EmptyState, Skeleton } from './ui'
import { t } from '@/lib/i18n'

interface AuthorsListProps {
  authors: Article[]
  allAuthors: Article[]
  loading: boolean
  error: string | null
}

function AuthorCardSkeleton(): React.ReactElement {
  return (
    <div className="border border-neon-cyan/30 rounded-lg p-6 bg-cyber-dark space-y-4">
      <div className="flex items-center gap-4">
        <Skeleton variant="circular" width={64} height={64} />
        <div className="flex-1 space-y-2">
          <Skeleton variant="rectangular" height={20} className="w-2/3" />
          <Skeleton variant="rectangular" height={16} className="w-1/2" />
        </div>
      </div>
      <Skeleton variant="rectangular" height={60} className="w-full" />
    </div>
  )
}

function LoadingState(): React.ReactElement {
  return (
    <div className="grid grid-cols-1 md:grid-cols-2 gap-6">
      {Array.from({ length: 4 }).map((_, index) => (
        <AuthorCardSkeleton key={index} />
      ))}
    </div>
  )
}

function AuthorsEmptyState({ hasAny }: { hasAny: boolean }): React.ReactElement {
  return (
    <EmptyState
      title={hasAny ? t('common.empty.authors.filtered') : t('common.empty.authors')}
    />
  )
}

export function AuthorsList({ authors, allAuthors, loading, error }: AuthorsListProps): React.ReactElement {
  if (loading) {
    return <LoadingState />
  }
  if (error) {
    return <ErrorState message={error} />
  }
  if (authors.length === 0) {
    return <AuthorsEmptyState hasAny={allAuthors.length > 0} />
  }

  return (
    <>
      <div className="mb-4 text-sm text-cyber-accent/70">
        Showing {authors.length} of {allAuthors.length} author{allAuthors.length !== 1 ? 's' : ''}
      </div>
      <div className="grid grid-cols-1 md:grid-cols-2 gap-6">
        {authors.map((author) => (
          <AuthorCard key={author.pubkey} presentation={author} />
        ))}
      </div>
    </>
  )
}
