// Screen Chat IA — version canvas (données statiques pour les artboards)
// La version live (live-chat.jsx) surcharge ChatListening et ChatResponse.

// ── Rendu markdown léger (réponses IA) ─────────────────────────────────────
// Couvre juste ce que Claude produit en pratique dans ce contexte : **gras**,
// titres # ## ###, listes à puces -/*. Pas de lib externe pour rester sans
// bundler ; un parseur ligne par ligne suffit largement à ce besoin borné.
function parseInlineMarkdown(str, keyPrefix) {
  const parts = String(str).split(/(\*\*[^*]+\*\*)/g).filter((s) => s !== '');
  return parts.map((part, i) => (
    part.startsWith('**') && part.endsWith('**') && part.length > 4
      ? <strong key={keyPrefix + '-b' + i}>{part.slice(2, -2)}</strong>
      : <React.Fragment key={keyPrefix + '-t' + i}>{part}</React.Fragment>
  ));
}
function renderMarkdown(text) {
  const lines = (text || '').split('\n');
  const nodes = [];
  let listBuffer = [];
  const flushList = (key) => {
    if (listBuffer.length) {
      nodes.push(<ul key={'ul-' + key} style={{ margin: '4px 0', paddingLeft: 18 }}>{listBuffer}</ul>);
      listBuffer = [];
    }
  };
  lines.forEach((line, i) => {
    const heading = /^(#{1,3})\s+(.*)$/.exec(line);
    const bullet = /^[-*]\s+(.*)$/.exec(line);
    if (heading) {
      flushList(i);
      const level = heading[1].length;
      const fontSize = level === 1 ? 17 : level === 2 ? 16 : 15;
      nodes.push(
        <div key={'h-' + i} style={{ fontWeight: 700, fontSize, margin: i === 0 ? '0 0 4px' : '10px 0 4px' }}>
          {parseInlineMarkdown(heading[2], 'h' + i)}
        </div>
      );
    } else if (bullet) {
      listBuffer.push(<li key={'li-' + i} style={{ marginBottom: 2 }}>{parseInlineMarkdown(bullet[1], 'li' + i)}</li>);
    } else if (line.trim() === '') {
      flushList(i);
      nodes.push(<div key={'sp-' + i} style={{ height: 6 }} />);
    } else {
      flushList(i);
      nodes.push(<div key={'p-' + i}>{parseInlineMarkdown(line, 'p' + i)}</div>);
    }
  });
  flushList('end');
  return nodes;
}

// ── Copier dans le presse-papiers ────────────────────────────────────────
// Bouton icône partagé bulle utilisateur/IA : copie `getText()` (calculé au
// clic, pas au montage, pour toujours copier le texte affiché à l'instant),
// bascule brièvement sur une coche verte pour confirmer visuellement.
function CopyButton({ getText }) {
  useLucide();
  const t = useTranslation();
  const [copied, setCopied] = useState(false);
  const handleCopy = async () => {
    try {
      await navigator.clipboard.writeText(getText());
      setCopied(true);
      setTimeout(() => setCopied(false), 1500);
    } catch {}
  };
  return (
    <button
      onClick={handleCopy}
      title={copied ? t('chat.copied') : t('chat.copy')}
      style={{
        width: 22, height: 22, borderRadius: '50%', border: 'none', flexShrink: 0,
        background: 'transparent', display: 'flex', alignItems: 'center', justifyContent: 'center',
        cursor: 'pointer',
      }}
    >
      <Icon name={copied ? 'check' : 'copy'} size={13} color={copied ? '#27AE60' : 'var(--sm-ink-500)'} />
    </button>
  );
}

// ── Bulle utilisateur ──────────────────────────────────────────────────────
// onEdit (optionnel) : callback(text) déclenché par le bouton "Modifier" —
// n'existe que côté live-chat.jsx (préremplit le champ de saisie), absent
// sur le canvas statique donc le bouton ne s'affiche pas là où il ne
// servirait à rien.
function ChatUserBubble({ text, image, onEdit }) {
  useLucide();
  const t = useTranslation();
  return (
    <div style={{ display: 'flex', flexDirection: 'column', alignItems: 'flex-end' }}>
      <div style={{
        maxWidth: '78%',
        padding: image ? 6 : '10px 14px',
        borderRadius: '18px 18px 4px 18px',
        background: 'var(--sm-blue)',
        color: 'white',
        fontSize: 15,
        lineHeight: 1.5,
      }}>
        {image && (
          <img
            src={image}
            alt="Photo envoyée"
            style={{ display: 'block', maxWidth: '100%', borderRadius: 14, marginBottom: text ? 6 : 0 }}
          />
        )}
        {text}
      </div>
      {text && (
        <div style={{ display: 'flex', alignItems: 'center', gap: 2, marginTop: 2 }}>
          {onEdit && (
            <button
              onClick={() => onEdit(text)}
              title={t('common.edit')}
              style={{ width: 22, height: 22, borderRadius: '50%', border: 'none', background: 'transparent', display: 'flex', alignItems: 'center', justifyContent: 'center', cursor: 'pointer' }}
            >
              <Icon name="pencil" size={13} color="var(--sm-ink-500)" />
            </button>
          )}
          <CopyButton getText={() => text} />
        </div>
      )}
    </div>
  );
}

// ── Bulle IA ────────────────────────────────────────────────────────────────
function ChatAIBubble({ text, actions, loading, id, lang }) {
  useLucide();
  const t = useTranslation();
  const speechId = id != null ? id : text;
  const isSpeaking = useSpeechActive(speechId);
  const speechUnavailable = useSpeechUnavailable(speechId);
  return (
    <div style={{ display: 'flex', gap: 8, alignItems: 'flex-start' }}>
      <div style={{
        width: 32, height: 32, borderRadius: 10,
        background: 'var(--sm-ink)',
        display: 'flex', alignItems: 'center', justifyContent: 'center',
        flexShrink: 0, marginTop: 2,
      }}>
        <Icon name="sparkles" size={14} color="white" />
      </div>
      <div style={{ flex: 1, minWidth: 0 }}>
        <div style={{
          display: 'flex', alignItems: 'flex-start', gap: 6,
          padding: '10px 14px',
          borderRadius: '4px 18px 18px 18px',
          background: 'white',
          border: '1px solid var(--sm-line)',
          fontSize: 15,
          lineHeight: 1.55,
          color: 'var(--sm-ink)',
        }}>
          <div className="sm-chat-selectable" style={{ flex: 1, minWidth: 0 }}>
            {loading ? <ChatTypingDots /> : renderMarkdown(text)}
          </div>
          {!loading && text && (
            <button
              onClick={() => speakText(speechId, text, lang)}
              title={isSpeaking ? t('chat.stop_reading') : t('chat.read_aloud')}
              style={{
                width: 24, height: 24, borderRadius: '50%', flexShrink: 0, marginTop: 1,
                background: isSpeaking ? 'var(--sm-blue)' : 'var(--sm-paper-2)',
                border: 'none', display: 'flex', alignItems: 'center', justifyContent: 'center',
                cursor: 'pointer',
              }}
            >
              <Icon name={isSpeaking ? 'volume-2' : 'volume-1'} size={13} color={isSpeaking ? 'white' : 'var(--sm-ink-500)'} />
            </button>
          )}
        </div>

        {!loading && text && (
          <div style={{ display: 'flex', alignItems: 'center', marginTop: 2, marginLeft: 2 }}>
            <CopyButton getText={() => stripMarkdownForSpeech(text)} />
          </div>
        )}

        {!loading && speechUnavailable && (
          <div style={{ fontSize: 11, color: 'var(--sm-ink-400)', marginTop: 4, marginLeft: 4 }}>
            {t('chat.speech_unavailable')}
          </div>
        )}

        {!loading && actions && actions.length > 0 && (
          <div style={{ display: 'flex', flexDirection: 'column', gap: 6, marginTop: 8 }}>
            {actions.map((a, i) =>
              a.type === 'call' ? (
                <a key={i} href={'tel:' + a.number} style={{
                  display: 'flex', alignItems: 'center', gap: 8,
                  padding: '9px 14px', borderRadius: 12,
                  background: 'var(--sm-soft-red)',
                  textDecoration: 'none', color: 'var(--sm-red)',
                }}>
                  <Icon name="phone" size={16} color="var(--sm-red)" />
                  <span style={{ fontWeight: 600, fontSize: 14 }}>{a.label}</span>
                  <Icon name="arrow-right" size={13} color="var(--sm-red)" style={{ marginLeft: 'auto' }} />
                </a>
              ) : null
            )}
          </div>
        )}
      </div>
    </div>
  );
}

// ── Indicateur "réflexion" — 3 points, aucun texte ─────────────────────────
function ChatTypingDots() {
  return (
    <span style={{ display: 'inline-flex', alignItems: 'center', gap: 3, height: 20 }}>
      {[0, 1, 2].map((i) => (
        <span
          key={i}
          className="sm-typing-dot"
          style={{ fontSize: 22, lineHeight: '10px', color: 'var(--sm-ink-400)', animationDelay: (i * 0.2) + 's' }}
        >
          ·
        </span>
      ))}
    </span>
  );
}

// ── Écran chat unifié (canvas — statique) ──────────────────────────────────
function ChatScreen({ nav, lang }) {
  useLucide();

  const demoMessages = [
    {
      role: 'assistant',
      text: 'Bonjour ! Je suis votre assistant de premiers secours. Décrivez ce qui se passe, je vous guide.',
      actions: [],
    },
    { role: 'user', text: 'Mon père est tombé et ne répond plus' },
    {
      role: 'assistant',
      text: 'Inconscience. Restez calme.\n1. Parlez fort, secouez doucement les épaules.\n2. Vérifiez la respiration : joue près de la bouche, 10 secondes.\n3. S\'il respire → Position Latérale de Sécurité.\n4. S\'il ne respire pas → appelez le 185 et commencez le massage cardiaque.',
      actions: [
        { type: 'call', label: 'Appeler SAMU 185', number: '185' },
      ],
    },
  ];

  return (
    <div style={{ position: 'absolute', inset: 0, background: 'var(--sm-paper)', display: 'flex', flexDirection: 'column' }}>

      {/* En-tête */}
      <div style={{
        display: 'flex', alignItems: 'center', gap: 10,
        padding: '12px 14px 10px',
        background: 'white',
        borderBottom: '1px solid var(--sm-line)',
      }}>
        <button
          onClick={() => goBack(nav)}
          style={{ width: 36, height: 36, borderRadius: '50%', display: 'flex', alignItems: 'center', justifyContent: 'center', background: 'var(--sm-paper-2)', border: 'none', flexShrink: 0, cursor: 'pointer' }}
        >
          <Icon name="arrow-left" size={18} />
        </button>
        <div style={{
          width: 38, height: 38, borderRadius: 11,
          background: 'var(--sm-ink)',
          display: 'flex', alignItems: 'center', justifyContent: 'center',
          flexShrink: 0,
        }}>
          <Icon name="sparkles" size={17} color="white" />
        </div>
        <div style={{ flex: 1, minWidth: 0 }}>
          <div style={{ fontWeight: 700, fontSize: 15 }}>Assistant Sauv'Moi</div>
          <div style={{ display: 'flex', alignItems: 'center', gap: 5, marginTop: 1 }}>
            <div style={{ width: 7, height: 7, borderRadius: '50%', background: '#22c55e' }} />
            <span style={{ fontSize: 12, color: 'var(--sm-ink-500)' }}>En ligne</span>
          </div>
        </div>
      </div>

      {/* Zone messages */}
      <div style={{ flex: 1, overflowY: 'auto', padding: '16px 14px', display: 'flex', flexDirection: 'column', gap: 14 }}>
        {demoMessages.map((m, i) =>
          m.role === 'user'
            ? <ChatUserBubble key={i} text={m.text} />
            : <ChatAIBubble key={i} text={m.text} actions={m.actions} />
        )}
      </div>

      {/* Barre de saisie */}
      <div style={{ padding: '10px 12px 22px', borderTop: '1px solid var(--sm-line)', background: 'white' }}>
        <div style={{
          display: 'flex', alignItems: 'center', gap: 8,
          padding: '8px 10px',
          borderRadius: 24,
          border: '1.5px solid var(--sm-line)',
          background: 'var(--sm-paper)',
        }}>
          <input
            placeholder="Décrivez la situation..."
            style={{ flex: 1, border: 'none', outline: 'none', background: 'transparent', fontSize: 15, fontFamily: 'inherit', padding: '2px 0' }}
          />
          <button style={{ width: 32, height: 32, borderRadius: '50%', background: 'var(--sm-paper-2)', border: 'none', display: 'flex', alignItems: 'center', justifyContent: 'center' }}>
            <Icon name="image" size={16} color="var(--sm-ink-400)" />
          </button>
          <button style={{ width: 32, height: 32, borderRadius: '50%', background: 'var(--sm-soft-red)', border: 'none', display: 'flex', alignItems: 'center', justifyContent: 'center' }}>
            <Icon name="mic" size={16} color="var(--sm-red)" />
          </button>
          <button style={{ width: 32, height: 32, borderRadius: '50%', background: 'var(--sm-blue)', border: 'none', display: 'flex', alignItems: 'center', justifyContent: 'center' }}>
            <Icon name="arrow-up" size={16} color="white" />
          </button>
        </div>
        <p style={{ fontSize: 11, color: 'var(--sm-ink-400)', textAlign: 'center', marginTop: 7 }}>
          Urgence vitale → 185 (SAMU) · 180 (pompiers) · 110 (police)
        </p>
      </div>
    </div>
  );
}

// ── Desktop artboard ────────────────────────────────────────────────────────
function ChatDesktop({ nav, lang }) {
  useLucide();

  return (
    <>
      {/* Sidebar conversations */}
      <aside style={{
        width: 280, background: 'var(--sm-paper-2)',
        borderRight: '1px solid var(--sm-line)',
        display: 'flex', flexDirection: 'column', flexShrink: 0,
      }}>
        <div style={{ padding: '20px 18px 12px', display: 'flex', alignItems: 'center', gap: 10 }}>
          <div style={{ width: 30, height: 30, borderRadius: 7, background: 'var(--sm-ink)', display: 'flex', alignItems: 'center', justifyContent: 'center' }}>
            <Icon name="life-buoy" size={18} color="var(--sm-paper)" />
          </div>
          <span className="sm-serif" style={{ fontSize: 19 }}>Sauv'Moi</span>
          <button onClick={() => nav.go('home')} style={{ marginLeft: 'auto', width: 28, height: 28, borderRadius: 6, display: 'flex', alignItems: 'center', justifyContent: 'center', border: '1px solid var(--sm-line)' }}>
            <Icon name="arrow-left" size={14} />
          </button>
        </div>
        <div style={{ padding: '4px 14px 14px' }}>
          <button className="sm-btn sm-btn-ink" style={{ width: '100%', borderRadius: 8, padding: '10px 14px', fontSize: 14 }}>
            <Icon name="plus" size={16} /> Nouvelle conversation
          </button>
        </div>
        <div style={{ flex: 1, overflowY: 'auto', padding: '0 8px 14px' }}>
          {[
            { title: 'Inconscience · père', sub: 'il y a 2 min', active: true },
            { title: 'Brûlure légère · main', sub: 'il y a 1 h' },
            { title: 'Massage cardiaque · rythme', sub: 'hier 19:04' },
            { title: "Crise d'asthme — enfant", sub: 'lun. 16:48' },
          ].map((c, i) => (
            <button key={i} style={{
              display: 'flex', flexDirection: 'column', alignItems: 'flex-start',
              width: '100%', padding: '8px 10px', borderRadius: 8,
              background: c.active ? 'white' : 'transparent',
              boxShadow: c.active ? 'var(--shadow-1)' : 'none',
              border: c.active ? '1px solid var(--sm-line)' : '1px solid transparent',
              textAlign: 'left', gap: 2, marginTop: i === 0 ? 10 : 2,
            }}>
              <span style={{ fontSize: 13, fontWeight: c.active ? 600 : 500 }}>{c.title}</span>
              <span style={{ fontSize: 11, color: 'var(--sm-ink-500)' }}>{c.sub}</span>
            </button>
          ))}
        </div>
        <div style={{ padding: 14, borderTop: '1px solid var(--sm-line)', display: 'flex', alignItems: 'center', gap: 10 }}>
          <div className="sm-photo-placeholder" style={{ width: 32, height: 32, borderRadius: '50%' }}>
            <span style={{ position: 'relative', zIndex: 1, fontSize: 12, fontWeight: 600 }}>AK</span>
          </div>
          <div style={{ flex: 1, lineHeight: 1.2 }}>
            <div style={{ fontSize: 13, fontWeight: 600 }}>Aïcha Kouassi</div>
            <div style={{ fontSize: 11, color: 'var(--sm-ink-500)' }}>Citoyenne · Abidjan</div>
          </div>
          <Icon name="settings" size={16} color="var(--sm-ink-500)" />
        </div>
      </aside>

      {/* Zone principale */}
      <div style={{ flex: 1, display: 'flex', flexDirection: 'column', minWidth: 0, background: 'var(--sm-paper)' }}>
        <header style={{ display: 'flex', alignItems: 'center', justifyContent: 'space-between', padding: '18px 32px', borderBottom: '1px solid var(--sm-line)', background: 'white' }}>
          <div>
            <h2 className="sm-serif" style={{ fontSize: 22 }}>Inconscience · père</h2>
            <div style={{ fontSize: 12, color: 'var(--sm-ink-500)', marginTop: 4 }}>
              <span>FR</span> · 4 messages · <span style={{ fontFamily: 'var(--font-mono)' }}>protocoles PSC1</span>
            </div>
          </div>
          <div style={{ display: 'flex', gap: 10 }}>
            <a href="tel:185" className="sm-btn sm-btn-primary" style={{ padding: '8px 14px', minHeight: 36, fontSize: 13, textDecoration: 'none', display: 'flex', alignItems: 'center', gap: 6 }}>
              <Icon name="phone" size={16} /> Appeler le 185
            </a>
          </div>
        </header>

        <div style={{ flex: 1, overflowY: 'auto', padding: '24px 32px' }}>
          <div style={{ maxWidth: 680, margin: '0 auto', display: 'flex', flexDirection: 'column', gap: 16 }}>
            <ChatAIBubble text="Bonjour ! Je suis votre assistant de premiers secours. Décrivez ce qui se passe, je vous guide." actions={[]} />
            <ChatUserBubble text="Mon père est tombé et ne répond plus" />
            <ChatAIBubble
              text={'Inconscience. Restez calme.\n1. Parlez fort, secouez doucement les épaules.\n2. Vérifiez la respiration : joue près de la bouche, 10 secondes.\n3. S\'il respire → Position Latérale de Sécurité.\n4. S\'il ne respire pas → appelez le 185 et commencez le massage cardiaque.'}
              actions={[{ type: 'call', label: 'Appeler SAMU 185', number: '185' }]}
            />
          </div>
        </div>

        <div style={{ padding: '14px 32px 20px', borderTop: '1px solid var(--sm-line)', background: 'white' }}>
          <div style={{ maxWidth: 680, margin: '0 auto' }}>
            <div style={{ display: 'flex', alignItems: 'center', gap: 10, padding: '10px 14px', borderRadius: 14, border: '1px solid var(--sm-line)', background: 'var(--sm-paper)', boxShadow: 'var(--shadow-1)' }}>
              <input placeholder="Décrivez la situation… ou parlez (FR · EN)" style={{ flex: 1, border: 'none', outline: 'none', background: 'transparent', fontSize: 15, fontFamily: 'inherit', padding: '6px 4px' }} />
              <button style={{ width: 32, height: 32, borderRadius: 8, background: 'var(--sm-paper-2)', border: 'none', display: 'flex', alignItems: 'center', justifyContent: 'center' }}>
                <Icon name="image" size={17} color="var(--sm-ink-400)" />
              </button>
              <button style={{ width: 32, height: 32, borderRadius: 8, background: 'var(--sm-soft-red)', border: 'none', display: 'flex', alignItems: 'center', justifyContent: 'center' }}>
                <Icon name="mic" size={17} color="var(--sm-red)" />
              </button>
              <button style={{ width: 34, height: 34, borderRadius: 8, background: 'var(--sm-ink)', border: 'none', display: 'flex', alignItems: 'center', justifyContent: 'center' }}>
                <Icon name="arrow-up" size={18} color="var(--sm-paper)" />
              </button>
            </div>
            <p style={{ fontSize: 11, color: 'var(--sm-ink-400)', textAlign: 'center', marginTop: 10 }}>
              L'IA peut se tromper. Pour une urgence vitale, appelez le 185 (SAMU) ou le 180 (pompiers).
            </p>
          </div>
        </div>
      </div>
    </>
  );
}

const ChatListening = ChatScreen;
const ChatResponse = ChatScreen;

Object.assign(window, {
  ChatListening, ChatResponse, ChatDesktop, ChatScreen,
  ChatUserBubble, ChatAIBubble, ChatTypingDots,
});
