import React, { useState } from 'react';
import type { Analysis, AppState } from '../types';
import EvidencePopover from './EvidencePopover';
import { reanalyzeCompetitor } from '../services/geminiService';
interface Step4AnalysisProps {
analyses: Analysis[];
company: AppState['company'];
onAnalysisUpdate: (index: number, analysis: Analysis) => void;
t: any;
}
const DownloadIcon = () => ();
const RefreshIcon = () => ();
const downloadJSON = (data: any, filename: string) => {
const jsonStr = JSON.stringify(data, null, 2);
const blob = new Blob([jsonStr], { type: "application/json" });
const url = URL.createObjectURL(blob);
const link = document.createElement("a");
link.href = url;
link.download = filename;
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
URL.revokeObjectURL(url);
};
const OverlapBar: React.FC<{ score: number }> = ({ score }) => (
);
const Step4Analysis: React.FC = ({ analyses, company, onAnalysisUpdate, t }) => {
const sortedAnalyses = [...analyses].sort((a, b) => b.overlap_score - a.overlap_score);
const [editingIndex, setEditingIndex] = useState(null);
const [manualUrls, setManualUrls] = useState("");
const [isReanalyzing, setIsReanalyzing] = useState(false);
const handleEditStart = (index: number) => {
setEditingIndex(index);
setManualUrls(""); // Reset
};
const handleReanalyze = async (index: number, competitor: Analysis['competitor']) => {
setIsReanalyzing(true);
try {
const urls = manualUrls.split('\n').map(u => u.trim()).filter(u => u);
// Construct a partial CompetitorCandidate object as expected by the service
const candidate = {
name: competitor.name,
url: competitor.url,
confidence: 0, // Not needed for re-analysis
why: "",
evidence: []
};
const updatedAnalysis = await reanalyzeCompetitor(company, candidate, urls);
// Find the original index in the unsorted 'analyses' array to update correctly
const originalIndex = analyses.findIndex(a => a.competitor.name === competitor.name);
if (originalIndex !== -1) {
onAnalysisUpdate(originalIndex, updatedAnalysis);
}
setEditingIndex(null);
} catch (error) {
console.error("Re-analysis failed:", error);
alert("Fehler bei der Re-Analyse. Bitte Logs prüfen.");
} finally {
setIsReanalyzing(false);
}
};
return (
{t.title}
{t.subtitle}
{sortedAnalyses.map((analysis, index) => (
{/* Re-Analysis UI */}
{editingIndex === index && (
Manuelle Produkt-URLs ergänzen (optional)
Falls Produkte fehlen, fügen Sie hier direkte Links zu den Produktseiten ein (eine pro Zeile).
)}
{t.portfolio}
{analysis.portfolio.map((p, i) => - {p.product}: {p.purpose}
)}
{t.differentiators}
{analysis.differentiators.map((d, i) => - {d}
)}
{t.targetIndustries}
{analysis.target_industries.map((ind, i) => (
{ind}
))}
{analysis.delivery_model}
{t.overlapScore}: {analysis.overlap_score}%
))}
);
};
export default Step4Analysis;