// terminal-news.jsx — News tab.
// Routed from the AI chip header. Curated feed with Council conviction tag
// per story (does the Council agree with the implied market reaction?).
// Stories link out to source; tapping a tagged ticker opens its Detail.

const NEWS_STORIES = [
  { sym: 'NVDA', headline: 'NVIDIA expands cuStateVec partnerships in financial simulation',
    source: 'Reuters', minsAgo: 14, body: 'Customers report 2,000x+ speedups on Monte Carlo paths through cuStateVec on Hopper-class GPUs. Pricing tiers adjust upward.',
    impact: 'bull', conviction: 0.92, agree: true },
  { sym: 'TSLA', headline: 'Tesla margins compress as price competition intensifies in China',
    source: 'Bloomberg', minsAgo: 38, body: 'New Q1 disclosures suggest auto gross margin slipped below 15%. Robotaxi reveal pushed to year-end.',
    impact: 'bear', conviction: 0.71, agree: true },
  { sym: 'BTC', headline: 'Spot Bitcoin ETF flows turn positive for sixth straight day',
    source: 'CoinDesk', minsAgo: 52, body: 'Net inflows of $312M yesterday, with IBIT leading. On-chain accumulation by long-term holders accelerating.',
    impact: 'bull', conviction: 0.88, agree: true },
  { sym: 'ETH', headline: 'Ethereum L2 fees drop 40% after blob-cap increase',
    source: 'The Block', minsAgo: 1*60+25, body: 'Following the latest fork, blob count per slot rose. Average L2 transaction now under $0.01 across Base, Arbitrum, Optimism.',
    impact: 'bull', conviction: 0.79, agree: true },
  { sym: 'SPY', headline: 'CPI prints in line — soft landing scenario gains ground',
    source: 'WSJ', minsAgo: 2*60+12, body: 'Core CPI matches expectations at 3.4% YoY. Fed dot-plot still implies two cuts this year.',
    impact: 'bull', conviction: 0.81, agree: true },
  { sym: 'GME', headline: 'Meme reignites as retail crowd circles the 30-day low',
    source: 'Twitter', minsAgo: 3*60+44, body: 'Social mention velocity up 4× week-over-week. No fundamental disclosures.',
    impact: 'bull', conviction: 0.32, agree: false },
  { sym: 'GOLD', headline: 'Gold hits fresh all-time high amid geopolitical uncertainty',
    source: 'FT', minsAgo: 4*60+50, body: 'Spot gold tops $2,420/oz. Central bank buying continues for the 18th consecutive month.',
    impact: 'bull', conviction: 0.86, agree: true },
];

function NewsScreen({ onOpenDetail }) {
  const [filter, setFilter] = React.useState('all');
  const visible = filter === 'all'
    ? NEWS_STORIES
    : filter === 'agree'
    ? NEWS_STORIES.filter(s => s.agree)
    : NEWS_STORIES.filter(s => !s.agree);

  return (
    <div style={{ flex: 1, display: 'flex', flexDirection: 'column', overflowY: 'auto' }} className="tx-scroll">
      <TopBar title="News" subtitle="Council-tagged · auto-refreshes hourly"
        right={<AIButton/>}/>

      <div style={{ padding: '14px 16px 8px' }}>
        <Segmented value={filter} onChange={setFilter} options={[
          { key: 'all',      label: 'All' },
          { key: 'agree',    label: 'Council agrees' },
          { key: 'disagree', label: 'Council disagrees' },
        ]}/>
      </div>

      {visible.map(story => <NewsRow key={story.headline} story={story} onTicker={() => onOpenDetail(story.sym)}/>)}

      <div style={{ padding: '20px 16px 32px', textAlign: 'center' }}>
        <Pill color={TM.muted}>END OF FEED · 7 STORIES</Pill>
      </div>
    </div>
  );
}

function NewsRow({ story, onTicker }) {
  const impactColor = story.impact === 'bull' ? TM.green : TM.red;
  const verifyColor = story.agree ? TM.cyan : TM.red;
  return (
    <div style={{
      padding: '14px 16px', borderBottom: `1px solid ${TM.border}`,
      display: 'flex', flexDirection: 'column', gap: 8,
    }}>
      <div style={{ display: 'flex', alignItems: 'center', gap: 8 }}>
        <button onClick={onTicker} style={{
          fontFamily: GM, fontSize: 11, fontWeight: 600, letterSpacing: 0.4,
          padding: '3px 8px', background: TM.surfaceHi, border: `1px solid ${TM.border}`,
          color: TM.text, borderRadius: 4,
        }}>{story.sym}</button>
        <span style={{ fontFamily: GM, fontSize: 10, color: impactColor, letterSpacing: 0.6, fontWeight: 600 }}>
          {story.impact === 'bull' ? '↗ BULLISH' : '↘ BEARISH'}
        </span>
        <span style={{ marginLeft: 'auto', fontFamily: GM, fontSize: 10.5, color: TM.muted, letterSpacing: 0.4 }}>
          {story.source} · {story.minsAgo < 60 ? `${story.minsAgo}m` : `${Math.floor(story.minsAgo/60)}h ${story.minsAgo%60}m`} ago
        </span>
      </div>
      <div style={{ fontFamily: GE, fontSize: 14.5, color: TM.text, fontWeight: 500, letterSpacing: -0.2, lineHeight: 1.4 }}>
        {story.headline}
      </div>
      <div style={{ fontFamily: GE, fontSize: 12.5, color: TM.muted, letterSpacing: -0.05, lineHeight: 1.55 }}>
        {story.body}
      </div>

      {/* Council tag */}
      <div style={{
        display: 'inline-flex', alignSelf: 'flex-start', alignItems: 'center', gap: 8,
        padding: '5px 10px', background: verifyColor === TM.cyan ? TM.cyanSoft : TM.redSoft,
        border: `1px solid ${verifyColor}55`, borderRadius: 4,
        fontFamily: GM, fontSize: 10, color: verifyColor, letterSpacing: 0.6, fontWeight: 600,
      }}>
        <span style={{ width: 6, height: 6, borderRadius: 3, background: verifyColor }}/>
        COUNCIL {story.agree ? 'CONFIRMS' : 'PUSHES BACK'} · {Math.round(story.conviction * 100)}%
      </div>
    </div>
  );
}

Object.assign(window, { NewsScreen, NewsRow });
