// Cybergard AI — dashboard scene (Scene 3 + 4)

// ── Compliance score gauge ──────────────────────────────────────────────────
function ScoreGauge({ value, size = 220, label = 'SCORE CONFORMITÉ' }) {
  const R = size / 2 - 16;
  const CX = size / 2, CY = size / 2;
  const circ = 2 * Math.PI * R;
  const v = clamp(value, 0, 100);
  const dash = circ * (v / 100);

  return (
    <div style={{ position: 'relative', width: size, height: size + 40 }}>
      <svg width={size} height={size} style={{ display: 'block' }}>
        <defs>
          <linearGradient id="gaugeGrad" x1="0" y1="0" x2="1" y2="1">
            <stop offset="0%" stopColor="oklch(0.72 0.16 25)"/>
            <stop offset="100%" stopColor="oklch(0.55 0.22 25)"/>
          </linearGradient>
          <filter id="gaugeGlow">
            <feGaussianBlur stdDeviation="4"/>
          </filter>
        </defs>
        <circle cx={CX} cy={CY} r={R} stroke={CG.line} strokeWidth="3" fill="none"/>
        <g transform={`rotate(-90 ${CX} ${CY})`}>
          <circle
            cx={CX} cy={CY} r={R}
            stroke="url(#gaugeGrad)"
            strokeWidth="6"
            fill="none"
            strokeDasharray={`${dash} ${circ}`}
            strokeLinecap="round"
            filter="url(#gaugeGlow)"
            opacity="0.6"
          />
          <circle
            cx={CX} cy={CY} r={R}
            stroke="url(#gaugeGrad)"
            strokeWidth="3"
            fill="none"
            strokeDasharray={`${dash} ${circ}`}
            strokeLinecap="round"
          />
        </g>
      </svg>
      <div style={{
        position: 'absolute', inset: 0,
        display: 'flex', alignItems: 'center', justifyContent: 'center',
        flexDirection: 'column',
        pointerEvents: 'none',
      }}>
        <div style={{
          fontFamily: FONT_TITLE,
          fontSize: 56,
          fontWeight: 700,
          color: CG.text,
          letterSpacing: '0.02em',
          fontVariantNumeric: 'tabular-nums',
          textShadow: `0 0 24px ${CG.crimsonGlow}`,
        }}>
          {Math.round(v)}<span style={{ fontSize: 28, color: CG.dim }}>%</span>
        </div>
      </div>
      <div style={{
        position: 'absolute', bottom: 0, left: 0, right: 0,
        textAlign: 'center',
        fontFamily: FONT_MONO,
        fontSize: 11,
        color: CG.mute,
        letterSpacing: '0.22em',
      }}>{label}</div>
    </div>
  );
}

// ── Framework badge ─────────────────────────────────────────────────────────
function FrameworkBadge({ name, sub, active, progress = 1 }) {
  return (
    <div style={{
      padding: '18px 22px',
      background: active ? CG.crimsonTint : CG.panel,
      border: `1px solid ${active ? CG.crimson : CG.line}`,
      borderRadius: 10,
      boxShadow: active ? `0 0 20px ${CG.crimsonGlow}` : 'none',
      minWidth: 180,
      transition: 'none',
    }}>
      <div style={{
        fontFamily: FONT_TITLE,
        fontSize: 20,
        fontWeight: 700,
        color: active ? CG.text : CG.dim,
        letterSpacing: '0.04em',
      }}>{name}</div>
      <div style={{
        fontFamily: FONT_MONO,
        fontSize: 10,
        color: active ? CG.crimsonSoft : CG.mute,
        letterSpacing: '0.18em',
        marginTop: 6,
      }}>{sub}</div>
      <div style={{
        marginTop: 12,
        height: 3,
        background: CG.line,
        borderRadius: 2,
        overflow: 'hidden',
      }}>
        <div style={{
          height: '100%',
          width: `${progress * 100}%`,
          background: `linear-gradient(90deg, ${CG.crimsonSoft}, ${CG.crimson})`,
          boxShadow: active ? `0 0 8px ${CG.crimsonGlow}` : 'none',
        }}/>
      </div>
    </div>
  );
}

// ── RAG chat panel ──────────────────────────────────────────────────────────
function RAGPanel({ typedProgress }) {
  const lines = [
    { who: 'rssi', text: 'Statut contrôle A.8.9 ?', delay: 0 },
    { who: 'ai', text: 'Gestion configurations : 94%. 3 serveurs hors baseline CIS.', delay: 0.3 },
    { who: 'ai', text: '→ Plan d\'action généré · EBIOS RM à jour', delay: 0.7 },
  ];

  return (
    <div style={{
      width: 480,
      background: CG.panel,
      border: `1px solid ${CG.line}`,
      borderRadius: 12,
      padding: 22,
      boxShadow: '0 20px 60px rgba(0,0,0,0.4)',
    }}>
      <div style={{
        display: 'flex', alignItems: 'center', gap: 10,
        paddingBottom: 14,
        borderBottom: `1px solid ${CG.line}`,
        marginBottom: 14,
      }}>
        <div style={{
          width: 8, height: 8,
          background: CG.crimson,
          borderRadius: 4,
          boxShadow: `0 0 10px ${CG.crimson}`,
        }}/>
        <div style={{
          fontFamily: FONT_MONO, fontSize: 11,
          color: CG.dim, letterSpacing: '0.22em',
        }}>ASSISTANT RAG · CYBERGARD</div>
      </div>

      {lines.map((l, i) => {
        const visible = typedProgress > l.delay;
        if (!visible) return null;
        const charP = clamp((typedProgress - l.delay) / 0.25, 0, 1);
        const chars = Math.floor(l.text.length * charP);
        const showCursor = charP < 1;
        const isAI = l.who === 'ai';
        return (
          <div key={i} style={{
            display: 'flex',
            justifyContent: isAI ? 'flex-start' : 'flex-end',
            marginBottom: 12,
          }}>
            <div style={{
              maxWidth: '85%',
              padding: '10px 14px',
              background: isAI ? 'transparent' : CG.crimsonTint,
              border: `1px solid ${isAI ? CG.line : CG.crimson}`,
              borderRadius: 8,
              fontFamily: FONT_BODY,
              fontSize: 15,
              color: isAI ? CG.text : CG.crimsonSoft,
              lineHeight: 1.4,
            }}>
              {l.text.slice(0, chars)}
              {showCursor && (
                <span style={{
                  display: 'inline-block',
                  width: 8, height: 16,
                  background: isAI ? CG.crimson : CG.crimsonSoft,
                  marginLeft: 2,
                  verticalAlign: 'text-bottom',
                  opacity: Math.floor(Date.now() / 250) % 2,
                }}/>
              )}
            </div>
          </div>
        );
      })}
    </div>
  );
}

// ── Live control feed ───────────────────────────────────────────────────────
function ControlFeed({ localTime }) {
  const controls = [
    { id: 'A.5.23', name: 'Sécurité services cloud', status: 'OK' },
    { id: 'A.8.9',  name: 'Gestion configuration',   status: 'OK' },
    { id: 'A.8.28', name: 'Codage sécurisé',          status: 'OK' },
    { id: 'A.5.30', name: 'Préparation TIC',          status: 'OK' },
    { id: 'A.8.16', name: 'Surveillance activités',   status: 'OK' },
    { id: 'A.6.3',  name: 'Sensibilisation',          status: 'OK' },
    { id: 'A.8.2',  name: 'Droits d\'accès',          status: 'OK' },
  ];

  return (
    <div style={{
      width: 480,
      background: CG.panel,
      border: `1px solid ${CG.line}`,
      borderRadius: 12,
      padding: 22,
      boxShadow: '0 20px 60px rgba(0,0,0,0.4)',
    }}>
      <div style={{
        display: 'flex', justifyContent: 'space-between', alignItems: 'center',
        paddingBottom: 14, borderBottom: `1px solid ${CG.line}`,
        marginBottom: 14,
      }}>
        <div style={{
          fontFamily: FONT_MONO, fontSize: 11,
          color: CG.dim, letterSpacing: '0.22em',
        }}>CONTRÔLES · TEMPS RÉEL</div>
        <div style={{
          display: 'flex', alignItems: 'center', gap: 6,
          fontFamily: FONT_MONO, fontSize: 10,
          color: CG.crimsonSoft, letterSpacing: '0.18em',
        }}>
          <span style={{
            width: 6, height: 6, borderRadius: 3,
            background: CG.crimson,
            boxShadow: `0 0 6px ${CG.crimson}`,
            opacity: 0.5 + 0.5 * Math.sin(localTime * 4),
          }}/>
          LIVE
        </div>
      </div>

      {controls.map((c, i) => {
        const appearAt = i * 0.18;
        if (localTime < appearAt) return null;
        const p = clamp((localTime - appearAt) / 0.35, 0, 1);
        const eased = Easing.easeOutCubic(p);
        return (
          <div key={i} style={{
            display: 'flex', alignItems: 'center', gap: 14,
            padding: '9px 0',
            borderBottom: i < controls.length - 1 ? `1px solid ${CG.line}` : 'none',
            opacity: eased,
            transform: `translateX(${(1 - eased) * 20}px)`,
          }}>
            <div style={{
              fontFamily: FONT_MONO, fontSize: 11,
              color: CG.mute, width: 64,
              letterSpacing: '0.05em',
            }}>{c.id}</div>
            <div style={{
              flex: 1,
              fontFamily: FONT_BODY, fontSize: 14,
              color: CG.text,
            }}>{c.name}</div>
            <div style={{
              padding: '3px 10px',
              background: CG.crimsonTint,
              border: `1px solid ${CG.crimson}`,
              borderRadius: 4,
              fontFamily: FONT_MONO, fontSize: 10,
              color: CG.crimsonSoft,
              letterSpacing: '0.15em',
              boxShadow: `0 0 10px ${CG.crimsonGlow}`,
            }}>{c.status}</div>
          </div>
        );
      })}
    </div>
  );
}

// ── Scene 3: AI Dashboard ──────────────────────────────────────────────────
function AIDashboard({ start, end, headline }) {
  return (
    <Sprite start={start} end={end}>
      {({ localTime, duration }) => {
        const enter = Easing.easeOutCubic(clamp(localTime / 0.8, 0, 1));
        const exit = localTime > duration - 0.8
          ? clamp((localTime - (duration - 0.8)) / 0.8, 0, 1)
          : 0;

        // Score climbs from 34 to 96
        const scoreT = clamp((localTime - 0.6) / 3.5, 0, 1);
        const score = 34 + Easing.easeOutCubic(scoreT) * 62;

        // Framework badge activation order
        const frameworks = [
          { name: 'ISO 27001', sub: '114 CONTRÔLES',  activate: 1.0 },
          { name: 'NIS2',      sub: 'DIRECTIVE UE',    activate: 1.4 },
          { name: 'DORA',      sub: 'RÉSILIENCE OPS',  activate: 1.8 },
          { name: 'CIS V8',    sub: '18 MESURES',       activate: 2.2 },
        ];

        const typedProgress = clamp((localTime - 0.8) / 2.2, 0, 1);
        const feedTime = Math.max(0, localTime - 0.4);

        return (
          <div style={{
            position: 'absolute', inset: 0,
            opacity: enter * (1 - exit),
            transform: `scale(${0.98 + enter * 0.02 - exit * 0.02})`,
          }}>
            {/* Top bar */}
            <div style={{
              position: 'absolute', top: 40, left: 80, right: 80,
              display: 'flex', justifyContent: 'space-between', alignItems: 'center',
            }}>
              {/* Brand logo removed — handled by the site navbar. Empty spacer keeps the date right-aligned via space-between. */}
              <div/>
              <div style={{
                fontFamily: FONT_MONO, fontSize: 11,
                color: CG.mute, letterSpacing: '0.22em',
              }}>AUDIT CONTINU · {new Date().toISOString().slice(0, 10)}</div>
            </div>

            {/* Left: Gauge + frameworks */}
            <div style={{
              position: 'absolute', left: 80, top: 140,
              width: 520,
              display: 'flex', flexDirection: 'column', gap: 24,
            }}>
              <div style={{
                background: CG.panel,
                border: `1px solid ${CG.line}`,
                borderRadius: 12,
                padding: 28,
                display: 'flex', alignItems: 'center', gap: 28,
                boxShadow: '0 20px 60px rgba(0,0,0,0.4)',
              }}>
                <ScoreGauge value={score}/>
                <div style={{ flex: 1 }}>
                  <div style={{
                    fontFamily: FONT_TITLE, fontSize: 15, fontWeight: 700,
                    color: CG.dim, letterSpacing: '0.18em', marginBottom: 10,
                  }}>CONFORMITÉ GLOBALE</div>
                  <div style={{
                    fontFamily: FONT_BODY, fontSize: 16,
                    color: CG.text, lineHeight: 1.5,
                  }}>
                    {Math.round(score) >= 90 ? 'Seuil certifiable atteint.' : 'Analyse en cours…'}
                  </div>
                  <div style={{
                    marginTop: 14,
                    display: 'flex', gap: 8,
                    fontFamily: FONT_MONO, fontSize: 10,
                    color: CG.crimsonSoft, letterSpacing: '0.15em',
                  }}>
                    <div style={{
                      padding: '4px 8px',
                      background: CG.crimsonTint,
                      border: `1px solid ${CG.crimson}`,
                      borderRadius: 4,
                    }}>+{Math.round((score - 34))}% EN 4H</div>
                  </div>
                </div>
              </div>

              <div style={{
                display: 'grid',
                gridTemplateColumns: '1fr 1fr',
                gap: 14,
              }}>
                {frameworks.map((f, i) => {
                  const active = localTime > f.activate;
                  const pActive = active ? clamp((localTime - f.activate) / 0.5, 0, 1) : 0;
                  return (
                    <FrameworkBadge
                      key={i}
                      name={f.name}
                      sub={f.sub}
                      active={active}
                      progress={pActive}
                    />
                  );
                })}
              </div>
            </div>

            {/* Right: RAG + Feed */}
            <div style={{
              position: 'absolute', right: 80, top: 140,
              width: 480,
              display: 'flex', flexDirection: 'column', gap: 20,
            }}>
              <RAGPanel typedProgress={typedProgress}/>
              <ControlFeed localTime={feedTime}/>
            </div>

            {/* Bottom headline */}
            <div style={{
              position: 'absolute', bottom: 50, left: 0, right: 0,
              textAlign: 'center',
            }}>
              <div style={{
                fontFamily: FONT_TITLE, fontSize: 34, fontWeight: 700,
                color: CG.text, letterSpacing: '0.04em',
                opacity: clamp((localTime - 3.2) / 0.7, 0, 1),
                textShadow: `0 0 24px ${CG.crimsonGlow}`,
              }}>{headline}</div>
              <div style={{
                marginTop: 10,
                fontFamily: FONT_MONO, fontSize: 12,
                color: CG.crimsonSoft, letterSpacing: '0.3em',
                opacity: clamp((localTime - 3.6) / 0.5, 0, 1),
              }}>CYBERGARD.AI</div>
            </div>
          </div>
        );
      }}
    </Sprite>
  );
}

Object.assign(window, { ScoreGauge, FrameworkBadge, RAGPanel, ControlFeed, AIDashboard });
