/* ============================================================================
 * service-backdrops.jsx — 11 증상별 배경 (LIGHT · 권위/차분 · 구도 자체가 다름)
 *   window.ServiceBackdrop({ botKey, accent })
 *   증상마다 그라디언트 방향 + 모티프 구도를 다르게 (아래빛/호흡원/분할/격자/능선…)
 * ========================================================================== */

const IVORY = '#f6f1e9';

const SCENES = {
  // 우울 — 아래에서 따뜻하게 차오르는 빛 + 잔잔한 수평
  depression: {
    bg: (a) => `linear-gradient(178deg, #f8f2ea 0%, ${a}10 56%, ${a}2c 100%)`,
    motif: (a) => (<g>
      <ellipse cx="720" cy="970" rx="760" ry="280" fill={a} opacity="0.22" />
      <path d="M0 700 Q720 676 1440 700" stroke={a} strokeWidth="1.6" fill="none" opacity="0.3" />
      <path d="M0 760 Q720 732 1440 760" stroke={a} strokeWidth="1.4" fill="none" opacity="0.2" />
    </g>),
  },
  // 불안 — 중앙에서 퍼지는 호흡원
  anxiety: {
    bg: (a) => `radial-gradient(78% 80% at 60% 44%, ${a}24 0%, ${a}10 40%, ${IVORY} 100%)`,
    motif: (a) => (<g fill="none" stroke={a}>
      {[90, 180, 290, 410, 540].map((r, i) => <circle key={i} cx="720" cy="430" r={r} strokeWidth="1.4" opacity={0.26 - i * 0.04} />)}
    </g>),
  },
  // 수면 — 서늘한 트와일라잇 + 초승달 + 별
  sleep: {
    bg: (a) => `linear-gradient(168deg, #eceaf3 0%, ${a}26 52%, #e8e6f1 100%)`,
    motif: (a) => (<g>
      <circle cx="300" cy="220" r="64" fill={a} opacity="0.5" /><circle cx="328" cy="200" r="54" fill="#eceaf3" />
      {[[470, 180], [560, 280], [380, 330], [640, 200], [520, 130]].map((p, i) => <circle key={i} cx={p[0]} cy={p[1]} r={i % 2 ? 2 : 3} fill={a} opacity="0.5" />)}
    </g>),
  },
  // 번아웃 — 대각선 따뜻한 빛 → 식어가는 호
  burnout: {
    bg: (a) => `linear-gradient(132deg, #f8f1e7 0%, ${a}18 66%, ${a}30 100%)`,
    motif: (a) => (<g fill="none" stroke={a} strokeLinecap="round">
      <circle cx="1150" cy="760" r="150" fill={a} opacity="0.16" stroke="none" />
      {[0, 1, 2, 3].map((i) => <path key={i} d={`M120 ${300 + i * 60} Q500 ${280 + i * 60} 900 ${320 + i * 60}`} strokeWidth="1.6" opacity={0.24 - i * 0.04} />)}
    </g>),
  },
  // PTSD — 강한 수평선 + 층층이 쌓인 안정 띠
  ptsd: {
    bg: (a) => `linear-gradient(180deg, #eef2f1 0%, ${a}12 50%, ${a}22 100%)`,
    motif: (a) => (<g stroke={a} fill="none">
      <line x1="0" y1="470" x2="1440" y2="470" strokeWidth="2" opacity="0.34" />
      {[360, 410, 540, 600, 660].map((y, i) => <line key={i} x1="120" y1={y} x2="1320" y2={y} strokeWidth="1.2" opacity={0.16 - i * 0.02} />)}
    </g>),
  },
  // OCD — 정돈된 전면 격자
  ocd: {
    bg: () => '#eff3f0',
    motif: (a) => (<g stroke={a} opacity="0.16">
      {[...Array(13)].map((_, i) => <line key={'v' + i} x1={60 + i * 110} y1="80" x2={60 + i * 110} y2="820" strokeWidth="1" />)}
      {[...Array(8)].map((_, i) => <line key={'h' + i} x1="60" y1={110 + i * 100} x2="1380" y2={110 + i * 100} strokeWidth="1" />)}
    </g>),
  },
  // 양극성 — 위/아래 분할 + 중앙 균형선
  bipolar: {
    bg: (a) => `linear-gradient(180deg, ${a}26 0%, #f4efe9 47%, #edeff2 53%, ${a}1e 100%)`,
    motif: (a) => (<g fill="none" stroke={a} strokeLinecap="round">
      <path d="M120 330 Q420 230 720 330 T1320 330" strokeWidth="2" opacity="0.34" />
      <path d="M120 540 Q420 640 720 540 T1320 540" strokeWidth="2" opacity="0.34" />
      <line x1="120" y1="435" x2="1320" y2="435" strokeWidth="1.4" strokeDasharray="2 9" opacity="0.4" />
    </g>),
  },
  // 자폐 — 또렷한 동심원 + 정렬 점
  autism: {
    bg: (a) => `radial-gradient(74% 78% at 60% 44%, ${a}22 0%, ${a}0e 42%, #eef2f5 100%)`,
    motif: (a) => (<g>
      <g fill="none" stroke={a}>{[80, 165, 260, 365].map((r, i) => <circle key={i} cx="1060" cy="400" r={r} strokeWidth="1.3" opacity={0.28 - i * 0.05} />)}</g>
      <g fill={a} opacity="0.3">{[...Array(15)].map((_, k) => { const r = Math.floor(k / 5), c = k % 5; return <circle key={k} cx={230 + c * 80} cy={250 + r * 80} r="3" />; })}</g>
    </g>),
  },
  // ADHD — 한 점으로 모이는 사선 광선
  adhd: {
    bg: (a) => `radial-gradient(92% 92% at 58% 42%, ${a}22 0%, ${a}0e 46%, #f6efe7 100%)`,
    motif: (a) => (<g stroke={a} strokeLinecap="round" opacity="0.4">
      {[[120, 160], [300, 110], [1320, 180], [1280, 520], [180, 560], [1180, 660], [80, 380], [1360, 360]].map((p, i) => {
        const dx = 880 - p[0], dy = 430 - p[1], L = Math.hypot(dx, dy), ux = dx / L, uy = dy / L;
        return <line key={i} x1={p[0]} y1={p[1]} x2={p[0] + ux * 90} y2={p[1] + uy * 90} strokeWidth="1.6" />;
      })}
    </g>),
  },
  // 학습 — 점을 잇는 호기심의 길 + 별
  learning: {
    bg: (a) => `linear-gradient(112deg, #f8f3e6 0%, ${a}14 70%, ${a}22 100%)`,
    motif: (a) => (<g>
      <polyline points="220,560 380,470 540,540 700,430 880,500 1060,380 1240,440" fill="none" stroke={a} strokeWidth="1.6" strokeDasharray="2 9" opacity="0.46" />
      {[[220, 560], [380, 470], [540, 540], [700, 430], [880, 500], [1060, 380], [1240, 440]].map((p, i) => <circle key={i} cx={p[0]} cy={p[1]} r={i === 5 ? 5 : 3.4} fill={a} opacity="0.6" />)}
    </g>),
  },
  // 피크 — 위에서 내리는 빛 + 능선
  peak: {
    bg: (a) => `linear-gradient(180deg, ${a}2c 0%, #f1f0ec 54%, #ecefe9 100%)`,
    motif: (a) => (<g>
      <path d="M0 700 L260 520 L440 630 L680 470 L900 640 L1120 520 L1440 670 L1440 760 L0 760Z" fill={a} opacity="0.14" />
      <path d="M0 700 L260 520 L440 630 L680 470 L900 640 L1120 520 L1440 670" fill="none" stroke={a} strokeWidth="1.8" opacity="0.32" />
      <circle cx="680" cy="470" r="6" fill={a} opacity="0.6" />
    </g>),
  },
};

function ServiceBackdrop({ botKey, accent }) {
  const scene = SCENES[botKey] || SCENES.depression;
  return (
    <div style={{ position: 'absolute', inset: 0, zIndex: 0, overflow: 'hidden', background: scene.bg(accent) }}>
      <svg viewBox="0 0 1440 900" preserveAspectRatio="xMidYMid slice"
        style={{ position: 'absolute', inset: 0, width: '100%', height: '100%' }}>
        {scene.motif(accent)}
      </svg>
    </div>
  );
}

Object.assign(window, { ServiceBackdrop });
