// ============================================================
// Andre — game mascot
// Friendly beanie kid. Same component API as the prior blob
// (Andre, AndreChatter, mood prop, size prop) so the rest of
// the app needs no changes beyond the import name.
//
// Mood: 'idle' | 'wink' | 'cheer' | 'oof' | 'think' | 'sleep'
//
// Design notes:
//   - Round face, peach skin, thick ink outline
//   - Olive beanie with brim + brown hair peeks
//   - Pink blush circles
//   - Navy sweater collar
//   - Mood variations swap brows / eyes / mouth only — head shape
//     stays consistent so the character reads as the same person
//     across states.
// ============================================================

const Andre = ({ mood = 'idle', size = 80, style = {} }) => {
  // Per design direction: no winks/blinks anywhere. Anything that would
  // have winked just reads as happy instead. Single-source alias here so
  // every call site (mid-game callouts, modals, splash) is covered without
  // touching them individually.
  if (mood === 'wink') mood = 'cheer';

  const animClass =
    mood === 'cheer' ? 'andre-bounce' :
    mood === 'oof'   ? 'andre-squish' :
                       'andre-idle';

  // Head wobbles a hair on cheer / oof — keeps the squishy feel from
  // the old blob mascot without changing the silhouette.
  const headScale =
    mood === 'oof'   ? 'scale(1.04, 0.96)' :
    mood === 'cheer' ? 'scale(0.98, 1.02)' :
                       'scale(1, 1)';

  return (
    <div
      style={{ width: size, height: size, display: 'inline-block', ...style }}
      className={animClass}
    >
      <svg
        viewBox="0 0 100 100"
        width={size}
        height={size}
        style={{ overflow: 'visible' }}
      >
        {/* Ground shadow */}
        <ellipse cx="50" cy="95" rx="22" ry="3" fill="rgba(42,36,56,0.22)" />

        {/* Sweater collar — sits behind the head, peeks out under chin */}
        <g>
          <ellipse
            cx="50" cy="86"
            rx="22" ry="8"
            fill="#2A4A6B"
            stroke="#2A2438"
            strokeWidth="2.8"
            strokeLinejoin="round"
          />
          {/* Inner neckline shadow */}
          <ellipse cx="50" cy="82" rx="11" ry="3.2" fill="#1E3550" opacity="0.85" />
        </g>

        {/* Head + hair group — wobbles per mood */}
        <g style={{ transform: headScale, transformOrigin: '50px 50px', transition: 'transform 0.18s ease' }}>
          {/* Hair base — sits behind face, brown tufts peek beside the beanie */}
          <path
            d="M22 48
               C 22 38, 30 26, 50 26
               C 70 26, 78 38, 78 48
               C 78 52, 76 56, 73 58
               L 73 44
               C 73 36, 64 32, 50 32
               C 36 32, 27 36, 27 44
               L 27 58
               C 24 56, 22 52, 22 48 Z"
            fill="#5A3825"
            stroke="#2A2438"
            strokeWidth="2.6"
            strokeLinejoin="round"
          />

          {/* Face — circle */}
          <circle
            cx="50" cy="56"
            r="24"
            fill="#F4CDA8"
            stroke="#2A2438"
            strokeWidth="3"
          />

          {/* Inner ear-to-jaw shadow on the right side, gives a touch of
              dimension without going full shading. */}
          <path
            d="M71 52 Q 73 60 70 68"
            stroke="#D9A87E"
            strokeWidth="1.5"
            fill="none"
            strokeLinecap="round"
            opacity="0.6"
          />

          {/* Cheek blush */}
          <ellipse cx="32" cy="62" rx="4.2" ry="2.8" fill="#E89AA0" opacity="0.75" />
          <ellipse cx="68" cy="62" rx="4.2" ry="2.8" fill="#E89AA0" opacity="0.75" />

          {/* Beanie — sits on top of the head, brim across the forehead */}
          <AndreBeanie mood={mood} />

          {/* Brows */}
          <AndreBrows mood={mood} />

          {/* Eyes */}
          <AndreEyes mood={mood} />

          {/* Mouth */}
          <AndreMouth mood={mood} />

          {/* Sleep Z's — only visible in sleep mood */}
          {mood === 'sleep' && (
            <g fill="#2A2438" fontFamily="'Bagel Fat One', sans-serif" fontSize="9">
              <text x="78" y="38" opacity="0.85">z</text>
              <text x="84" y="30" opacity="0.6" fontSize="7">z</text>
            </g>
          )}
        </g>
      </svg>
    </div>
  );
};

// ============================================================
// Beanie — olive knit cap. Subtle highlight ribbon on the dome,
// flat brim across the forehead. Tilts slightly for cheer.
// ============================================================
const AndreBeanie = ({ mood }) => {
  const tilt =
    mood === 'cheer' ? 'rotate(-4deg)' :
    mood === 'wink'  ? 'rotate(-2deg)' :
                       'rotate(0deg)';
  return (
    <g style={{ transform: tilt, transformOrigin: '50px 38px', transition: 'transform 0.2s ease' }}>
      {/* Dome */}
      <path
        d="M28 42
           C 28 28, 38 20, 50 20
           C 62 20, 72 28, 72 42
           Z"
        fill="#5E6B3E"
        stroke="#2A2438"
        strokeWidth="3"
        strokeLinejoin="round"
      />
      {/* Highlight on the dome */}
      <path
        d="M40 26 Q 50 22 60 26"
        stroke="#7B8A55"
        strokeWidth="3"
        fill="none"
        strokeLinecap="round"
        opacity="0.7"
      />
      {/* Brim — flat band across the forehead */}
      <path
        d="M26 41
           Q 50 47 74 41
           L 74 47
           Q 50 53 26 47 Z"
        fill="#4A5532"
        stroke="#2A2438"
        strokeWidth="3"
        strokeLinejoin="round"
      />
    </g>
  );
};

// ============================================================
// Brows — thick brown brushstrokes. Mood swaps the curve.
// ============================================================
const AndreBrows = ({ mood }) => {
  const stroke = '#3E2818';
  const w = 3.2;
  if (mood === 'oof') {
    // Worried — angled inward
    return (
      <g stroke={stroke} strokeWidth={w} strokeLinecap="round" fill="none">
        <path d="M34 49 L 42 51" />
        <path d="M66 49 L 58 51" />
      </g>
    );
  }
  if (mood === 'think') {
    // One raised
    return (
      <g stroke={stroke} strokeWidth={w} strokeLinecap="round" fill="none">
        <path d="M34 51 Q 38 49 42 50" />
        <path d="M58 47 Q 62 45 66 47" />
      </g>
    );
  }
  if (mood === 'cheer') {
    // Both raised + arched
    return (
      <g stroke={stroke} strokeWidth={w} strokeLinecap="round" fill="none">
        <path d="M34 47 Q 38 43 42 47" />
        <path d="M58 47 Q 62 43 66 47" />
      </g>
    );
  }
  if (mood === 'sleep') {
    // Relaxed, lower
    return (
      <g stroke={stroke} strokeWidth={w} strokeLinecap="round" fill="none">
        <path d="M34 52 Q 38 51 42 52" />
        <path d="M58 52 Q 62 51 66 52" />
      </g>
    );
  }
  // idle / wink — neutral, gently arched
  return (
    <g stroke={stroke} strokeWidth={w} strokeLinecap="round" fill="none">
      <path d="M34 50 Q 38 47 42 50" />
      <path d="M58 50 Q 62 47 66 50" />
    </g>
  );
};

// ============================================================
// Eyes — round brown pupils with white highlights.
// ============================================================
const AndreEyes = ({ mood }) => {
  const ink = '#2A2438';
  const irisColor = '#5A3825';

  if (mood === 'idle' || mood === 'think') {
    return (
      <>
        {/* whites */}
        <ellipse cx="40" cy="56" rx="4.2" ry="4.6" fill="#FFFBF2" stroke={ink} strokeWidth="1.5" />
        <ellipse cx="60" cy="56" rx="4.2" ry="4.6" fill="#FFFBF2" stroke={ink} strokeWidth="1.5" />
        {/* irises */}
        <ellipse cx="40.5" cy="56.5" rx="2.6" ry="3" fill={irisColor} />
        <ellipse cx="60.5" cy="56.5" rx="2.6" ry="3" fill={irisColor} />
        {/* highlights */}
        <circle cx="41.5" cy="55" r="1" fill="#FFFBF2" />
        <circle cx="61.5" cy="55" r="1" fill="#FFFBF2" />
      </>
    );
  }
  if (mood === 'wink') {
    // Left open, right closed (curved arc, lashes hint)
    return (
      <>
        <ellipse cx="40" cy="56" rx="4.2" ry="4.6" fill="#FFFBF2" stroke={ink} strokeWidth="1.5" />
        <ellipse cx="40.5" cy="56.5" rx="2.6" ry="3" fill={irisColor} />
        <circle cx="41.5" cy="55" r="1" fill="#FFFBF2" />
        <path
          d="M55.5 57 Q 60 53 64.5 57"
          stroke={ink} strokeWidth="2.6" fill="none" strokeLinecap="round"
        />
      </>
    );
  }
  if (mood === 'cheer') {
    // Both closed in happy upward arcs ^^
    return (
      <g stroke={ink} strokeWidth="2.6" fill="none" strokeLinecap="round">
        <path d="M35.5 57 Q 40 52 44.5 57" />
        <path d="M55.5 57 Q 60 52 64.5 57" />
      </g>
    );
  }
  if (mood === 'oof') {
    // X eyes
    return (
      <g stroke={ink} strokeWidth="2.6" strokeLinecap="round">
        <line x1="36.5" y1="53" x2="43.5" y2="60" />
        <line x1="43.5" y1="53" x2="36.5" y2="60" />
        <line x1="56.5" y1="53" x2="63.5" y2="60" />
        <line x1="63.5" y1="53" x2="56.5" y2="60" />
      </g>
    );
  }
  if (mood === 'sleep') {
    // Closed eyes, gentle downward curves with lash hint
    return (
      <g stroke={ink} strokeWidth="2.6" fill="none" strokeLinecap="round">
        <path d="M35.5 56 Q 40 60 44.5 56" />
        <path d="M55.5 56 Q 60 60 64.5 56" />
      </g>
    );
  }
  return null;
};

// ============================================================
// Mouth — small expressive line.
// ============================================================
const AndreMouth = ({ mood }) => {
  const ink = '#2A2438';
  if (mood === 'idle') {
    // Gentle smile
    return (
      <path
        d="M44 70 Q 50 74 56 70"
        stroke={ink} strokeWidth="2.6" fill="none" strokeLinecap="round"
      />
    );
  }
  if (mood === 'wink') {
    // Bigger smile
    return (
      <path
        d="M43 69 Q 50 76 57 69"
        stroke={ink} strokeWidth="2.6" fill="none" strokeLinecap="round"
      />
    );
  }
  if (mood === 'cheer') {
    // Open smile with tongue blush
    return (
      <g>
        <path
          d="M41 68 Q 50 80 59 68 Q 50 73 41 68 Z"
          fill={ink} stroke={ink} strokeWidth="2" strokeLinejoin="round"
        />
        <path
          d="M46 73 Q 50 76 54 73"
          fill="#E89AA0" stroke="none"
        />
      </g>
    );
  }
  if (mood === 'oof') {
    // Wide O
    return (
      <ellipse cx="50" cy="72" rx="3.5" ry="4.2" fill={ink} />
    );
  }
  if (mood === 'think') {
    // Pursed — tiny line off-center
    return (
      <path
        d="M46 71 Q 50 70 54 71"
        stroke={ink} strokeWidth="2.6" fill="none" strokeLinecap="round"
      />
    );
  }
  if (mood === 'sleep') {
    // Tiny open mouth (sleeping)
    return (
      <ellipse cx="50" cy="71" rx="2" ry="1.4" fill={ink} opacity="0.85" />
    );
  }
  return null;
};

// ============================================================
// Speech-bubble wrapper — used throughout the app.
// ============================================================
const AndreChatter = ({ mood = 'idle', size = 72, message, side = 'right', style = {} }) => {
  const flip = side === 'left';
  return (
    <div style={{
      display: 'flex',
      alignItems: 'flex-end',
      gap: 12,
      flexDirection: flip ? 'row-reverse' : 'row',
      ...style
    }}>
      <Andre mood={mood} size={size} />
      {message && (
        <div className="mascot-bubble" style={{ marginBottom: 6 }}>
          {message}
        </div>
      )}
    </div>
  );
};

Object.assign(window, { Andre, AndreChatter });
