// aux-surfaces.jsx — §04f Auxiliary surfaces
//
// Real surfaces from src/renderer/windows/main/components/{quick-chat, sidebar,
// composer, chat/citation, chat/indicators}.

// ── Quick Chat (floating macOS menubar window) ──
function AuxQuickChat({ dark, variant = 'idle' }) {
  const t = dark ? G.d : G.l;
  const W = 380, H = 480;
  return (
    <div style={{
      width: W, height: H + 30, position: 'relative',
      display: 'flex', alignItems: 'center', justifyContent: 'center',
      background: dark ? '#0a0a0a' : '#f4f4f2',
    }}>
      {/* Connection arrow up to a fake menubar — design hint */}
      <div style={{
        position: 'absolute', top: 8, left: '50%', transform: 'translateX(-50%) rotate(45deg)',
        width: 16, height: 16,
        background: t.surface, border: `1px solid ${t.border}`,
        borderRight: 'none', borderBottom: 'none',
      }} />
      {/* Quick chat panel */}
      <div style={{
        width: W - 24, height: H, marginTop: 14,
        background: t.surface, border: `1px solid ${t.border}`,
        borderRadius: 12, boxShadow: t.shadowLg, overflow: 'hidden',
        display: 'flex', flexDirection: 'column',
      }}>
        {/* Header */}
        <div style={{
          padding: '10px 14px', borderBottom: `0.5px solid ${t.border}`,
          display: 'flex', alignItems: 'center', gap: 10,
        }}>
          <GhastLogo size={18} dark={dark} />
          <div style={{ flex: 1 }}>
            <div style={{ fontSize: 12, fontWeight: 600 }}>Quick chat</div>
            <div style={{ fontSize: 10, color: t.fg3, fontFamily: G.mono }}>⌘⇧G to toggle</div>
          </div>
          <button style={{ background: 'transparent', border: 'none', color: t.fg3, cursor: 'pointer' }}><Icon name="ext" size={12} /></button>
        </div>
        {/* Content */}
        <div className="gh-scroll" style={{ flex: 1, overflow: 'auto', padding: 12 }}>
          {variant === 'idle' ? (
            <div style={{ textAlign: 'center', padding: '32px 16px', color: t.fg3 }}>
              <Icon name="sparkle" size={22} color={t.fg4} />
              <div style={{ fontSize: 12, marginTop: 8 }}>Ask anything without opening the main window.</div>
              <div style={{ fontSize: 10, color: t.fg4, marginTop: 4, fontFamily: G.mono }}>⌘N main window · ⌘Q quit</div>
            </div>
          ) : (
            <div style={{ display: 'flex', flexDirection: 'column', gap: 10 }}>
              <div style={{ alignSelf: 'flex-end', maxWidth: '85%', padding: '8px 10px', background: t.surface2, borderRadius: 12, fontSize: 12 }}>
                What's on the calendar today?
              </div>
              <div style={{ display: 'flex', gap: 8, fontSize: 12, color: t.fg, lineHeight: 1.55 }}>
                <GhastLogo size={16} dark={dark} style={{ marginTop: 2 }} />
                <div>3 things: 10:00 standup · 14:30 with Marcus · 17:00 design review.</div>
              </div>
            </div>
          )}
        </div>
        {/* Composer */}
        <div style={{ padding: 10, borderTop: `0.5px solid ${t.border}` }}>
          <div style={{
            display: 'flex', alignItems: 'center', gap: 6,
            padding: '6px 10px', background: t.surface2, borderRadius: 18,
          }}>
            <Icon name="search" size={11} color={t.fg3} />
            <span style={{ flex: 1, fontSize: 12, color: t.fg4 }}>Ask…</span>
            <Kbd>↵</Kbd>
          </div>
        </div>
      </div>
    </div>
  );
}


// ── Thread context menu (right-click on a thread) ──
function AuxThreadContextMenu({ dark }) {
  const t = dark ? G.d : G.l;
  const items = [
    { icon: 'edit', label: 'Rename' },
    { icon: 'pin', label: 'Pin to top' },
    { icon: 'archive', label: 'Archive' },
    { icon: 'copy', label: 'Duplicate' },
    { icon: 'share', label: 'Export…' },
    null,
    { icon: 'trash', label: 'Delete', danger: true },
  ];
  return (
    <ChatLayout dark={dark} title="Refactor auth middleware">
      <div style={{
        position: 'absolute', top: 100, left: 50,
        width: 240, padding: 4,
        background: t.surface, border: `1px solid ${t.border}`,
        borderRadius: 10, boxShadow: t.shadowLg,
      }}>
        {items.map((it, i) => it === null ? (
          <div key={i} style={{ height: 1, background: t.border, margin: '4px 0' }} />
        ) : (
          <div key={i} style={{
            padding: '6px 10px', borderRadius: 6,
            display: 'flex', alignItems: 'center', gap: 10,
            fontSize: 12, color: it.danger ? '#ff5f57' : t.fg,
            cursor: 'pointer',
          }}>
            <Icon name={it.icon} size={12} color={it.danger ? '#ff5f57' : t.fg2} />
            <span style={{ flex: 1 }}>{it.label}</span>
            {it.label.includes('→') && <Icon name="chevronRight" size={10} color={t.fg3} />}
          </div>
        ))}
      </div>
    </ChatLayout>
  );
}


// ── Tool selector (composer, MCP-grouped) ──
function AuxToolSelector({ dark, variant = 'open' }) {
  const t = dark ? G.d : G.l;
  const groups = [
    { name: 'Built-in', tools: [
      { name: 'web_search', enabled: true },
      { name: 'computer_use', enabled: true },
      { name: 'browser', enabled: false },
    ]},
    { name: 'MCP · github', tools: [
      { name: 'list_repos', enabled: true },
      { name: 'create_issue', enabled: true },
      { name: 'merge_pr', enabled: false },
    ]},
    { name: 'MCP · linear', tools: [
      { name: 'list_issues', enabled: true },
      { name: 'add_comment', enabled: true },
    ]},
  ];
  return (
    <ChatLayout dark={dark} title="Refactor auth middleware">
      <UserMsg dark={dark}>Audit auth.ts</UserMsg>
      <div style={{
        position: 'absolute', bottom: 80, left: 24,
        width: 320, padding: 0,
        background: t.surface, border: `1px solid ${t.border}`,
        borderRadius: 12, boxShadow: t.shadowLg,
      }}>
        <div style={{ padding: '10px 14px', borderBottom: `0.5px solid ${t.border2}`, display: 'flex', alignItems: 'center', gap: 8 }}>
          <Icon name="zap" size={12} color={t.fg2} />
          <span style={{ fontSize: 12, fontWeight: 600 }}>Tools available</span>
          <span style={{ flex: 1 }} />
          <span style={{ fontSize: 11, fontFamily: G.mono, color: t.fg3 }}>{groups.flatMap(g => g.tools).filter(t => t.enabled).length} on</span>
        </div>
        <div className="gh-scroll" style={{ maxHeight: 320, overflow: 'auto' }}>
          {groups.map((g, i) => (
            <div key={i}>
              <div style={{
                padding: '6px 14px', fontSize: 10, fontFamily: G.mono, letterSpacing: 0.7,
                color: t.fg3, textTransform: 'uppercase',
                background: t.surface2,
                borderTop: i === 0 ? 'none' : `0.5px solid ${t.border2}`,
                borderBottom: `0.5px solid ${t.border2}`,
              }}>{g.name}</div>
              {g.tools.map((tool, j) => (
                <div key={j} style={{
                  padding: '7px 14px', display: 'flex', alignItems: 'center', gap: 10,
                  fontSize: 12, fontFamily: G.mono, color: tool.enabled ? t.fg : t.fg3,
                }}>
                  <span style={{ flex: 1 }}>{tool.name}</span>
                  <div style={{
                    width: 26, height: 14, borderRadius: 7,
                    background: tool.enabled ? t.fg : t.surface2,
                    padding: 1, display: 'flex',
                    border: `1px solid ${tool.enabled ? t.fg : t.border}`,
                  }}>
                    <div style={{ width: 10, height: 10, borderRadius: 5, background: tool.enabled ? t.bg : t.fg3, marginLeft: tool.enabled ? 12 : 0 }} />
                  </div>
                </div>
              ))}
            </div>
          ))}
        </div>
        <div style={{ padding: '8px 14px', borderTop: `0.5px solid ${t.border2}`, display: 'flex', alignItems: 'center', gap: 6 }}>
          <Button variant="ghost" size="sm">All on</Button>
          <Button variant="ghost" size="sm">All off</Button>
          <span style={{ flex: 1 }} />
          <Button variant="ghost" size="sm">Configure MCP →</Button>
        </div>
      </div>
    </ChatLayout>
  );
}


// ── Prompts picker (composer / picks a prompt app to run) ──
function AuxPromptsPicker({ dark }) {
  const t = dark ? G.d : G.l;
  const apps = [
    { name: 'Translate to natural English', icon: 'docs', kbd: '⌘⇧T', desc: 'Casual / formal / academic tone' },
    { name: 'Summarize this thread', icon: 'sparkle', kbd: '⌘⇧S', desc: '5-bullet recap with action items' },
    { name: 'Audit security', icon: 'lock', kbd: '⌘⇧A', desc: 'OWASP-style review of selection' },
    { name: 'Explain like I\'m 5', icon: 'chat', kbd: '', desc: 'Simplify any technical input' },
    { name: 'Make tests', icon: 'check', kbd: '', desc: 'Generate unit + integration tests' },
  ];
  return (
    <ChatLayout dark={dark} title="Auth audit">
      <UserMsg dark={dark}>/</UserMsg>
      <div style={{
        position: 'absolute', bottom: 80, left: 24,
        width: 360, padding: 0,
        background: t.surface, border: `1px solid ${t.border}`,
        borderRadius: 12, boxShadow: t.shadowLg,
      }}>
        <div style={{ padding: '8px 14px', borderBottom: `0.5px solid ${t.border2}`, display: 'flex', alignItems: 'center', gap: 8, fontSize: 11, color: t.fg3, fontFamily: G.mono }}>
          <Icon name="bolt" size={11} color={t.fg2} />
          <span>prompt apps</span>
          <span style={{ flex: 1 }} />
          <Kbd>↑↓</Kbd> <Kbd>↵</Kbd>
        </div>
        {apps.map((a, i) => (
          <div key={i} style={{
            padding: '10px 14px', display: 'flex', alignItems: 'center', gap: 10,
            background: i === 0 ? t.surface2 : 'transparent',
            borderBottom: i === apps.length - 1 ? 'none' : `0.5px solid ${t.border2}`,
            cursor: 'pointer',
          }}>
            <div style={{ width: 26, height: 26, borderRadius: 7, background: t.surface2, border: `1px solid ${t.border}`, display: 'flex', alignItems: 'center', justifyContent: 'center', flexShrink: 0 }}>
              <Icon name={a.icon} size={13} color={t.fg2} />
            </div>
            <div style={{ flex: 1, minWidth: 0 }}>
              <div style={{ fontSize: 13, fontWeight: 500 }}>{a.name}</div>
              <div style={{ fontSize: 11, color: t.fg3, marginTop: 1 }}>{a.desc}</div>
            </div>
            {a.kbd && <span style={{ fontSize: 10, fontFamily: G.mono, color: t.fg3 }}>{a.kbd}</span>}
          </div>
        ))}
        <div style={{ padding: '8px 14px', borderTop: `0.5px solid ${t.border2}`, fontSize: 11, color: t.fg3, display: 'flex', alignItems: 'center', gap: 6 }}>
          <Icon name="plus" size={11} />
          <span>Create new prompt app</span>
        </div>
      </div>
    </ChatLayout>
  );
}


// ── TodoListIndicator (live progress bar at top of chat) ──
function AuxTodoIndicator({ dark, variant = 'progress' }) {
  const t = dark ? G.d : G.l;
  const totals = variant === 'progress' ? { done: 2, total: 5, active: 'Add regression test' } :
    variant === 'all-done' ? { done: 5, total: 5, active: null } :
    { done: 0, total: 4, active: null };
  return (
    <ChatLayout dark={dark} title="Refactor auth middleware">
      {/* Sticky-feeling indicator at the top */}
      <div style={{
        maxWidth: 720, padding: '10px 14px',
        background: t.surface, border: `1px solid ${t.border}`, borderRadius: 14,
        display: 'flex', alignItems: 'center', gap: 12,
      }}>
        <div style={{ position: 'relative', width: 28, height: 28, flexShrink: 0 }}>
          <svg width={28} height={28} viewBox="0 0 28 28">
            <circle cx={14} cy={14} r={11} fill="none" stroke={t.surface2} strokeWidth={3} />
            <circle cx={14} cy={14} r={11} fill="none" stroke={t.fg} strokeWidth={3}
              strokeDasharray={`${(totals.done / totals.total) * 69.1} 69.1`}
              transform="rotate(-90 14 14)" strokeLinecap="round" />
          </svg>
          <span style={{
            position: 'absolute', inset: 0, display: 'flex', alignItems: 'center', justifyContent: 'center',
            fontSize: 9, fontFamily: G.mono, color: t.fg, fontWeight: 600,
          }}>{totals.done}/{totals.total}</span>
        </div>
        <div style={{ flex: 1, minWidth: 0 }}>
          <div style={{ fontSize: 13, fontWeight: 500 }}>
            {totals.active ? `Working on: ${totals.active}` : (totals.done === totals.total ? 'All todos complete' : 'Plan ready · waiting to start')}
          </div>
          <div style={{ fontSize: 11, color: t.fg3, marginTop: 2 }}>
            {totals.done} of {totals.total} done · click to expand
          </div>
        </div>
        {totals.active && (
          <span style={{ width: 10, height: 10, border: `1.5px solid ${t.fg2}`, borderTopColor: 'transparent', borderRadius: 5, animation: 'gh-spin .8s linear infinite' }} />
        )}
        {totals.done === totals.total && <Icon name="check" size={14} color="#28c840" />}
      </div>
    </ChatLayout>
  );
}


// ── Suggestions (next-step chips after assistant reply) ──
function AuxSuggestions({ dark }) {
  const t = dark ? G.d : G.l;
  const suggestions = [
    { icon: 'edit', text: 'Apply the diff and re-run tests' },
    { icon: 'docs', text: 'Show me the final auth.ts file' },
    { icon: 'plus', text: 'Add a similar fix to session.ts' },
  ];
  return (
    <ChatLayout dark={dark} title="Refactor auth middleware">
      <UserMsg dark={dark}>What's the safest fix?</UserMsg>
      <AssistantMsg dark={dark}>
        Remove the bypass and require the JWT path always. The header was a debug shortcut — production can't trust it.
      </AssistantMsg>
      <div style={{ display: 'flex', gap: 6, flexWrap: 'wrap', maxWidth: 720 }}>
        {suggestions.map((s, i) => (
          <div key={i} style={{
            padding: '8px 12px', borderRadius: 999,
            background: t.surface, border: `1px solid ${t.border}`,
            display: 'flex', alignItems: 'center', gap: 8,
            fontSize: 12, color: t.fg2, cursor: 'pointer',
          }}>
            <Icon name={s.icon} size={11} color={t.fg2} />
            <span>{s.text}</span>
            <Icon name="arrowRight" size={10} color={t.fg3} />
          </div>
        ))}
      </div>
    </ChatLayout>
  );
}


// ── Quote display (replying to specific message or file region) ──
function AuxQuoteDisplay({ dark, variant = 'message' }) {
  const t = dark ? G.d : G.l;
  return (
    <ChatLayout dark={dark} title="Auth audit">
      {variant === 'message' && (
        <>
          <AssistantMsg dark={dark}>
            The bypass on lines 5–7 is the highest-priority finding. Two more medium issues, but they don't block.
          </AssistantMsg>
          {/* Quote of the previous message in user composer */}
          <div style={{ alignSelf: 'flex-end', maxWidth: '85%' }}>
            <div style={{
              padding: '8px 10px', marginBottom: 4,
              background: t.surface2, borderLeft: `3px solid ${t.fg}`, borderRadius: '0 8px 8px 0',
              fontSize: 11, color: t.fg2, lineHeight: 1.5,
            }}>
              <div style={{ fontSize: 9, color: t.fg3, fontFamily: G.mono, marginBottom: 2, letterSpacing: 0.6, textTransform: 'uppercase' }}>Replying to</div>
              "The bypass on lines 5–7 is the highest-priority finding."
            </div>
            <div style={{ padding: '10px 12px', background: t.fg, color: t.bg, borderRadius: 14, fontSize: 13 }}>
              Why "highest"? What's the medium-severity stuff?
            </div>
          </div>
        </>
      )}
      {variant === 'file' && (
        <>
          <UserMsg dark={dark}>What does this code do?</UserMsg>
          {/* Quote of a file region */}
          <div style={{
            maxWidth: 720, padding: 0,
            background: t.surface, border: `1px solid ${t.border}`, borderRadius: 12, overflow: 'hidden',
          }}>
            <div style={{ padding: '6px 12px', background: t.surface2, borderBottom: `0.5px solid ${t.border2}`, display: 'flex', alignItems: 'center', gap: 8, fontSize: 11, fontFamily: G.mono, color: t.fg2 }}>
              <Icon name="file" size={11} />
              <span>auth.ts · lines 5–7</span>
              <span style={{ flex: 1 }} />
              <span style={{ color: t.fg3 }}>quoted by user</span>
            </div>
            <pre style={{ margin: 0, padding: '10px 14px', fontFamily: G.mono, fontSize: 11, lineHeight: 1.55, color: t.fg2, whiteSpace: 'pre' }}>{`5: if (req.headers['x-internal-trust'] === 'true') {
6:   return next();
7: }`}</pre>
          </div>
          <AssistantMsg dark={dark}>
            That's the unsafe bypass — any client can set <code style={{ fontFamily: G.mono, fontSize: 12, padding: '0 4px', background: t.surface2, borderRadius: 3 }}>x-internal-trust: true</code> and skip JWT verification entirely.
          </AssistantMsg>
        </>
      )}
      {variant === 'code' && (
        <>
          <UserMsg dark={dark}>What about this snippet from a different repo?</UserMsg>
          {/* Quoted code block (pasted with attribution) */}
          <div style={{
            maxWidth: 720, padding: 0,
            background: t.surface, border: `1px solid ${t.border}`, borderRadius: 12, overflow: 'hidden',
          }}>
            <div style={{ padding: '6px 12px', background: t.surface2, borderBottom: `0.5px solid ${t.border2}`, display: 'flex', alignItems: 'center', gap: 8, fontSize: 11, fontFamily: G.mono, color: t.fg2 }}>
              <Icon name="code" size={11} />
              <span>pasted snippet · 14 lines</span>
              <span style={{ flex: 1 }} />
              <span style={{ color: t.fg3 }}>typescript</span>
            </div>
            <pre style={{ margin: 0, padding: '10px 14px', fontFamily: G.mono, fontSize: 11, lineHeight: 1.55, color: t.fg2, whiteSpace: 'pre', maxHeight: 140, overflow: 'auto' }}>{`function isInternal(req) {
  const trusted = process.env.TRUSTED_PROXIES.split(',');
  const ip = req.socket.remoteAddress;
  return trusted.includes(ip);
}`}</pre>
          </div>
          <AssistantMsg dark={dark}>That's safer — checks the actual remote address against a configured list. But still consider source-spoofing under proxy.</AssistantMsg>
        </>
      )}
    </ChatLayout>
  );
}


Object.assign(window, {
  AuxQuickChat, AuxThreadContextMenu,
  AuxToolSelector, AuxPromptsPicker, AuxTodoIndicator, AuxSuggestions, AuxQuoteDisplay,
});
