// figures.jsx — activity figures (walking, cooking, working) in the energy-body style,
// plus the "masters" and "scenes" data for the teaching Witness.

// ── shared drawing atoms (teal wireframe) ────────────────────────
function FigDefs() {
  return (
    <defs>
      <radialGradient id="figAura" cx="50%" cy="40%" r="60%">
        <stop offset="0%" stopColor="rgba(86,180,255,0.30)" />
        <stop offset="48%" stopColor="rgba(54,232,210,0.09)" />
        <stop offset="100%" stopColor="rgba(54,232,210,0)" />
      </radialGradient>
      <radialGradient id="figHead" cx="50%" cy="42%" r="62%">
        <stop offset="0%" stopColor="rgba(120,205,255,0.85)" />
        <stop offset="55%" stopColor="rgba(56,150,220,0.32)" />
        <stop offset="100%" stopColor="rgba(40,110,170,0.04)" />
      </radialGradient>
      <filter id="figSoft" x="-60%" y="-60%" width="220%" height="220%"><feGaussianBlur stdDeviation="3.2" /></filter>
    </defs>
  );
}
const FIG_STROKE = 'rgba(54,232,210,0.44)';
const FIG_FILL = 'rgba(54,232,210,0.07)';
// capsule limb
function Limb({ a, b, w }) {
  return <line x1={a[0]} y1={a[1]} x2={b[0]} y2={b[1]} stroke={FIG_STROKE} strokeWidth={w} strokeLinecap="round" />;
}
function Joint({ c, r }) {
  return <circle cx={c[0]} cy={c[1]} r={r} fill={FIG_FILL} stroke={FIG_STROKE} strokeWidth="1.2" />;
}
function Head({ c, rx = 30, ry = 34 }) {
  return <ellipse cx={c[0]} cy={c[1]} rx={rx} ry={ry} fill="url(#figHead)" stroke="rgba(140,210,255,0.7)" strokeWidth="1.4" />;
}

// ── the three activity figures (viewBox 0 0 360 460) ─────────────
function WalkingBody() {
  return (
    <g>
      {/* back leg (pushing off) */}
      <Limb a={[178, 250]} b={[120, 312]} w={30} /><Limb a={[120, 312]} b={[96, 398]} w={24} />
      <ellipse cx="92" cy="404" rx="22" ry="12" fill={FIG_FILL} stroke={FIG_STROKE} strokeWidth="1.2" />
      {/* back arm */}
      <Limb a={[168, 120]} b={[122, 178]} w={20} /><Limb a={[122, 178]} b={[140, 236]} w={16} />
      {/* torso */}
      <Limb a={[166, 96]} b={[190, 250]} w={62} />
      {/* front leg (stepping) */}
      <Limb a={[192, 250]} b={[250, 318]} w={30} /><Limb a={[250, 318]} b={[262, 402]} w={24} />
      <ellipse cx="270" cy="408" rx="24" ry="12" fill="rgba(110,245,230,0.12)" stroke={FIG_STROKE} strokeWidth="1.3" />
      {/* front arm */}
      <Limb a={[182, 120]} b={[236, 174]} w={20} /><Limb a={[236, 174]} b={[230, 236]} w={16} />
      <Joint c={[174, 110]} r={11} />
      <Head c={[160, 58]} />
    </g>
  );
}
function CookingBody() {
  return (
    <g>
      {/* counter + board + steam */}
      <line x1="214" y1="286" x2="350" y2="286" stroke="rgba(120,210,205,0.34)" strokeWidth="2" />
      <rect x="240" y="274" width="68" height="12" rx="3" fill="rgba(54,232,210,0.10)" stroke={FIG_STROKE} strokeWidth="1" />
      <g stroke="rgba(124,240,220,0.4)" strokeWidth="1.4" fill="none" opacity="0.7">
        <path d="M260 270 q-6 -12 0 -22 q6 -10 0 -20" /><path d="M288 270 q-6 -12 0 -22 q6 -10 0 -20" />
      </g>
      {/* legs (standing, weight even) */}
      <Limb a={[168, 268]} b={[160, 360]} w={32} /><Limb a={[160, 360]} b={[158, 424]} w={25} />
      <Limb a={[192, 268]} b={[202, 360]} w={32} /><Limb a={[202, 360]} b={[206, 424]} w={25} />
      <ellipse cx="156" cy="430" rx="22" ry="11" fill={FIG_FILL} stroke={FIG_STROKE} strokeWidth="1.2" />
      <ellipse cx="208" cy="430" rx="22" ry="11" fill={FIG_FILL} stroke={FIG_STROKE} strokeWidth="1.2" />
      {/* hips → shoulders: a gentle forward stoop */}
      <Limb a={[180, 270]} b={[176, 188]} w={58} /> {/* lower torso, upright */}
      <Limb a={[176, 190]} b={[200, 124]} w={52} /> {/* upper torso, leaning toward the board */}
      {/* neck + head, following the lean forward */}
      <Limb a={[200, 128]} b={[212, 96]} w={20} />
      <Head c={[220, 74]} rx={28} ry={31} />
      {/* shoulder + both arms reaching down to the board */}
      <Joint c={[206, 118]} r={12} />
      <Limb a={[206, 120]} b={[238, 188]} w={20} /><Limb a={[238, 188]} b={[268, 264]} w={16} />
      <Limb a={[214, 122]} b={[246, 190]} w={18} /><Limb a={[246, 190]} b={[272, 266]} w={15} />
    </g>
  );
}
function WorkingBody() {
  return (
    <g>
      {/* desk + laptop */}
      <line x1="206" y1="232" x2="350" y2="232" stroke="rgba(120,210,205,0.34)" strokeWidth="2" />
      <path d="M250 232 l10 -34 h44 l-6 34 z" fill="rgba(54,232,210,0.08)" stroke={FIG_STROKE} strokeWidth="1.1" />
      <line x1="258" y1="216" x2="300" y2="216" stroke="rgba(124,240,220,0.4)" strokeWidth="1" />
      {/* chair seat + seated legs */}
      <Limb a={[166, 232]} b={[250, 244]} w={30} /> {/* thigh forward */}
      <Limb a={[250, 244]} b={[252, 330]} w={24} /> {/* shin down */}
      <ellipse cx="250" cy="340" rx="22" ry="11" fill={FIG_FILL} stroke={FIG_STROKE} strokeWidth="1.2" />
      <Limb a={[160, 232]} b={[238, 250]} w={26} />
      {/* torso upright */}
      <Limb a={[152, 104]} b={[166, 232]} w={58} />
      {/* arms forward to the laptop */}
      <Limb a={[166, 130]} b={[214, 178]} w={19} /><Limb a={[214, 178]} b={[256, 214]} w={15} />
      <Limb a={[176, 132]} b={[220, 182]} w={17} /><Limb a={[220, 182]} b={[258, 216]} w={14} />
      <Joint c={[158, 118]} r={11} />
      <Head c={[150, 70]} rx={28} ry={32} />
    </g>
  );
}
const FIG_BODY = { walking: WalkingBody, cooking: CookingBody, working: WorkingBody };

// pose-pulsing wrapper + steady "attention anchor" glow
function ActivityFigure({ scene, breathing = true, anchor = null }) {
  const Body = FIG_BODY[scene] || WalkingBody;
  return (
    <svg viewBox="0 0 360 460" style={{ width: '100%', height: '100%', overflow: 'visible' }}>
      <FigDefs />
      <ellipse cx="180" cy="240" rx="200" ry="230" fill="url(#figAura)" />
      <g style={{ transformOrigin: '180px 240px', animation: breathing ? 'mtBreathe 9s ease-in-out infinite' : 'none' }}>
        <Body />
      </g>
      {anchor && (
        <g style={{ pointerEvents: 'none' }}>
          <circle cx={anchor[0] * 360} cy={anchor[1] * 460} r="26" fill="rgba(54,232,210,0.16)" filter="url(#figSoft)" />
          <circle cx={anchor[0] * 360} cy={anchor[1] * 460} r="7" fill="#9CFFF2"
            style={{ transformOrigin: `${anchor[0] * 360}px ${anchor[1] * 460}px`, animation: breathing ? 'mtPulse 3s ease-in-out infinite' : 'none' }} />
        </g>
      )}
    </svg>
  );
}

// ── scenes ───────────────────────────────────────────────────────
// figure 'sitting' renders BodyFigure(seated); others render ActivityFigure.
// positions are fractional [x,y] within the figure box.
const MT_SCENES = {
  sitting: {
    label: 'Sitting', icon: 'lotus', figure: 'sitting', aspect: [360, 500],
    anchor: [0.5, 0.5], anchorLabel: 'the breath', headPos: [0.5, 0.12],
    mind: [
      { word: 'planning', detail: 'tomorrow’s list' }, { word: 'remembering', detail: 'a face from years ago' },
      { word: 'doubting', detail: '“am I doing this right?”' }, { word: 'wanting', detail: 'for it to be over' },
      { word: 'hearing', detail: 'a car in the street' }, { word: 'judging', detail: 'comparing to yesterday' },
    ],
    body: [
      { word: 'tension', detail: 'the jaw, clenched', sid: 'tension', pos: [0.5, 0.15] },
      { word: 'tension', detail: 'a band in the chest', sid: 'tension', pos: [0.5, 0.34] },
      { word: 'heat', detail: 'warmth in the belly', sid: 'heat', pos: [0.5, 0.47] },
      { word: 'tingling', detail: 'the hands, alive', sid: 'tingling', pos: [0.46, 0.65] },
      { word: 'numbness', detail: 'a foot, far away', sid: 'numb', pos: [0.55, 0.86] },
    ],
  },
  walking: {
    label: 'Walking', icon: 'person', figure: 'walking', aspect: [360, 460],
    anchor: [0.72, 0.88], anchorLabel: 'each footstep', headPos: [0.44, 0.12],
    mind: [
      { word: 'planning', detail: 'the day ahead' }, { word: 'a face', detail: 'someone passing by' },
      { word: 'wanting', detail: 'to arrive already' }, { word: 'naming', detail: '“tree… sky… cold”' },
      { word: 'worrying', detail: 'a thought from home' }, { word: 'judging', detail: 'my own slow pace' },
    ],
    body: [
      { word: 'pressing', detail: 'the sole meeting earth', sid: 'pressure', pos: [0.72, 0.86] },
      { word: 'swinging', detail: 'the arm, loose', sid: 'tremor', pos: [0.34, 0.42] },
      { word: 'breeze', detail: 'cool on the skin', sid: 'tingling', pos: [0.48, 0.26] },
      { word: 'shifting', detail: 'weight, side to side', sid: 'pressure', pos: [0.55, 0.58] },
    ],
  },
  cooking: {
    label: 'Cooking', icon: 'flame', figure: 'cooking', aspect: [360, 460],
    anchor: [0.73, 0.57], anchorLabel: 'the hands at work', headPos: [0.41, 0.12],
    mind: [
      { word: 'worrying', detail: 'about the children' }, { word: 'impatience', detail: '“hurry, hurry”' },
      { word: 'remembering', detail: 'an old kitchen' }, { word: 'planning', detail: 'the next dish' },
      { word: 'irritation', detail: 'at the mess' }, { word: 'wanting', detail: 'to be done' },
    ],
    body: [
      { word: 'gripping', detail: 'the knife in hand', sid: 'pressure', pos: [0.73, 0.57] },
      { word: 'heat', detail: 'rising from the stove', sid: 'heat', pos: [0.82, 0.46] },
      { word: 'steam', detail: 'damp on the skin', sid: 'tingling', pos: [0.78, 0.34] },
      { word: 'standing', detail: 'weight in the feet', sid: 'pressure', pos: [0.5, 0.88] },
    ],
  },
  working: {
    label: 'Working', icon: 'today', figure: 'working', aspect: [360, 460],
    anchor: [0.72, 0.47], anchorLabel: 'the task at hand', headPos: [0.42, 0.16],
    mind: [
      { word: 'the inbox', detail: 'one more message' }, { word: 'self-doubt', detail: '“is this good enough?”' },
      { word: 'the deadline', detail: 'looming closer' }, { word: 'craving', detail: 'a distraction' },
      { word: 'frustration', detail: 'a snag in the work' }, { word: 'planning', detail: 'the evening' },
    ],
    body: [
      { word: 'tension', detail: 'tight shoulders', sid: 'tension', pos: [0.44, 0.3] },
      { word: 'shallow', detail: 'the breath, held high', sid: 'pressure', pos: [0.48, 0.4] },
      { word: 'tingling', detail: 'the hands, typing', sid: 'tingling', pos: [0.72, 0.47] },
      { word: 'tension', detail: 'a clenched jaw', sid: 'tension', pos: [0.42, 0.16] },
    ],
  },
};

// ── the masters ──────────────────────────────────────────────────
const MT_SIM_TEACHERS = {
  dipama: {
    name: 'Dipa Ma', short: 'Dipa Ma', accent: '#7CF0DC', initials: 'DM',
    blurb: 'The householder master — awake in every act of daily life.',
    scenes: ['sitting', 'walking', 'cooking', 'working'],
    responses: [
      'notes it gently — back to', 'meets it with love, lets it pass — to', 'knows it, and returns to',
      'smiles, releases it — back to', 'simply aware, returns to',
    ],
    guidance: [
      'Whatever you are doing, be aware of it.',
      'If you can be mindful one moment, you can be mindful the next.',
      'The thought is not the enemy. Only know it, and let it go.',
      'There is no separation between practice and life.',
      'Be aware — and a little kinder than you need to be.',
    ],
  },
  goenka: {
    name: 'S. N. Goenka', short: 'Goenka', accent: '#36E8D2', initials: 'G',
    blurb: 'Vipassana — equanimous to every sensation, craving nothing.',
    scenes: ['sitting', 'walking'],
    responses: [
      'observes — anicca — back to', 'stays equanimous, returns to', 'neither craves nor averts — to',
      'just observes, and sweeps back to', 'it too shall pass — back to',
    ],
    guidance: [
      'Observe reality as it is, not as you would like it to be.',
      'Anicca — it arose, it will pass away.',
      'Remain equanimous, knowing everything changes.',
      'Do not react. Only observe, with a balanced mind.',
      'Every sensation is an opportunity to deepen equanimity.',
    ],
  },
};
const MT_SIM_ORDER = ['dipama', 'goenka'];

Object.assign(window, { ActivityFigure, MT_SCENES, MT_SIM_TEACHERS, MT_SIM_ORDER });
