File size: 18,364 Bytes
6248bf4
5b8761d
6b2ad5a
5b8761d
 
 
6248bf4
5b8761d
 
 
6248bf4
 
 
cfeac02
a966957
6248bf4
 
 
 
2ab1980
6b2ad5a
6248bf4
 
 
 
5b8761d
 
 
 
 
6248bf4
 
2ab1980
6248bf4
 
 
 
cfeac02
 
 
6248bf4
 
cfeac02
 
6248bf4
a966957
6248bf4
 
 
 
 
 
2ab1980
6248bf4
 
 
 
 
6b2ad5a
6248bf4
 
 
 
6b2ad5a
6248bf4
5b8761d
 
 
 
 
 
 
 
 
 
d80fec4
5b8761d
 
 
 
 
 
d80fec4
 
 
 
5b8761d
 
6248bf4
6b2ad5a
 
6248bf4
 
 
6b2ad5a
 
6248bf4
 
 
 
 
 
6b2ad5a
 
6248bf4
 
 
 
 
 
 
 
 
 
6b2ad5a
6248bf4
 
97f541f
6248bf4
 
 
 
6b2ad5a
6248bf4
 
97f541f
6248bf4
 
 
 
6b2ad5a
6248bf4
 
97f541f
6248bf4
 
 
b438786
 
 
 
 
6248bf4
 
 
 
 
 
 
 
 
6b2ad5a
6248bf4
0c9a5b4
6248bf4
0c9a5b4
6248bf4
 
 
 
 
 
 
 
 
 
 
 
 
 
6b2ad5a
0c9a5b4
6248bf4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6b2ad5a
6248bf4
 
6b2ad5a
6248bf4
 
 
6b2ad5a
6248bf4
 
6b2ad5a
6248bf4
 
 
6b2ad5a
6248bf4
 
 
 
5b8761d
b438786
 
 
 
 
 
 
 
d80fec4
 
b438786
 
 
 
 
 
d80fec4
 
 
 
 
b438786
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d80fec4
b438786
 
 
 
 
 
 
 
5b8761d
 
 
 
 
 
 
d80fec4
 
5b8761d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d80fec4
5b8761d
 
 
 
 
 
 
 
d80fec4
5b8761d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d80fec4
5b8761d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d80fec4
5b8761d
 
 
 
6248bf4
 
 
 
 
 
 
 
 
 
 
97f541f
 
 
 
 
6248bf4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430

import { useEffect, useState, useRef } from 'react';
import { useTranslation } from 'react-i18next';
import {
  BarChart, Bar, XAxis, YAxis, CartesianGrid, Tooltip, ResponsiveContainer,
  PieChart, Pie, Cell
} from 'recharts';
import {
  TrendingUp, Users, MessageSquare, BrainCircuit,
  ChevronRight, Award, Clock, Search, Loader2, Terminal
} from 'lucide-react';
import { useAuth } from '../lib/auth';
import { useTenant } from '../lib/tenant';
import { api } from '../lib/api';
import { logError } from '../lib/logger';

const COLORS = ['#6366f1', '#10b981', '#f59e0b', '#ef4444'];

export default function AnalyticsPage() {
  const { token } = useAuth();
  const { t } = useTranslation();
  const { selectedOrgId } = useTenant();
  const [usage, setUsage] = useState<any>(null);
  const [pedagogy, setPedagogy] = useState<any>(null);
  const [loading, setLoading] = useState(true);
  const [sqlQuestion, setSqlQuestion] = useState('');
  const [sqlLoading, setSqlLoading] = useState(false);
  const [sqlResult, setSqlResult] = useState<{ rows: Record<string, unknown>[]; sql: string; count: number } | null>(null);
  const [sqlError, setSqlError] = useState<string | null>(null);
  const sqlInputRef = useRef<HTMLInputElement>(null);

  useEffect(() => {
    if (!selectedOrgId || !token) return;

    const fetchData = async () => {
      setLoading(true);
      try {
        const [usageData, pedagogyData] = await Promise.all([
          api.get('/v1/analytics/usage', token, selectedOrgId),
          api.get('/v1/analytics/pedagogy', token, selectedOrgId)
        ]);

        setUsage(usageData);
        setPedagogy(pedagogyData);
      } catch (err) {
        logError('Analytics fetch error:', err);
      } finally {
        setLoading(false);
      }
    };

    fetchData();
  }, [selectedOrgId, token]);

  if (!selectedOrgId) {
    return (
      <div className="p-12 text-center text-gray-400">
        <Building2 className="w-12 h-12 mx-auto mb-4 opacity-20" />
        {t('dashboard.select_org_hint')}
      </div>
    );
  }

  if (loading) return <div className="p-12 animate-pulse text-gray-400">{t('common.loading')}</div>;

  const handleSqlQuery = async (e: React.FormEvent) => {
    e.preventDefault();
    if (!sqlQuestion.trim() || !selectedOrgId || !token) return;
    setSqlLoading(true);
    setSqlResult(null);
    setSqlError(null);
    try {
      const data = await api.post('/v1/analytics/query', { question: sqlQuestion, language: 'FR' }, token, selectedOrgId);
      setSqlResult(data);
    } catch (err: any) {
      setSqlError(err?.message ?? t('analytics.sql_error'));
    } finally {
      setSqlLoading(false);
    }
  };

  const EXAMPLE_QUESTIONS = [
    t('analytics.sql_example_1'),
    t('analytics.sql_example_2'),
    t('analytics.sql_example_3'),
    t('analytics.sql_example_4'),
  ];

  const messageData = [
    { name: t('analytics.messages.inbound'), value: usage?.messages?.inbound || 0 },
    { name: t('analytics.messages.outbound'), value: usage?.messages?.outbound || 0 },
  ];

  const completionData = [
    { name: t('analytics.completion.completed'), value: pedagogy?.completion?.completed || 0 },
    { name: t('analytics.completion.in_progress'), value: pedagogy?.completion?.active || 0 },
  ];

  return (
    <div className="p-8 max-w-7xl mx-auto space-y-8">
      <div className="flex items-center justify-between">
        <div>
          <h1 className="text-3xl font-bold text-slate-900">{t('dashboard.title')}</h1>
          <p className="text-slate-500">{t('dashboard.subtitle')}</p>
        </div>
        <div className="bg-white px-4 py-2 rounded-2xl border border-slate-100 flex items-center gap-3 shadow-sm">
          <div className="w-2 h-2 bg-green-500 rounded-full animate-pulse" />
          <span className="text-xs font-bold text-slate-600 uppercase tracking-wider">Live System</span>
        </div>
      </div>

      {/* KPI Cards */}
      <div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-4 gap-6">
        <StatCard 
          title={t('dashboard.stats.total_messages')} 
          value={usage?.messages?.total || 0} 
          icon={<MessageSquare className="w-5 h-5" />} 
          trend={null}
          color="text-indigo-600"
          bg="bg-indigo-50"
        />
        <StatCard 
          title={t('dashboard.stats.active_users')} 
          value={usage?.users?.activeLast24h || 0} 
          icon={<Users className="w-5 h-5" />} 
          trend={null}
          color="text-emerald-600"
          bg="bg-emerald-50"
        />
        <StatCard 
          title={t('dashboard.stats.completion_rate')} 
          value={`${Math.round(pedagogy?.completion?.rate || 0)}%`} 
          icon={<TrendingUp className="w-5 h-5" />} 
          trend={null}
          color="text-amber-600"
          bg="bg-amber-50"
        />
        <StatCard
          title={t('dashboard.stats.ai_cost')}
          value={`$${(usage?.costs?.totalUsd ?? 0).toFixed(4)}`}
          icon={<BrainCircuit className="w-5 h-5" />}
          trend={`${((usage?.costs?.totalTokens ?? 0) / 1000).toFixed(1)}k tokens`}
          color="text-purple-600"
          bg="bg-purple-50"
        />
      </div>

      <div className="grid grid-cols-1 lg:grid-cols-2 gap-8">
        {/* Messages Chart */}
        <div className="bg-white p-8 rounded-3xl border border-slate-100 shadow-sm">
          <h3 className="text-lg font-bold text-slate-800 mb-6 flex items-center gap-2">
             {t('analytics.messages.title')}
          </h3>
          <div className="h-64 min-h-[256px] w-full">
            <ResponsiveContainer width="100%" height="100%">
              <BarChart data={messageData} margin={{ top: 10, right: 10, left: -20, bottom: 0 }}>
                <CartesianGrid strokeDasharray="3 3" vertical={false} stroke="#f1f5f9" />
                <XAxis dataKey="name" axisLine={false} tickLine={false} tick={{fill: '#94a3b8', fontSize: 12}} />
                <YAxis axisLine={false} tickLine={false} tick={{fill: '#94a3b8', fontSize: 12}} />
                <Tooltip 
                  contentStyle={{borderRadius: '16px', border: 'none', boxShadow: '0 10px 15px -3px rgb(0 0 0 / 0.1)'}} 
                />
                <Bar dataKey="value" fill="#6366f1" radius={[8, 8, 0, 0]} barSize={40} />
              </BarChart>
            </ResponsiveContainer>
          </div>
        </div>

        {/* Completion Pie */}
        <div className="bg-white p-8 rounded-3xl border border-slate-100 shadow-sm">
          <h3 className="text-lg font-bold text-slate-800 mb-6">{t('analytics.completion.title')}</h3>
          <div className="h-64 min-h-[256px] w-full flex items-center">
            <ResponsiveContainer width="100%" height="100%">
              <PieChart>
                <Pie
                  data={completionData}
                  cx="50%"
                  cy="50%"
                  innerRadius={60}
                  outerRadius={80}
                  paddingAngle={5}
                  dataKey="value"
                >
                  {completionData.map((_, index) => (
                    <Cell key={`cell-${index}`} fill={COLORS[index % COLORS.length]} />
                  ))}
                </Pie>
                <Tooltip />
              </PieChart>
            </ResponsiveContainer>
            <div className="space-y-4 pr-8">
              {completionData.map((d, i) => (
                <div key={d.name} className="flex items-center gap-2">
                  <div className="w-3 h-3 rounded-full" style={{backgroundColor: COLORS[i]}} />
                  <span className="text-xs text-slate-500 font-medium">{d.name}: {d.value}</span>
                </div>
              ))}
            </div>
          </div>
        </div>
      </div>

      {/* Detailed Pedagogy */}
      <div className="bg-slate-900 text-white rounded-3xl p-10 overflow-hidden relative">
        <div className="absolute top-0 right-0 w-64 h-64 bg-indigo-500/10 blur-3xl rounded-full -mr-20 -mt-20" />
        <div className="relative z-10 grid grid-cols-1 md:grid-cols-3 gap-12">
          <div>
            <div className="flex items-center gap-3 text-indigo-300 mb-4 font-bold uppercase tracking-widest text-[10px]">
              <Award className="w-4 h-4" /> {t('analytics.performance.title')}
            </div>
            <div className="text-4xl font-bold mb-1">{pedagogy?.performance?.averageScore?.toFixed(1) || 0}</div>
            <div className="text-sm text-slate-400">{t('analytics.performance.avg_score')}</div>
          </div>
          <div>
            <div className="flex items-center gap-3 text-indigo-300 mb-4 font-bold uppercase tracking-widest text-[10px]">
              <Clock className="w-4 h-4" /> {t('analytics.engagement.title')}
            </div>
            <div className="text-4xl font-bold mb-1">{pedagogy?.performance?.averageProgressDays?.toFixed(1) || 0}</div>
            <div className="text-sm text-slate-400">{t('analytics.engagement.avg_days')}</div>
          </div>
          <div className="flex items-end justify-end">
             <button className="bg-white/10 hover:bg-white/20 transition px-6 py-3 rounded-xl text-sm font-bold flex items-center gap-2">
               {t('analytics.export')} <ChevronRight className="w-4 h-4" />
             </button>
          </div>
        </div>
      </div>

      {/* ── AI Cost Breakdown ─────────────────────────────────────────── */}
      {usage?.costs?.byFeature?.length > 0 && (
        <div className="bg-white rounded-3xl border border-slate-100 shadow-sm p-8">
          <div className="flex items-center gap-3 mb-6">
            <div className="p-2.5 bg-purple-50 rounded-xl">
              <BrainCircuit className="w-5 h-5 text-purple-600" />
            </div>
            <div>
              <h2 className="font-bold text-slate-800">{t('analytics.ai_cost_title')}</h2>
              <p className="text-xs text-slate-400">{t('analytics.ai_cost_subtitle')}</p>
            </div>
          </div>
          <div className="overflow-x-auto">
            <table className="w-full text-sm">
              <thead>
                <tr className="text-left text-xs text-slate-400 uppercase tracking-wider border-b border-slate-100">
                  <th className="pb-3 font-semibold">{t('analytics.col_feature')}</th>
                  <th className="pb-3 font-semibold text-right">{t('analytics.col_calls')}</th>
                  <th className="pb-3 font-semibold text-right">{t('analytics.col_tokens_in')}</th>
                  <th className="pb-3 font-semibold text-right">{t('analytics.col_tokens_out')}</th>
                  <th className="pb-3 font-semibold text-right">{t('analytics.col_cost')}</th>
                </tr>
              </thead>
              <tbody className="divide-y divide-slate-50">
                {(usage.costs.byFeature as Array<{ feature: string; calls: number; tokensIn: number; tokensOut: number; costUsd: number }>)
                  .sort((a, b) => b.costUsd - a.costUsd)
                  .map((row) => (
                    <tr key={row.feature} className="hover:bg-slate-50 transition">
                      <td className="py-3 font-medium text-slate-700">
                        <span className="px-2 py-0.5 bg-purple-50 text-purple-700 rounded-full text-[11px] font-mono">{row.feature}</span>
                      </td>
                      <td className="py-3 text-right text-slate-600">{row.calls.toLocaleString()}</td>
                      <td className="py-3 text-right text-slate-500">{row.tokensIn.toLocaleString()}</td>
                      <td className="py-3 text-right text-slate-500">{row.tokensOut.toLocaleString()}</td>
                      <td className="py-3 text-right font-bold text-slate-800">${row.costUsd.toFixed(4)}</td>
                    </tr>
                  ))}
              </tbody>
              <tfoot className="border-t-2 border-slate-200">
                <tr>
                  <td className="pt-3 font-bold text-slate-800" colSpan={4}>{t('analytics.total')}</td>
                  <td className="pt-3 text-right font-bold text-purple-700">${(usage.costs.totalUsd as number).toFixed(4)}</td>
                </tr>
              </tfoot>
            </table>
          </div>
        </div>
      )}

      {/* ── Text-to-SQL Search ─────────────────────────────────────────── */}
      <div className="bg-white rounded-3xl border border-slate-100 shadow-sm p-8">
        <div className="flex items-center gap-3 mb-6">
          <div className="p-2.5 bg-violet-50 rounded-xl">
            <Search className="w-5 h-5 text-violet-600" />
          </div>
          <div>
            <h2 className="font-bold text-slate-800">{t('analytics.nl_search_title')}</h2>
            <p className="text-xs text-slate-400">{t('analytics.nl_search_subtitle')}</p>
          </div>
        </div>

        {/* Example questions */}
        <div className="flex flex-wrap gap-2 mb-4">
          {EXAMPLE_QUESTIONS.map(q => (
            <button
              key={q}
              onClick={() => { setSqlQuestion(q); sqlInputRef.current?.focus(); }}
              className="text-[11px] px-3 py-1.5 bg-slate-50 hover:bg-violet-50 hover:text-violet-700 text-slate-500 rounded-full border border-slate-200 hover:border-violet-200 transition"
            >
              {q}
            </button>
          ))}
        </div>

        <form onSubmit={handleSqlQuery} className="flex gap-3">
          <input
            ref={sqlInputRef}
            type="text"
            value={sqlQuestion}
            onChange={e => setSqlQuestion(e.target.value)}
            placeholder={t('analytics.nl_search_placeholder')}
            className="flex-1 px-4 py-3 rounded-xl border border-slate-200 text-sm focus:outline-none focus:ring-2 focus:ring-violet-300 bg-slate-50"
          />
          <button
            type="submit"
            disabled={sqlLoading || !sqlQuestion.trim()}
            className="px-5 py-3 bg-violet-600 hover:bg-violet-700 disabled:opacity-40 text-white text-sm font-bold rounded-xl flex items-center gap-2 transition"
          >
            {sqlLoading ? <Loader2 className="w-4 h-4 animate-spin" /> : <Search className="w-4 h-4" />}
            {t('analytics.search_btn')}
          </button>
        </form>

        {/* Error */}
        {sqlError && (
          <div className="mt-4 p-4 bg-red-50 border border-red-100 rounded-xl text-sm text-red-600">
            {sqlError}
          </div>
        )}

        {/* Results */}
        {sqlResult && (
          <div className="mt-5">
            <div className="flex items-center justify-between mb-3">
              <span className="text-xs font-bold text-slate-500">{sqlResult.count} rΓ©sultat{sqlResult.count > 1 ? 's' : ''}</span>
              <button
                onClick={() => setSqlResult(r => r ? { ...r, _showSql: !((r as any)._showSql) } as any : r)}
                className="flex items-center gap-1.5 text-[11px] text-slate-400 hover:text-slate-600 transition"
              >
                <Terminal className="w-3.5 h-3.5" /> {t('analytics.view_sql')}
              </button>
            </div>

            {(sqlResult as any)._showSql && (
              <pre className="mb-4 p-3 bg-slate-900 text-emerald-400 text-[11px] rounded-xl overflow-x-auto">
                {sqlResult.sql}
              </pre>
            )}

            {sqlResult.rows.length > 0 ? (
              <div className="overflow-x-auto rounded-xl border border-slate-100">
                <table className="w-full text-sm">
                  <thead className="bg-slate-50 text-xs text-slate-500 uppercase">
                    <tr>
                      {Object.keys(sqlResult.rows[0]).map(col => (
                        <th key={col} className="px-4 py-3 text-left font-bold">{col}</th>
                      ))}
                    </tr>
                  </thead>
                  <tbody className="divide-y divide-slate-50">
                    {sqlResult.rows.map((row, i) => (
                      <tr key={i} className="hover:bg-slate-50/50">
                        {Object.values(row).map((val, j) => (
                          <td key={j} className="px-4 py-2.5 text-slate-700 text-xs">
                            {val === null ? <span className="text-slate-300">β€”</span> : String(val)}
                          </td>
                        ))}
                      </tr>
                    ))}
                  </tbody>
                </table>
              </div>
            ) : (
              <div className="text-center py-6 text-slate-400 text-sm">{t('analytics.no_results')}</div>
            )}
          </div>
        )}
      </div>
    </div>
  );
}

function StatCard({ title, value, icon, trend, color, bg }: any) {
  return (
    <div className="bg-white p-6 rounded-3xl border border-slate-100 shadow-sm hover:shadow-md transition group">
      <div className="flex items-center justify-between mb-4">
        <div className={`p-3 rounded-2xl ${bg} ${color} transition-colors`}>
          {icon}
        </div>
        {trend != null && (
          <div className="text-[10px] font-bold bg-slate-50 text-slate-400 px-2 py-1 rounded-lg uppercase">
            {trend}
          </div>
        )}
      </div>
      <div>
        <div className="text-sm text-slate-500 font-medium mb-1">{title}</div>
        <div className="text-2xl font-bold text-slate-900">{value}</div>
      </div>
    </div>
  );
}

function Building2(props: any) {
  return (
    <svg
      {...props}
      xmlns="http://www.w3.org/2000/svg"
      width="24"
      height="24"
      viewBox="0 0 24 24"
      fill="none"
      stroke="currentColor"
      strokeWidth="2"
      strokeLinecap="round"
      strokeLinejoin="round"
    >
      <rect width="16" height="20" x="4" y="2" rx="2" ry="2" />
      <path d="M9 22v-4h6v4" />
      <path d="M8 6h.01" />
      <path d="M16 6h.01" />
      <path d="M12 6h.01" />
      <path d="M12 10h.01" />
      <path d="M12 14h.01" />
      <path d="M16 10h.01" />
      <path d="M16 14h.01" />
      <path d="M8 10h.01" />
      <path d="M8 14h.01" />
    </svg>
  );
}