/* global React */
// ---------------------------------------------------------------
// STERLING — Imagery primitives
// Frames, captions, and stylized placeholders for hero/proof shots
// ---------------------------------------------------------------

const { useRef, useState, useEffect, useCallback } = React;

// Real Unsplash photos load grayscale with a subtle contrast filter.
// If they fail, the underlying placeholder still reads beautifully.
function Frame({ children, caption, className = "", style = {} }) {
  return (
    <figure className={`frame ${className}`} style={style}>
      {children}
      {caption ? <figcaption className="frame-caption">{caption}</figcaption> : null}
    </figure>
  );
}

// Stylized "before" — dark, moody, with subtle streaks suggesting residue
function PlaceholderBefore() {
  return (
    <div className="placeholder placeholder-dark placeholder-grid" aria-hidden="true">
      {/* drips / streaks via SVG, very restrained */}
      <svg className="absolute inset-0 w-full h-full" viewBox="0 0 800 600" preserveAspectRatio="none">
        <defs>
          <radialGradient id="grime" cx="50%" cy="20%" r="80%">
            <stop offset="0%" stopColor="#3a3b42" stopOpacity="0.45" />
            <stop offset="100%" stopColor="#0c0d11" stopOpacity="0" />
          </radialGradient>
          <linearGradient id="streak" x1="0" y1="0" x2="0" y2="1">
            <stop offset="0%" stopColor="#0a0a0d" stopOpacity="0.0" />
            <stop offset="100%" stopColor="#0a0a0d" stopOpacity="0.55" />
          </linearGradient>
        </defs>
        <rect width="800" height="600" fill="url(#grime)" />
        {/* "drips" — vertical streaks */}
        {Array.from({ length: 14 }).map((_, i) => {
          const x = 30 + i * 55 + (i % 3) * 8;
          const h = 180 + ((i * 37) % 220);
          const w = 1 + (i % 3);
          return <rect key={i} x={x} y={0} width={w} height={h} fill="url(#streak)" opacity={0.55} />;
        })}
        {/* irregular grime patches */}
        <ellipse cx="220" cy="380" rx="160" ry="40" fill="#0a0a0d" opacity="0.35" />
        <ellipse cx="560" cy="460" rx="200" ry="50" fill="#0a0a0d" opacity="0.30" />
        <ellipse cx="380" cy="520" rx="240" ry="36" fill="#0a0a0d" opacity="0.25" />
      </svg>
    </div>
  );
}

// Stylized "after" — bright, clean, with soft water glints
function PlaceholderAfter() {
  return (
    <div className="placeholder placeholder-light" aria-hidden="true">
      <svg className="absolute inset-0 w-full h-full" viewBox="0 0 800 600" preserveAspectRatio="none">
        <defs>
          <radialGradient id="sheen" cx="35%" cy="20%" r="70%">
            <stop offset="0%" stopColor="#ffffff" stopOpacity="0.7" />
            <stop offset="100%" stopColor="#ffffff" stopOpacity="0" />
          </radialGradient>
          <linearGradient id="floor" x1="0" y1="0" x2="0" y2="1">
            <stop offset="0%" stopColor="#ECE9E3" stopOpacity="0" />
            <stop offset="100%" stopColor="#C7C3BA" stopOpacity="0.5" />
          </linearGradient>
        </defs>
        <rect width="800" height="600" fill="url(#sheen)" />
        <rect width="800" height="600" fill="url(#floor)" />
        {/* faint highlights */}
        <ellipse cx="640" cy="120" rx="220" ry="40" fill="#ffffff" opacity="0.35" />
        <ellipse cx="180" cy="430" rx="120" ry="14" fill="#ffffff" opacity="0.25" />
      </svg>
    </div>
  );
}

// Hero side image — placeholder of a Charlotte residential exterior.
// Uses an Unsplash architectural shot, grayscale.
function HeroSidePlaceholder() {
  // Composition: house silhouette + driveway, drawn in restrained SVG so it
  // reads as "tasteful editorial placeholder" rather than clipart.
  return (
    <div className="placeholder" aria-hidden="true" style={{ background: "#1a1b21" }}>
      <svg className="absolute inset-0 w-full h-full" viewBox="0 0 800 1000" preserveAspectRatio="xMidYMid slice">
        <defs>
          <linearGradient id="skyGrad" x1="0" y1="0" x2="0" y2="1">
            <stop offset="0%" stopColor="#2a2c33" />
            <stop offset="60%" stopColor="#1c1d23" />
            <stop offset="100%" stopColor="#121318" />
          </linearGradient>
          <linearGradient id="drive" x1="0" y1="0" x2="0" y2="1">
            <stop offset="0%" stopColor="#22232a" />
            <stop offset="100%" stopColor="#0c0d11" />
          </linearGradient>
          <linearGradient id="house" x1="0" y1="0" x2="0" y2="1">
            <stop offset="0%" stopColor="#3a3c44" />
            <stop offset="100%" stopColor="#22242b" />
          </linearGradient>
        </defs>
        <rect width="800" height="1000" fill="url(#skyGrad)" />
        {/* foliage hint */}
        <ellipse cx="120" cy="540" rx="220" ry="120" fill="#171820" opacity="0.7" />
        <ellipse cx="700" cy="500" rx="220" ry="110" fill="#171820" opacity="0.7" />
        {/* house silhouette */}
        <polygon points="200,560 400,420 600,560 600,760 200,760" fill="url(#house)" />
        <polygon points="200,560 400,420 600,560 580,560 400,440 220,560" fill="#0d0e13" opacity="0.6" />
        {/* windows */}
        <rect x="270" y="600" width="60" height="90" fill="#cfd2d8" opacity="0.18" />
        <rect x="370" y="600" width="60" height="90" fill="#cfd2d8" opacity="0.22" />
        <rect x="470" y="600" width="60" height="90" fill="#cfd2d8" opacity="0.18" />
        {/* driveway */}
        <polygon points="0,760 800,760 800,1000 0,1000" fill="url(#drive)" />
        {/* two bins in foreground (silhouette only — no clipart detail) */}
        <g opacity="0.95">
          <rect x="320" y="700" width="60" height="90" rx="3" fill="#0a0a0d" />
          <rect x="320" y="700" width="60" height="6" rx="2" fill="#1a1b21" />
          <rect x="410" y="700" width="60" height="90" rx="3" fill="#0a0a0d" />
          <rect x="410" y="700" width="60" height="6" rx="2" fill="#1a1b21" />
        </g>
        {/* faint light glow on driveway */}
        <ellipse cx="400" cy="820" rx="260" ry="40" fill="#ffffff" opacity="0.05" />
      </svg>
    </div>
  );
}

// Editorial portrait-orientation placeholder (used for proof secondary frames)
function MoodFrame({ tone = "dark" }) {
  return (
    <div className={`placeholder ${tone === "dark" ? "placeholder-warm" : "placeholder-light"}`} aria-hidden="true">
      <svg className="absolute inset-0 w-full h-full" viewBox="0 0 600 800" preserveAspectRatio="xMidYMid slice">
        {tone === "dark" ? (
          <g>
            <rect width="600" height="800" fill="#1a1b21" />
            <ellipse cx="300" cy="180" rx="280" ry="120" fill="#2c2e36" opacity="0.6" />
            <rect x="220" y="320" width="160" height="380" rx="4" fill="#0c0d11" />
            <rect x="220" y="320" width="160" height="10" fill="#23252c" />
          </g>
        ) : (
          <g>
            <rect width="600" height="800" fill="#ECE9E3" />
            <ellipse cx="300" cy="200" rx="320" ry="140" fill="#fbfaf7" opacity="0.7" />
            <rect x="220" y="340" width="160" height="380" rx="4" fill="#D9D6CF" />
            <rect x="220" y="340" width="160" height="10" fill="#C7C3BA" />
          </g>
        )}
      </svg>
    </div>
  );
}

window.Frame = Frame;
window.PlaceholderBefore = PlaceholderBefore;
window.PlaceholderAfter = PlaceholderAfter;
window.HeroSidePlaceholder = HeroSidePlaceholder;
window.MoodFrame = MoodFrame;
