// chatflix-ui.jsx — original Chatflix product UI primitives
// Brand mark: public/logos/chatflix-final-logo.png

const { useState, useEffect, useRef } = React;

// ─── Brand mark (logo asset) ───────────────────────────────────
function CMark({ size = 36, radius: _r }) {
  const aspect = 1;
  const w = Math.round(size * aspect);
  return (
    <img
      src="/logos/chatflix-final-logo.png"
      alt="Chatflix"
      width={w}
      height={size}
      style={{
        width: w,
        height: size,
        objectFit: 'contain',
        display: 'block',
        flexShrink: 0,
      }}
    />
  );
}

// ─── Routing badge "Chatflix Auto" ───────────────────────────────
function AutoBadge({ subtitle = '1.0' }) {
  return (
    <div className="auto-badge" style={{
      display: 'inline-flex', alignItems: 'center', gap: 8,
      height: 32, padding: '0 12px 0 8px',
      borderRadius: 999,
      background: 'rgba(37, 99, 235, 0.08)',
      border: '1px solid rgba(37, 99, 235, 0.18)',
      color: '#2563EB', fontSize: 13, fontWeight: 500,
      letterSpacing: '-0.005em',
    }}>
      <span style={{
        width: 18, height: 18, borderRadius: 999,
        background: 'linear-gradient(135deg, #2563EB, #4C7DFF)',
        display: 'flex', alignItems: 'center', justifyContent: 'center',
        color: '#fff', fontSize: 11, fontWeight: 700,
      }}>A</span>
      Chatflix Auto · {subtitle}
    </div>
  );
}

// ─── Model pill (monospace, neutral) ─────────────────────────────
function ModelPill({ name, dim = false, glow = false, iconSrc, className }) {
  return (
    <div className={className} style={{
      display: 'inline-flex', alignItems: 'center', gap: 6,
      height: 28, padding: '0 10px',
      borderRadius: 8,
      background: dim ? 'rgba(255,255,255,0.5)' : 'rgba(255,255,255,0.85)',
      border: '1px solid rgba(10,10,15,0.06)',
      fontFamily: "'JetBrains Mono', ui-monospace, monospace",
      fontSize: 11.5,
      color: dim ? '#8A8A95' : '#0A0A0F',
      letterSpacing: 0,
      whiteSpace: 'nowrap',
      transition: 'all 510ms ease',
      opacity: dim ? 0.5 : 1,
      boxShadow: glow ? '0 0 0 3px rgba(37,99,235,0.12), 0 4px 12px rgba(37,99,235,0.18)' : 'none',
    }}>
      {iconSrc ? (
        <img
          src={iconSrc}
          alt=""
          width={14}
          height={14}
          draggable={false}
          decoding="async"
          style={{
            width: 14,
            height: 14,
            objectFit: 'contain',
            flexShrink: 0,
            display: 'block',
            opacity: dim ? 0.55 : 1,
          }}
        />
      ) : (
        <span style={{
          width: 6, height: 6, borderRadius: 99,
          background: glow ? '#2563EB' : (dim ? '#C5C5CC' : '#0A0A0F'),
          flexShrink: 0,
        }} />
      )}
      {name}
    </div>
  );
}

// The "20+ models" pool — neutral monospace IDs only
const MODEL_NAMES = [
  'claude-opus-4.7', 'claude-sonnet-4.6', 'gpt-5.4', 'gpt-5-mini',
  'gemini-3.1-pro', 'gemini-flash', 'grok-4', 'mistral-large',
  'deepseek-v4', 'perplexity-sonar', 'qwen-3-max', 'kimi-k3',
  'llama-4', 'command-r+', 'phi-4', 'jamba-1.7'
];

// ─── Composer (the input bar w/ Auto badge) ──────────────────────
function Composer({ value = '', placeholder = 'Pose ta question…', autoLabel, ghostText = '' }) {
  return (
    <div style={{
      background: '#FFFFFF',
      border: '1px solid rgba(10,10,15,0.08)',
      borderRadius: 32,
      padding: '14px 16px 12px',
    }}>
      <div style={{
        minHeight: 28, fontSize: 16, color: value ? '#0A0A0F' : '#8A8A95',
        letterSpacing: '-0.005em', lineHeight: 1.4,
      }}>
        {value || placeholder}
        {ghostText && <span style={{ color: '#C5C5CC' }}>{ghostText}</span>}
        {value && <span className="caret" style={{
          display: 'inline-block', width: 2, height: 18, background: '#2563EB',
          marginLeft: 2, verticalAlign: 'middle', animation: 'caret 1s steps(1) infinite',
        }} />}
      </div>
      <div style={{
        display: 'flex', alignItems: 'center', justifyContent: 'space-between',
        marginTop: 12, gap: 12,
      }}>
        <div style={{ display: 'flex', gap: 6, alignItems: 'center' }}>
          <AutoBadge subtitle={autoLabel || '1.0'} />
          <button type="button" className="composer-ghost-btn" style={iconBtn}>
            <svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2">
              <path d="M21 15a2 2 0 0 1-2 2H7l-4 4V5a2 2 0 0 1 2-2h14a2 2 0 0 1 2 2z"/>
            </svg>
          </button>
          <button type="button" className="composer-ghost-btn" style={iconBtn}>
            <svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2">
              <path d="M21.44 11.05l-9.19 9.19a6 6 0 0 1-8.49-8.49l9.19-9.19a4 4 0 0 1 5.66 5.66l-9.2 9.19a2 2 0 0 1-2.83-2.83l8.49-8.48"/>
            </svg>
          </button>
        </div>
        <button type="button" className="composer-send-btn" style={{
          width: 36, height: 36, borderRadius: 999,
          background: '#2563EB', border: 'none', color: '#fff',
          display: 'flex', alignItems: 'center', justifyContent: 'center',
        }}>
          <svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2.5" strokeLinecap="round">
            <path d="M5 12h14M13 5l7 7-7 7"/>
          </svg>
        </button>
      </div>
    </div>
  );
}

const iconBtn = {
  width: 28, height: 28, borderRadius: 999,
  background: 'transparent', border: 'none',
  color: '#8A8A95', display: 'inline-flex', alignItems: 'center', justifyContent: 'center',
};

// ─── Model row (the cascade under the composer) ──────────────────
function ModelRow({ models, glowIndex = -1 }) {
  return (
    <div style={{
      display: 'flex', flexWrap: 'wrap', gap: 6,
      justifyContent: 'center', maxWidth: 720,
    }}>
      {models.map((m, i) =>
      <div key={m} className="model-row-pill-slot" style={{ ['--pill-i']: i }}>
        <ModelPill name={m} dim={glowIndex >= 0 && i !== glowIndex} glow={i === glowIndex} />
      </div>
      )}
    </div>
  );
}

// ─── Maquette produit (hero) — rail d’icônes étroit ─────────────
function MockRailIcon({ children }) {
  return (
    <svg
      viewBox="0 0 24 24"
      width={22}
      height={22}
      fill="none"
      stroke="currentColor"
      strokeWidth={1.75}
      strokeLinecap="round"
      strokeLinejoin="round"
      style={{ display: 'block' }}
    >
      {children}
    </svg>
  );
}

function MockIconRail() {
  const items = [
    <><circle cx="11" cy="11" r="7" /><path d="M21 21l-4.2-4.2" /></>, // search
    <><path d="M12 20h9M16.5 3.5a2.12 2.12 0 013 3L7 19l-4 1 1-4L16.5 3.5z" /></>, // edit
    <><path d="M21 15a2 2 0 01-2 2H7l-4 4V5a2 2 0 012-2h14a2 2 0 012 2z" /></>, // chat
    <><polyline points="4 17 10 11 4 5" /><line x1="12" y1="19" x2="20" y2="19" /></>, // terminal-ish
    <><rect x="2" y="7" width="15" height="10" rx="2" /><path d="M17 12l5-3v8l-5-3v-2z" /></>, // video
    <><path d="M22 19a2 2 0 01-2 2H4a2 2 0 01-2-2V5a2 2 0 012-2h5l2 3h9a2 2 0 012 2z" /></>, // folder
    <><path d="M22 16.92v3a2 2 0 01-2.18 2 19.79 19.79 0 01-8.63-3.07 19.5 19.5 0 01-6-6 19.79 19.79 0 01-3.07-8.67A2 2 0 014.11 2h3a2 2 0 012 1.72 12.84 12.84 0 00.7 2.81 2 2 0 01-.45 2.11L8.09 9.91a16 16 0 006 6l1.27-1.27a2 2 0 012.11-.45 12.84 12.84 0 002.81.7A2 2 0 0122 16.92z" /></>, // phone
    <><path d="M9 18V5l12-2v15M9 9l12-2" /></>, // music
  ];
  return (
    <div style={{
      width: 52,
      flexShrink: 0,
      padding: '16px 0 12px',
      display: 'flex',
      flexDirection: 'column',
      alignItems: 'center',
      gap: 6,
      borderRight: '1px solid rgba(10,10,15,0.06)',
      background: 'rgba(255,255,255,0.45)',
    }}>
      {items.map((paths, i) => (
        <button key={i} type="button" style={{
          width: 40, height: 40, borderRadius: 999, border: 'none',
          background: i === 2 ? 'rgba(37,99,235,0.08)' : 'transparent',
          color: i === 2 ? '#2563EB' : '#6B6B76',
          cursor: 'default',
          display: 'flex', alignItems: 'center', justifyContent: 'center',
        }}>
          <MockRailIcon>{paths}</MockRailIcon>
        </button>
      ))}
      <div style={{ flex: 1, minHeight: 12 }} />
      <div style={{
        width: 32, height: 32, borderRadius: 999,
        background: 'linear-gradient(135deg, #FF6B9D, #C44CFF)',
        color: '#fff', fontSize: 11, fontWeight: 700,
        display: 'flex', alignItems: 'center', justifyContent: 'center',
        letterSpacing: '-0.02em',
      }}>TA</div>
    </div>
  );
}

// ─── Maquette — champ unique type « pilule » ─────────────────────
function MockPillComposer() {
  return (
    <div style={{
      display: 'flex',
      alignItems: 'center',
      gap: 10,
      padding: '8px 12px 8px 10px',
      background: '#FFFFFF',
      borderRadius: 999,
      border: '1px solid rgba(10,10,15,0.07)',
      boxShadow: '0 6px 28px rgba(15,23,42,0.06), 0 1px 3px rgba(0,0,0,0.04)',
      width: '100%',
      maxWidth: 580,
      boxSizing: 'border-box',
    }}>
      <button type="button" style={{
        width: 34, height: 34, borderRadius: 999, flexShrink: 0,
        border: 'none', background: 'rgba(10,10,15,0.05)',
        color: '#3A3A45', fontSize: 20, fontWeight: 400, lineHeight: 1,
        cursor: 'default', display: 'flex', alignItems: 'center', justifyContent: 'center',
      }} aria-hidden>+</button>
      <span style={{
        flex: 1, fontSize: 15, color: '#A8A8B2', letterSpacing: '-0.01em',
        whiteSpace: 'nowrap', overflow: 'hidden', textOverflow: 'ellipsis',
      }}>Écris ton message ici...</span>
      <AutoBadge subtitle="1.0" />
      <button type="button" style={{
        width: 42, height: 42, borderRadius: 999, flexShrink: 0,
        border: 'none', background: '#2563EB', color: '#fff',
        cursor: 'default',
        display: 'flex', alignItems: 'center', justifyContent: 'center',
        boxShadow: '0 4px 14px rgba(37,99,235,0.35)',
      }} aria-label="Voix">
        <svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round">
          <path d="M12 3v18M8 7v10M16 7v10M4 11v2M20 11v2" />
        </svg>
      </button>
    </div>
  );
}

// ─── Maquette — bande de logos modèles (style grayscale, tuck scroll) ─
function MockModelLogoStrip({ models, glowIndex = -1 }) {
  return (
    <div style={{
      display: 'flex', flexWrap: 'wrap', gap: 8,
      justifyContent: 'center', maxWidth: 720,
    }}>
      {models.map((name, i) =>
      <div key={name} className="model-row-pill-slot" style={{ ['--pill-i']: i }}>
        <div style={{
          padding: '7px 14px',
          borderRadius: 10,
          fontSize: 11.5,
          fontWeight: 700,
          letterSpacing: '-0.02em',
          fontFamily: 'var(--font-sans)',
          color: glowIndex === i ? '#2563EB' : '#5C5C66',
          background: glowIndex === i ? 'rgba(37,99,235,0.08)' : 'rgba(255,255,255,0.65)',
          border: glowIndex === i ? '1px solid rgba(37,99,235,0.35)' : '1px solid rgba(10,10,15,0.07)',
          boxShadow: glowIndex === i ? '0 0 0 3px rgba(37,99,235,0.12), 0 4px 14px rgba(37,99,235,0.15)' : 'none',
          filter: glowIndex >= 0 && i !== glowIndex ? 'grayscale(1) opacity(0.55)' : 'grayscale(0.85)',
          transition: 'all 420ms ease',
          whiteSpace: 'nowrap',
        }}>{name}</div>
      </div>
      )}
    </div>
  );
}

function MockCategoryChips() {
  const cats = [
    { label: 'Apprendre', d: 'M22 10v6M2 10l10-5 10 5-10 5z M6 12v5c3 3 9 3 12 0v-5' },
    { label: 'Stratégie', d: 'M3 3v18h18 M9 9l12-6M9 9v12M9 9H3' },
    { label: 'Écrire', d: 'M12 19l7-7 3 3-7 7-3-3z M18 13l-6-6a2.828 2.828 0 00-4 4l6 6' },
    { label: 'Vie quotidienne', d: 'M18 8h1a4 4 0 010 8h-1M2 8h16v9a4 4 0 01-4 4H6a4 4 0 01-4-4V8z M6 1v3M10 1v3M14 1v3' },
  ];
  return (
    <div style={{
      display: 'flex', flexWrap: 'wrap', gap: 10,
      justifyContent: 'center', marginTop: 4,
    }}>
      {cats.map((c) => (
        <button key={c.label} type="button" style={{
          display: 'inline-flex', alignItems: 'center', gap: 8,
          padding: '10px 16px',
          borderRadius: 999,
          border: '1px solid rgba(10,10,15,0.08)',
          background: 'rgba(255,255,255,0.85)',
          fontSize: 13.5, fontWeight: 600,
          color: 'var(--ink)',
          letterSpacing: '-0.01em',
          cursor: 'default',
          boxShadow: '0 2px 8px rgba(0,0,0,0.04)',
        }}>
          <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round">
            <path d={c.d} />
          </svg>
          {c.label}
        </button>
      ))}
    </div>
  );
}

// ─── Sidebar (mini) ──────────────────────────────────────────────
function Sidebar() {
  const items = [
    { label: 'Chat', active: true },
    { label: 'Image' },
    { label: 'Vidéo' },
    { label: 'Code' },
    { label: 'Voix' },
    { label: 'Groupes' },
  ];
  return (
    <div style={{
      width: 200, padding: '20px 12px',
      borderRight: '1px solid rgba(10,10,15,0.05)',
      background: 'rgba(255,255,255,0.4)',
      display: 'flex', flexDirection: 'column', gap: 2,
    }}>
      <div style={{ display: 'flex', alignItems: 'center', gap: 10, padding: '6px 10px 18px' }}>
        <CMark size={28} />
        <span style={{ fontSize: 15, fontWeight: 600, letterSpacing: '-0.01em' }}>Chatflix</span>
      </div>
      {items.map(it => (
        <div key={it.label} style={{
          padding: '8px 12px', borderRadius: 8,
          background: it.active ? 'rgba(37,99,235,0.08)' : 'transparent',
          color: it.active ? '#2563EB' : '#3A3A45',
          fontSize: 14, fontWeight: it.active ? 500 : 400,
          letterSpacing: '-0.005em',
        }}>{it.label}</div>
      ))}
      <div style={{ flex: 1 }} />
      <div style={{
        padding: '10px 12px', borderRadius: 10,
        background: 'rgba(10,10,15,0.03)',
        fontSize: 12, color: '#8A8A95',
      }}>
        <div style={{ color: '#0A0A0F', fontWeight: 500, fontSize: 13 }}>Plan Pro</div>
        Mémoire active · 47 facts
      </div>
    </div>
  );
}

// ─── Reveal-on-scroll wrapper ────────────────────────────────────
function Reveal({ children, delay = 0, stagger = false, as = 'div', className = '', style = {} }) {
  const ref = useRef(null);
  useEffect(() => {
    const el = ref.current;
    if (!el) return;
    const io = new IntersectionObserver(([e]) => {
      if (e.isIntersecting) {
        setTimeout(() => el.classList.add('in'), delay);
        io.unobserve(el);
      }
    }, { threshold: 0.15, rootMargin: '0px 0px -80px 0px' });
    io.observe(el);
    return () => io.disconnect();
  }, [delay]);
  const cls = (stagger ? 'reveal-stagger ' : 'reveal ') + className;
  const Tag = as;
  return <Tag ref={ref} className={cls} style={style}>{children}</Tag>;
}

Object.assign(window, {
  CMark,
  AutoBadge,
  ModelPill,
  Composer,
  ModelRow,
  Sidebar,
  MockIconRail,
  MockPillComposer,
  MockModelLogoStrip,
  MockCategoryChips,
  Reveal,
  MODEL_NAMES,
  iconBtn,
});
