File size: 30,379 Bytes
86d71bd
 
 
 
 
 
 
 
 
 
 
 
7fff83a
86d71bd
 
 
7fff83a
86d71bd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7fff83a
 
 
 
 
 
 
 
86d71bd
 
 
 
 
 
7fff83a
86d71bd
 
 
 
 
 
7fff83a
 
 
 
 
 
 
 
86d71bd
 
 
 
 
 
7fff83a
86d71bd
 
 
 
 
 
7fff83a
86d71bd
 
 
7fff83a
86d71bd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7fff83a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
86d71bd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7fff83a
86d71bd
 
 
 
 
 
 
7fff83a
 
 
 
 
86d71bd
 
 
 
 
 
 
 
 
 
 
 
 
7fff83a
86d71bd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7fff83a
86d71bd
 
 
 
 
 
7fff83a
86d71bd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7fff83a
 
 
 
 
86d71bd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
/**
 * useInterviewAnalytics - Real-time Interview Performance Tracking
 * 
 * PRODUCTION-GRADE analytics for Strict and High-Pressure interview modes.
 * 
 * Features:
 * - Eye Contact Tracking (% of time maintaining eye contact)
 * - Posture/Stability Score (face position variance)
 * - Filler Word Detection (um, uh, like, you know)
 * - Silence Penalty Tracking (awkward pauses)
 * - Speaking Speed Analysis (words per minute)
 * - Video Recording of user
 * - Real-time confidence data submission to backend (strict/final_boss)
 */

import { useState, useRef, useCallback, useEffect } from 'react';
import { visionApi } from '../services/api';

// Filler words to detect
const FILLER_WORDS = [
    'um', 'uh', 'uhh', 'umm', 'er', 'err', 'ah', 'ahh',
    'like', 'you know', 'basically', 'actually', 'literally',
    'so', 'well', 'right', 'okay', 'i mean', 'kind of', 'sort of'
];

// Configuration for different modes
const MODE_CONFIG = {
    strict: {
        questionTimeLimit: 120, // 2 minutes per question
        silenceThreshold: 3000, // 3 seconds of silence is penalized
        eyeContactThreshold: 0.7, // 70% eye contact needed for "good"
        enabled: true,
        trackConfidence: true, // Enable confidence graph for strict mode
    },
    final_boss: {
        questionTimeLimit: 90, // 1.5 minutes per question (more pressure)
        silenceThreshold: 2000, // 2 seconds silence penalty
        eyeContactThreshold: 0.8, // 80% eye contact needed
        enabled: true,
        trackConfidence: true, // Enable confidence graph for final_boss mode
    },
    stress: {
        questionTimeLimit: 90, // 1.5 minutes per question (more pressure)
        silenceThreshold: 2000, // 2 seconds silence penalty
        eyeContactThreshold: 0.8, // 80% eye contact needed
        enabled: true,
        trackConfidence: false,
    },
    friendly: {
        questionTimeLimit: 0, // No time limit
        silenceThreshold: 0, // No silence penalty
        eyeContactThreshold: 0,
        enabled: false,
        trackConfidence: false,
    },
    balanced: {
        questionTimeLimit: 0,
        silenceThreshold: 0,
        eyeContactThreshold: 0,
        enabled: false,
        trackConfidence: false,
    },
    neutral: {
        questionTimeLimit: 0,
        silenceThreshold: 0,
        eyeContactThreshold: 0,
        enabled: false,
        trackConfidence: false,
    },
    professional: {
        questionTimeLimit: 0,
        silenceThreshold: 0,
        eyeContactThreshold: 0,
        enabled: false,
        trackConfidence: false,
    }
};

export default function useInterviewAnalytics(mode = 'neutral', interviewId = null) {
    // Get config for current mode
    const config = MODE_CONFIG[mode] || MODE_CONFIG.neutral;
    const isAnalyticsEnabled = config.enabled;

    // ===========================================
    // STATE
    // ===========================================

    // Eye Contact Tracking
    const [eyeContactData, setEyeContactData] = useState({
        totalFrames: 0,
        eyeContactFrames: 0,
        percentage: 0,
        currentlyLooking: false,
        history: [], // timestamps of eye contact status
    });

    // Posture/Stability Tracking
    const [postureData, setPostureData] = useState({
        stabilityScore: 100, // 0-100, higher is more stable
        totalMovement: 0,
        fidgetCount: 0,
        positionHistory: [], // {x, y, timestamp}
    });

    // Filler Words
    const [fillerWordData, setFillerWordData] = useState({
        totalCount: 0,
        breakdown: {}, // { "um": 5, "uh": 3 }
        percentageOfSpeech: 0,
        instances: [], // { word, timestamp, questionIndex }
    });

    // Silence Tracking
    const [silenceData, setSilenceData] = useState({
        totalSilenceMs: 0,
        silenceInstances: [], // { startTime, duration, questionIndex }
        longestSilence: 0,
        averageSilence: 0,
    });

    // Speaking Speed
    const [speedData, setSpeedData] = useState({
        wordsPerMinute: 0,
        totalWords: 0,
        totalSpeakingTimeMs: 0,
        speedHistory: [], // { wpm, timestamp }
        isTooFast: false,
        isTooSlow: false,
    });

    // Question Timer
    const [timerData, setTimerData] = useState({
        timeRemaining: config.questionTimeLimit,
        isRunning: false,
        timedOutQuestions: [],
        questionStartTime: null,
    });

    // Video Recording
    const [videoData, setVideoData] = useState({
        isRecording: false,
        blob: null,
        url: null,
        duration: 0,
    });

    // Overall Summary
    const [summary, setSummary] = useState(null);

    // ===========================================
    // REFS
    // ===========================================
    const mediaRecorderRef = useRef(null);
    const videoStreamRef = useRef(null);
    const recordedChunksRef = useRef([]);
    const timerIntervalRef = useRef(null);
    const lastFacePositionRef = useRef(null);
    const lastSpeechTimeRef = useRef(null);
    const silenceStartRef = useRef(null);
    const currentQuestionIndexRef = useRef(0);
    const analyticsStartTimeRef = useRef(null);
    const speechStartTimeRef = useRef(null);
    const wordCountRef = useRef(0);
    
    // Confidence tracking refs for backend submission
    const confidenceBufferRef = useRef([]);
    const lastSubmitTimeRef = useRef(0);
    const confidenceSubmitIntervalRef = useRef(null);
    const interviewIdRef = useRef(interviewId);

    // Update interviewId ref when it changes
    useEffect(() => {
        interviewIdRef.current = interviewId;
    }, [interviewId]);

    // ===========================================
    // CONFIDENCE DATA SUBMISSION (Backend)
    // ===========================================

    /**
     * Submit buffered confidence data to backend
     */
    const submitConfidenceBuffer = useCallback(async () => {
        if (!config.trackConfidence || !interviewIdRef.current || confidenceBufferRef.current.length === 0) {
            return;
        }

        const dataToSubmit = [...confidenceBufferRef.current];
        confidenceBufferRef.current = []; // Clear buffer

        try {
            await visionApi.submitConfidenceData(
                interviewIdRef.current,
                dataToSubmit,
                mode
            );
            console.log(`[Analytics] Submitted ${dataToSubmit.length} confidence data points`);
        } catch (error) {
            console.error('[Analytics] Failed to submit confidence data:', error);
            // Re-add failed data to buffer for retry
            confidenceBufferRef.current = [...dataToSubmit, ...confidenceBufferRef.current];
        }
    }, [config.trackConfidence, mode]);

    /**
     * Add confidence data point to buffer
     */
    const addConfidenceDataPoint = useCallback((eyeContactPercentage, faceDetected = true) => {
        if (!config.trackConfidence || !analyticsStartTimeRef.current) return;

        const elapsedSeconds = Math.floor((Date.now() - analyticsStartTimeRef.current) / 1000);
        
        // Calculate confidence score (simplified formula based on eye contact and face detection)
        const confidence = faceDetected 
            ? Math.min(100, Math.max(0, eyeContactPercentage * 0.8 + 20))
            : 0;

        confidenceBufferRef.current.push({
            timestamp: elapsedSeconds,
            confidence: Math.round(confidence * 10) / 10,
            eye_contact: Math.round(eyeContactPercentage * 10) / 10,
            face_detected: faceDetected,
            gaze_direction: faceDetected ? 'center' : 'unknown',
        });
    }, [config.trackConfidence]);

    /**
     * Start periodic confidence data submission
     */
    const startConfidenceTracking = useCallback(() => {
        if (!config.trackConfidence || !interviewId) return;

        console.log('[Analytics] Starting confidence tracking for backend');
        
        // Submit confidence data every 10 seconds
        confidenceSubmitIntervalRef.current = setInterval(() => {
            submitConfidenceBuffer();
        }, 10000);
    }, [config.trackConfidence, interviewId, submitConfidenceBuffer]);

    /**
     * Stop confidence tracking and finalize data
     */
    const stopConfidenceTracking = useCallback(async () => {
        if (confidenceSubmitIntervalRef.current) {
            clearInterval(confidenceSubmitIntervalRef.current);
            confidenceSubmitIntervalRef.current = null;
        }

        // Submit any remaining buffered data
        await submitConfidenceBuffer();

        // Finalize the confidence data on backend
        if (config.trackConfidence && interviewIdRef.current) {
            try {
                await visionApi.finalizeConfidenceData(interviewIdRef.current);
                console.log('[Analytics] Confidence data finalized');
            } catch (error) {
                console.error('[Analytics] Failed to finalize confidence data:', error);
            }
        }
    }, [config.trackConfidence, submitConfidenceBuffer]);

    // ===========================================
    // VIDEO RECORDING
    // ===========================================

    const startVideoRecording = useCallback(async () => {
        if (!isAnalyticsEnabled) return;

        try {
            const stream = await navigator.mediaDevices.getUserMedia({
                video: { facingMode: 'user', width: 640, height: 480 },
                audio: false // We don't need audio in this recording
            });

            videoStreamRef.current = stream;
            recordedChunksRef.current = [];

            const mediaRecorder = new MediaRecorder(stream, {
                mimeType: 'video/webm;codecs=vp9'
            });

            mediaRecorder.ondataavailable = (event) => {
                if (event.data.size > 0) {
                    recordedChunksRef.current.push(event.data);
                }
            };

            mediaRecorder.onstop = () => {
                const blob = new Blob(recordedChunksRef.current, { type: 'video/webm' });
                const url = URL.createObjectURL(blob);
                setVideoData(prev => ({
                    ...prev,
                    isRecording: false,
                    blob,
                    url,
                }));
            };

            mediaRecorderRef.current = mediaRecorder;
            mediaRecorder.start(1000); // Collect data every second
            analyticsStartTimeRef.current = Date.now();

            setVideoData(prev => ({ ...prev, isRecording: true }));
            console.log('[Analytics] Video recording started');
        } catch (error) {
            console.error('[Analytics] Failed to start video recording:', error);
        }
    }, [isAnalyticsEnabled]);

    const stopVideoRecording = useCallback(() => {
        if (mediaRecorderRef.current && mediaRecorderRef.current.state !== 'inactive') {
            mediaRecorderRef.current.stop();

            const duration = analyticsStartTimeRef.current
                ? (Date.now() - analyticsStartTimeRef.current) / 1000
                : 0;

            setVideoData(prev => ({ ...prev, duration }));
        }

        if (videoStreamRef.current) {
            videoStreamRef.current.getTracks().forEach(track => track.stop());
            videoStreamRef.current = null;
        }

        console.log('[Analytics] Video recording stopped');
    }, []);

    // ===========================================
    // EYE CONTACT TRACKING
    // ===========================================

    const updateEyeContact = useCallback((isLookingAtCamera, facePosition = null, faceDetected = true) => {
        if (!isAnalyticsEnabled) return;

        setEyeContactData(prev => {
            const newTotalFrames = prev.totalFrames + 1;
            const newEyeContactFrames = isLookingAtCamera ? prev.eyeContactFrames + 1 : prev.eyeContactFrames;
            const newPercentage = (newEyeContactFrames / newTotalFrames) * 100;

            // Add confidence data point for backend (every second approximately)
            if (config.trackConfidence && newTotalFrames % 30 === 0) {
                addConfidenceDataPoint(newPercentage, faceDetected);
            }

            return {
                totalFrames: newTotalFrames,
                eyeContactFrames: newEyeContactFrames,
                percentage: Math.round(newPercentage * 10) / 10,
                currentlyLooking: isLookingAtCamera,
                history: [...prev.history.slice(-100), { looking: isLookingAtCamera, timestamp: Date.now() }],
            };
        });

        // Also update posture tracking if face position provided
        if (facePosition) {
            updatePosture(facePosition);
        }
    }, [isAnalyticsEnabled, config.trackConfidence, addConfidenceDataPoint]);

    // ===========================================
    // POSTURE/STABILITY TRACKING
    // ===========================================

    const updatePosture = useCallback((facePosition) => {
        if (!isAnalyticsEnabled || !facePosition) return;

        const { x, y } = facePosition;
        const now = Date.now();

        setPostureData(prev => {
            const newHistory = [...prev.positionHistory.slice(-60), { x, y, timestamp: now }];

            // Calculate movement from last position
            let movementDelta = 0;
            let fidgetIncrement = 0;

            if (lastFacePositionRef.current) {
                const dx = Math.abs(x - lastFacePositionRef.current.x);
                const dy = Math.abs(y - lastFacePositionRef.current.y);
                movementDelta = Math.sqrt(dx * dx + dy * dy);

                // Consider it fidgeting if movement is significant but short
                if (movementDelta > 0.05 && movementDelta < 0.15) {
                    fidgetIncrement = 1;
                }
            }

            lastFacePositionRef.current = { x, y };

            // Calculate stability score based on variance
            let stabilityScore = 100;
            if (newHistory.length >= 10) {
                const recentPositions = newHistory.slice(-30);
                const avgX = recentPositions.reduce((sum, p) => sum + p.x, 0) / recentPositions.length;
                const avgY = recentPositions.reduce((sum, p) => sum + p.y, 0) / recentPositions.length;

                const variance = recentPositions.reduce((sum, p) => {
                    return sum + Math.pow(p.x - avgX, 2) + Math.pow(p.y - avgY, 2);
                }, 0) / recentPositions.length;

                // Convert variance to stability score (lower variance = higher stability)
                stabilityScore = Math.max(0, Math.min(100, 100 - variance * 2000));
            }

            return {
                stabilityScore: Math.round(stabilityScore),
                totalMovement: prev.totalMovement + movementDelta,
                fidgetCount: prev.fidgetCount + fidgetIncrement,
                positionHistory: newHistory,
            };
        });
    }, [isAnalyticsEnabled]);

    // ===========================================
    // FILLER WORD DETECTION
    // ===========================================

    const analyzeTranscript = useCallback((transcript, questionIndex = 0) => {
        if (!isAnalyticsEnabled || !transcript) return;

        const lowerTranscript = transcript.toLowerCase();
        const words = lowerTranscript.split(/\s+/);
        const totalWords = words.length;

        // Count filler words
        const fillerCounts = {};
        FILLER_WORDS.forEach(filler => {
            const regex = new RegExp(`\\b${filler}\\b`, 'gi');
            const matches = lowerTranscript.match(regex);
            if (matches && matches.length > 0) {
                fillerCounts[filler] = matches.length;
            }
        });

        const totalFillers = Object.values(fillerCounts).reduce((sum, count) => sum + count, 0);

        setFillerWordData(prev => ({
            totalCount: totalFillers,
            breakdown: fillerCounts,
            percentageOfSpeech: totalWords > 0 ? (totalFillers / totalWords) * 100 : 0,
            instances: Object.entries(fillerCounts).map(([word, count]) => ({
                word,
                count,
                questionIndex,
                timestamp: Date.now(),
            })),
        }));

        // Update word count for speed calculation
        wordCountRef.current = totalWords;
    }, [isAnalyticsEnabled]);

    // ===========================================
    // SILENCE TRACKING
    // ===========================================

    const trackSpeech = useCallback((isSpeaking, questionIndex = 0) => {
        if (!isAnalyticsEnabled) return;

        const now = Date.now();

        if (isSpeaking) {
            // User is speaking
            if (!speechStartTimeRef.current) {
                speechStartTimeRef.current = now;
            }

            // Check if there was a silence gap
            if (silenceStartRef.current) {
                const silenceDuration = now - silenceStartRef.current;

                if (silenceDuration >= config.silenceThreshold) {
                    setSilenceData(prev => {
                        const newInstances = [...prev.silenceInstances, {
                            startTime: silenceStartRef.current,
                            duration: silenceDuration,
                            questionIndex,
                        }];
                        const totalSilence = newInstances.reduce((sum, s) => sum + s.duration, 0);
                        const avgSilence = newInstances.length > 0 ? totalSilence / newInstances.length : 0;

                        return {
                            totalSilenceMs: totalSilence,
                            silenceInstances: newInstances,
                            longestSilence: Math.max(prev.longestSilence, silenceDuration),
                            averageSilence: avgSilence,
                        };
                    });
                }
                silenceStartRef.current = null;
            }

            lastSpeechTimeRef.current = now;
        } else {
            // User stopped speaking - start silence timer
            if (lastSpeechTimeRef.current && !silenceStartRef.current) {
                silenceStartRef.current = now;
            }
        }
    }, [isAnalyticsEnabled, config.silenceThreshold]);

    // ===========================================
    // SPEAKING SPEED ANALYSIS
    // ===========================================

    const updateSpeakingSpeed = useCallback((wordCount, speakingTimeMs) => {
        if (!isAnalyticsEnabled || speakingTimeMs <= 0) return;

        const wpm = (wordCount / speakingTimeMs) * 60000; // Words per minute

        setSpeedData(prev => ({
            wordsPerMinute: Math.round(wpm),
            totalWords: wordCount,
            totalSpeakingTimeMs: speakingTimeMs,
            speedHistory: [...prev.speedHistory.slice(-20), { wpm: Math.round(wpm), timestamp: Date.now() }],
            isTooFast: wpm > 180, // Speaking too fast (nervous)
            isTooSlow: wpm < 100, // Speaking too slow (unprepared)
        }));
    }, [isAnalyticsEnabled]);

    // ===========================================
    // QUESTION TIMER
    // ===========================================

    const startQuestionTimer = useCallback((questionIndex = 0) => {
        if (!isAnalyticsEnabled || config.questionTimeLimit <= 0) return;

        currentQuestionIndexRef.current = questionIndex;

        setTimerData(prev => ({
            ...prev,
            timeRemaining: config.questionTimeLimit,
            isRunning: true,
            questionStartTime: Date.now(),
        }));

        // Clear any existing timer
        if (timerIntervalRef.current) {
            clearInterval(timerIntervalRef.current);
        }

        timerIntervalRef.current = setInterval(() => {
            setTimerData(prev => {
                const newTime = prev.timeRemaining - 1;

                if (newTime <= 0) {
                    clearInterval(timerIntervalRef.current);
                    return {
                        ...prev,
                        timeRemaining: 0,
                        isRunning: false,
                        timedOutQuestions: [...prev.timedOutQuestions, questionIndex],
                    };
                }

                return { ...prev, timeRemaining: newTime };
            });
        }, 1000);

        console.log('[Analytics] Question timer started:', config.questionTimeLimit, 'seconds');
    }, [isAnalyticsEnabled, config.questionTimeLimit]);

    const stopQuestionTimer = useCallback(() => {
        if (timerIntervalRef.current) {
            clearInterval(timerIntervalRef.current);
            timerIntervalRef.current = null;
        }

        setTimerData(prev => ({
            ...prev,
            isRunning: false,
        }));
    }, []);

    const pauseTimer = useCallback(() => {
        if (timerIntervalRef.current) {
            clearInterval(timerIntervalRef.current);
            timerIntervalRef.current = null;
        }
        setTimerData(prev => ({ ...prev, isRunning: false }));
    }, []);

    const resumeTimer = useCallback(() => {
        if (!isAnalyticsEnabled || timerData.timeRemaining <= 0) return;

        timerIntervalRef.current = setInterval(() => {
            setTimerData(prev => {
                const newTime = prev.timeRemaining - 1;
                if (newTime <= 0) {
                    clearInterval(timerIntervalRef.current);
                    return {
                        ...prev,
                        timeRemaining: 0,
                        isRunning: false,
                        timedOutQuestions: [...prev.timedOutQuestions, currentQuestionIndexRef.current],
                    };
                }
                return { ...prev, timeRemaining: newTime, isRunning: true };
            });
        }, 1000);
    }, [isAnalyticsEnabled, timerData.timeRemaining]);

    // ===========================================
    // GENERATE FINAL SUMMARY
    // ===========================================

    const generateSummary = useCallback(() => {
        // Calculate speaking speed from total data
        const totalTimeMs = analyticsStartTimeRef.current
            ? Date.now() - analyticsStartTimeRef.current
            : speedData.totalSpeakingTimeMs;

        const finalWPM = totalTimeMs > 0
            ? (wordCountRef.current / totalTimeMs) * 60000
            : speedData.wordsPerMinute;

        // Eye contact grade
        let eyeContactGrade = 'Needs Improvement';
        if (eyeContactData.percentage >= 80) eyeContactGrade = 'Excellent';
        else if (eyeContactData.percentage >= 70) eyeContactGrade = 'Good';
        else if (eyeContactData.percentage >= 50) eyeContactGrade = 'Fair';

        // Posture grade
        let postureGrade = 'Needs Improvement';
        if (postureData.stabilityScore >= 85) postureGrade = 'Excellent';
        else if (postureData.stabilityScore >= 70) postureGrade = 'Good';
        else if (postureData.stabilityScore >= 50) postureGrade = 'Fair';

        // Speed grade
        let speedGrade = 'Good';
        if (finalWPM > 180) speedGrade = 'Too Fast (Nervous)';
        else if (finalWPM < 100) speedGrade = 'Too Slow';
        else if (finalWPM >= 120 && finalWPM <= 160) speedGrade = 'Excellent';

        const summaryData = {
            // Eye Contact
            eyeContact: {
                percentage: Math.round(eyeContactData.percentage),
                grade: eyeContactGrade,
                totalFrames: eyeContactData.totalFrames,
                recommendation: eyeContactData.percentage < 70
                    ? 'Try to maintain eye contact with the camera more consistently. This shows confidence and engagement.'
                    : 'Great job maintaining eye contact!',
            },

            // Posture/Stability
            posture: {
                stabilityScore: postureData.stabilityScore,
                grade: postureGrade,
                fidgetCount: postureData.fidgetCount,
                recommendation: postureData.stabilityScore < 70
                    ? 'Try to stay more steady during your responses. Excessive movement can appear nervous.'
                    : 'Good posture control throughout the interview!',
            },

            // Filler Words
            fillerWords: {
                totalCount: fillerWordData.totalCount,
                breakdown: fillerWordData.breakdown,
                percentageOfSpeech: Math.round(fillerWordData.percentageOfSpeech * 100) / 100,
                recommendation: fillerWordData.totalCount > 10
                    ? 'Work on reducing filler words. Practice pausing instead of using "um" or "uh".'
                    : fillerWordData.totalCount > 5
                        ? 'Some filler words detected. A bit of practice can help reduce these.'
                        : 'Excellent! Minimal filler words used.',
            },

            // Silence
            silence: {
                totalSilenceSeconds: Math.round(silenceData.totalSilenceMs / 1000),
                instanceCount: silenceData.silenceInstances.length,
                longestSilenceSeconds: Math.round(silenceData.longestSilence / 1000),
                recommendation: silenceData.silenceInstances.length > 3
                    ? 'Several awkward pauses detected. Practice your responses to speak more fluently.'
                    : 'Good flow! Minimal awkward silences.',
            },

            // Speaking Speed
            speed: {
                wordsPerMinute: Math.round(finalWPM),
                grade: speedGrade,
                recommendation: finalWPM > 180
                    ? 'You\'re speaking quite fast. Try to slow down to ensure clarity.'
                    : finalWPM < 100
                        ? 'Consider speaking a bit faster to maintain engagement.'
                        : 'Great speaking pace!',
            },

            // Timer
            timer: {
                timedOutQuestions: timerData.timedOutQuestions.length,
                questionIndexes: timerData.timedOutQuestions,
            },

            // Video
            video: {
                available: videoData.blob !== null,
                url: videoData.url,
                duration: Math.round(videoData.duration),
            },

            // Overall Score (weighted average)
            overallScore: Math.round(
                (eyeContactData.percentage * 0.25) +
                (postureData.stabilityScore * 0.20) +
                (Math.max(0, 100 - fillerWordData.totalCount * 3) * 0.20) +
                (Math.max(0, 100 - silenceData.silenceInstances.length * 10) * 0.15) +
                (finalWPM >= 120 && finalWPM <= 160 ? 100 : Math.max(0, 100 - Math.abs(140 - finalWPM))) * 0.20
            ),

            timestamp: new Date().toISOString(),
            mode: mode,
        };

        setSummary(summaryData);
        console.log('[Analytics] Summary generated:', summaryData);
        return summaryData;
    }, [eyeContactData, postureData, fillerWordData, silenceData, speedData, timerData, videoData, mode]);

    // ===========================================
    // CLEANUP
    // ===========================================

    useEffect(() => {
        return () => {
            stopVideoRecording();
            stopQuestionTimer();
        };
    }, [stopVideoRecording, stopQuestionTimer]);

    // ===========================================
    // RESET ALL DATA
    // ===========================================

    const resetAnalytics = useCallback(() => {
        setEyeContactData({
            totalFrames: 0,
            eyeContactFrames: 0,
            percentage: 0,
            currentlyLooking: false,
            history: [],
        });
        setPostureData({
            stabilityScore: 100,
            totalMovement: 0,
            fidgetCount: 0,
            positionHistory: [],
        });
        setFillerWordData({
            totalCount: 0,
            breakdown: {},
            percentageOfSpeech: 0,
            instances: [],
        });
        setSilenceData({
            totalSilenceMs: 0,
            silenceInstances: [],
            longestSilence: 0,
            averageSilence: 0,
        });
        setSpeedData({
            wordsPerMinute: 0,
            totalWords: 0,
            totalSpeakingTimeMs: 0,
            speedHistory: [],
            isTooFast: false,
            isTooSlow: false,
        });
        setTimerData({
            timeRemaining: config.questionTimeLimit,
            isRunning: false,
            timedOutQuestions: [],
            questionStartTime: null,
        });
        setVideoData({
            isRecording: false,
            blob: null,
            url: null,
            duration: 0,
        });
        setSummary(null);

        lastFacePositionRef.current = null;
        lastSpeechTimeRef.current = null;
        silenceStartRef.current = null;
        analyticsStartTimeRef.current = null;
        speechStartTimeRef.current = null;
        wordCountRef.current = 0;
        recordedChunksRef.current = [];
        confidenceBufferRef.current = [];
    }, [config.questionTimeLimit]);

    return {
        // Config
        isAnalyticsEnabled,
        config,
        shouldTrackConfidence: config.trackConfidence,

        // Data
        eyeContactData,
        postureData,
        fillerWordData,
        silenceData,
        speedData,
        timerData,
        videoData,
        summary,

        // Video Recording
        startVideoRecording,
        stopVideoRecording,

        // Confidence Tracking (for strict/final_boss modes)
        startConfidenceTracking,
        stopConfidenceTracking,
        addConfidenceDataPoint,

        // Tracking Functions
        updateEyeContact,
        updatePosture,
        analyzeTranscript,
        trackSpeech,
        updateSpeakingSpeed,

        // Timer Functions
        startQuestionTimer,
        stopQuestionTimer,
        pauseTimer,
        resumeTimer,

        // Summary
        generateSummary,
        resetAnalytics,
    };
}

// Export mode config for other components
export { MODE_CONFIG };