// ════════════════════════════════════════════════════════════════
//  Augmented Engineering — SVG Diagram component
// ════════════════════════════════════════════════════════════════

function AoDiagram({ active, onPick, theme }) {
  const W = 960, H = 700;
  const isLight = theme === 'light';

  // Alpha hex suffixes — higher in light mode for readability
  const aLabel  = isLight ? 'ff' : 'cc';  // zone title
  const aSub    = isLight ? 'cc' : '66';  // zone sub-label
  const aItems  = isLight ? 'ee' : '88';  // zone item lines
  const aStroke = isLight ? 'cc' : '77';  // circle stroke inactive

  // Rect zone text alphas
  const rTitle  = isLight ? 1.00 : 0.85;
  const rSub    = isLight ? 0.90 : 0.70;
  const rItems  = isLight ? 0.85 : 0.42;
  const rFine   = isLight ? 0.78 : 0.38;
  const rBullet = isLight ? 0.82 : 0.48;

  // Context-space hex alphas
  const csTitle = isLight ? 0.95 : 0.75;
  const csText  = isLight ? 1.00 : 0.88;

  const isActive = (id) => active === id;

  // Pointed-top hexagon: r=75, cx=320, cy=345
  // VIBING  : cx=135, cy=242, r=115  — upper-left
  // SPECING : cx=505, cy=242, r=110  — upper-right  (same cy as VIBING ✓)
  // HARNESSING: cx=320, cy=550, r=108 — bottom-centre (cx = HCX ✓)
  // All circle edges are ≥ 8 px clear of the hexagon edge; circles don't overlap each other.
  const HCX = 320, HCY = 345, HR = 75;
  const hexPts = Array.from({ length: 6 }, (_, i) => {
    const a = (-90 + i * 60) * Math.PI / 180;
    return (HCX + HR * Math.cos(a)).toFixed(1) + ',' + (HCY + HR * Math.sin(a)).toFixed(1);
  }).join(' ');

  const hexFiles = [
    'ubiquitous_language.md',
    'scenarios.md',
    'gherkin.md',
    'vision.md',
    'ddd_rules.md',
    'skill_*.md',
    'bounded_contexts.md',
  ];

  function CircleZone({ id, cx, cy, r, color, label, sub, items }) {
    const on = isActive(id);
    const dimItems = color + aItems;
    return (
      <g onClick={() => onPick(id)} style={{ cursor: 'pointer' }}>
        <circle cx={cx} cy={cy} r={r}
          fill={on ? color + '22' : color + '18'}
          stroke={on ? color : color + aStroke}
          strokeWidth={on ? 2.5 : 1.5}
          style={on ? { filter: 'drop-shadow(0 0 18px ' + color + '66)' } : {}}
        />
        <text x={cx} y={cy - r * 0.5} textAnchor="middle"
          fill={on ? color : color + aLabel}
          fontSize="13.5" fontWeight="700" letterSpacing="3" fontFamily="monospace">
          {label}
        </text>
        {sub && (
          <text x={cx} y={cy - r * 0.5 + 16} textAnchor="middle"
            fill={color + aSub} fontSize="9.5" letterSpacing="1.5" fontFamily="monospace">
            {sub}
          </text>
        )}
        {items.map((item, i) => (
          <text key={i} x={cx} y={cy - r * 0.08 + i * 17} textAnchor="middle"
            fill={dimItems} fontSize="11" fontFamily="'Segoe UI',system-ui">
            {item}
          </text>
        ))}
      </g>
    );
  }

  return (
    <div id="venn-wrapper">
      <svg viewBox={'0 0 ' + W + ' ' + H}
        style={{ width: '100%', height: 'auto', display: 'block', overflow: 'visible' }}>
        <defs>
          <marker id="arr-o" viewBox="0 0 8 8" refX="7" refY="4" markerWidth="5" markerHeight="5" orient="auto-start-reverse">
            <path d="M0,0 L8,4 L0,8 Z" fill="#F5A62388" />
          </marker>
          <marker id="arr-b" viewBox="0 0 8 8" refX="7" refY="4" markerWidth="5" markerHeight="5" orient="auto-start-reverse">
            <path d="M0,0 L8,4 L0,8 Z" fill="#4A90E288" />
          </marker>
          <marker id="arr-g" viewBox="0 0 8 8" refX="7" refY="4" markerWidth="5" markerHeight="5" orient="auto-start-reverse">
            <path d="M0,0 L8,4 L0,8 Z" fill="#27AE6088" />
          </marker>
        </defs>

        {/* ── Arrows (rendered behind circles) ── */}

        {/* intent → formalisatie: VIBING upper-right arc over to SPECING upper-left */}
        <path d="M 216,161 Q 320,72 424,164"
          fill="none" stroke="#F5A62366" strokeWidth="1.5" markerEnd="url(#arr-o)" />
        <text x="320" y="66" textAnchor="middle"
          fill="#F5A62355" fontSize="10" fontFamily="monospace" fontStyle="italic">
          intent → formalisatie
        </text>

        {/* VIBING → context hexagon */}
        <path d="M 235,298 L 255,309"
          fill="none" stroke="#F5A62355" strokeWidth="1.2" markerEnd="url(#arr-o)" />

        {/* SPECING → context hexagon */}
        <path d="M 409,295 L 385,309"
          fill="none" stroke="#4A90E255" strokeWidth="1.2" markerEnd="url(#arr-b)" />

        {/* HARNESSING → context hexagon */}
        <path d="M 320,420 L 320,442"
          fill="none" stroke="#27AE6055" strokeWidth="1.2" markerStart="url(#arr-g)" markerEnd="url(#arr-g)" />

        {/* HARNESSING → VIBING */}
        <path d="M 320,442 Q 220,500 170,340" fill="none" stroke="#27AE6055" strokeWidth="1.2" markerEnd="url(#arr-g)" />
        {/* HARNESSING → SPECING */}
        <path d="M 320,442 Q 420,500 470,340" fill="none" stroke="#27AE6055" strokeWidth="1.2" markerEnd="url(#arr-g)" />

        {/* slot output label — left outward from VIBING */}
        <path d="M 46,242 L 24,242"
          fill="none" stroke="#F5A62333" strokeWidth="1" markerEnd="url(#arr-o)" />
        <text x="18" y="236" fill="#F5A62333" fontSize="9" fontFamily="monospace">slot</text>
        <text x="18" y="249" fill="#F5A62333" fontSize="9" fontFamily="monospace">output formaat</text>

        {/* context → AI-IMPLEMENTATION (dashed) */}
        <path d="M 395,343 Q 600,370 648,400"
          fill="none" stroke="#3A7BC855" strokeWidth="1.2" strokeDasharray="5,4" markerEnd="url(#arr-b)" />
        <text x="570" y="390" fill="#3A7BC844" fontSize="9" fontFamily="monospace">DDD-driven commonlanguage</text>
        <text x="570" y="402" fill="#3A7BC844" fontSize="9" fontFamily="monospace">intent-level</text>

        {/* HARNESSING → AI system (dashed) */}
        <path d="M 422,515 Q 540,468 648,435"
          fill="none" stroke="#27AE6044" strokeWidth="1.2" strokeDasharray="5,4" markerEnd="url(#arr-g)" />
        <text x="514" y="508" fill="#27AE6033" fontSize="9" fontFamily="monospace">kentnis → corpus</text>
        <text x="514" y="521" fill="#27AE6033" fontSize="9" fontFamily="monospace">rentals → corpus context</text>

        {/* AI-IMPLEMENTATION → SOFTWARE PLATFORM */}
        <path d="M 757,278 L 757,300"
          fill="none" stroke="#4A90E266" strokeWidth="1.5" markerStart="url(#arr-b)" />
        <text x="766" y="293" fill="#4A90E244" fontSize="9" fontFamily="monospace">robot platform</text>

        {/* ── THREE CIRCLES ── */}
        <CircleZone id="vibing" cx={135} cy={242} r={115} color="#F5A623"
          label="VIBING" sub="social context"
          items={['non-coder als actor', 'intent expliciet maken', 'socratisch interview']} />

        <CircleZone id="specing" cx={505} cy={242} r={110} color="#4A90E2"
          label="SPECING" sub={null}
          items={['intent formaliseren', 'Gherkin schrijven', 'beslissingstabellen']} />

        <CircleZone id="harnessing" cx={320} cy={550} r={108} color="#27AE60"
          label="HARNESSING" sub={null}
          items={['skills definiëren', 'harnessen bouwen', 'validatie automatiseren']} />

        {/* ── CONTEXT SPACE hexagon ── */}
        <g onClick={() => onPick('context_space')} style={{ cursor: 'pointer' }}>
          <polygon points={hexPts}
            fill={isActive('context_space') ? 'rgba(155,89,182,0.2)' : 'rgba(155,89,182,0.12)'}
            stroke={isActive('context_space') ? '#9B59B6' : 'rgba(155,89,182,0.65)'}
            strokeWidth={isActive('context_space') ? 2 : 1.5}
            style={isActive('context_space') ? { filter: 'drop-shadow(0 0 14px rgba(155,89,182,0.55))' } : {}}
          />
          <text x={HCX} y={HCY - HR + 22} textAnchor="middle"
            fill={`rgba(155,89,182,${csTitle})`} fontSize="8.5" fontWeight="700" letterSpacing="2.5" fontFamily="monospace">
            CONTEXT SPACE
          </text>
          {hexFiles.map((f, i) => {
            const ry = HCY - HR + 34 + i * 14;
            return (
              <g key={f}>
                <rect x={HCX - 68} y={ry} width="136" height="13" rx="2"
                  fill="rgba(155,89,182,0.2)" stroke="rgba(155,89,182,0.28)" strokeWidth="0.5" />
                <text x={HCX} y={ry + 9.5} textAnchor="middle"
                  fill={`rgba(155,89,182,${csText})`} fontSize="9" fontFamily="monospace">{f}</text>
              </g>
            );
          })}
        </g>

        {/* ── SOFTWARE PLATFORM rect ── */}
        <g onClick={() => onPick('software_platform')} style={{ cursor: 'pointer' }}>
          <rect x={648} y={148} width={228} height={128} rx="5"
            fill={isActive('software_platform') ? 'rgba(58,123,200,0.18)' : 'rgba(58,123,200,0.08)'}
            stroke={isActive('software_platform') ? '#3A7BC8' : 'rgba(58,123,200,0.4)'}
            strokeWidth={isActive('software_platform') ? 2 : 1}
            style={isActive('software_platform') ? { filter: 'drop-shadow(0 0 12px rgba(58,123,200,0.45))' } : {}}
          />
          <text x="762" y="170" textAnchor="middle"
            fill={`rgba(58,123,200,${rTitle})`} fontSize="9.5" fontWeight="700" letterSpacing="2" fontFamily="monospace">
            SOFTWARE PLATFORM
          </text>
          {['projectie van contextruimte', 'DDD + Events AI', 'business context via Events', 'comments via AI tech'].map((l, i) => (
            <text key={i} x="762" y={190 + i * 18} textAnchor="middle"
              fill={`rgba(58,123,200,${rItems})`} fontSize="9.5" fontFamily="monospace">{l}</text>
          ))}
        </g>

        {/* ── AI-IMPLEMENTATION SYSTEM rect ── */}
        <g onClick={() => onPick('ai_system')} style={{ cursor: 'pointer' }}>
          <rect x={648} y={300} width={228} height={198} rx="5"
            fill={isActive('ai_system') ? 'rgba(39,174,96,0.14)' : 'rgba(39,174,96,0.07)'}
            stroke={isActive('ai_system') ? '#27AE60' : 'rgba(39,174,96,0.4)'}
            strokeWidth={isActive('ai_system') ? 2 : 1}
            style={isActive('ai_system') ? { filter: 'drop-shadow(0 0 12px rgba(39,174,96,0.45))' } : {}}
          />
          <text x="762" y="323" textAnchor="middle"
            fill={`rgba(39,174,96,${rTitle})`} fontSize="9.5" fontWeight="700" letterSpacing="2" fontFamily="monospace">
            AI-IMPLEMENTATION
          </text>
          <text x="762" y="337" textAnchor="middle"
            fill={`rgba(39,174,96,${rTitle})`} fontSize="9.5" fontWeight="700" letterSpacing="2" fontFamily="monospace">
            SYSTEM
          </text>
          <text x="762" y="358" textAnchor="middle"
            fill={`rgba(39,174,96,${rSub})`} fontSize="11.5" fontFamily="monospace">CDev / CI / CD</text>
          {['• Continuous Dev', '• Continuous Int', '• Continuous Del'].map((l, i) => (
            <text key={i} x="762" y={376 + i * 16} textAnchor="middle"
              fill={`rgba(39,174,96,${rBullet})`} fontSize="9.5" fontFamily="monospace">{l}</text>
          ))}
          <text x="762" y="432" textAnchor="middle" fill={`rgba(39,174,96,${rFine})`} fontSize="9" fontFamily="monospace">plan → build → review</text>
          <text x="762" y="448" textAnchor="middle" fill={`rgba(39,174,96,${rFine})`} fontSize="9" fontFamily="monospace">harnessing workflows</text>
          <text x="762" y="464" textAnchor="middle" fill={`rgba(39,174,96,${rFine})`} fontSize="9" fontFamily="monospace">space-driven ubiquering</text>
        </g>

      </svg>
    </div>
  );
}

Object.assign(window, { AoDiagram });
