import React from 'react'

/**
 * Compact artwork card for embedding inside a PostCard.
 * Shows thumbnail, title and original author with attribution.
 */
export default function EmbeddedArtworkCard({ artwork }) {
  if (!artwork) return null

  const artUrl    = `/art/${artwork.id}/${slugify(artwork.title)}`
  const authorUrl = `/@${artwork.author.username}`

  return (
    <a
      href={artUrl}
      className="group flex gap-3 rounded-xl border border-white/[0.08] bg-black/30 p-3 hover:border-sky-500/30 transition-colors"
      title={artwork.title}
    >
      {/* Thumbnail */}
      <div className="w-20 h-16 rounded-lg overflow-hidden shrink-0 bg-white/5">
        {artwork.thumb_url ? (
          <img
            src={artwork.thumb_url}
            alt={artwork.title}
            className="w-full h-full object-cover transition-transform duration-300 group-hover:scale-105"
            loading="lazy"
          />
        ) : (
          <div className="w-full h-full flex items-center justify-center text-slate-600">
            <i className="fa-solid fa-image" />
          </div>
        )}
      </div>

      {/* Meta */}
      <div className="flex flex-col justify-center min-w-0">
        <p className="text-sm font-medium text-white/90 truncate">{artwork.title}</p>
        <a
          href={authorUrl}
          onClick={(e) => e.stopPropagation()}
          className="text-xs text-slate-400 hover:text-sky-400 transition-colors mt-0.5 truncate"
        >
          <i className="fa-solid fa-user-circle fa-fw mr-1 opacity-60" />
          by {artwork.author.name || `@${artwork.author.username}`}
        </a>
        <span className="text-[10px] text-slate-600 mt-1 uppercase tracking-wider">Artwork</span>
      </div>
    </a>
  )
}

function slugify(str) {
  return (str || '').toLowerCase().replace(/[^a-z0-9]+/g, '-').replace(/^-|-$/g, '')
}
