File size: 27,144 Bytes
6c2d132
c8a4f4b
41fe9a9
7b0c22b
b150436
cfbb685
30d60ea
 
 
 
42a2598
6c2d132
6dd9bad
 
 
 
 
 
b150436
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2a3e2a1
 
b150436
6c2d132
41fe9a9
 
 
 
 
 
 
 
cfbb685
 
 
 
 
6c2d132
b150436
6282d86
 
 
 
 
 
 
b150436
0f2f80a
 
 
 
6dd9bad
 
b150436
 
 
cfbb685
0f2f80a
 
 
cfbb685
 
9b8717a
cfbb685
6dd9bad
 
6c2d132
 
3b473c3
0f2f80a
 
 
3b473c3
6dd9bad
3b473c3
6dd9bad
3b473c3
6dd9bad
3b473c3
 
99f6bca
 
 
 
 
 
 
 
42a2598
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b150436
0f2f80a
 
 
 
9fe2cc7
 
 
 
 
 
99f6bca
a8e18d6
9fe2cc7
 
 
 
99f6bca
9fe2cc7
 
6c2d132
b150436
99f6bca
 
 
 
 
 
 
 
41fe9a9
 
0f2f80a
 
 
 
41fe9a9
 
0f2f80a
41fe9a9
6dd9bad
41fe9a9
 
 
 
 
 
 
 
97f541f
41fe9a9
97f541f
 
41fe9a9
97f541f
 
 
 
 
 
 
 
a8e18d6
97f541f
 
 
 
 
 
 
 
41fe9a9
97f541f
 
 
41fe9a9
 
16c2960
 
97f541f
41fe9a9
97f541f
41fe9a9
 
 
 
cfa14f7
a888244
 
 
41fe9a9
 
 
 
 
a8e18d6
 
 
 
 
 
 
 
 
 
41fe9a9
a8e18d6
a888244
a8e18d6
41fe9a9
a8e18d6
 
 
 
 
 
 
41fe9a9
a8e18d6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
41fe9a9
 
16c2960
 
 
 
 
a8e18d6
 
 
16c2960
 
a8e18d6
6dd9bad
16c2960
 
 
 
 
 
7b0c22b
6dd9bad
7b0c22b
 
6dd9bad
7b0c22b
 
16c2960
 
 
 
b150436
 
 
 
0f2f80a
 
 
6dd9bad
b150436
 
 
0f2f80a
 
 
6dd9bad
0f2f80a
b150436
 
 
 
 
 
a888244
 
6dd9bad
b150436
 
 
 
0f2f80a
 
 
b150436
 
 
6dd9bad
b150436
a966957
 
0f2f80a
b150436
 
 
 
0f2f80a
 
 
6dd9bad
 
 
b150436
 
ef0913c
 
 
 
 
6dd9bad
ef0913c
6dd9bad
d9879cf
cfbb685
ef0913c
 
 
b150436
 
 
 
0f2f80a
 
 
6dd9bad
b150436
 
 
 
 
a888244
 
6dd9bad
b150436
 
 
 
0f2f80a
 
 
b150436
 
 
6dd9bad
a966957
 
0f2f80a
b150436
 
 
 
0f2f80a
 
 
b150436
6dd9bad
b150436
a966957
 
0f2f80a
b150436
 
83c2a9a
 
 
cfbb685
 
 
 
6dd9bad
83c2a9a
 
 
 
 
 
 
 
 
 
 
 
6dd9bad
 
83c2a9a
 
eac938a
a8e18d6
eac938a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6dd9bad
eac938a
 
cfbb685
6dd9bad
cfbb685
 
eac938a
6dd9bad
eac938a
 
 
 
cfbb685
 
eac938a
 
 
 
a8e18d6
cfbb685
 
eac938a
6dd9bad
eac938a
 
 
cfbb685
eac938a
6dd9bad
 
eac938a
 
 
 
6dd9bad
eac938a
 
7b0c22b
 
 
 
 
6dd9bad
 
7b0c22b
6dd9bad
7b0c22b
 
 
 
 
 
 
 
 
6dd9bad
7b0c22b
 
 
 
 
 
83c2a9a
6dd9bad
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6c2d132
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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
import { FastifyInstance } from 'fastify';
import { prisma } from '../services/prisma';
import { whatsappQueue } from '../services/queue';
import { logger } from '../logger';
import { z } from 'zod';
import { calculateWER, formatError } from '../utils/metrics';
import fs from 'fs';
import path from 'path';
import * as diff from 'diff';
import { normalizeWolof } from '../scripts/normalizeWolof';
import { redis } from '../lib/redis';

// Services
import { AnalyticsService } from '../services/analytics-service';
import { UserAdminService } from '../services/user-admin-service';
import { TrackService } from '../services/track-service';
import { TrainingService } from '../services/training-service';

// ─── Zod Schemas ───────────────────────────────────────────────────────────────
const TrackSchema = z.object({
    title: z.string().min(1),
    description: z.string().optional(),
    duration: z.number().int().positive(),
    language: z.enum(['FR', 'WOLOF']).default('FR'),
    isPremium: z.boolean().default(false),
    priceAmount: z.number().int().optional(),
});

const TrackDaySchema = z.object({
    dayNumber: z.number().int().positive(),
    title: z.string().optional(),
    lessonText: z.string().optional(),
    audioUrl: z.string().url().optional().or(z.literal('')),
    exerciseType: z.enum(['TEXT', 'AUDIO', 'BUTTON']).default('TEXT'),
    exercisePrompt: z.string().optional(),
    validationKeyword: z.string().optional(),
    buttonsJson: z.array(z.object({ id: z.string(), title: z.string() })).optional(),
    unlockCondition: z.string().optional(),
    exerciseCriteria: z.record(z.unknown()).optional(),
    badges: z.array(z.string()).optional(),
});

const OverrideFeedbackSchema = z.object({
    userId: z.string(),
    trackId: z.string(),
    transcription: z.string().min(1),
    overrideAudioUrl: z.string().url(),
    adminId: z.string()
});

const PaginationSchema = z.object({
    page: z.coerce.number().int().positive().default(1),
    limit: z.coerce.number().int().positive().max(100).default(50)
});

export async function adminRoutes(fastify: FastifyInstance) {

    fastify.addHook('preHandler', async (request, reply) => {
        const role = (request as any).user?.role;
        if (!role || !['ORG_ADMIN', 'ADMIN', 'SUPER_ADMIN'].includes(role)) {
            return reply.code(403).send({ error: 'Admin access required' });
        }
    });

    // ── Dashboard Stats ────────────────────────────────────────────────────────
    fastify.get('/stats', async (req, reply) => {
        const organizationId = req.organizationId;
        if (!organizationId) return reply.code(400).send({ error: 'Organization ID required' });

        const stats = await AnalyticsService.getDashboardStats(organizationId);
        return stats;
    });

    // ── Users ──────────────────────────────────────────────────────────────────
    fastify.get('/users', async (req, reply) => {
        const organizationId = req.organizationId;
        if (!organizationId) return reply.code(400).send({ error: 'Organization ID required' });

        const parsed = PaginationSchema.safeParse(req.query);
        if (!parsed.success) return reply.code(400).send({ error: 'Invalid query parameters', details: parsed.error.flatten() });

        const { page, limit } = parsed.data;
        const result = await UserAdminService.listUsers(organizationId, page, limit);
        return result;
    });

    fastify.get('/users/:userId/messages', async (req, reply) => {
        const organizationId = req.organizationId;
        if (!organizationId) return reply.code(400).send({ error: 'Organization ID required' });

        const { userId } = req.params as { userId: string };
        const result = await UserAdminService.getUserMessages(organizationId, userId);

        if (!result) return reply.status(404).send({ error: 'User not found or access denied' });

        return result;
    });

    // Soft-delete a user (sets deletedAt instead of hard delete)
    fastify.delete<{ Params: { userId: string } }>('/users/:userId', async (req, reply) => {
        const organizationId = req.organizationId;
        if (!organizationId) return reply.code(400).send({ error: 'Organization ID required' });
        const ok = await UserAdminService.softDeleteUser(organizationId, req.params.userId);
        return ok ? { ok: true } : reply.code(404).send({ error: 'User not found' });
    });

    // Release a user from AI handoff mode (clears the 24h Redis pause flag)
    fastify.delete<{ Params: { userId: string } }>('/users/:userId/handoff', async (req, reply) => {
        const organizationId = req.organizationId;
        if (!organizationId) return reply.code(400).send({ error: 'Organization ID required' });
        const key = `handoff:${req.params.userId}:${organizationId}`;
        const deleted = await redis.del(key);
        return { ok: true, wasActive: deleted > 0 };
    });

    // Check handoff status for a user
    fastify.get<{ Params: { userId: string } }>('/users/:userId/handoff', async (req, reply) => {
        const organizationId = req.organizationId;
        if (!organizationId) return reply.code(400).send({ error: 'Organization ID required' });
        const key = `handoff:${req.params.userId}:${organizationId}`;
        const active = await redis.exists(key);
        return { handoffActive: active > 0 };
    });

    // ── Enrollments ────────────────────────────────────────────────────────────
    fastify.get('/enrollments', async (req, reply) => {
        const organizationId = req.organizationId;
        if (!organizationId) return reply.code(400).send({ error: 'Organization ID required' });

        const parsed = PaginationSchema.safeParse(req.query);
        if (!parsed.success) return reply.code(400).send({ error: 'Invalid query parameters', details: parsed.error.flatten() });

        const { page, limit } = parsed.data;
        const [enrollments, total] = await Promise.all([
            prisma.enrollment.findMany({
                where: { organizationId, deletedAt: null },
                include: { user: { select: { id: true, phone: true, name: true, language: true, activity: true, role: true, createdAt: true } }, track: true },
                orderBy: { startedAt: 'desc' },
                skip: (page - 1) * limit,
                take: limit,
            }),
            prisma.enrollment.count({ where: { organizationId, deletedAt: null } }),
        ]);
        return { data: enrollments, total, page, limit, totalPages: Math.ceil(total / limit) };
    });

    // Soft-delete an enrollment
    fastify.delete<{ Params: { enrollmentId: string } }>('/enrollments/:enrollmentId', async (req, reply) => {
        const organizationId = req.organizationId;
        if (!organizationId) return reply.code(400).send({ error: 'Organization ID required' });
        const ok = await UserAdminService.softDeleteEnrollment(organizationId, req.params.enrollmentId);
        return ok ? { ok: true } : reply.code(404).send({ error: 'Enrollment not found' });
    });

    // ── Human-in-the-Loop / Audio Overdrive ────────────────────────────────────

    fastify.get('/live-feed', async (req, reply) => {
        const organizationId = req.organizationId;
        if (!organizationId) return reply.code(400).send({ error: 'Organization ID required' });

        const pendingReviews = await prisma.userProgress.findMany({
            where: {
                organizationId,
                exerciseStatus: 'PENDING_REVIEW',
                user: { language: 'WOLOF' }
            },
            include: {
                user: { select: { id: true, name: true, phone: true, activity: true, language: true } },
                track: { select: { id: true, title: true } }
            },
            orderBy: { updatedAt: 'asc' }
        });

        if (pendingReviews.length === 0) return [];

        const userIds = pendingReviews.map(p => p.userId);
        const trackIds = [...new Set(pendingReviews.map(p => p.trackId))];

        // Batch fetch enrollments and latest inbound media messages β€” no N+1
        const [enrollments, messages] = await Promise.all([
            prisma.enrollment.findMany({
                where: { userId: { in: userIds }, trackId: { in: trackIds }, status: 'ACTIVE', organizationId },
            }),
            prisma.message.findMany({
                where: { userId: { in: userIds }, direction: 'INBOUND', mediaUrl: { not: null }, organizationId },
                orderBy: { createdAt: 'desc' },
                take: 200,
            }),
        ]);

        const enrollmentMap = new Map(enrollments.map(e => [`${e.userId}:${e.trackId}`, e]));
        const latestMessageMap = new Map<string, typeof messages[0]>();
        for (const msg of messages) {
            if (msg.userId && !latestMessageMap.has(msg.userId)) latestMessageMap.set(msg.userId, msg);
        }

        const liveFeed = pendingReviews.map(progress => {
            const enrollment = enrollmentMap.get(`${progress.userId}:${progress.trackId}`);
            const lastMessage = latestMessageMap.get(progress.userId);
            return {
                ...progress,
                audioUrl: lastMessage?.mediaUrl || null,
                content: progress.adminTranscription || lastMessage?.content || null,
                dayNumber: enrollment ? enrollment.currentDay : null,
            };
        });

        return liveFeed;
    });

    fastify.post('/override-feedback', async (req, reply) => {
        const organizationId = req.organizationId;
        if (!organizationId) return reply.code(400).send({ error: 'Organization context missing' });

        const body = OverrideFeedbackSchema.safeParse(req.body);
        if (!body.success) return reply.code(400).send({ error: body.error.flatten() });

        const { userId, trackId, transcription, overrideAudioUrl, adminId } = body.data;

        try {
            const progress = await prisma.userProgress.update({
                where: { userId_trackId: { userId, trackId } },
                data: {
                    exerciseStatus: 'COMPLETED',
                    adminTranscription: transcription,
                    overrideAudioUrl: overrideAudioUrl,
                    reviewedBy: adminId
                }
            });

            const enrollment = await prisma.enrollment.findFirst({
                where: { userId, trackId, organizationId, status: 'ACTIVE' }
            });

            const currentDay = enrollment ? Math.floor(enrollment.currentDay) : 0;

            await prisma.businessProfile.upsert({
                where: { userId },
                update: { lastUpdatedFromDay: currentDay, organizationId },
                create: { userId, lastUpdatedFromDay: currentDay, organizationId }
            });

            try {
                await whatsappQueue.add('send-admin-audio-override', { userId, trackId, overrideAudioUrl, adminId, organizationId });
                logger.info({ userId, trackId }, '[ADMIN] send-admin-audio-override enqueued');
            } catch (qErr) {
                logger.error({ qErr, userId }, '[ADMIN] Failed to enqueue send-admin-audio-override');
            }

            return reply.code(200).send({ ok: true, progress });
        } catch (err: any) {
            if (err.code === 'P2025') {
                return reply.code(404).send({ error: 'UserProgress not found' });
            }
            logger.error({ err, userId, trackId }, '[ADMIN] override-feedback failed');
            return reply.code(500).send({ error: 'Failed to update progress' });
        }
    });

    fastify.post('/messages/send', async (req, reply) => {
        const schema = z.object({
            userId: z.string(),
            text: z.string().min(1)
        });
        const parsed = schema.safeParse(req.body);
        if (!parsed.success) return reply.code(400).send({ error: parsed.error.flatten() });
        const body = parsed.data;

        const user = await prisma.user.findUnique({
            where: { id: body.userId, organizationId: req.organizationId ?? undefined },
            select: { phone: true, organizationId: true }
        });

        if (!user || !user.phone) {
            return reply.code(404).send({ error: "User or phone number not found." });
        }

        try {
            await whatsappQueue.add('send-message-direct', { phone: user.phone, text: body.text, organizationId: user.organizationId });
            logger.info({ phone: user.phone }, '[ADMIN] send-message-direct enqueued');
        } catch (qErr) {
            logger.error({ qErr, userId: body.userId }, '[ADMIN] Failed to enqueue send-message-direct');
            return reply.code(500).send({ error: 'Failed to queue message' });
        }

        return reply.code(200).send({ ok: true, message: "Custom message queued for sending." });
    });

    // ══════════════════════════════════════════════════════════════════════════
    // TRACKS CRUD
    // ══════════════════════════════════════════════════════════════════════════

    fastify.get('/tracks', async (req, reply) => {
        const organizationId = req.organizationId;
        if (!organizationId) return reply.code(400).send({ error: 'Organization ID required' });
        return TrackService.listTracks(organizationId);
    });

    fastify.get<{ Params: { id: string } }>('/tracks/:id', async (req, reply) => {
        const organizationId = req.organizationId;
        if (!organizationId) return reply.code(400).send({ error: 'Organization ID required' });

        const track = await TrackService.getTrack(organizationId, req.params.id);
        if (!track) return reply.code(404).send({ error: 'Track not found or access denied' });
        return track;
    });

    fastify.post('/tracks', async (req, reply) => {
        const body = TrackSchema.safeParse(req.body);
        if (!body.success) return reply.code(400).send({ error: body.error.flatten() });
        const organizationId = req.organizationId;
        if (!organizationId) return reply.code(400).send({ error: 'Organization context missing' });
        const track = await TrackService.createTrack(organizationId, body.data);
        return reply.code(201).send(track);
    });

    fastify.put<{ Params: { id: string } }>('/tracks/:id', async (req, reply) => {
        const organizationId = req.organizationId;
        if (!organizationId) return reply.code(400).send({ error: 'Organization ID required' });

        const body = TrackSchema.partial().safeParse(req.body);
        if (!body.success) return reply.code(400).send({ error: body.error.flatten() });
        try {
            const track = await TrackService.updateTrack(organizationId, req.params.id, body.data);
            return track;
        } catch (err) {
            logger.warn({ err, trackId: req.params.id }, '[ADMIN] updateTrack failed');
            return reply.code(404).send({ error: 'Track not found or access denied' });
        }
    });

    fastify.delete<{ Params: { id: string } }>('/tracks/:id', async (req, reply) => {
        const organizationId = req.organizationId;
        if (!organizationId) return reply.code(400).send({ error: 'Organization ID required' });

        const ok = await TrackService.deleteTrack(organizationId, req.params.id);
        if (!ok) return reply.code(404).send({ error: 'Track not found or access denied' });
        return { ok: true };
    });

    // ── STT Quality Calibration Endpoint ───────────────────────────────────────
    fastify.get('/stats/confidence-distribution', async (_req, reply) => {
        const statsPath = path.join(__dirname, '../../data/calibration_stats.json');
        try {
            if (fs.existsSync(statsPath)) {
                return JSON.parse(fs.readFileSync(statsPath, 'utf8'));
            }
            return reply.code(404).send({ error: "Calibration not run yet" });
        } catch (err: unknown) {
            return reply.code(500).send({ error: formatError(err) });
        }
    });

    // ══════════════════════════════════════════════════════════════════════════
    // TRACK DAYS CRUD
    // ══════════════════════════════════════════════════════════════════════════

    fastify.get<{ Params: { trackId: string } }>('/tracks/:trackId/days', async (req, reply) => {
        const organizationId = req.organizationId;
        if (!organizationId) return reply.code(400).send({ error: 'Organization ID required' });
        return TrackService.listDays(organizationId, req.params.trackId);
    });

    fastify.post<{ Params: { trackId: string } }>('/tracks/:trackId/days', async (req, reply) => {
        const body = TrackDaySchema.safeParse(req.body);
        if (!body.success) return reply.code(400).send({ error: body.error.flatten() });
        const organizationId = req.organizationId;
        if (!organizationId) return reply.code(400).send({ error: 'Organization context missing' });
        const day = await TrackService.createDay(organizationId, req.params.trackId, body.data);
        return reply.code(201).send(day);
    });

    fastify.put<{ Params: { trackId: string; dayId: string } }>('/tracks/:trackId/days/:dayId', async (req, reply) => {
        const organizationId = req.organizationId;
        if (!organizationId) return reply.code(400).send({ error: 'Organization ID required' });

        const body = TrackDaySchema.partial().safeParse(req.body);
        if (!body.success) return reply.code(400).send({ error: body.error.flatten() });
        try {
            return await TrackService.updateDay(organizationId, req.params.dayId, body.data);
        } catch (err) {
            logger.warn({ err, dayId: req.params.dayId }, '[ADMIN] updateDay failed');
            return reply.code(404).send({ error: 'Day not found or access denied' });
        }
    });

    fastify.delete<{ Params: { trackId: string; dayId: string } }>('/tracks/:trackId/days/:dayId', async (req, reply) => {
        const organizationId = req.organizationId;
        if (!organizationId) return reply.code(400).send({ error: 'Organization ID required' });

        try {
            await TrackService.deleteDay(organizationId, req.params.dayId);
            return { ok: true };
        } catch (err) {
            logger.warn({ err, dayId: req.params.dayId }, '[ADMIN] deleteDay failed');
            return reply.code(404).send({ error: 'Day not found or access denied' });
        }
    });

    // ── Training Lab Endpoints ───────────────────────────────────────────────

    fastify.get('/training/audios', async (req, reply) => {
        const parsed = PaginationSchema.safeParse(req.query);
        if (!parsed.success) return reply.code(400).send({ error: 'Invalid query', details: parsed.error.flatten() });
        const { page, limit } = parsed.data;
        return TrainingService.getPendingAudios(page, limit);
    });

    fastify.post('/training/submit', async (req, reply) => {
        const schema = z.object({
            id: z.string().uuid().optional(),
            audioUrl: z.string(),
            transcription: z.string(),
            manualCorrection: z.string()
        });
        const body = schema.safeParse(req.body);
        if (!body.success) return reply.code(400).send({ error: body.error.flatten() });

        const result = await TrainingService.submitCorrection(body.data.id, body.data.audioUrl, body.data.transcription, body.data.manualCorrection);
        return reply.send(result);
    });

    fastify.get('/training/suggestions', async (_req, reply) => {
        const trainingData = await prisma.trainingData.findMany({ where: { status: 'REVIEWED' }, take: 500 });
        const substitutionCounts: Record<string, { original: string, replacement: string, count: number }> = {};

        trainingData.forEach(item => {
            if (!item.manualCorrection) return;
            const changes = diff.diffWords(item.transcription, item.manualCorrection);
            for (let i = 0; i < changes.length - 1; i++) {
                if (changes[i].removed && changes[i + 1].added) {
                    const original = changes[i].value.trim().toLowerCase().replace(/[.,/#!$%^&*;:{}=\-_`~()]/g, "");
                    const replacement = changes[i + 1].value.trim().toLowerCase().replace(/[.,/#!$%^&*;:{}=\-_`~()]/g, "");
                    if (original && replacement && original !== replacement && original.split(' ').length === 1 && replacement.split(' ').length === 1) {
                        const key = `${original}->${replacement}`;
                        if (!substitutionCounts[key]) substitutionCounts[key] = { original, replacement, count: 0 };
                        substitutionCounts[key].count++;
                    }
                }
            }
        });

        return reply.send(Object.values(substitutionCounts).sort((a, b) => b.count - a.count).slice(0, 20));
    });

    fastify.get('/training/rules', async () => {
        return prisma.normalizationRule.findMany({ orderBy: { original: 'asc' } });
    });

    fastify.post('/training/apply-suggestions', async (req, reply) => {
        const schema = z.object({ suggestions: z.array(z.object({ original: z.string(), replacement: z.string() })) });
        const body = schema.safeParse(req.body);
        if (!body.success) return reply.code(400).send({ error: body.error.flatten() });
        if (body.data.suggestions.length === 0) return reply.send({ ok: true, message: "No suggestions provided" });

        const { normalizationService } = await import('../services/normalization');
        await normalizationService.saveRules(body.data.suggestions);
        return reply.send({ ok: true, injectedCount: body.data.suggestions.length });
    });

    fastify.post('/training/recalculate-wer', async (_req, reply) => {
        const trainingData = await prisma.trainingData.findMany({ where: { status: 'REVIEWED' }, take: 500 });
        const { normalizationService } = await import('../services/normalization');
        const customRules = await normalizationService.getRules('WOLOF');

        let totalRawWER = 0, totalNormalizedWER = 0, count = 0;
        for (const item of trainingData) {
            if (!item.manualCorrection) continue;
            const rawWER = calculateWER(item.manualCorrection, item.transcription);
            const normResult = normalizeWolof(item.transcription, customRules);
            const normalizedWER = calculateWER(item.manualCorrection, normResult.normalizedText);
            await prisma.trainingData.update({ where: { id: item.id }, data: { rawWER, normalizedWER } });
            totalRawWER += rawWER; totalNormalizedWER += normalizedWER; count++;
        }

        const avgRaw = count > 0 ? totalRawWER / count : 0;
        const avgNorm = count > 0 ? totalNormalizedWER / count : 0;
        return reply.send({ processed: count, avgRawWER: avgRaw, avgNormalizedWER: avgNorm, improvementPercent: count > 0 && avgRaw > 0 ? ((avgRaw - avgNorm) / avgRaw) * 100 : 0 });
    });

    fastify.post('/training/upload', async (req, reply) => {
        const organizationId = req.organizationId;
        if (!organizationId) return reply.code(400).send({ error: 'Organization ID required' });

        const parts = req.parts();
        let fileBuffer: Buffer | null = null, filename = 'audio.ogg', mimeType = 'audio/ogg';
        for await (const part of parts) { if (part.type === 'file') { fileBuffer = await part.toBuffer(); filename = part.filename || filename; mimeType = part.mimetype || mimeType; } }

        if (!fileBuffer || fileBuffer.length === 0) return reply.code(400).send({ error: 'No audio file provided' });

        try {
            const { uploadFile } = await import('../services/storage');
            const { aiService } = await import('../services/ai');
            const { convertToMp3IfNeeded } = await import('@repo/ai-sdk');

            const audioUrl = await uploadFile(fileBuffer, filename, mimeType, organizationId);
            const { buffer: mp3Buffer, format } = await convertToMp3IfNeeded(fileBuffer, filename);
            const { text: transcription } = await aiService.transcribeAudio(mp3Buffer, `upload.${format}`);
            const record = await TrainingService.createTrainingRecord(audioUrl, transcription);

            return reply.send({ ok: true, id: record.id, audioUrl, transcription });
        } catch (err: any) {
            logger.error({ err, organizationId }, '[TRAINING_UPLOAD] Failed');
            return reply.code(500).send({ error: 'Upload failed', detail: err.message });
        }
    });

    // Simple file upload for admin override audio (no training record created)
    fastify.post('/upload', async (req, reply) => {
        const organizationId = req.organizationId;
        if (!organizationId) return reply.code(400).send({ error: 'Organization ID required' });

        const parts = req.parts();
        let fileBuffer: Buffer | null = null, filename = 'audio.webm', mimeType = 'audio/webm';
        for await (const part of parts) {
            if (part.type === 'file') { fileBuffer = await part.toBuffer(); filename = part.filename || filename; mimeType = part.mimetype || mimeType; }
        }

        if (!fileBuffer || fileBuffer.length === 0) return reply.code(400).send({ error: 'No file provided' });

        try {
            const { uploadFile } = await import('../services/storage');
            const url = await uploadFile(fileBuffer, filename, mimeType, organizationId);
            return reply.send({ ok: true, url });
        } catch (err: any) {
            logger.error({ err, organizationId }, '[ADMIN_UPLOAD] Failed');
            return reply.code(500).send({ error: 'Upload failed', detail: err.message });
        }
    });
}