import React, { useEffect, useState } from 'react'
import LevelBadge from '../../components/xp/LevelBadge'

export default function TopCreatorsWidget() {
  const [data, setData] = useState({ items: [] })
  const [loading, setLoading] = useState(true)

  useEffect(() => {
    let cancelled = false

    async function load() {
      try {
        const response = await window.axios.get('/api/leaderboard/creators?period=weekly')
        if (!cancelled && response.data) {
          setData(response.data)
        }
      } finally {
        if (!cancelled) {
          setLoading(false)
        }
      }
    }

    load()

    return () => {
      cancelled = true
    }
  }, [])

  const items = Array.isArray(data?.items) ? data.items.slice(0, 5) : []

  return (
    <section className="rounded-[28px] border border-white/10 bg-[linear-gradient(180deg,rgba(15,23,42,0.92),rgba(15,23,42,0.82))] p-5 shadow-[0_24px_90px_rgba(2,8,23,0.32)]">
      <div className="flex flex-col gap-4 sm:flex-row sm:items-center sm:justify-between">
        <div>
          <p className="text-[11px] uppercase tracking-[0.24em] text-sky-200/70">Leaderboard</p>
          <h2 className="mt-2 text-xl font-semibold text-white">Top Creators</h2>
          <p className="mt-2 max-w-xs text-sm leading-6 text-slate-300">A quick weekly pulse on who is gaining the most traction across the platform.</p>
        </div>
        <a href="/leaderboard?type=creators&period=weekly" className="inline-flex items-center justify-center gap-2 rounded-full border border-sky-400/25 bg-sky-400/10 px-3 py-1.5 text-xs font-semibold uppercase tracking-wide text-sky-100 transition hover:border-sky-300/35 hover:bg-sky-400/15 sm:justify-start">
          View all
          <i className="fa-solid fa-arrow-right text-[10px]" />
        </a>
      </div>

      {loading ? <p className="mt-4 text-sm text-slate-400">Loading leaderboard...</p> : null}

      {!loading && items.length === 0 ? (
        <div className="mt-4 rounded-2xl border border-white/8 bg-white/[0.04] px-4 py-5 text-sm text-slate-300">
          <p className="font-medium text-white">No creators ranked yet.</p>
          <p className="mt-2 text-slate-400">Rankings will appear here once weekly creator scoring is available.</p>
        </div>
      ) : null}

      {!loading && items.length > 0 ? (
        <div className="mt-4 space-y-3">
          {items.map((item) => {
            const entity = item.entity || {}

            return (
              <a key={item.rank} href={entity.url || '#'} className="flex items-center gap-3 rounded-2xl border border-white/8 bg-white/[0.04] p-4 transition hover:-translate-y-0.5 hover:border-white/15 hover:bg-white/[0.06]">
                <div className="flex h-10 w-10 shrink-0 items-center justify-center rounded-xl border border-sky-300/20 bg-sky-400/12 text-sm font-black text-white">
                  #{item.rank}
                </div>
                {entity.avatar ? <img src={entity.avatar} alt={entity.name || 'Creator'} className="h-11 w-11 rounded-xl border border-white/10 object-cover" loading="lazy" /> : null}
                <div className="min-w-0 flex-1">
                  <p className="truncate text-sm font-semibold text-white">{entity.name}</p>
                  <div className="mt-1 flex items-center gap-2">
                    <LevelBadge level={entity.level} rank={entity.rank} compact />
                  </div>
                </div>
                <div className="shrink-0 text-right">
                  <span className="block text-sm font-semibold text-sky-300">{Math.round(item.score)}</span>
                  <span className="mt-1 block text-[11px] uppercase tracking-[0.16em] text-slate-400">Weekly score</span>
                </div>
              </a>
            )
          })}
        </div>
      ) : null}
    </section>
  )
}
