// Main chat screen — real send with attachment support
function Chat({
  model, tier, onOpenHistory, onOpenModelPicker, onOpenSettings, onNewChat,
  messages, isStreaming, onSend, onVoice, onUpgrade, attachOpen, setAttachOpen,
}) {
  const S = window.SOL;
  const [input, setInput]   = React.useState('');
  const [pendingAttachments, setPendingAttachments] = React.useState([]);
  const scrollRef  = React.useRef(null);
  const fileImgRef = React.useRef(null);
  const fileCamRef = React.useRef(null);
  const fileVidRef = React.useRef(null);

  React.useEffect(() => {
    if (scrollRef.current) scrollRef.current.scrollTop = scrollRef.current.scrollHeight;
  }, [messages, isStreaming]);

  const currentModel = model === 'apollo'
    ? { name:'Apollo 1', variant:'apollo', tier:'Plus' }
    : { name:'Solus X5',  variant:'solus',  tier:'Free' };

  const send = () => {
    if (!input.trim() && pendingAttachments.length === 0) return;
    onSend(input.trim(), pendingAttachments);
    setInput(''); setPendingAttachments([]);
  };

  const readFile = (file) => new Promise((resolve, reject) => {
    const r = new FileReader();
    r.onload = e => resolve({ dataUrl: e.target.result, name: file.name, type: file.type });
    r.onerror = reject;
    r.readAsDataURL(file);
  });

  const handleFileChange = async (e) => {
    const files = Array.from(e.target.files || []);
    if (!files.length) return;
    const attachments = await Promise.all(files.map(readFile));
    setPendingAttachments(prev => [...prev, ...attachments]);
    setAttachOpen(false);
    e.target.value = '';
  };

  const removeAttachment = (i) => setPendingAttachments(prev => prev.filter((_,idx) => idx !== i));

  return (
    <div style={{ height:'100%', background:S.cream, display:'flex', flexDirection:'column', position:'relative' }}>
      {/* Hidden file inputs */}
      <input ref={fileImgRef} type="file" accept="image/*" multiple style={{display:'none'}} onChange={handleFileChange}/>
      <input ref={fileCamRef} type="file" accept="image/*" capture="environment" style={{display:'none'}} onChange={handleFileChange}/>
      <input ref={fileVidRef} type="file" accept="video/*" style={{display:'none'}} onChange={handleFileChange}/>

      {/* Status bar spacer */}
      <div style={{ height:'env(safe-area-inset-top, 44px)', minHeight:44, flexShrink:0 }}/>

      {/* Header */}
      <div style={{ padding:'8px 16px 10px', display:'flex', alignItems:'center', justifyContent:'space-between', gap:8, borderBottom:`1px solid ${S.line}`, background:S.cream, flexShrink:0 }}>
        <button onClick={onOpenHistory} style={iconBtn()}>
          <Icon name="menu" size={22} color={S.ink}/>
        </button>
        <button key={currentModel.variant} onClick={onOpenModelPicker} style={{
          display:'flex', alignItems:'center', gap:8, padding:'6px 10px 6px 8px', borderRadius:999,
          border:`1px solid ${S.line2}`, background:S.paper, fontFamily:S.sans, fontSize:14, fontWeight:500,
          color:S.ink, cursor:'pointer', animation:'sol-model-pop 0.35s cubic-bezier(0.2,0.8,0.2,1)',
        }}>
          <SunMark size={18} variant={currentModel.variant}/>
          <span>{currentModel.name}</span>
          <span style={{ fontFamily:S.mono, fontSize:9, letterSpacing:0.6, padding:'2px 5px', borderRadius:4, background:currentModel.variant==='apollo'?S.ink:S.cream2, color:currentModel.variant==='apollo'?S.cream:S.ink2 }}>
            {currentModel.tier.toUpperCase()}
          </span>
          <Icon name="chevDown" size={14} color={S.ink3}/>
        </button>
        <button onClick={onNewChat} style={iconBtn()}>
          <Icon name="plus" size={22} color={S.ink}/>
        </button>
      </div>

      {/* Messages */}
      <div ref={scrollRef} style={{ flex:1, overflowY:'auto', padding:'12px 0', paddingBottom: pendingAttachments.length ? 170 : 130 }}>
        {messages.length === 0
          ? <EmptyState model={currentModel.variant} onSuggest={t => { setInput(t); setTimeout(() => onSend(t, []), 60); }}/>
          : messages.map(m => <Message key={m.id} msg={m}/>)
        }
        {isStreaming && <StreamingIndicator/>}
      </div>

      {/* Floating composer */}
      <div style={{ position:'absolute', left:0, right:0, bottom:0, background:`linear-gradient(to top, ${S.cream} 72%, rgba(245,239,228,0))`, padding:'10px 12px', paddingBottom:'calc(10px + env(safe-area-inset-bottom, 16px))', zIndex:5 }}>
        {/* Pending attachment thumbnails */}
        {pendingAttachments.length > 0 && (
          <div style={{ display:'flex', gap:8, marginBottom:8, flexWrap:'wrap' }}>
            {pendingAttachments.map((a, i) => (
              <div key={i} style={{ position:'relative', width:56, height:56, borderRadius:10, overflow:'hidden', border:`1px solid ${S.line2}` }}>
                {a.type.startsWith('video/') ? (
                  <div style={{ width:'100%', height:'100%', background:S.ink, display:'flex', alignItems:'center', justifyContent:'center' }}>
                    <Icon name="video" size={20} color={S.cream}/>
                  </div>
                ) : (
                  <img src={a.dataUrl} alt="" style={{ width:'100%', height:'100%', objectFit:'cover' }}/>
                )}
                <button onClick={() => removeAttachment(i)} style={{ position:'absolute', top:2, right:2, width:18, height:18, borderRadius:'50%', border:'none', background:'rgba(28,26,23,0.7)', cursor:'pointer', display:'flex', alignItems:'center', justifyContent:'center' }}>
                  <Icon name="close" size={10} color="#fff"/>
                </button>
              </div>
            ))}
          </div>
        )}
        <Composer
          value={input} setValue={setInput}
          onSend={send} onAttachClick={() => setAttachOpen(true)}
          onVoice={onVoice} isStreaming={isStreaming}
        />
      </div>

      {/* Attach sheet */}
      {attachOpen && (
        <AttachSheet
          tier={tier} model={model} onClose={() => setAttachOpen(false)} onUpgrade={onUpgrade}
          onCamera={() => fileCamRef.current?.click()}
          onPhoto={() => fileImgRef.current?.click()}
          onVideo={() => fileVidRef.current?.click()}
          onFile={() => fileImgRef.current?.click()}
        />
      )}
    </div>
  );
}

function iconBtn() {
  return { width:40, height:40, borderRadius:12, border:'none', background:'transparent', display:'flex', alignItems:'center', justifyContent:'center', cursor:'pointer' };
}

// Empty state
function EmptyState({ model, onSuggest }) {
  const S = window.SOL;
  const greeting = model==='apollo' ? 'What should Apollo answer?' : 'What are we working on?';
  const suggestions = model==='apollo'
    ? ['Rewrite this paragraph with sharper verbs','Explain this — don\'t hedge','Draft a cold email that doesn\'t sound like one']
    : ['Help me name this project','Summarize this article','Plan a 3-day trip to Kyoto'];
  return (
    <div style={{ height:'100%', display:'flex', flexDirection:'column', alignItems:'center', justifyContent:'center', padding:'40px 28px 60px', textAlign:'center', animation:'sol-fade-in 0.35s ease' }}>
      <div style={{ position:'relative', marginBottom:22 }}>
        <div style={{ position:'absolute', inset:-20, borderRadius:'50%', background:`radial-gradient(circle, rgba(244,199,123,0.28), transparent 70%)`, pointerEvents:'none' }}/>
        <SunMark size={56} variant={model}/>
      </div>
      <div style={{ fontFamily:S.serif, fontSize:30, lineHeight:1.05, letterSpacing:-0.8, color:S.ink }}>{greeting}</div>
      <div style={{ fontFamily:S.sans, fontSize:13.5, color:S.ink3, marginTop:10, maxWidth:260, lineHeight:1.5 }}>Ask anything. Paste a draft. Share a file.</div>
      <div style={{ marginTop:28, display:'flex', flexDirection:'column', gap:8, width:'100%' }}>
        {suggestions.map((s,i) => (
          <button key={i} onClick={() => onSuggest(s)} style={{ padding:'12px 14px', borderRadius:14, background:S.paper, border:`1px solid ${S.line}`, fontFamily:S.sans, fontSize:14, color:S.ink2, textAlign:'left', cursor:'pointer', display:'flex', alignItems:'center', gap:10 }}>
            <Icon name="sparkle" size={14} color={S.amber} strokeWidth={1.6}/>
            <span style={{ flex:1 }}>{s}</span>
            <Icon name="chevRight" size={14} color={S.ink3}/>
          </button>
        ))}
      </div>
    </div>
  );
}

// Single message
function Message({ msg }) {
  const S = window.SOL;
  if (msg.role === 'switch') {
    const fromName = msg.from==='apollo'?'Apollo 1':'Solus X5';
    const toName   = msg.to==='apollo'  ?'Apollo 1':'Solus X5';
    return (
      <div style={{ padding:'16px 28px', display:'flex', alignItems:'center', gap:10, animation:'sol-fade-in 0.35s ease' }}>
        <div style={{ flex:1, height:1, background:S.line }}/>
        <div style={{ display:'flex', alignItems:'center', gap:8, fontFamily:S.mono, fontSize:10, letterSpacing:0.8, textTransform:'uppercase', color:S.ink3 }}>
          <SunMark size={14} variant={msg.from}/><span style={{opacity:.6}}>{fromName}</span>
          <Icon name="chevRight" size={11} color={S.ink3} strokeWidth={1.8}/>
          <SunMark size={14} variant={msg.to}/><span style={{color:S.ink2,fontWeight:500}}>{toName}</span>
        </div>
        <div style={{ flex:1, height:1, background:S.line }}/>
      </div>
    );
  }
  if (msg.role === 'user') {
    return (
      <div style={{ display:'flex', justifyContent:'flex-end', padding:'6px 16px', flexDirection:'column', alignItems:'flex-end', gap:6 }}>
        {msg.attachments?.map((a,i) => (
          a.type?.startsWith('video/')
            ? <div key={i} style={{ maxWidth:'80%', background:S.ink, borderRadius:14, padding:'10px 14px', display:'flex', alignItems:'center', gap:8, color:S.cream, fontFamily:S.sans, fontSize:13 }}>
                <Icon name="video" size={16} color={S.sun}/> {a.name||'Video'}
              </div>
            : <img key={i} src={a.dataUrl} alt="" style={{ maxWidth:'80%', maxHeight:200, borderRadius:14, objectFit:'cover' }}/>
        ))}
        {msg.text && <div style={{ maxWidth:'80%', background:S.ink, color:S.cream, borderRadius:'20px 20px 6px 20px', padding:'10px 14px', fontFamily:S.sans, fontSize:15, lineHeight:1.45, letterSpacing:-0.1, whiteSpace:'pre-wrap' }}>{msg.text}</div>}
      </div>
    );
  }
  return (
    <div style={{ padding:'12px 16px 4px' }}>
      <div style={{ display:'flex', gap:10, alignItems:'flex-start' }}>
        <SunMark size={22} variant={msg.model||'solus'} style={{ marginTop:3, flexShrink:0 }}/>
        <div style={{ flex:1 }}>
          <div style={{ fontFamily:S.sans, fontSize:15, lineHeight:1.55, color:S.ink, letterSpacing:-0.1, whiteSpace:'pre-wrap' }}>
            <AssistantText text={msg.text}/>
            {msg.streaming && <span style={{ display:'inline-block', width:5, height:13, background:S.ink, verticalAlign:'baseline', marginLeft:2, borderRadius:1, opacity:.7, animation:'sol-blink 1s steps(2) infinite' }}/>}
          </div>
          {!msg.streaming && msg.text && (
            <div style={{ display:'flex', gap:4, marginTop:10, marginLeft:-6, opacity:.8 }}>
              {['copy','refresh','thumbUp','thumbDown'].map(ic => (
                <button key={ic} onClick={() => ic==='copy' && navigator.clipboard?.writeText(msg.text)} style={{ width:32, height:32, borderRadius:8, border:'none', background:'transparent', display:'flex', alignItems:'center', justifyContent:'center', cursor:'pointer' }}>
                  <Icon name={ic} size={15} color={S.ink3}/>
                </button>
              ))}
            </div>
          )}
        </div>
      </div>
    </div>
  );
}

// Render assistant text with basic markdown: **bold**, *italic*, `code`, bullet lists
function AssistantText({ text }) {
  const lines = text.split('\n');
  return lines.map((line, li) => {
    const parts = line.split(/(\*\*[^*]+\*\*|\*[^*]+\*|`[^`]+`)/g);
    const rendered = parts.map((p, i) => {
      if (p.startsWith('**') && p.endsWith('**')) return <strong key={i}>{p.slice(2,-2)}</strong>;
      if (p.startsWith('*')  && p.endsWith('*'))  return <em key={i}>{p.slice(1,-1)}</em>;
      if (p.startsWith('`')  && p.endsWith('`'))  return <code key={i} style={{ fontFamily:'JetBrains Mono, monospace', fontSize:13, background:'rgba(28,26,23,0.06)', padding:'1px 4px', borderRadius:4 }}>{p.slice(1,-1)}</code>;
      return <React.Fragment key={i}>{p}</React.Fragment>;
    });
    return <React.Fragment key={li}>{rendered}{li < lines.length-1 ? <br/> : null}</React.Fragment>;
  });
}

function StreamingIndicator() {
  const S = window.SOL;
  return (
    <div style={{ padding:'8px 16px', display:'flex', alignItems:'center', gap:10 }}>
      <div style={{ width:22, height:22, borderRadius:'50%', background:`radial-gradient(circle at 30% 30%, ${S.sun}, ${S.amber})`, animation:'sol-pulse 1.4s ease-in-out infinite' }}/>
      <span style={{ fontFamily:S.mono, fontSize:11, color:S.ink3, letterSpacing:0.4 }}>thinking…</span>
    </div>
  );
}

function Composer({ value, setValue, onSend, onAttachClick, onVoice, isStreaming }) {
  const S = window.SOL;
  const hasText = value.trim().length > 0;
  const textareaRef = React.useRef(null);

  const handleInput = (e) => {
    setValue(e.target.value);
    const el = e.target;
    el.style.height = 'auto';
    el.style.height = Math.min(el.scrollHeight, 120) + 'px';
  };

  return (
    <div style={{ background:S.paper, borderRadius:26, border:`1px solid ${S.line2}`, padding:'10px 10px 10px 14px', display:'flex', alignItems:'flex-end', gap:8, boxShadow:'0 6px 24px rgba(28,26,23,0.06)' }}>
      <button onClick={onAttachClick} style={{ width:36, height:36, borderRadius:12, border:'none', background:'transparent', cursor:'pointer', display:'flex', alignItems:'center', justifyContent:'center', marginBottom:2, flexShrink:0 }}>
        <Icon name="plus" size={22} color={S.ink2}/>
      </button>
      <textarea
        ref={textareaRef}
        value={value}
        onChange={handleInput}
        onKeyDown={e => { if (e.key==='Enter' && !e.shiftKey) { e.preventDefault(); onSend(); } }}
        placeholder="Ask anything…"
        rows={1}
        style={{ flex:1, border:'none', outline:'none', background:'transparent', resize:'none', fontFamily:S.sans, fontSize:16, lineHeight:1.4, color:S.ink, minHeight:24, maxHeight:120, padding:'8px 0', letterSpacing:-0.1 }}
      />
      {hasText || isStreaming ? (
        <button onClick={onSend} disabled={isStreaming} style={{ width:40, height:40, borderRadius:14, border:'none', cursor:isStreaming?'default':'pointer', background:`radial-gradient(circle at 30% 30%, ${S.sun}, ${S.amber})`, display:'flex', alignItems:'center', justifyContent:'center', boxShadow:`0 0 0 1px rgba(196,122,34,0.2), 0 4px 14px rgba(232,155,60,0.4)`, opacity:isStreaming?0.5:1, flexShrink:0 }}>
          <Icon name="send" size={18} color={S.ink}/>
        </button>
      ) : (
        <button onClick={onVoice} style={{ width:40, height:40, borderRadius:14, border:'none', cursor:'pointer', background:S.ink, display:'flex', alignItems:'center', justifyContent:'center', flexShrink:0 }}>
          <Icon name="mic" size={18} color={S.cream}/>
        </button>
      )}
    </div>
  );
}

function AttachSheet({ tier, model, onClose, onUpgrade, onCamera, onPhoto, onVideo, onFile }) {
  const S = window.SOL;
  const isPlus = tier==='plus' || tier==='trial';
  const items = [
    { icon:'camera', label:'Camera',  onPress: onCamera, tier:'all' },
    { icon:'image',  label:'Photos',  onPress: onPhoto,  tier:'all' },
    { icon:'file',   label:'Files',   onPress: onFile,   tier:'all' },
    { icon:'video',  label:'Video',   onPress: isPlus ? onVideo : onUpgrade, tier:'plus' },
  ];
  return (
    <div style={{ position:'absolute', inset:0, zIndex:20, display:'flex', flexDirection:'column', justifyContent:'flex-end', contain:'strict' }}>
      <div onClick={onClose} style={{ position:'absolute', inset:0, background:'rgba(28,26,23,0.32)', opacity:0, animation:'sol-fade-in 0.18s ease forwards' }}/>
      <div style={{ position:'relative', background:S.paper, borderTopLeftRadius:28, borderTopRightRadius:28, padding:'14px 16px', paddingBottom:'calc(14px + env(safe-area-inset-bottom, 20px))', boxShadow:'0 -10px 40px rgba(28,26,23,0.12)', transform:'translateY(100%)', animation:'sol-slide-up 0.22s cubic-bezier(0.2,0.8,0.2,1) forwards', willChange:'transform' }}>
        <div style={{ width:36, height:4, borderRadius:2, background:S.line2, margin:'0 auto 14px' }}/>
        <div style={{ fontFamily:S.sans, fontSize:13, fontWeight:500, color:S.ink2, textTransform:'uppercase', letterSpacing:0.6, marginBottom:10 }}>Add to message</div>
        <div style={{ display:'grid', gridTemplateColumns:'repeat(4,1fr)', gap:8 }}>
          {items.map(it => {
            const locked = it.tier==='plus' && !isPlus;
            return (
              <button key={it.label} onClick={it.onPress} style={{ aspectRatio:'1/1.1', borderRadius:16, border:`1px solid ${S.line2}`, background:S.cream, cursor:'pointer', padding:10, display:'flex', flexDirection:'column', alignItems:'center', justifyContent:'center', gap:6, position:'relative', opacity:locked?.72:1 }}>
                <Icon name={it.icon} size={24} color={S.ink}/>
                <span style={{ fontFamily:S.sans, fontSize:12, color:S.ink, fontWeight:500 }}>{it.label}</span>
                {locked&&<div style={{ position:'absolute', top:6, right:6, padding:'2px 5px', borderRadius:4, background:S.ink, fontFamily:S.mono, fontSize:8, letterSpacing:0.6, color:S.cream, textTransform:'uppercase' }}>PLUS</div>}
              </button>
            );
          })}
        </div>
      </div>
    </div>
  );
}

window.Chat = Chat;
