import React, { useMemo, useCallback } from "react"; import { Stack, Typography, Chip, Divider, Box, alpha, Paper, Tooltip } from "@mui/material"; import { Insights as InsightsIcon, Search as SearchIcon, AttachMoney as AttachMoneyIcon, EditNote as EditNoteIcon, Article as ArticleIcon, AutoAwesome as AutoAwesomeIcon, FormatQuote as FormatQuoteIcon, Campaign as CampaignIcon, Explore as ExploreIcon, } from "@mui/icons-material"; import { Research, ResearchInsight } from "../types"; import { GlassyCard, glassyCardSx, PrimaryButton } from "../ui"; import { FactCard } from "../FactCard"; interface ResearchSummaryProps { research: Research; canGenerateScript: boolean; onGenerateScript: () => void; } export const ResearchSummary: React.FC = ({ research, canGenerateScript, onGenerateScript, }) => { // Simple markdown-to-HTML converter const renderMarkdown = useCallback((text: string) => { if (!text) return null; return text .split('\n') .filter(line => line.trim() !== '') // Remove empty lines .map((line, i) => { // Handle bold let processedLine = line.replace(/\*\*(.*?)\*\*/g, '$1'); // Handle lists if (processedLine.trim().startsWith('- ') || processedLine.trim().startsWith('* ')) { return
  • ; } // Handle headers - make them smaller if (processedLine.startsWith('### ')) { return {processedLine.substring(4)}; } if (processedLine.startsWith('## ')) { return {processedLine.substring(3)}; } // Paragraphs - compact spacing return processedLine.trim() ?

    : null; }); }, []); return ( Research Summary {/* Research Metadata - Moved alongside title */} {research.searchQueries && research.searchQueries.length > 0 && ( } label={`${research.searchQueries.length} search${research.searchQueries.length > 1 ? "es" : ""}`} size="small" sx={{ background: alpha("#667eea", 0.1), color: "#667eea", fontWeight: 600, border: "1px solid rgba(102, 126, 234, 0.2)", }} /> )} {research.searchType && ( )} {research.sourceCount !== undefined && ( )} {research.cost !== undefined && ( } label={`$${research.cost.toFixed(3)}`} size="small" sx={{ background: alpha("#f59e0b", 0.1), color: "#d97706", fontWeight: 600, border: "1px solid rgba(245, 158, 11, 0.2)", }} /> )} } tooltip={!canGenerateScript ? "Complete research to generate script" : "Generate AI-powered script from research"} > Generate Script {/* Main Summary */} {research.summary && ( Executive Summary {renderMarkdown(research.summary)} )} {/* Deep Insights */} {(research.keyInsights && research.keyInsights.length > 0) ? ( Deep Insights {research.keyInsights.map((insight: ResearchInsight, idx: number) => ( {insight.title} {insight.source_indices && insight.source_indices.length > 0 && ( {insight.source_indices.map(sIdx => { const sourceIdx = sIdx - 1; const fact = research.factCards[sourceIdx]; const sourceUrl = fact?.url; const hasUrl = !!sourceUrl; const hue = (sIdx * 47 + 220) % 360; const gradientFrom = `hsl(${hue}, 70%, 55%)`; const gradientTo = `hsl(${(hue + 30) % 360}, 80%, 65%)`; return ( Source {sIdx}
    {sourceUrl}
    ) : `Source ${sIdx}`} arrow placement="top" > window.open(sourceUrl, "_blank", "noopener,noreferrer") : undefined} sx={{ height: 24, minWidth: 36, fontSize: '0.7rem', fontWeight: 800, fontFamily: "'Inter', 'Roboto', monospace", letterSpacing: "0.02em", border: "none", background: hasUrl ? `linear-gradient(135deg, ${gradientFrom}, ${gradientTo})` : `linear-gradient(135deg, ${alpha(gradientFrom, 0.3)}, ${alpha(gradientTo, 0.3)})`, color: hasUrl ? "#fff" : alpha("#fff", 0.7), cursor: hasUrl ? "pointer" : "default", borderRadius: "8px", px: 0.5, boxShadow: hasUrl ? `0 2px 8px ${alpha(gradientFrom, 0.35)}, inset 0 1px 0 ${alpha("#fff", 0.2)}` : "none", transition: "all 0.2s ease", "&:hover": hasUrl ? { background: `linear-gradient(135deg, ${gradientTo}, ${gradientFrom})`, boxShadow: `0 4px 14px ${alpha(gradientFrom, 0.5)}, inset 0 1px 0 ${alpha("#fff", 0.3)}`, transform: "translateY(-1px)", } : {}, }} /> ); })}
    )} {renderMarkdown(insight.content)} ))} ) : ( /* Fallback if keyInsights is missing but we have summary paragraphs */ research.summary && research.summary.length > 500 && !research.keyInsights && ( Additional Insights {/* Render parts of summary that might contain insights if structured data is missing */} {renderMarkdown(research.summary.split('\n\n').slice(1).join('\n\n'))} ) )} {/* Expert Quotes Section */} {research.expertQuotes && research.expertQuotes.length > 0 && ( Expert Quotes ({research.expertQuotes.length}) {research.expertQuotes.map((eq, idx) => ( “{eq.quote}” {eq.source_index !== undefined && (() => { const fact = research.factCards[eq.source_index - 1]; const sourceUrl = fact?.url; const hasUrl = !!sourceUrl; const hue = (eq.source_index * 47 + 270) % 360; const gradientFrom = `hsl(${hue}, 70%, 55%)`; const gradientTo = `hsl(${(hue + 30) % 360}, 80%, 65%)`; return ( Source {eq.source_index}
    {sourceUrl}
    ) : `Source ${eq.source_index}`} arrow placement="top"> window.open(sourceUrl, "_blank", "noopener,noreferrer") : undefined} sx={{ height: 24, fontSize: "0.7rem", fontWeight: 800, fontFamily: "'Inter', 'Roboto', monospace", border: "none", background: hasUrl ? `linear-gradient(135deg, ${gradientFrom}, ${gradientTo})` : `linear-gradient(135deg, ${alpha(gradientFrom, 0.3)}, ${alpha(gradientTo, 0.3)})`, color: hasUrl ? "#fff" : alpha("#fff", 0.7), cursor: hasUrl ? "pointer" : "default", borderRadius: "8px", px: 1, boxShadow: hasUrl ? `0 2px 8px ${alpha(gradientFrom, 0.35)}, inset 0 1px 0 ${alpha("#fff", 0.2)}` : "none", transition: "all 0.2s ease", "&:hover": hasUrl ? { background: `linear-gradient(135deg, ${gradientTo}, ${gradientFrom})`, boxShadow: `0 4px 14px ${alpha(gradientFrom, 0.5)}, inset 0 1px 0 ${alpha("#fff", 0.3)}`, transform: "translateY(-1px)", } : {}, }} />
    ); })()}
    ))} )} {/* Search Queries Used */} {research.searchQueries && research.searchQueries.length > 0 && ( Search Queries Used {research.searchQueries.map((query, idx) => ( ))} )} {research.factCards.length > 0 && ( <> Research Sources & Facts ({research.factCards.length}) Click to expand • Hover to see source {research.factCards.map((fact) => ( ))} )} {/* Listener CTA Section */} {research.listenerCta && research.listenerCta.length > 0 && ( <> Listener Call-to-Action Ideas ({research.listenerCta.length}) {research.listenerCta.map((cta, idx) => ( {cta} ))} )} {/* Mapped Angles Section */} {research.mappedAngles && research.mappedAngles.length > 0 && ( <> Content Angles ({research.mappedAngles.length}) {research.mappedAngles.map((angle, idx) => ( {angle.title} {angle.mappedFactIds && angle.mappedFactIds.length > 0 && ( {angle.mappedFactIds.slice(0, 4).map((fid: string) => ( ))} {angle.mappedFactIds.length > 4 && ( )} )} {angle.why} ))} )}
    ); };