import React, { useEffect, useMemo, useRef } from 'react'
import { Card } from 'flowbite-react'
import { FiBell } from 'react-icons/fi'

import type { Notification } from '../types'
import { default as NotificationItem } from './NotificationItem'

interface NotificationListProps {
  notifications: Notification[]
  hasMore?: boolean
  isLoadingMore?: boolean
  isLoadingInitial?: boolean
  onLoadMore?: () => void
  onMarkAsRead: (id: number) => Promise<void>
  onDeleteNotification: (id: number) => Promise<void>
}

export default function NotificationList({
  notifications,
  hasMore = false,
  isLoadingMore = false,
  isLoadingInitial = false,
  onLoadMore,
  onMarkAsRead,
  onDeleteNotification,
}: NotificationListProps) {
  const sentinelRef = useRef<HTMLDivElement | null>(null)

  useEffect(() => {
    if (!hasMore) return
    if (!sentinelRef.current) return

    const el = sentinelRef.current
    const io = new IntersectionObserver(
      (entries) => {
        const first = entries[0]

        if (first.isIntersecting && !isLoadingMore) {
          onLoadMore?.()
        }
      },
      { rootMargin: '200px 0px' },
    )

    io.observe(el)
    return () => io.unobserve(el)
  }, [hasMore, isLoadingMore, onLoadMore])

  const skeletonItems = useMemo(() => Array.from({ length: 5 }), [])

  if (isLoadingInitial) {
    return (
      <div className="space-y-4">
        {skeletonItems.map((_, index) => (
          <Card key={index} className="p-4 animate-pulse shadow-none">
            <div className="flex items-start gap-4">
              <div className="w-10 h-10 bg-gray-200 rounded-full"></div>
              <div className="flex-1 space-y-2">
                <div className="h-4 bg-gray-200 rounded w-3/4"></div>
                <div className="h-3 bg-gray-200 rounded w-1/2"></div>
                <div className="h-3 bg-gray-200 rounded w-1/4"></div>
              </div>
            </div>
          </Card>
        ))}
      </div>
    )
  }

  if (notifications.length === 0) {
    return (
      <Card className="p-12 text-center shadow-none">
        <div className="flex flex-col items-center gap-4">
          <div className="w-16 h-16 bg-gray-100 rounded-full flex items-center justify-center">
            <FiBell className="w-8 h-8 text-gray-400" />
          </div>
          <div>
            <h3 className="text-lg font-medium text-gray-900 mb-1">
              No notifications
            </h3>
            <p className="text-gray-500">
              You&apos;re all caught up! No new notifications to show.
            </p>
          </div>
        </div>
      </Card>
    )
  }

  return (
    <React.Fragment>
      <div className="space-y-3">
        {notifications.map((notification) => (
          <NotificationItem
            key={notification.id}
            notification={notification}
            onMarkAsRead={onMarkAsRead}
            onDeleteNotification={onDeleteNotification}
          />
        ))}

        {isLoadingMore && (
          <div className="space-y-3">
            {Array.from({ length: 3 }).map((_, index) => (
              <Card key={index} className="p-4 animate-pulse shadow-none">
                <div className="flex items-start gap-4">
                  <div className="w-10 h-10 bg-gray-200 rounded-full"></div>
                  <div className="flex-1 space-y-2">
                    <div className="h-4 bg-gray-200 rounded w-3/4"></div>
                    <div className="h-3 bg-gray-200 rounded w-1/2"></div>
                    <div className="h-3 bg-gray-200 rounded w-1/4"></div>
                  </div>
                </div>
              </Card>
            ))}
          </div>
        )}
      </div>

      {/* Infinite scroll sentinel and loader */}
      <div className="mt-4 flex flex-col items-center">
        <div ref={sentinelRef} className="h-1 w-full" />
        {!hasMore && notifications.length > 0 && (
          <p className="text-xs text-slate-400 mt-2">No more results</p>
        )}
      </div>
    </React.Fragment>
  )
}
