File size: 15,506 Bytes
62fe6d4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
import { motion, AnimatePresence } from 'framer-motion'
import ReactMarkdown from 'react-markdown'
import remarkGfm from 'remark-gfm'
import {
    LineChart, Line, XAxis, YAxis, CartesianGrid, Tooltip, ResponsiveContainer,
    BarChart, Bar, Legend
} from 'recharts'
import { Button } from '@/components/ui/button'
import {
    Brain, TrendingUp, Lightbulb, Activity, Globe, Users, Zap, AlertTriangle
} from 'lucide-react'

// Mappage des icônes
const iconMap = {
    trending: TrendingUp,
    globe: Globe,
    users: Users,
    activity: Activity,
    zap: Zap,
    peak: Activity
}

const WidgetFactory = ({ widget, chartsData, themeColor }) => {
    const Icon = iconMap[widget.icon] || Activity

    switch (widget.type) {
        case 'header_section':
            return (
                <div className="mb-12">
                    <h3 className="text-2xl md:text-3xl font-black text-foreground mb-6 tracking-tight">{widget.title}</h3>
                    <div className="prose prose-lg max-w-none text-muted-foreground leading-relaxed font-medium">
                        <ReactMarkdown remarkPlugins={[remarkGfm]}>
                            {widget.content}
                        </ReactMarkdown>
                    </div>
                </div>
            )

        case 'kpi_grid':
            return (
                <div className="grid grid-cols-1 md:grid-cols-3 gap-8 mb-12">
                    {widget.items.map((item, idx) => {
                        const ItemIcon = iconMap[item.icon] || Activity
                        return (
                            <motion.div
                                key={idx}
                                initial={{ opacity: 0, y: 20 }}
                                animate={{ opacity: 1, y: 0 }}
                                transition={{ delay: idx * 0.1 }}
                                className="glass p-6 md:p-8 rounded-[1.5rem] md:rounded-[2rem] shadow-xl flex items-center space-x-6 group hover:scale-105 transition-transform"
                            >
                                <div className={`p-5 rounded-2xl bg-muted shadow-inner group-hover:rotate-12 transition-transform`}>
                                    <ItemIcon className={`h-8 w-8 ${item.color || 'text-primary'}`} />
                                </div>
                                <div>
                                    <p className="text-xs text-muted-foreground font-black uppercase tracking-widest mb-1">{item.label}</p>
                                    <p className="text-3xl font-black text-foreground">{item.value}</p>
                                </div>
                            </motion.div>
                        )
                    })}
                </div>
            )

        case 'chart_section': {
            const isEvolution = widget.chart_type === 'evolution'
            const data = isEvolution ? chartsData.evolution : chartsData.top_countries

            return (
                <div className="glass p-6 md:p-10 rounded-[2rem] md:rounded-[3rem] shadow-xl mb-12 relative overflow-hidden">
                    <div className="relative z-10">
                        <h3 className="text-xl md:text-2xl font-black text-foreground mb-8 flex items-center">
                            <div className="p-3 rounded-xl bg-primary/10 mr-4">
                                {isEvolution ? <TrendingUp className="h-6 w-6 text-primary" /> : <Activity className="h-6 w-6 text-primary" />}
                            </div>
                            {widget.title}
                        </h3>

                        <div className="h-64 md:h-96 mb-8">
                            <ResponsiveContainer width="100%" height="100%">
                                {isEvolution ? (
                                    <LineChart data={data}>
                                        <defs>
                                            <linearGradient id="lineGradient" x1="0" y1="0" x2="0" y2="1">
                                                <stop offset="5%" stopColor={`var(--color-${themeColor})`} stopOpacity={0.3} />
                                                <stop offset="95%" stopColor={`var(--color-${themeColor})`} stopOpacity={0} />
                                            </linearGradient>
                                        </defs>
                                        <CartesianGrid strokeDasharray="3 3" stroke="currentColor" opacity={0.1} vertical={false} />
                                        <XAxis dataKey="year" stroke="currentColor" opacity={0.5} fontSize={12} fontWeight="bold" />
                                        <YAxis stroke="currentColor" opacity={0.5} fontSize={12} fontWeight="bold" />
                                        <Tooltip
                                            contentStyle={{ borderRadius: '20px', border: '1px solid var(--border)', backgroundColor: 'var(--card)', boxShadow: '0 20px 25px -5px rgba(0, 0, 0, 0.1)', padding: '15px' }}
                                            itemStyle={{ fontWeight: 'bold' }}
                                            labelStyle={{ color: 'var(--foreground)', fontWeight: 'black', marginBottom: '5px' }}
                                        />
                                        <Line
                                            type="monotone"
                                            dataKey="value"
                                            stroke={`var(--color-${themeColor})`}
                                            strokeWidth={4}
                                            dot={{ fill: `var(--color-${themeColor})`, strokeWidth: 3, r: 6 }}
                                            activeDot={{ r: 10, strokeWidth: 0 }}
                                        />
                                    </LineChart>
                                ) : (
                                    <BarChart data={data} layout="vertical">
                                        <CartesianGrid strokeDasharray="3 3" stroke="currentColor" opacity={0.1} horizontal={false} />
                                        <XAxis type="number" stroke="currentColor" opacity={0.5} fontSize={12} fontWeight="bold" />
                                        <YAxis dataKey="country" type="category" width={120} stroke="currentColor" opacity={0.7} fontSize={12} fontWeight="bold" />
                                        <Tooltip
                                            contentStyle={{ borderRadius: '20px', border: '1px solid var(--border)', backgroundColor: 'var(--card)', boxShadow: '0 20px 25px -5px rgba(0, 0, 0, 0.1)', padding: '15px' }}
                                            itemStyle={{ fontWeight: 'bold' }}
                                            labelStyle={{ color: 'var(--foreground)', fontWeight: 'black', marginBottom: '5px' }}
                                        />
                                        <Bar dataKey="value" fill={`var(--color-${themeColor})`} radius={[0, 10, 10, 0]} barSize={30} />
                                    </BarChart>
                                )}
                            </ResponsiveContainer>
                        </div>

                        <div className="bg-muted/30 backdrop-blur-md p-6 rounded-2xl border border-border">
                            <p className="text-foreground font-medium italic leading-relaxed">
                                "{widget.commentary}"
                            </p>
                        </div>
                    </div>
                </div>
            )
        }

        case 'insight_cards':
            return (
                <div className="mb-12">
                    <h3 className="text-2xl font-black text-foreground mb-8">{widget.title}</h3>
                    <div className="grid grid-cols-1 md:grid-cols-3 gap-8">
                        {widget.items.map((insight, idx) => (
                            <motion.div
                                key={idx}
                                initial={{ opacity: 0, scale: 0.95 }}
                                animate={{ opacity: 1, scale: 1 }}
                                transition={{ delay: idx * 0.1 }}
                                className="glass p-6 md:p-8 rounded-[1.5rem] md:rounded-[2rem] shadow-lg border border-border group hover:bg-muted/40 transition-all"
                            >
                                <div className="flex flex-col space-y-4">
                                    <div className="w-12 h-12 rounded-2xl bg-primary/10 flex items-center justify-center group-hover:scale-110 transition-transform">
                                        <Lightbulb className={`h-6 w-6 text-primary`} />
                                    </div>
                                    <p className="text-foreground font-bold leading-relaxed">{insight}</p>
                                </div>
                            </motion.div>
                        ))}
                    </div>
                </div>
            )

        case 'text_section':
            return (
                <div className="relative overflow-hidden rounded-[2rem] md:rounded-[3rem] p-6 md:p-12 text-white shadow-2xl mb-12">
                    <div className="absolute inset-0 bg-[#166534] opacity-95"></div>
                    <div className="absolute inset-0 bg-mesh opacity-20"></div>

                    <div className="relative z-10">
                        <h3 className="text-xl md:text-3xl font-black mb-8 flex items-center">
                            <Brain className="h-8 w-8 mr-4 text-yellow-300" />
                            {widget.title}
                        </h3>
                        <div className="prose prose-invert max-w-none text-white/90 leading-relaxed text-lg md:text-xl font-medium">
                            <ReactMarkdown remarkPlugins={[remarkGfm]}>
                                {widget.content}
                            </ReactMarkdown>
                        </div>
                    </div>
                </div>
            )

        default:
            return null
    }
}

const DatasetAnalysis = ({ dataset, analysisData, onBack }) => {
    const { analysis, charts_data } = analysisData

    // Extraire la couleur du thème (par défaut green-700)
    const themeColor = analysis.theme_color ? analysis.theme_color.split('-')[0] + '-600' : 'primary'

    // Style CSS variable pour les graphiques
    const style = {
        [`--color-primary`]: '#166534',
        [`--color-emerald-600`]: '#059669',
        [`--color-blue-600`]: '#2563eb',
        [`--color-red-600`]: '#dc2626',
        [`--color-green-600`]: '#16a34a',
    }

    return (
        <motion.div
            initial={{ opacity: 0 }}
            animate={{ opacity: 1 }}
            className="min-h-screen bg-background pb-20"
            style={style}
        >
            {/* Header Premium avec Mesh Gradient */}
            <div className="relative overflow-hidden pt-12 pb-16 md:pb-24 px-4 md:px-8">
                <div className="absolute inset-0 bg-mesh opacity-30"></div>
                <div className="absolute inset-0 bg-primary/5"></div>

                <div className="max-w-7xl mx-auto relative z-10">
                    <Button
                        variant="ghost"
                        onClick={onBack}
                        className="rounded-xl hover:bg-primary/10 text-primary font-bold mb-12"
                    >
                        ← Retour aux datasets
                    </Button>

                    <div className="flex flex-col md:flex-row md:items-end justify-between gap-12">
                        <div className="flex-1">
                            <div className="flex items-center space-x-3 mb-6">
                                <div className="px-4 py-2 bg-primary/10 rounded-full border border-primary/20 flex items-center">
                                    <Brain className="h-4 w-4 text-primary mr-2" />
                                    <span className="text-primary font-black uppercase tracking-widest text-xs">Intelligence Artificielle</span>
                                </div>
                                <div className="px-4 py-2 bg-secondary/10 rounded-full border border-secondary/20 flex items-center">
                                    <Zap className="h-4 w-4 text-secondary mr-2" />
                                    <span className="text-secondary font-black uppercase tracking-widest text-xs">Rapport Stratégique</span>
                                </div>
                            </div>
                            <h1 className="text-3xl md:text-6xl font-black text-foreground tracking-tight mb-6 leading-tight">
                                {analysis.report_title || dataset.title}
                            </h1>
                            <p className="text-lg md:text-2xl text-muted-foreground font-medium max-w-3xl leading-relaxed">
                                Analyse scientifique approfondie générée pour <span className="text-foreground font-black">{dataset.source_name}</span>.
                            </p>
                        </div>

                        <div className="hidden lg:block">
                            <div className="w-64 h-64 glass rounded-[3rem] flex items-center justify-center relative">
                                <div className="absolute inset-0 bg-primary/5 rounded-[3rem] animate-pulse"></div>
                                <Brain className="h-32 w-32 text-primary opacity-20" />
                                <div className="absolute -bottom-4 -right-4 bg-card shadow-2xl rounded-2xl p-4 border border-border">
                                    <div className="flex items-center space-x-2">
                                        <div className="w-3 h-3 rounded-full bg-emerald-500 animate-pulse"></div>
                                        <span className="text-xs font-black uppercase tracking-tighter text-foreground">Gemini 2.5 Live</span>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>

            <div className="max-w-7xl mx-auto px-4 md:px-8 -mt-12">
                {/* Rendu dynamique des widgets */}
                {analysis.layout ? (
                    analysis.layout.map((widget, idx) => (
                        <WidgetFactory
                            key={idx}
                            widget={widget}
                            chartsData={charts_data}
                            themeColor={themeColor}
                        />
                    ))
                ) : (
                    <div className="glass rounded-[3rem] py-24 text-center">
                        <div className="w-20 h-20 bg-primary/10 rounded-full flex items-center justify-center mx-auto mb-6">
                            <AlertTriangle className="h-10 w-10 text-primary" />
                        </div>
                        <h3 className="text-2xl font-black text-foreground mb-2">Format non reconnu</h3>
                        <p className="text-muted-foreground font-medium">Veuillez régénérer l'analyse pour corriger ce problème.</p>
                    </div>
                )}
            </div>
        </motion.div>
    )
}

export default DatasetAnalysis