// ── Sawyer's little people (Jul 26) ──
// "I wanna make it so the dots look like people."
// One tiny SVG footballer, used on every pitch in the game. Same team colors
// as the old dots so you can still tell instantly who is who.
(function () {
  // Every player gets their own skin + hair, picked from their card id so it
  // never changes between matches.
  const SKINS = ['#f6cfa6', '#ecb684', '#d59a63', '#b87a45', '#8f5730', '#6b3f22'];
  const HAIRS = ['#1c1512', '#33231a', '#54341c', '#8a5a2b', '#c99a4a', '#0f0f10', '#7a4a24'];

  function hashOf(seed) {
    const s = String(seed);
    let h = 0;
    for (let i = 0; i < s.length; i++) h = (h * 31 + s.charCodeAt(i)) | 0;
    return Math.abs(h);
  }

  // Darken a #rrggbb by a factor — used for shorts + socks so they match the kit.
  function shade(hex, f) {
    const m = /^#?([0-9a-f]{6})$/i.exec(String(hex).trim());
    if (!m) return hex;
    const n = parseInt(m[1], 16);
    const r = Math.round(((n >> 16) & 255) * f);
    const g = Math.round(((n >> 8) & 255) * f);
    const b = Math.round((n & 255) * f);
    return `rgb(${r},${g},${b})`;
  }

  window.SoccerPerson = function SoccerPerson({
    color, seed, height = 28, moving = false, lean = 0, hasBall = false, isGK = false, dim = false,
  }) {
    const h = hashOf(seed);
    const skin = SKINS[h % SKINS.length];
    const hair = HAIRS[(h >> 3) % HAIRS.length];
    const kit = isGK ? shade(color, 0.62) : color;
    const shorts = shade(color, 0.42);
    const socks = isGK ? '#e9eef2' : shade(color, 0.72);
    const ink = 'rgba(0,0,0,0.55)'; // thin outline so they pop off the grass

    const RUN = 0.4; // seconds per stride
    const swing = (name) => ({
      transformBox: 'fill-box',
      transformOrigin: '50% 0%',
      animation: moving ? `${name} ${RUN}s ease-in-out infinite` : 'none',
    });

    return (
      <svg
        viewBox="0 0 22 34"
        width={height * (22 / 34)}
        height={height}
        style={{
          overflow: 'visible',
          display: 'block',
          opacity: dim ? 0.72 : 1,
          transform: `rotate(${lean}deg)`,
          transformOrigin: '50% 90%',
          filter: hasBall ? `drop-shadow(0 0 7px ${color})` : 'drop-shadow(0 1px 2px rgba(0,0,0,0.8))',
          transition: 'transform 0.12s linear',
        }}
      >
        {/* shadow on the grass */}
        <ellipse cx="11" cy="31.5" rx="5.6" ry="1.6" fill="rgba(0,0,0,0.5)" />

        {/* legs — they swing from the hip, so they really run */}
        <g style={swing('pitchLegA')}>
          <rect x="7.2" y="19" width="2.9" height="9.6" rx="1.3" fill={socks} stroke={ink} strokeWidth="0.4" />
          <rect x="6.6" y="27.6" width="4.1" height="2.9" rx="1.1" fill="#12161a" />
        </g>
        <g style={swing('pitchLegB')}>
          <rect x="11.9" y="19" width="2.9" height="9.6" rx="1.3" fill={socks} stroke={ink} strokeWidth="0.4" />
          <rect x="11.3" y="27.6" width="4.1" height="2.9" rx="1.1" fill="#12161a" />
        </g>

        {/* arms */}
        <g style={swing('pitchArmB')}>
          <rect x="4.7" y="10.8" width="2.2" height="7.8" rx="1.1" fill={skin} stroke={ink} strokeWidth="0.4" />
          {isGK && <circle cx="5.8" cy="18.6" r="1.5" fill="#e9eef2" stroke={ink} strokeWidth="0.4" />}
        </g>
        <g style={swing('pitchArmA')}>
          <rect x="15.1" y="10.8" width="2.2" height="7.8" rx="1.1" fill={skin} stroke={ink} strokeWidth="0.4" />
          {isGK && <circle cx="16.2" cy="18.6" r="1.5" fill="#e9eef2" stroke={ink} strokeWidth="0.4" />}
        </g>

        {/* neck */}
        <rect x="9.8" y="7.6" width="2.4" height="3.4" fill={shade(skin, 0.8)} />

        {/* shorts (drawn over the top of the legs) */}
        <rect x="6.6" y="17.6" width="8.8" height="4.6" rx="1.4" fill={shorts} stroke={ink} strokeWidth="0.4" />
        <rect x="10.7" y="18" width="0.7" height="4" fill="rgba(0,0,0,0.3)" />

        {/* shirt + sleeves */}
        <rect x="4.5" y="10.2" width="2.9" height="3.6" rx="1.2" fill={kit} stroke={ink} strokeWidth="0.4" />
        <rect x="14.6" y="10.2" width="2.9" height="3.6" rx="1.2" fill={kit} stroke={ink} strokeWidth="0.4" />
        <path
          d="M6.4 11.9 Q6.4 9.7 8.6 9.4 L13.4 9.4 Q15.6 9.7 15.6 11.9 L15.6 18.6 L6.4 18.6 Z"
          fill={kit}
          stroke={ink}
          strokeWidth="0.4"
          strokeLinejoin="round"
        />
        {/* a little collar so the shirt reads as a shirt */}
        <path d="M9.7 9.5 L11 11.3 L12.3 9.5 Z" fill="rgba(255,255,255,0.55)" />

        {/* head */}
        <circle cx="11" cy="5" r="3.8" fill={skin} stroke={ink} strokeWidth="0.45" />
        <path
          d="M7.3 4.4 Q7.5 0.9 11 0.9 Q14.5 0.9 14.7 4.4 Q12.9 2.7 11 3.1 Q9.1 2.7 7.3 4.4 Z"
          fill={hair}
        />
      </svg>
    );
  };
})();
