"use client"

import { useState } from "react"
import { Palette } from "lucide-react"
import { cn } from "@/lib/utils"

interface SafeImageProps {
  src?: string | null
  className?: string
  /** Empty for decorative images (the default: titles sit next to them). */
  alt?: string
}

/** A cover, avatar or photo that fills its (relative) box. */
export default function SafeImage({ src, className = "", alt = "" }: SafeImageProps) {
  const [error, setError] = useState(false)

  if (!src || error) {
    return (
      <div
        aria-hidden="true"
        className={cn("absolute inset-0 grid place-items-center bg-gradient-to-br from-paper-2 via-ochre-soft/70 to-sienna-soft", className)}
      >
        <Palette className="w-[26%] max-w-12 min-w-5 aspect-square h-auto text-sienna/35" strokeWidth={1.6} />
      </div>
    )
  }

  return (
    <img
      src={src}
      alt={alt}
      loading="lazy"
      decoding="async"
      className={cn("absolute inset-0 w-full h-full object-cover", className)}
      onError={() => setError(true)}
    />
  )
}
