// Book cover — uses real image URL when available, falls back to a generated
// gradient placeholder with serif typography.

function Cover({ book, size = "md", onClick }) {
  const sizes = {
    sm: { w: 64, h: 96, title: 9, author: 7 },
    md: { w: 124, h: 186, title: 14, author: 9 },
    lg: { w: 180, h: 270, title: 20, author: 11 },
    xl: { w: 280, h: 420, title: 32, author: 14 },
  };
  const s = sizes[size] || sizes.md;
  const [imgError, setImgError] = React.useState(false);
  const hasImage = book.cover && !imgError;

  const wrapStyle = {
    width: s.w,
    height: s.h,
    position: "relative",
    cursor: onClick ? "pointer" : "inherit",
    boxShadow: "0 2px 8px rgba(40, 28, 14, 0.25), inset 0 0 0 1px rgba(255,255,255,0.06), inset 2px 0 6px rgba(0,0,0,0.18)",
    overflow: "hidden",
    flexShrink: 0,
    borderRadius: "2px",
    transition: "transform 180ms ease, box-shadow 180ms ease",
    background: "#2a1f14",
  };

  if (hasImage) {
    return (
      <div
        className="book-cover"
        style={wrapStyle}
        onClick={onClick}
        onMouseEnter={(e) => { if (onClick) { e.currentTarget.style.transform = "translateY(-2px)"; e.currentTarget.style.boxShadow = "0 6px 14px rgba(40, 28, 14, 0.35), inset 0 0 0 1px rgba(255,255,255,0.06), inset 2px 0 6px rgba(0,0,0,0.18)"; } }}
        onMouseLeave={(e) => { if (onClick) { e.currentTarget.style.transform = "translateY(0)"; e.currentTarget.style.boxShadow = wrapStyle.boxShadow; } }}
      >
        <img
          src={book.cover}
          alt={book.title}
          loading="lazy"
          referrerPolicy="no-referrer"
          onError={() => setImgError(true)}
          style={{
            width: "100%",
            height: "100%",
            objectFit: "cover",
            display: "block",
          }}
        />
      </div>
    );
  }

  // Fallback: generated cover
  const [c1, c2, c3] = book.palette || ["#8a6b3a", "#2a1808", "#e8c878"];
  const variant = (book.id.charCodeAt(book.id.length - 1) || 0) % 4;

  const baseStyle = {
    ...wrapStyle,
    background: `linear-gradient(140deg, ${c1} 0%, ${c2} 100%)`,
    color: c3,
    fontFamily: "'Cormorant Garamond', Georgia, serif",
  };

  const deco = (() => {
    if (variant === 0) {
      return (
        <React.Fragment>
          <div style={{ position: "absolute", top: "12%", left: "10%", right: "10%", height: 1, background: c3, opacity: 0.6 }}></div>
          <div style={{ position: "absolute", bottom: "12%", left: "10%", right: "10%", height: 1, background: c3, opacity: 0.6 }}></div>
        </React.Fragment>
      );
    }
    if (variant === 1) {
      return (
        <div style={{ position: "absolute", top: "50%", left: "50%", transform: "translate(-50%,-50%)", fontSize: s.w * 0.6, color: c3, opacity: 0.07, fontFamily: "'Cormorant Garamond', serif", fontWeight: 600 }}>
          {book.title[0]}
        </div>
      );
    }
    if (variant === 2) {
      return (
        <div style={{ position: "absolute", top: 6, left: 6, right: 6, bottom: 6, border: `1px solid ${c3}`, opacity: 0.4, pointerEvents: "none" }}></div>
      );
    }
    return (
      <div style={{ position: "absolute", top: "42%", bottom: "42%", left: 0, right: 0, background: c3, opacity: 0.1 }}></div>
    );
  })();

  return (
    <div
      className="book-cover"
      style={baseStyle}
      onClick={onClick}
      onMouseEnter={(e) => { if (onClick) { e.currentTarget.style.transform = "translateY(-2px)"; e.currentTarget.style.boxShadow = "0 6px 14px rgba(40, 28, 14, 0.35), inset 0 0 0 1px rgba(255,255,255,0.06), inset 2px 0 6px rgba(0,0,0,0.18)"; } }}
      onMouseLeave={(e) => { if (onClick) { e.currentTarget.style.transform = "translateY(0)"; e.currentTarget.style.boxShadow = baseStyle.boxShadow; } }}
    >
      {deco}
      <div style={{
        position: "absolute",
        top: "16%",
        left: "10%",
        right: "10%",
        bottom: "16%",
        display: "flex",
        flexDirection: "column",
        justifyContent: "space-between",
        textAlign: "center",
      }}>
        <div style={{
          fontSize: s.title,
          lineHeight: 1.05,
          fontWeight: 600,
          letterSpacing: "0.01em",
          textWrap: "balance",
          fontVariant: "small-caps",
        }}>
          {book.title}
        </div>
        <div style={{
          fontSize: s.author,
          letterSpacing: "0.18em",
          textTransform: "uppercase",
          opacity: 0.85,
          fontFamily: "'JetBrains Mono', monospace",
          fontWeight: 400,
        }}>
          {(book.author || "").split(" ").pop()}
        </div>
      </div>
    </div>
  );
}

window.Cover = Cover;
