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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
import streamlit as st
import os
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import base64
import time
import sqlite3
import datetime
import sys
import re
import queue
from concurrent.futures import ThreadPoolExecutor
from PIL import Image
from io import BytesIO
from gamification import GamificationSystem
import random
from collections import defaultdict
import io
from vocam_ui import apply_custom_css
from streamlit.components.v1 import components
import hashlib
from functools import lru_cache
from example_sentences import ExampleSentenceGenerator
import tensorflow as tf
import tensorflow_hub as hub
import requests
from deep_translator import GoogleTranslator
from transformers import pipeline, AutoTokenizer, AutoModelForSeq2SeqLM

# First, display Python version for
st.set_page_config(
    page_title="Vocam",
    page_icon="🌍",
    layout="wide",
    initial_sidebar_state="expanded"
)

# Import the UI enhancement module
from vocam_ui import (
    apply_custom_css, 
    success_message, 
    info_message, 
    warning_message, 
    error_message,
    show_loading_spinner, 
    vocam_card, 
    word_card,
    add_result_separator,
    add_scroll_indicator,
    style_title,
    style_section_title,
    add_footer
)

apply_custom_css()

try:
    from pronunciation_practice import create_pronunciation_practice
    has_pronunciation_practice = True
    print("✅ Enhanced pronunciation practice with AI feedback loaded")
except ImportError as e:
    has_pronunciation_practice = False
    print(f"❌ Pronunciation practice not available: {e}")
    
# Try importing OCR with fallback
try:
    import pytesseract
    has_tesseract = True
except ImportError as e:
    has_tesseract = False
    # Dummy implementation
    class DummyTesseract:
        def image_to_string(self, *args, **kwargs):
            return "OCR requires pytesseract. Install with: pip install pytesseract"
    pytesseract = DummyTesseract()

# Try importing OpenCV with robust fallback mechanism
try:
    import cv2
except ImportError as e:
    # Create dummy CV2 class to prevent crashes
    class DummyCV2:
        def __init__(self):
            pass
            
        def __getattr__(self, name):
            def dummy_method(*args, **kwargs):
                return None
            return dummy_method
            
        def cvtColor(self, *args, **kwargs):
            return args[0]  # Return the input image unchanged
            
        @staticmethod
        def imread(path):
            try:
                from PIL import Image
                import numpy as np
                img = Image.open(path)
                return np.array(img)
            except Exception:
                return None
                
        @staticmethod
        def imwrite(path, img):
            try:
                from PIL import Image
                import numpy as np
                Image.fromarray(img).save(path)
                return True
            except False:
                return False
    
    # Replace cv2 with our dummy implementation
    cv2 = DummyCV2()

# Import other dependencies with careful error handling
try:
    import torch
except ImportError as e:
    # Dummy torch for fallback
    class DummyTorch:
        def __init__(self):
            self.hub = type('obj', (object,), {
                'load': lambda *args, **kwargs: DummyModel()
            })
            
    class DummyModel:
        def __call__(self, *args, **kwargs):
            return type('obj', (object,), {
                'xyxy': [[]], 
                'render': lambda: [[np.zeros((300, 300, 3), dtype=np.uint8)]],
                'names': {0: 'unknown'}
            })
            
        def eval(self):
            return self
            
    torch = DummyTorch()


# Try importing gTTS
try:
    from gtts import gTTS
except ImportError as e:
    # Create a dummy gTTS class
    class DummyGTTS:
        def __init__(self, text="", lang="en", slow=False):
            self.text = text
            self.lang = lang
            
        def write_to_fp(self, fp):
            fp.write(b'dummy audio data')
    
    gTTS = DummyGTTS

# Import database module with error handling
try:
    from database import LanguageLearningDB
except ImportError as e:
    # Define a basic LanguageLearningDB class for fallback
    class LanguageLearningDB:
        def __init__(self, db_path):
            self.db_path = db_path
            
        def start_session(self):
            return None
            
        def end_session(self, session_id, words_studied, words_learned):
            return True

# Import custom audio recorder
try:
    from custom_audio_recorder import audio_recorder
    has_custom_recorder = True
    print("Custom audio recorder imported successfully")
except ImportError as e:
    has_custom_recorder = False
    print(f"Custom audio recorder not available: {e}")

def check_pronunciation_dependencies():
    """Check and report pronunciation practice dependencies"""
    dependencies = {
        'streamlit_webrtc': False,
        'speech_recognition': False,
        'librosa': False,
        'Levenshtein': False,
        'av': False
    }
    
    try:
        import streamlit_webrtc
        dependencies['streamlit_webrtc'] = True
    except ImportError:
        pass
    
    try:
        import speech_recognition
        dependencies['speech_recognition'] = True
    except ImportError:
        pass
    
    try:
        import librosa
        dependencies['librosa'] = True
    except ImportError:
        pass
    
    try:
        import Levenshtein
        dependencies['Levenshtein'] = True
    except ImportError:
        pass
    
    try:
        import av
        dependencies['av'] = True
    except ImportError:
        pass
    
    return dependencies

def draw_detections(image_np, detections):
    """Draw bounding boxes and labels on the image."""
    result_image = image_np.copy()
    
    for detection in detections:
        bbox = detection['bbox']
        left, top, right, bottom = [int(x) for x in bbox]
        label = detection['label']
        confidence = detection['confidence']
        
        # Use different colors for different object types
        color = get_detection_color(label)
        
        # Draw bounding box
        cv2.rectangle(result_image, (left, top), (right, bottom), color, 3)
        
        # Prepare label text
        label_text = f"{label} {confidence:.2f}"
        label_size, _ = cv2.getTextSize(label_text, cv2.FONT_HERSHEY_SIMPLEX, 0.7, 2)
        
        # Draw background for text
        cv2.rectangle(result_image, 
                     (left, top - label_size[1] - 10), 
                     (left + label_size[0], top), 
                     color, -1)
        
        # Draw text (white or black depending on background)
        text_color = (255, 255, 255) if sum(color) < 400 else (0, 0, 0)
        cv2.putText(result_image, label_text,
                   (left, top - 5),
                   cv2.FONT_HERSHEY_SIMPLEX, 0.7, text_color, 2)
    
    return result_image


def apply_nms(boxes, classes, scores, image_shape, iou_threshold=0.45):
    """Apply Non-Maximum Suppression to remove duplicate detections."""
    
    final_detections = []
    unique_classes = np.unique(classes)
    height, width = image_shape[:2]
    
    for class_id in unique_classes:
        # Get all detections for this class
        class_mask = classes == class_id
        class_boxes = boxes[class_mask]
        class_scores = scores[class_mask]
        
        if len(class_boxes) == 0:
            continue
        
        # Convert normalized coordinates to pixel coordinates
        pixel_boxes = []
        for box in class_boxes:
            ymin, xmin, ymax, xmax = box
            pixel_boxes.append([
                int(xmin * width),   # left
                int(ymin * height),  # top
                int(xmax * width),   # right
                int(ymax * height)   # bottom
            ])
        pixel_boxes = np.array(pixel_boxes)
        
        # Apply simple NMS (since OpenCV might cause issues)
        keep_indices = simple_nms(pixel_boxes, class_scores, iou_threshold)
        
        # Add kept detections to final list
        for idx in keep_indices:
            class_name = COCO_CLASS_NAMES.get(class_id, f"unknown_{class_id}")
            bbox = pixel_boxes[idx]
            
            final_detections.append({
                'label': class_name.lower(),
                'confidence': float(class_scores[idx]),
                'bbox': [float(bbox[0]), float(bbox[1]), float(bbox[2]), float(bbox[3])],
                'class_id': int(class_id)
            })
    
    # Sort detections by confidence (highest first)
    final_detections.sort(key=lambda x: x['confidence'], reverse=True)
    return final_detections

# Alternative simplified NMS function if OpenCV NMS doesn't work
def simple_nms(boxes, scores, iou_threshold=0.5):
    """Simple Non-Maximum Suppression implementation."""
    if len(boxes) == 0:
        return []
    
    # Sort by confidence score (highest first)
    sorted_indices = np.argsort(scores)[::-1]
    
    keep = []
    while len(sorted_indices) > 0:
        # Take the detection with highest confidence
        current = sorted_indices[0]
        keep.append(current)
        
        if len(sorted_indices) == 1:
            break
        
        # Calculate IoU with all other boxes
        current_box = boxes[current]
        remaining_indices = sorted_indices[1:]
        
        # Calculate IoU with remaining boxes
        ious = []
        for idx in remaining_indices:
            iou = calculate_iou(current_box, boxes[idx])
            ious.append(iou)
        
        # Keep only boxes with IoU below threshold
        ious = np.array(ious)
        keep_mask = ious < iou_threshold
        sorted_indices = remaining_indices[keep_mask]
    
    return keep

def calculate_iou(box1, box2):
    """Calculate Intersection over Union (IoU) of two bounding boxes."""
    # box format: [left, top, right, bottom]
    x1 = max(box1[0], box2[0])
    y1 = max(box1[1], box2[1])
    x2 = min(box1[2], box2[2])
    y2 = min(box1[3], box2[3])
    
    if x2 <= x1 or y2 <= y1:
        return 0.0
    
    intersection = (x2 - x1) * (y2 - y1)
    area1 = (box1[2] - box1[0]) * (box1[3] - box1[1])
    area2 = (box2[2] - box2[0]) * (box2[3] - box2[1])
    union = area1 + area2 - intersection
    
    return intersection / union if union > 0 else 0.0

def get_detection_color(label):
    """Get a consistent color for each object type."""
    # Color mapping for different object categories
    color_map = {
        # Electronics - Blue shades
        'cell phone': (255, 100, 100),
        'laptop': (255, 150, 100),
        'tv': (255, 200, 100),
        'mouse': (200, 255, 100),
        'keyboard': (150, 255, 100),
        'remote': (100, 255, 100),
        
        # People - Green shades
        'person': (100, 255, 150),
        
        # Furniture - Purple shades
        'chair': (150, 100, 255),
        'couch': (200, 100, 255),
        'bed': (255, 100, 255),
        
        # Food - Orange/Red shades
        'bottle': (100, 150, 255),
        'cup': (100, 200, 255),
        'bowl': (100, 255, 255),
        
        # Default color
        'default': (0, 255, 0)
    }
    
    return color_map.get(label, color_map['default'])


def show_detection_settings():
    """Show detection settings in the sidebar."""
    with st.sidebar.expander("🎛️ Detection Settings"):
        st.markdown("**Non-Maximum Suppression (NMS)**")
        st.markdown("✅ Enabled - Removes duplicate detections")
        
        # Allow user to adjust IOU threshold
        iou_threshold = st.slider(
            "Overlap Threshold", 
            min_value=0.1, 
            max_value=0.9, 
            value=0.45,
            step=0.05,
            help="Lower values = fewer duplicates, Higher values = more detections"
        )
        
        st.markdown(f"**Current Settings:**")
        st.markdown(f"- Overlap: {iou_threshold:.2f}")
        st.markdown("- Confidence: Set below ⬇️")
        
        return iou_threshold
    
# Helper function to convert AttrDict to a regular dict recursively
def convert_to_dict(obj):
    if isinstance(obj, dict):
        return {key: convert_to_dict(value) for key, value in obj.items()}
    elif isinstance(obj, list):
        return [convert_to_dict(item) for item in obj]
    else:
        return obj


# Define object categories for better organization
OBJECT_CATEGORIES = {
    "food": ["banana", "apple", "sandwich", "orange", "broccoli", "carrot", 
             "hot dog", "pizza", "donut", "cake", "bottle", "wine glass", 
             "cup", "fork", "knife", "spoon", "bowl"],
    
    "animals": ["bird", "cat", "dog", "horse", "sheep", "cow", "elephant", 
                "bear", "zebra", "giraffe"],
    
    "vehicles": ["bicycle", "car", "motorcycle", "airplane", "bus", "train", 
                 "truck", "boat"],
    
    "electronics": ["tv", "laptop", "mouse", "remote", "keyboard", "cell phone", 
                   "microwave", "oven", "toaster", "refrigerator"],
    
    "furniture": ["chair", "couch", "potted plant", "bed", "dining table", 
                  "toilet", "bench"],
    
    "personal": ["backpack", "umbrella", "handbag", "tie", "suitcase"],
    
    "sports": ["frisbee", "skis", "snowboard", "sports ball", "kite", 
              "baseball bat", "baseball glove", "skateboard", "surfboard", 
              "tennis racket"],
    
    "household": ["bottle", "wine glass", "cup", "fork", "knife", "spoon", 
                 "bowl", "book", "clock", "vase", "scissors", "teddy bear", 
                 "hair drier", "toothbrush", "sink"]
}

COCO_CLASS_NAMES = {
    1: 'person', 2: 'bicycle', 3: 'car', 4: 'motorcycle', 5: 'airplane',
    6: 'bus', 7: 'train', 8: 'truck', 9: 'boat', 10: 'traffic light',
    11: 'fire hydrant', 13: 'stop sign', 14: 'parking meter', 15: 'bench',
    16: 'bird', 17: 'cat', 18: 'dog', 19: 'horse', 20: 'sheep',
    21: 'cow', 22: 'elephant', 23: 'bear', 24: 'zebra', 25: 'giraffe',
    27: 'backpack', 28: 'umbrella', 31: 'handbag', 32: 'tie', 33: 'suitcase',
    34: 'frisbee', 35: 'skis', 36: 'snowboard', 37: 'sports ball',
    38: 'kite', 39: 'baseball bat', 40: 'baseball glove', 41: 'skateboard',
    42: 'surfboard', 43: 'tennis racket', 44: 'bottle', 46: 'wine glass',
    47: 'cup', 48: 'fork', 49: 'knife', 50: 'spoon', 51: 'bowl',
    52: 'banana', 53: 'apple', 54: 'sandwich', 55: 'orange', 56: 'broccoli',
    57: 'carrot', 58: 'hot dog', 59: 'pizza', 60: 'donut', 61: 'cake',
    62: 'chair', 63: 'couch', 64: 'potted plant', 65: 'bed', 67: 'dining table',
    70: 'toilet', 72: 'tv', 73: 'laptop', 74: 'mouse', 75: 'remote',
    76: 'keyboard', 77: 'cell phone', 78: 'microwave', 79: 'oven',
    80: 'toaster', 81: 'sink', 82: 'refrigerator', 84: 'book', 85: 'clock',
    86: 'vase', 87: 'scissors', 88: 'teddy bear', 89: 'hair drier', 90: 'toothbrush'
}

# Define question types
QUESTION_TYPES = [
    "translation_to_target",     # English → Target language
    "translation_to_english",    # Target language → English
    "image_recognition",         # Show image, select correct word
    "category_match",            # Match word to correct category
    "sentence_completion",       # Fill in blank in a sentence
    "multiple_choice_category",  # Choose words from same category
    "audio_recognition"          # Hear word, select correct option
]


def get_object_category(label):
    """Get the category for a detected object label."""
    label = label.lower()
    for category, items in OBJECT_CATEGORIES.items():
        if label in items:
            return category
    return "other"

# Add this to optimize API usage and reduce costs
@lru_cache(maxsize=100)
def cached_vision_detection(image_hash, confidence_threshold):
    """Cache detection results based on image hash to avoid redundant API calls."""
    # This is a placeholder - the actual implementation would be tied to your caching mechanism
    # Return None to indicate cache miss
    return None

def get_image_hash(image):
    """Create a hash of an image for caching purposes."""
    img_byte_arr = io.BytesIO()
    image.save(img_byte_arr, format='JPEG', quality=70)  # Lower quality for hash stability
    return hashlib.md5(img_byte_arr.getvalue()).hexdigest()

# Add rate limiting to avoid excessive API calls
last_api_call = 0
MIN_API_CALL_INTERVAL = 0.5  # seconds

def rate_limited_detection(image, confidence_threshold=0.5, iou_threshold=0.45):
    """Rate-limited version of detect_objects to avoid excessive API calls."""
    global last_api_call
    
    # Check cache first
    image_hash = get_image_hash(image)
    cached_result = cached_vision_detection(image_hash, confidence_threshold)
    if cached_result:
        return cached_result
    
    # Rate limiting
    current_time = time.time()
    time_since_last_call = current_time - last_api_call
    if time_since_last_call < MIN_API_CALL_INTERVAL:
        time.sleep(MIN_API_CALL_INTERVAL - time_since_last_call)
    
    # Make the API call
    result = detect_objects(image, confidence_threshold, iou_threshold)
    last_api_call = time.time()
    
    return result

# Function to detect objects in image
def detect_objects(image, confidence_threshold=0.5, iou_threshold=0.45):
    """Detect objects using Faster R-CNN with Non-Maximum Suppression to remove duplicates."""
    
    try:
        # Load the Faster R-CNN model
        detector = load_faster_rcnn_model()
        if detector is None:
            error_message("Failed to load Faster R-CNN model")
            return [], np.array(image)
        
        # Convert PIL image to numpy array if needed
        if hasattr(image, 'convert'):
            image_np = np.array(image.convert('RGB'))
        else:
            image_np = np.array(image)
        
        # Convert to tensor
        image_tensor = tf.convert_to_tensor(image_np)
        image_tensor = image_tensor[tf.newaxis, ...]
        
        # Run object detection
        results = detector(image_tensor)
        
        # Extract results
        boxes = results['detection_boxes'][0].numpy()
        classes = results['detection_classes'][0].numpy().astype(int)
        scores = results['detection_scores'][0].numpy()
        
        # Filter by confidence threshold first
        valid_indices = scores >= confidence_threshold
        filtered_boxes = boxes[valid_indices]
        filtered_classes = classes[valid_indices]
        filtered_scores = scores[valid_indices]
        
        if len(filtered_boxes) == 0:
            return [], image_np
        
        # Apply Non-Maximum Suppression to remove duplicate detections
        final_detections = apply_nms(filtered_boxes, filtered_classes, filtered_scores, image_np.shape, iou_threshold)
        
        # Draw bounding boxes on result image
        result_image = draw_detections(image_np, final_detections)
        
        print(f"✅ Faster R-CNN detected {len(final_detections)} unique objects (after NMS)")
        return final_detections, result_image
        
    except Exception as e:
        error_message(f"Faster R-CNN detection error: {str(e)}")
        # Return empty result on error
        dummy_image = np.array(image) if hasattr(image, 'convert') else image
        return [], dummy_image

# Function to enhance image quality
def enhance_image(image, enhance_type="auto"):
    """Enhance the image to improve object detection."""
    try:
        # Convert PIL image to numpy array
        img_array = np.array(image)
        
        if enhance_type == "auto" or enhance_type == "brightness":
            # Auto-adjust brightness
            gray = cv2.cvtColor(img_array, cv2.COLOR_RGB2GRAY)
            mean_brightness = np.mean(gray)
            
            if mean_brightness < 100:  # Image is too dark
                # Increase brightness
                hsv = cv2.cvtColor(img_array, cv2.COLOR_RGB2HSV)
                h, s, v = cv2.split(hsv)
                
                # Calculate how much to increase brightness (more for darker images)
                brightness_factor = max(1.0, (130 - mean_brightness) / 80)
                v = cv2.add(v, np.array([brightness_factor * 30.0], dtype=np.uint8))
                
                final_hsv = cv2.merge((h, s, v))
                img_array = cv2.cvtColor(final_hsv, cv2.COLOR_HSV2RGB)
            
            elif mean_brightness > 200:  # Image is too bright
                # Decrease brightness
                hsv = cv2.cvtColor(img_array, cv2.COLOR_RGB2HSV)
                h, s, v = cv2.split(hsv)
                
                # Reduce brightness
                v = cv2.subtract(v, np.array([30], dtype=np.uint8))
                
                final_hsv = cv2.merge((h, s, v))
                img_array = cv2.cvtColor(final_hsv, cv2.COLOR_HSV2RGB)
        
        if enhance_type == "auto" or enhance_type == "contrast":
            # Enhance contrast
            lab = cv2.cvtColor(img_array, cv2.COLOR_RGB2LAB)
            l, a, b = cv2.split(lab)
            
            # Apply CLAHE (Contrast Limited Adaptive Histogram Equalization)
            clahe = cv2.createCLAHE(clipLimit=3.0, tileGridSize=(8, 8))
            cl = clahe.apply(l)
            
            # Merge the CLAHE enhanced L-channel with the a and b channels
            enhanced_lab = cv2.merge((cl, a, b))
            img_array = cv2.cvtColor(enhanced_lab, cv2.COLOR_LAB2RGB)
        
        # Convert back to PIL image
        enhanced_image = Image.fromarray(img_array)
        return enhanced_image
    
    except Exception as e:
        error_message(f"Image enhancement error: {e}")
        return image  # Return original image on error

# Function to detect text in image (OCR)
def detect_text_in_image(image):
    """Detect text in image using OCR."""
    try:
        if not has_tesseract:
            return "OCR functionality requires installing pytesseract."
        
        # Convert PIL image to numpy array
        img_array = np.array(image)
        
        # Convert to grayscale
        gray = cv2.cvtColor(img_array, cv2.COLOR_RGB2GRAY)
        
        # Apply threshold to get image with only black and white
        _, binary = cv2.threshold(gray, 150, 255, cv2.THRESH_BINARY_INV)
        
        # Apply dilation and erosion to remove noise
        kernel = np.ones((1, 1), np.uint8)
        processed = cv2.dilate(binary, kernel, iterations=1)
        processed = cv2.erode(processed, kernel, iterations=1)
        
        # Invert back
        processed = cv2.bitwise_not(processed)
        
        # Detect text
        detected_text = pytesseract.image_to_string(processed)
        
        # Clean and process the text
        detected_text = detected_text.strip()
        
        return detected_text
    except Exception as e:
        return f"Text detection error: {e}"

# Function to get example sentence
def get_example_sentence(word, target_language):
    """Generate an example sentence using the word via the example generator."""
    # Try to determine category from OBJECT_CATEGORIES
    category = None
    for cat_name, items in OBJECT_CATEGORIES.items():
        if word.lower() in [item.lower() for item in items]:
            category = cat_name
            break
    
    # Call the generator with the category hint
    return example_generator.get_example_sentence(word, target_language, category)
        


# Function to get pronunciation guide
def get_pronunciation_guide(word, language_code):
    """Generate a simple pronunciation guide for the word."""
    try:
        # Map of common sounds in different languages
        pronunciation_maps = {
            "es": {  # Spanish
                'j': 'h', 'll': 'y', 'ñ': 'ny', 'rr': 'rolled r'
            },
            "fr": {  # French
                'eau': 'oh', 'au': 'oh', 'ai': 'eh', 'ou': 'oo', 'u': 'ü', 'r': 'guttural r'
            },
            "de": {  # German
                'sch': 'sh', 'ch': 'kh/sh', 'ei': 'eye', 'ie': 'ee', 'ä': 'eh', 'ö': 'er', 'ü': 'ü'
            },
            "it": {  # Italian
                'gn': 'ny', 'gli': 'ly', 'ch': 'k', 'c+e/i': 'ch', 'c+a/o/u': 'k'
            }
        }
        
        # Get pronunciation map for this language
        sound_map = pronunciation_maps.get(language_code, {})
        
        # Build pronunciation guide
        notes = []
        
        for sound, pronunciation in sound_map.items():
            if sound in word.lower():
                notes.append(f"'{sound}' sounds like '{pronunciation}'")
        
        return notes
    except Exception as e:
        return [f"Pronunciation guide unavailable: {str(e)}"]

# Function to create a database session
def create_session_direct():
    """Create a session directly using SQLite."""
    try:
        # Connect to the database
        conn = sqlite3.connect("language_learning.db")
        cursor = conn.cursor()
        
        # Insert a new session with the current time
        current_time = datetime.datetime.now()
        cursor.execute(
            "INSERT INTO sessions (start_time, words_studied, words_learned) VALUES (?, 0, 0)",
            (current_time,)
        )
        conn.commit()
        
        # Get the last inserted ID
        session_id = cursor.lastrowid
        conn.close()
        
        return session_id
    except Exception as e:
        error_message(f"Direct session creation error: {str(e)}")
        return None

# Function to add vocabulary to the database
def add_vocabulary_direct(word_original, word_translated, language_translated, category=None, image_path=None):
    """Add vocabulary directly using SQLite with improved error handling for duplicates and locks."""
    try:
        # Original function code here...
        # Connect to the database with timeout to handle locks
        conn = sqlite3.connect("language_learning.db", timeout=10.0)
        cursor = conn.cursor()
        
        # Check if this word already exists in this language
        cursor.execute(
            "SELECT id FROM vocabulary WHERE word_original = ? AND language_translated = ?",
            (word_original, language_translated)
        )
        existing_word = cursor.fetchone()
        
        # If word exists, update it rather than inserting a new one
        if existing_word:
            vocab_id = existing_word[0]
            
            # Update the existing word with new translation and image if provided
            cursor.execute(
                "UPDATE vocabulary SET word_translated = ?, category = ?, image_path = ? WHERE id = ?",
                (word_translated, category, image_path, vocab_id)
            )
            
            # Let the user know we're updating
            info_message(f"Word '{word_original}' already exists in {language_translated}. Updating with new information.")
        else:
            # Current time for timestamps
            current_time = datetime.datetime.now()
            
            # Insert a new word
            try:
                # Try with source column
                cursor.execute('''

                INSERT INTO vocabulary 

                (word_original, word_translated, language_translated, category, image_path, date_added, source)

                VALUES (?, ?, ?, ?, ?, ?, 'manual')

                ''', (word_original, word_translated, language_translated, category, image_path, current_time))
            except sqlite3.OperationalError as e:
                if 'no column named source' in str(e):
                    # Try without source column
                    cursor.execute('''

                    INSERT INTO vocabulary 

                    (word_original, word_translated, language_translated, category, image_path, date_added)

                    VALUES (?, ?, ?, ?, ?, ?)

                    ''', (word_original, word_translated, language_translated, category, image_path, current_time))
                else:
                    raise e
            
            # Get the last inserted ID
            vocab_id = cursor.lastrowid
            
            # Check if we need to add user progress
            cursor.execute("SELECT id FROM user_progress WHERE vocabulary_id = ?", (vocab_id,))
            if not cursor.fetchone():
                # Initialize user progress for this vocabulary
                cursor.execute('''

                INSERT INTO user_progress (vocabulary_id, last_reviewed, proficiency_level)

                VALUES (?, ?, 0)

                ''', (vocab_id, current_time))
        
        # Commit changes and close
        conn.commit()
        conn.close()
        
        # NEW CODE: Integration with gamification system - with error handling
        if vocab_id:
            try:
                # Check for gamification achievements
                gamification.check_achievements(
                    "word_learned",
                    word=word_original,
                    category=category,
                    language=language_translated
                )
                
                # Check for daily challenges
                gamification.check_challenge_progress(
                    word_original=word_original,
                    word_translated=word_translated,
                    language=language_translated
                )
            except Exception as e:
                print(f"Gamification error in add_vocabulary_direct: {e}")
        
        return vocab_id
    except sqlite3.OperationalError as e:
        # Handle database locks with specific advice
        if 'database is locked' in str(e):
            error_message("Database is currently locked. Please wait a moment and try again.")
            # Add a small delay to allow the database to unlock
            time.sleep(1.5)
        else:
            error_message(f"Database error: {str(e)}")
        return None
    except Exception as e:
        error_message(f"Direct vocabulary save error: {str(e)}")
        return None
    

# Function to get all vocabulary items from the database
def get_all_vocabulary_direct():
    """Get all vocabulary items directly from SQLite."""
    try:
        # Connect to the database
        conn = sqlite3.connect("language_learning.db")
        
        # Use dictionary cursor for easier access
        conn.row_factory = sqlite3.Row
        cursor = conn.cursor()
        
        # Get all vocabulary with user progress info
        cursor.execute('''

        SELECT v.id, v.word_original, v.word_translated, v.language_translated,

               v.category, v.image_path, v.date_added,

               up.proficiency_level, up.review_count, up.correct_count, up.last_reviewed

        FROM vocabulary v

        LEFT JOIN user_progress up ON v.id = up.vocabulary_id

        ORDER BY v.date_added DESC

        ''')
        
        # Fetch all results
        results = cursor.fetchall()
        
        # Convert to list of dictionaries
        vocabulary = []
        for row in results:
            # Convert row to dictionary
            word = dict(row)
            vocabulary.append(word)
        
        conn.close()
        return vocabulary
    except Exception as e:
        error_message(f"Error retrieving vocabulary: {str(e)}")
        return []

# Function to get session statistics
def get_session_stats_direct(days=30):
    """Get session statistics directly from SQLite."""
    try:
        # Connect to the database
        conn = sqlite3.connect("language_learning.db")
        cursor = conn.cursor()
        
        # Calculate date for filtering
        current_time = datetime.datetime.now()
        start_date = current_time - datetime.timedelta(days=days)
        
        # Convert to string format
        start_date_str = start_date.strftime("%Y-%m-%d")
        
        # Get total sessions
        cursor.execute(
            "SELECT COUNT(*) FROM sessions WHERE start_time >= ?",
            (start_date_str,)
        )
        total_sessions = cursor.fetchone()[0]
        
        # Get words studied and learned
        cursor.execute(
            "SELECT SUM(words_studied), SUM(words_learned) FROM sessions WHERE start_time >= ?",
            (start_date_str,)
        )
        result = cursor.fetchone()
        total_words_studied = result[0] if result[0] else 0
        total_words_learned = result[1] if result[1] else 0
        
        # Calculate averages
        avg_words_per_session = total_words_studied / total_sessions if total_sessions > 0 else 0
        
        # Get session durations
        cursor.execute(
            """

            SELECT start_time, end_time 

            FROM sessions 

            WHERE start_time >= ? AND end_time IS NOT NULL

            """,
            (start_date_str,)
        )
        
        # Calculate average session length
        total_minutes = 0
        session_count = 0
        
        for start_time_str, end_time_str in cursor.fetchall():
            try:
                # Parse the datetime strings
                start_time = datetime.datetime.fromisoformat(start_time_str.replace(' ', 'T'))
                end_time = datetime.datetime.fromisoformat(end_time_str.replace(' ', 'T'))
                
                # Calculate duration in minutes
                duration = (end_time - start_time).total_seconds() / 60
                total_minutes += duration
                session_count += 1
            except:
                pass
        
        avg_session_minutes = total_minutes / session_count if session_count > 0 else 0
        
        conn.close()
        
        # Return stats dictionary
        return {
            'total_sessions': total_sessions,
            'total_words_studied': total_words_studied,
            'total_words_learned': total_words_learned,
            'avg_words_per_session': avg_words_per_session,
            'avg_session_minutes': avg_session_minutes
        }
    except Exception as e:
        error_message(f"Error retrieving session stats: {str(e)}")
        return {}

# Function to check if database is properly set up
def check_database_setup():
    """Check if the database is properly set up and try to fix if needed."""
    try:
        conn = sqlite3.connect("language_learning.db")
        cursor = conn.cursor()
        
        # Check if tables exist
        cursor.execute("SELECT name FROM sqlite_master WHERE type='table';")
        tables = [table[0] for table in cursor.fetchall()]
        
        required_tables = ['vocabulary', 'user_progress', 'sessions', 'camera_translations']
        missing_tables = [table for table in required_tables if table not in tables]
        
        if missing_tables:
            # Create missing tables
            if 'vocabulary' in missing_tables:
                cursor.execute('''

                CREATE TABLE IF NOT EXISTS vocabulary (

                    id INTEGER PRIMARY KEY,

                    word_original TEXT NOT NULL,

                    word_translated TEXT NOT NULL,

                    language_translated TEXT NOT NULL,

                    category TEXT,

                    image_path TEXT,

                    date_added TIMESTAMP DEFAULT CURRENT_TIMESTAMP,

                    source TEXT DEFAULT 'manual'

                );

                ''')
            
            if 'user_progress' in missing_tables:
                cursor.execute('''

                CREATE TABLE IF NOT EXISTS user_progress (

                    id INTEGER PRIMARY KEY,

                    vocabulary_id INTEGER,

                    review_count INTEGER DEFAULT 0,

                    correct_count INTEGER DEFAULT 0,

                    last_reviewed TIMESTAMP,

                    proficiency_level INTEGER DEFAULT 0,

                    FOREIGN KEY (vocabulary_id) REFERENCES vocabulary (id)

                );

                ''')
            
            if 'sessions' in missing_tables:
                cursor.execute('''

                CREATE TABLE IF NOT EXISTS sessions (

                    id INTEGER PRIMARY KEY,

                    start_time TIMESTAMP,

                    end_time TIMESTAMP,

                    words_studied INTEGER DEFAULT 0,

                    words_learned INTEGER DEFAULT 0

                );

                ''')
            
            if 'camera_translations' in missing_tables:
                cursor.execute('''

                CREATE TABLE IF NOT EXISTS camera_translations (

                    id INTEGER PRIMARY KEY,

                    image_path TEXT,

                    detected_text TEXT,

                    translated_text TEXT,

                    source_language TEXT,

                    target_language TEXT,

                    date_captured TIMESTAMP DEFAULT CURRENT_TIMESTAMP,

                    is_saved_to_vocabulary BOOLEAN DEFAULT 0

                );

                ''')
            
            conn.commit()
        
        conn.close()
        return True
    except Exception as e:
        error_message(f"Database error: {e}")
        return False
    
def prepare_vocabulary_for_diverse_questions(vocabulary, languages):
    """Enhance vocabulary data to support diverse question types."""
    total_words = len(vocabulary)
    words_with_categories = 0
    words_with_images = 0
    words_with_examples = 0
    
    # Count and prepare vocabulary for diverse questions
    for word in vocabulary:
        # Check/count category
        if word.get('category') and word['category'] not in ['other', 'manual', '']:
            words_with_categories += 1
        
        # Check/count image
        if word.get('image_path') and os.path.exists(word.get('image_path', '')):
            words_with_images += 1
        
        # Test for example sentence
        try:
            example = get_example_sentence(word.get('word_original', ''), word.get('language_translated', 'en'))
            if example and example.get('translated'):
                words_with_examples += 1
        except:
            pass
    
    if st.session_state.debug_quiz:
        st.sidebar.markdown("### Vocabulary Stats")
        st.sidebar.markdown(f"Total words: {total_words}")
        st.sidebar.markdown(f"With categories: {words_with_categories}")
        st.sidebar.markdown(f"With images: {words_with_images}")
        st.sidebar.markdown(f"With examples: {words_with_examples}")
    
    return vocabulary

if 'db_checked' not in st.session_state:
    st.session_state.db_checked = check_database_setup()

def debug_button(label, **kwargs):
    """Debug wrapper that shows what parameters are being passed to a button and ensures uniqueness"""
    import inspect
    import time
    
    # Get the caller info
    caller = inspect.getframeinfo(inspect.currentframe().f_back)
    
    # Create a unique key based on the calling file, line number, and timestamp
    if 'key' not in kwargs:
        caller_id = f"{caller.filename.split('/')[-1]}_{caller.lineno}"
        timestamp = int(time.time() * 1000) % 10000  # Use last 4 digits of timestamp for readability
        unique_key = f"{label.replace(' ', '_')}_{caller_id}_{timestamp}"
        kwargs['key'] = unique_key
    
    # For debugging, uncomment this line to see what keys are being generated
    # print(f"Button: {label}, Key: {kwargs['key']}")
    
    # Remove any problematic parameters if present
    if 'use_column_width' in kwargs:
        del kwargs['use_column_width']
    if 'type' in kwargs and kwargs['type'] == 'primary':
        del kwargs['type']
    
    # Use the cleaned kwargs
    return st.button(label, **kwargs)

def safe_button(label, **kwargs):
    """Safe wrapper for st.button that ensures uniqueness and removes problematic parameters"""
    import time
    
    # Generate a unique key if none provided
    if 'key' not in kwargs:
        # Create unique key based on label and timestamp
        timestamp = int(time.time() * 1000) % 10000  # Use last 4 digits of timestamp for readability
        unique_key = f"{label.replace(' ', '_')}_{timestamp}"
        kwargs['key'] = unique_key
    
    # Remove problematic parameters if present
    if 'use_column_width' in kwargs:
        del kwargs['use_column_width']
    if 'type' in kwargs and kwargs['type'] == 'primary':
        del kwargs['type']
    
    # Use the cleaned kwargs
    return st.button(label, **kwargs)

# Initialize database
@st.cache_resource
def get_database():
    return LanguageLearningDB("language_learning.db")

db = get_database()

# Initialize processing queue in session state for background tasks
if 'processing_queue' not in st.session_state:
    st.session_state.processing_queue = queue.Queue()
if 'processing_results' not in st.session_state:
    st.session_state.processing_results = {}
if 'processing_complete' not in st.session_state:
    st.session_state.processing_complete = False

# Initialize session state for manual mode
if 'manual_mode' not in st.session_state:
    st.session_state.manual_mode = False
if 'manual_label' not in st.session_state:
    st.session_state.manual_label = ""

# Initialize session state variables
if 'target_language' not in st.session_state:
    st.session_state.target_language = "es"  # Default to Spanish
if 'session_id' not in st.session_state:
    st.session_state.session_id = None
if 'words_studied' not in st.session_state:
    st.session_state.words_studied = 0
if 'words_learned' not in st.session_state:
    st.session_state.words_learned = 0
if 'quiz_score' not in st.session_state:
    st.session_state.quiz_score = 0
if 'quiz_total' not in st.session_state:
    st.session_state.quiz_total = 0
if 'current_quiz_word' not in st.session_state:
    st.session_state.current_quiz_word = None
if 'quiz_options' not in st.session_state:
    st.session_state.quiz_options = []
if 'answered' not in st.session_state:
    st.session_state.answered = False
if 'detection_checkboxes' not in st.session_state:
    st.session_state.detection_checkboxes = {}
# Ensure session state variables are initialized first
if 'level' not in st.session_state:
    st.session_state.level = 1
if 'points' not in st.session_state:
    st.session_state.points = 0
if 'streak_days' not in st.session_state:
    st.session_state.streak_days = 0
if 'daily_challenges' not in st.session_state:
    st.session_state.daily_challenges = []
if 'word_of_the_day' not in st.session_state:
    st.session_state.word_of_the_day = None
# For debugging question type selection
if 'debug_quiz' not in st.session_state:
    st.session_state.debug_quiz = False
# Add these initializations with your other session state initializations
# Add these initializations with your other session state initializations
if 'audio_data' not in st.session_state:
    st.session_state.audio_data = None
if 'audio_data_received' not in st.session_state:
    st.session_state.audio_data_received = False
if 'current_recording_word' not in st.session_state:
    st.session_state.current_recording_word = None
if 'use_vision_api' not in st.session_state:
    st.session_state.use_vision_api = True
# Always force it to be True
st.session_state.use_vision_api = True
if 'app_mode' not in st.session_state:
    st.session_state.app_mode = "Camera Mode"
# Add flag to track save button state
if 'save_button_clicked' not in st.session_state:
    st.session_state.save_button_clicked = False
if 'words_just_saved' not in st.session_state:
    st.session_state.words_just_saved = False
if 'saved_count' not in st.session_state:
    st.session_state.saved_count = 0
if 'saved_items' not in st.session_state:
    st.session_state.saved_items = []
if 'faster_rcnn_model_loaded' not in st.session_state:
    st.session_state.faster_rcnn_model_loaded = False


def get_gamification():
    # Initialize GamificationSystem without the translate function for now
    return GamificationSystem()

# Initialize gamification
gamification = get_gamification()
# Make sure state is explicitly initialized
gamification.initialize_state()


# Function to translate text
class FreeTranslationService:
    def __init__(self):
        self.translation_cache = {}
        self.last_request_time = 0
        self.rate_limit_delay = 1.0  # seconds between requests
        
    def translate_text(self, text, target_language, source_language='en'):
        """

        Translate text using multiple free services with fallbacks

        """
        # Check cache first
        cache_key = f"{text}_{source_language}_{target_language}"
        if cache_key in self.translation_cache:
            return self.translation_cache[cache_key]
        
        # Rate limiting
        current_time = time.time()
        if current_time - self.last_request_time < self.rate_limit_delay:
            time.sleep(self.rate_limit_delay - (current_time - self.last_request_time))
        
        translation = None
        
        # Method 1: Deep Translator (Free Google Translate web interface)
        try:
            translator = GoogleTranslator(source=source_language, target=target_language)
            translation = translator.translate(text)
            if translation and translation != text:
                self.translation_cache[cache_key] = translation
                self.last_request_time = time.time()
                return translation
        except Exception as e:
            print(f"Deep Translator failed: {e}")
        
        # Method 2: MyMemory Translation API (Free tier: 10,000 chars/day)
        try:
            translation = self._translate_with_mymemory(text, source_language, target_language)
            if translation:
                self.translation_cache[cache_key] = translation
                self.last_request_time = time.time()
                return translation
        except Exception as e:
            print(f"MyMemory failed: {e}")
        
        # Method 3: LibreTranslate (if you have a server)
        try:
            translation = self._translate_with_libretranslate(text, source_language, target_language)
            if translation:
                self.translation_cache[cache_key] = translation
                self.last_request_time = time.time()
                return translation
        except Exception as e:
            print(f"LibreTranslate failed: {e}")
        
        # Method 4: Hugging Face models (for specific language pairs)
        try:
            translation = self._translate_with_huggingface(text, source_language, target_language)
            if translation:
                self.translation_cache[cache_key] = translation
                self.last_request_time = time.time()
                return translation
        except Exception as e:
            print(f"Hugging Face translation failed: {e}")
        
        # Fallback: Return formatted message
        return f"[Translation to {target_language} unavailable]"
    
    def _translate_with_mymemory(self, text, source_lang, target_lang):
        """MyMemory Translation API - Free tier"""
        url = "https://api.mymemory.translated.net/get"
        params = {
            'q': text,
            'langpair': f"{source_lang}|{target_lang}"
        }
        
        response = requests.get(url, params=params, timeout=10)
        if response.status_code == 200:
            data = response.json()
            if data.get('responseStatus') == 200:
                return data['responseData']['translatedText']
        return None
    
    def _translate_with_libretranslate(self, text, source_lang, target_lang):
        """LibreTranslate - Free self-hosted option"""
        # You can use the free public instance (limited) or host your own
        url = "https://libretranslate.de/translate"  # Public instance
        
        data = {
            'q': text,
            'source': source_lang,
            'target': target_lang,
            'format': 'text'
        }
        
        response = requests.post(url, data=data, timeout=10)
        if response.status_code == 200:
            result = response.json()
            return result.get('translatedText')
        return None
    
    def _translate_with_huggingface(self, text, source_lang, target_lang):
        """Hugging Face translation models - Completely free"""
        try:
            # Map language codes to model names (add more as needed)
            model_map = {
                ('en', 'es'): 'Helsinki-NLP/opus-mt-en-es',
                ('en', 'fr'): 'Helsinki-NLP/opus-mt-en-fr',
                ('en', 'de'): 'Helsinki-NLP/opus-mt-en-de',
                ('en', 'it'): 'Helsinki-NLP/opus-mt-en-it',
                ('en', 'pt'): 'Helsinki-NLP/opus-mt-en-pt',
                ('en', 'ru'): 'Helsinki-NLP/opus-mt-en-ru',
                # Add reverse translations
                ('es', 'en'): 'Helsinki-NLP/opus-mt-es-en',
                ('fr', 'en'): 'Helsinki-NLP/opus-mt-fr-en',
                ('de', 'en'): 'Helsinki-NLP/opus-mt-de-en',
            }
            
            model_name = model_map.get((source_lang, target_lang))
            if not model_name:
                return None
            
            # Load model and tokenizer
            translator = pipeline(
                "translation", 
                model=model_name,
                return_all_scores=False,
                max_length=512
            )
            
            result = translator(text)
            return result[0]['translation_text']
            
        except Exception as e:
            print(f"Hugging Face model error: {e}")
            return None

# Initialize the service
free_translator = FreeTranslationService()

# Replace your translate_text function with this:
def translate_text(text, target_language, source_language='en'):
    return free_translator.translate_text(text, target_language, source_language)


# Connect the translation function to gamification system after both are initialized
gamification.set_translate_func(translate_text)

@st.cache_resource
def get_example_generator():
    """Initialize and cache the example sentence generator."""
    return ExampleSentenceGenerator(translate_func=translate_text, debug=True)

example_generator = get_example_generator()

# Function for text-to-speech
def text_to_speech(text, lang):
    try:
        tts = gTTS(text=text, lang=lang, slow=False)
        mp3_fp = BytesIO()
        tts.write_to_fp(mp3_fp)
        mp3_fp.seek(0)
        audio_bytes = mp3_fp.read()
        return audio_bytes
    except Exception as e:
        error_message(f"Text-to-speech error: {e}")
        return None

# Function to generate HTML for audio playback
def get_audio_html(audio_bytes):
    """Generate HTML for audio playback without autoplay."""
    audio_base64 = base64.b64encode(audio_bytes).decode()
    # Remove the autoplay attribute - only keep controls
    audio_tag = f'<audio src="data:audio/mp3;base64,{audio_base64}" controls></audio>'
    return audio_tag


# Function to load RCNN Model
@st.cache_resource
def load_faster_rcnn_model():
    """Load and cache the Faster R-CNN model from TensorFlow Hub."""
    try:
        print("Loading Faster R-CNN model...")
        model_url = "https://tfhub.dev/tensorflow/faster_rcnn/resnet50_v1_640x640/1"
        detector = hub.load(model_url)
        print("✅ Faster R-CNN model loaded successfully!")
        return detector
    except Exception as e:
        print(f"❌ Error loading Faster R-CNN model: {e}")
        return None

# Background worker function for object detection
def detect_objects_worker(image, confidence_threshold, iou_threshold, task_id):
    """Worker function to run detection in background."""
    try:
        # Run detection
        detections, rendered_image = detect_objects(image, confidence_threshold, iou_threshold)
        
        # Store results
        st.session_state.processing_results[task_id] = {
            'detections': detections,
            'result_image': rendered_image
        }
        
        # Mark task as complete
        st.session_state.processing_complete = True
    except Exception as e:
        # Store error
        st.session_state.processing_results[task_id] = {
            'error': str(e)
        }
        st.session_state.processing_complete = True



# Function to start or end a learning session
def manage_session(action):
    """Start or end learning session with improved error handling."""
    if action == "start":
        try:
            # Try to use the direct method instead of the database object
            session_id = create_session_direct()
            
            if session_id:
                st.session_state.session_id = session_id
                st.session_state.words_studied = 0
                st.session_state.words_learned = 0
                success_message(f"Started new learning session!")
                return True
            else:
                error_message("Failed to create a session directly. Check database permissions.")
                return False
                
        except Exception as e:
            error_message(f"Error starting session: {str(e)}")
            return False
            
    elif action == "end" and st.session_state.session_id:
        try:
            # Connect directly to the database
            conn = sqlite3.connect("language_learning.db")
            cursor = conn.cursor()
            
            # Update the session with end time and stats
            current_time = datetime.datetime.now()
            cursor.execute(
                "UPDATE sessions SET end_time = ?, words_studied = ?, words_learned = ? WHERE id = ?",
                (current_time, st.session_state.words_studied, st.session_state.words_learned, st.session_state.session_id)
            )
            conn.commit()
            conn.close()
            
            success_message(f"Session completed! Words studied: {st.session_state.words_studied}, Words learned: {st.session_state.words_learned}")
            # Clear session state
            st.session_state.session_id = None
            st.session_state.words_studied = 0
            st.session_state.words_learned = 0
            return True
                
        except Exception as e:
            error_message(f"Error ending session: {str(e)}")
            return False
    
    return False

# Function to save image
def save_image(image, label):
    try:
        # Convert PIL Image to OpenCV format
        img_array = np.array(image)
        img_cv = cv2.cvtColor(img_array, cv2.COLOR_RGB2BGR)
        
        # Create directory if it doesn't exist
        os.makedirs("object_images", exist_ok=True)
        
        # Save image
        filename = f"object_images/{label}_{int(time.time())}.jpg"
        cv2.imwrite(filename, img_cv)
        
        return filename
    except Exception as e:
        error_message(f"Error saving image: {e}")
        return None

# Function to start a new quiz
def start_new_quiz(vocabulary, num_questions=5):
    # Reset quiz state
    st.session_state.quiz_score = 0
    st.session_state.quiz_total = 0
    st.session_state.answered = False
    
    if not vocabulary or len(vocabulary) < 4:
        warning_message("Not enough vocabulary words for a quiz (need at least 4).")
        return False
    
    # Start a new session if needed
    if not st.session_state.session_id:
        st.session_state.session_id = create_session_direct()
        st.session_state.words_studied = 0
        st.session_state.words_learned = 0
    
    # Set up first question
    setup_new_question(vocabulary)
    return True

# Function to set up a new quiz question
def setup_new_question(vocabulary):
    if not vocabulary:
        return False
    
    # Select a random word as the question
    st.session_state.current_quiz_word = np.random.choice(vocabulary)
    
    # Create options (3 wrong + 1 correct)
    options = [st.session_state.current_quiz_word]
    while len(options) < 4:
        wrong_option = np.random.choice(vocabulary)
        if wrong_option['id'] != st.session_state.current_quiz_word['id'] and not any(o['id'] == wrong_option['id'] for o in options):
            options.append(wrong_option)
    
    # Shuffle options
    np.random.shuffle(options)
    st.session_state.quiz_options = options
    st.session_state.answered = False
    
    return True

# Function to update word progress in the database
def update_word_progress_direct(vocab_id, is_correct):
    """Update word progress directly using SQLite."""
    try:
        # Connect to the database
        conn = sqlite3.connect("language_learning.db")
        cursor = conn.cursor()
        
        # Current time for timestamp
        current_time = datetime.datetime.now()
        
        # Get current progress
        cursor.execute(
            """

            SELECT review_count, correct_count, proficiency_level 

            FROM user_progress 

            WHERE vocabulary_id = ?

            """,
            (vocab_id,)
        )
        
        result = cursor.fetchone()
        
        if result:
            review_count, correct_count, proficiency_level = result
            
            # Increment counts
            review_count = review_count + 1 if review_count else 1
            correct_count = correct_count + 1 if correct_count and is_correct else (1 if is_correct else 0)
            
            # Calculate proficiency (0-5 scale)
            if review_count > 0:
                accuracy = correct_count / review_count
                if accuracy >= 0.9 and review_count >= 5:
                    proficiency_level = 5
                elif accuracy >= 0.8 and review_count >= 4:
                    proficiency_level = 4
                elif accuracy >= 0.6 and review_count >= 3:
                    proficiency_level = 3
                elif accuracy >= 0.4 and review_count >= 2:
                    proficiency_level = 2
                elif accuracy >= 0.2:
                    proficiency_level = 1
                else:
                    proficiency_level = 0
            
            # Update progress
            cursor.execute(
                """

                UPDATE user_progress 

                SET review_count = ?, correct_count = ?, proficiency_level = ?, last_reviewed = ? 

                WHERE vocabulary_id = ?

                """,
                (review_count, correct_count, proficiency_level, current_time, vocab_id)
            )
        else:
            # Create new progress entry
            cursor.execute(
                """

                INSERT INTO user_progress 

                (vocabulary_id, review_count, correct_count, proficiency_level, last_reviewed)

                VALUES (?, ?, ?, ?, ?)

                """,
                (vocab_id, 1, 1 if is_correct else 0, 1 if is_correct else 0, current_time)
            )
        
        conn.commit()
        conn.close()
        return True
    except Exception as e:
        error_message(f"Error updating word progress: {str(e)}")
        return False

# Function to check quiz answer
def check_answer(selected_index):
    """Check if selected quiz answer is correct and update progress."""
    if st.session_state.answered:
        return
    
    selected_word = st.session_state.quiz_options[selected_index]
    is_correct = selected_word['id'] == st.session_state.current_quiz_word['id']
    
    # Update database using direct method instead of db class
    update_word_progress_direct(st.session_state.current_quiz_word['id'], is_correct)
    
    # Update session stats
    st.session_state.words_studied += 1
    if is_correct:
        st.session_state.words_learned += 1
        st.session_state.quiz_score += 1
    
    st.session_state.quiz_total += 1
    st.session_state.answered = True
    
    # Check if any challenges are completed - with error handling
    try:
        gamification.check_challenge_progress(
            quiz_score=st.session_state.quiz_score,
            quiz_total=st.session_state.quiz_total
        )
        
        # Check for quiz-related achievements
        if st.session_state.quiz_total >= 5:  # Only check if quiz is substantial
            gamification.check_achievements(
                "quiz_completed",
                score=st.session_state.quiz_score,
                total=st.session_state.quiz_total
            )
    except Exception as e:
        print(f"Gamification error in check_answer: {e}")
    
    return is_correct

# Global counter for truly unique widget IDs
if 'widget_counter' not in st.session_state:
    st.session_state.widget_counter = 0

def truly_safe_button(label, **kwargs):
    """Button helper that guarantees unique keys even with rapid clicks"""
    # Increment the global counter
    st.session_state.widget_counter += 1
    
    # Generate a unique key if none provided
    if 'key' not in kwargs:
        # Create unique key based on counter + millisecond timestamp
        import time
        timestamp = int(time.time() * 1000000) % 1000000  # Microsecond part only
        counter = st.session_state.widget_counter
        unique_key = f"{label.replace(' ', '_').lower()}_{counter}_{timestamp}"
        kwargs['key'] = unique_key
    
    # Remove problematic parameters if present
    if 'use_column_width' in kwargs:
        del kwargs['use_column_width']
    if 'type' in kwargs and kwargs['type'] == 'primary':
        kwargs['type'] = None  # Set to None instead of deleting
    
    # Use the cleaned kwargs
    return st.button(label, **kwargs)

def safe_button(label, **kwargs):
    """Alias for truly_safe_button for backward compatibility"""
    return truly_safe_button(label, **kwargs)


# Main sidebar for navigation
st.sidebar.title("🌍 Vocam")
app_mode_options = ["Camera Mode", "My Vocabulary", "Quiz Mode", "Statistics", "My Progress", "Pronunciation Practice"]
if 'app_mode' in st.session_state:
    # Use the session state value as the default index for the selectbox
    default_index = app_mode_options.index(st.session_state.app_mode) if st.session_state.app_mode in app_mode_options else 0
else:
    default_index = 0

app_mode = st.sidebar.selectbox(
    "Choose a mode",
    app_mode_options,
    index=default_index
)

# Update session state with the current selection (might be from selectbox or previous setting)
st.session_state.app_mode = app_mode

# Add gamification info to the sidebar
try:
    gamification.update_sidebar()
except Exception as e:
    st.sidebar.markdown('<div style="background-color: #1679AB; padding: 10px; border-radius: 5px; margin-top: 10px;">'
                        '<h3 style="color: #C5FF95; margin: 0;">🏆 Gamification</h3>'
                        '<p style="color: white; margin-top: 5px;">System initializing...</p>'
                        '</div>', unsafe_allow_html=True)
    print(f"Sidebar update error: {e}")

# Language selection
languages = {
    "Spanish": "es",
    "French": "fr",
    "German": "de",
    "Italian": "it",
    "Portuguese": "pt",
    "Russian": "ru",
    "Japanese": "ja",
    "Chinese (Simplified)": "zh-CN"
}

selected_language = st.sidebar.selectbox(
    "Select target language",
    list(languages.keys()),
    index=list(languages.values()).index(st.session_state.target_language) if st.session_state.target_language in languages.values() else 0
)

# Get the language code from the dropdown selection
st.session_state.target_language = languages[selected_language]

# Force update word of the day translation immediately (UPDATED APPROACH)
if 'word_of_the_day' in st.session_state and st.session_state.word_of_the_day:
    wotd = st.session_state.word_of_the_day
    original_word = wotd.get('original', '')
    
    # Hard-coded translations for "book" in different languages
    book_translations = {
        "es": "libro",
        "fr": "livre",
        "de": "Buch",
        "it": "libro",
        "pt": "livro",
        "ru": "книга",
        "ja": "本",
        "zh-CN": "书"
    }
    
    # Check if the word is "book" and update translation
    if original_word.lower() == "book" and st.session_state.target_language in book_translations:
        wotd['translated'] = book_translations[st.session_state.target_language]
        st.session_state.word_of_the_day = wotd

        
# Add this right after your language selection code
# Force update word of the day when language changes
if 'previous_language' not in st.session_state:
    st.session_state.previous_language = st.session_state.target_language
    
# Check if language has changed
if st.session_state.previous_language != st.session_state.target_language:
    # Language changed - force update word of the day
    if 'word_of_the_day' in st.session_state and st.session_state.word_of_the_day:
        wotd = st.session_state.word_of_the_day
        original = wotd.get('original', '')
        
        # Translation dictionary for common words
        translations = {
            "book": {
                "es": "libro", "fr": "livre", "de": "Buch", "it": "libro",
                "pt": "livro", "ru": "книга", "ja": "本", "zh-CN": "书"
            },
            "hello": {
                "es": "hola", "fr": "bonjour", "de": "hallo", "it": "ciao",
                "pt": "olá", "ru": "привет", "ja": "こんにちは", "zh-CN": "你好"
            },
            # Include all other translations from the previous solution
        }
        
        # Get updated translation for current language
        new_lang = st.session_state.target_language
        if original.lower() in translations and new_lang in translations[original.lower()]:
            wotd['translated'] = translations[original.lower()][new_lang]
            wotd['language'] = new_lang
            st.session_state.word_of_the_day = wotd
    
    # Update previous language to current
    st.session_state.previous_language = st.session_state.target_language
    # Force rerun to update UI
    st.rerun()

# Add help section to the sidebar
with st.sidebar.expander("ℹ️ Need Help?"):
    st.markdown("""

    **Quick Tips:**

    - 📸 Use **Camera Mode** to capture objects and learn new words

    - 📚 Review your words in **My Vocabulary**

    - 🎮 Test yourself in **Quiz Mode**

    - 📊 Track your progress in **Statistics**

    

    **On Mobile:**

    - After taking a picture, scroll down to see results

    - Tap buttons to navigate between sections

    """)


# Display appropriate content based on selected mode
if app_mode == "Camera Mode":
    style_title("📸 Camera Mode")
    # Use the enhanced info message
    info_message("Take a photo or upload an image to identify objects and learn new vocabulary.")
    
    # Session management
    session_container = st.container()
    with session_container:
        col1, col2 = st.columns(2)
        with col1:
            # Always create a placeholder for the start button
            start_button_placeholder = st.empty()
            
            # Conditionally show the button or message
            if st.session_state.session_id is None:
                if start_button_placeholder.button("Start Learning Session", key="start_session_btn"):
                    if manage_session("start"):
                        st.rerun()
            else:
                # Show session info in the same place
                start_button_placeholder.markdown(
                    f'<div class="info-box" style="margin: 0.5rem 0; height: 38px; display: flex; align-items: center;">'
                    f'Session in progress - Words: {st.session_state.words_learned}'
                    f'</div>', 
                    unsafe_allow_html=True
                )
        
        with col2:
            # Always create a placeholder for the end button
            end_button_placeholder = st.empty()
            
            # Only show the end button when a session is active
            if st.session_state.session_id is not None:
                if end_button_placeholder.button("End Session", key="end_session_btn"):
                    if manage_session("end"):
                        st.rerun()
            else:
                # Empty space with same height to maintain layout
                end_button_placeholder.markdown(
                    '<div style="height: 38px;"></div>', 
                    unsafe_allow_html=True
                )
    
    # Image input options
    image_tab1, image_tab2 = st.tabs(["📷 Take a Photo", "📁 Upload Image"])

    image = None
    # First tab: Camera
    with image_tab1:
        picture = st.camera_input("Take a picture", key="camera_input")
        if picture is not None:
            image = Image.open(picture)

    # Second tab: Upload
    with image_tab2:
        uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"], key="file_uploader")
        if uploaded_file is not None:
            image = Image.open(uploaded_file)
    
    # Detection options
    detection_type = st.radio(
        "What would you like to detect?",
        ["Objects", "Text (OCR)"],
        index=0
    )
    
    # Detection settings for objects
    if detection_type == "Objects":
        col1, col2 = st.columns(2)
        with col1:
            confidence_threshold = st.slider(
                "Detection Confidence", 
                min_value=0.3, 
                max_value=0.9, 
                value=0.5,
                step=0.05
            )
        with col2:
            iou_threshold = st.slider(
                "Duplicate Removal", 
                min_value=0.1, 
                max_value=0.9, 
                value=0.45,
                step=0.05,
                help="Lower = fewer duplicates"
            )
        
        # Set iou_threshold for optimal detection (balance between precision and maximum detection)
        iou_threshold = 0.45  # Using a lower threshold to detect more objects while maintaining precision
        
        # Auto-enhancement is always applied
        enhancement_type = "auto"
    
    # Process image if available
    if image is not None:
        # Always apply enhancement for object detection
        if detection_type == "Objects":
            with st.spinner("Enhancing image for better detection..."):
                enhanced_image = enhance_image(image, enhancement_type)
                # Use the enhanced image for detection
                image_for_detection = enhanced_image
        else:
            image_for_detection = image
        
        # Process based on detection type
        if detection_type == "Objects":
            # Use a placeholder for the spinner that we can clear later
            spinner_placeholder = st.empty()
            with spinner_placeholder.container():
                show_loading_spinner("Detecting objects... This may take a few seconds.")
            
            # Add visual separator for mobile
            separator_placeholder = st.empty()
            separator_placeholder.markdown('<div class="result-separator"></div>', unsafe_allow_html=True)
            
            # Perform the detection while showing a native spinner
            with st.spinner("Processing..."):
                detections, result_image = detect_objects(image_for_detection, confidence_threshold, iou_threshold)
            
            # Clear the spinner and separator completely
            spinner_placeholder.empty()
            separator_placeholder.empty()
            
            # Display results
            if detections:
                style_section_title("✨ Detected Objects")
                
                # Display image with detection boxes
                st.image(result_image, caption="Detected Objects")
                    
                # Display selection prompt
                st.write("Select objects to save to your vocabulary:")
                    
                # Group detections by category
                # Group detections by label to avoid duplicates
                unique_detections = {}
                for i, detection in enumerate(detections):
                    label = detection['label']
                    confidence = detection['confidence']
                    
                    # If label already exists and new confidence is lower, skip
                    if label in unique_detections and unique_detections[label][1]['confidence'] >= confidence:
                        continue
                    
                    # Otherwise add/update this label with highest confidence detection
                    unique_detections[label] = (i, detection)

                # Now group the unique detections by category
                categorized_detections = {}
                for i, detection in unique_detections.values():
                    label = detection['label']
                    category = get_object_category(label)
                    
                    if category not in categorized_detections:
                        categorized_detections[category] = []
                    
                    categorized_detections[category].append((i, detection))
                
                # Clear detection checkboxes for new image
                if 'last_image_hash' not in st.session_state or st.session_state.last_image_hash != get_image_hash(image):
                    st.session_state.detection_checkboxes = {}
                    st.session_state.last_image_hash = get_image_hash(image)
                
                # Display objects by category in expandable sections
                for category, category_detections in categorized_detections.items():
                    with st.expander(f"{category.title()} ({len(category_detections)} items)", expanded=True):
                        # Process each detection in this category
                        for i, detection in category_detections:
                            label = detection['label']
                            confidence = detection['confidence']
                            checkbox_key = f"detect_{i}"
                            
                            # Translate the label
                            translated_label = translate_text(label, st.session_state.target_language)
                            
                            # Create a container for this object
                            with st.container():
                                # Display the object info
                                st.markdown(f"**{label}** ({confidence:.2f})")
                                st.markdown(f"→ **{translated_label}**")
                                
                                # Create columns for audio, example, checkbox
                                col1, col2, col3 = st.columns([2, 2, 1])
                                
                                with col1:
                                    # Generate audio for the translated word
                                    audio_bytes = text_to_speech(translated_label, st.session_state.target_language)
                                    if audio_bytes:
                                        st.markdown(get_audio_html(audio_bytes), unsafe_allow_html=True)
                                    
                                    # Add pronunciation helpers
                                    pronunciation_tips = get_pronunciation_guide(translated_label, st.session_state.target_language)
                                    if pronunciation_tips:
                                        st.markdown("**Pronunciation Tips:**")
                                        for tip in pronunciation_tips:
                                            st.markdown(f"- {tip}")
                                
                                with col2:
                                    # Add example sentence directly (no nested expander)
                                    example = get_example_sentence(label, st.session_state.target_language)
                                    st.markdown("**Example:**")
                                    st.markdown(f"EN: {example['english']}")
                                    
                                    # Only display translated example if available
                                    if example['translated']:
                                        source = example.get('source', 'unknown')
                                        source_name = source.replace('_', ' ').replace('api', 'API').title()
                                        st.markdown(f"{selected_language}: {example['translated']}")
                                        st.markdown(f"<small><i>Source: {source_name}</i></small>", unsafe_allow_html=True)
                                        
                                        # Only generate audio if there's text to speak
                                        example_audio = text_to_speech(example['translated'], st.session_state.target_language)
                                        if example_audio:
                                            st.markdown(get_audio_html(example_audio), unsafe_allow_html=True)
                                    else:
                                        st.markdown("*Translation not available. Please install deep-translator package.*")
                                
                                with col3:
                                    # Default to checked
                                    if checkbox_key not in st.session_state.detection_checkboxes:
                                        st.session_state.detection_checkboxes[checkbox_key] = True
                                        
                                    # Add checkbox for this object
                                    st.session_state.detection_checkboxes[checkbox_key] = st.checkbox(
                                        "Save", 
                                        value=st.session_state.detection_checkboxes[checkbox_key],
                                        key=checkbox_key
                                    )
                                
                                st.markdown("---")  # Add separator

                # Create a stable persistent key for our save button
                save_button_id = "save_objects_button_fixed"
                
                # Display save button if objects haven't been saved yet
                if not st.session_state.words_just_saved:
                    # Create a button with a fixed, consistent key
                    if st.button("💾 Save Selected Objects to Vocabulary", key=save_button_id):
                        # Auto-start session if needed
                        if st.session_state.session_id is None:
                            if manage_session("start"):
                                success_message("Created a new learning session!")
                            else:
                                error_message("Failed to create a session. Please check database connection.")
                                st.stop()
                        
                        # Count selected objects
                        selected_objects = []
                        for i in range(len(detections)):
                            if st.session_state.detection_checkboxes.get(f"detect_{i}", False):
                                selected_objects.append(i)
                        
                        if not selected_objects:
                            warning_message("No objects were selected to save. Please check at least one 'Save' box.")
                        else:
                            # Save the selected objects
                            saved_count = 0
                            saved_items = []
                            
                            for i in selected_objects:
                                try:
                                    detection = detections[i]
                                    label = detection['label']
                                    translated_label = translate_text(label, st.session_state.target_language)
                                    
                                    # Save the image
                                    image_path = save_image(image, label)
                                    
                                    # Get object category
                                    category = get_object_category(label)
                                    
                                    # Add to database using direct method
                                    vocab_id = add_vocabulary_direct(
                                        word_original=label,
                                        word_translated=translated_label,
                                        language_translated=st.session_state.target_language,
                                        category=category,
                                        image_path=image_path
                                    )
                                    
                                    if vocab_id:
                                        saved_count += 1
                                        saved_items.append(f"{label}{translated_label}")
                                        # Update session stats
                                        st.session_state.words_studied += 1
                                        st.session_state.words_learned += 1
                                    else:
                                        error_message(f"Failed to save {label} to vocabulary.")
                                except Exception as e:
                                    error_message(f"Error saving {label}: {str(e)}")
                            
                            if saved_count > 0:
                                # Store the saved state and items in session state
                                st.session_state.words_just_saved = True
                                st.session_state.saved_count = saved_count
                                st.session_state.saved_items = saved_items
                                st.rerun()  # Rerun once to update the UI
                            else:
                                error_message("Failed to save any words. Please check database connection.")

                # Show success message and navigation AFTER saving (persists across reruns)
                if st.session_state.words_just_saved:
                    # Create a container for the success message
                    success_container = st.container()
                    
                    with success_container:
                        success_message(f"Successfully added {st.session_state.saved_count} new words to your vocabulary!")
                        
                        # Show saved words in a visually appealing list
                        st.markdown('<h4 style="color: #1679AB;">Words saved:</h4>', unsafe_allow_html=True)
                        for item in st.session_state.saved_items:
                            st.markdown(f"✅ {item}")
                        
                        # Show navigation options
                        st.markdown("### What would you like to do next?")
                        next_col1, next_col2, next_col3 = st.columns(3)
                        
                        # Define navigation callback functions
                        def go_to_quiz_mode():
                            st.session_state.words_just_saved = False  # Reset the saved state
                            st.session_state.app_mode = "Quiz Mode"
                            st.session_state.detection_checkboxes = {}  # Clear checkboxes
                            st.rerun()

                        def go_to_vocabulary():
                            st.session_state.words_just_saved = False  # Reset the saved state
                            st.session_state.app_mode = "My Vocabulary"
                            st.session_state.detection_checkboxes = {}  # Clear checkboxes
                            st.rerun()

                        def continue_capturing():
                            st.session_state.words_just_saved = False
                            st.session_state.detection_checkboxes = {}  # Clear checkboxes
                            st.rerun()
                        
                        # Each button with fixed keys
                        with next_col1:
                            if st.button("🎮 Go to Quiz Mode", key="quiz_nav_button"):
                                go_to_quiz_mode()
                                
                        with next_col2:
                            if st.button("📚 View My Vocabulary", key="vocab_nav_button"):
                                go_to_vocabulary()
                                
                        with next_col3:
                            if st.button("📸 Continue Capturing", key="continue_button"):
                                continue_capturing()
                
            # Add manual selection UI if enabled
            if st.session_state.manual_mode:
                st.subheader("Manual Object Selection")
                st.write("Enter a label for the object you want to learn.")
                
                # Object label input
                st.session_state.manual_label = st.text_input("Object Label:", 
                                                            value=st.session_state.manual_label,
                                                            placeholder="e.g., cup, book, chair")
                
                # Translate the label
                if st.session_state.manual_label:
                    translated_label = translate_text(st.session_state.manual_label, 
                                                    st.session_state.target_language)
                    
                    st.write(f"Original: **{st.session_state.manual_label}**")
                    st.write(f"Translation: **{translated_label}**")
                    
                    # Generate audio for the translated word
                    audio_bytes = text_to_speech(translated_label, st.session_state.target_language)
                    if audio_bytes:
                        st.markdown(get_audio_html(audio_bytes), unsafe_allow_html=True)
                    
                    # Save button for manual selection
                    if st.button("Save to Vocabulary"):
                        # Auto-start session if needed
                        if st.session_state.session_id is None:
                            if manage_session("start"):
                                success_message("Created a new learning session!")
                            else:
                                error_message("Failed to create a session.")
                                st.stop()
                        
                        # Save the image
                        image_path = save_image(image, st.session_state.manual_label)
                        
                        # Add to database
                        vocab_id = add_vocabulary_direct(
                            word_original=st.session_state.manual_label,
                            word_translated=translated_label,
                            language_translated=st.session_state.target_language,
                            category="manual",
                            image_path=image_path
                        )
                        
                        if vocab_id:
                            success_message(f"Successfully added '{st.session_state.manual_label}' to your vocabulary!")
                            st.session_state.words_studied += 1
                            st.session_state.words_learned += 1
                            
                            # Reset manual mode
                            st.session_state.manual_mode = False
                            st.session_state.manual_label = ""
                            time.sleep(1.5)
                            st.rerun()
                        else:
                            error_message("Failed to save word to vocabulary.")
                
                # Button to exit manual mode
                if st.button("Cancel Manual Selection"):
                    st.session_state.manual_mode = False
                    st.session_state.manual_label = ""
                    st.rerun()
        
        # Text OCR mode
        # Text OCR mode
        else:  # Text OCR mode
            # Create container for loading spinner
            spinner_container = st.container()
            with spinner_container:
                show_loading_spinner("Detecting text... This may take a few seconds.")
                
            # Add visual separator for mobile
            add_result_separator()
            
            with st.spinner("Detecting text..."):
                detected_text = detect_text_in_image(image)
                
                # Clear the spinner
                spinner_container.empty()
                
                # Add scroll indicator for mobile
                add_scroll_indicator()
                
                if detected_text:
                    style_section_title("📝 Detected Text")
                    st.write(detected_text)
                    
                    # Split into words for learning
                    words = [word.strip() for word in re.split(r'[^\w]', detected_text) if word.strip()]
                    
                    if words:
                        st.subheader("Words to Learn")
                        
                        # Create containers for each word
                        for i, word in enumerate(words):
                            if len(word) <= 2:  # Skip very short words
                                continue
                                
                            # Translate the word
                            translated_word = translate_text(word, st.session_state.target_language)
                            
                            # Display in a container
                            with st.container():
                                cols = st.columns([3, 1])
                                
                                with cols[0]:
                                    st.write(f"**{word}** → {translated_word}")
                                    # Add audio
                                    audio_bytes = text_to_speech(translated_word, st.session_state.target_language)
                                    if audio_bytes:
                                        st.markdown(get_audio_html(audio_bytes), unsafe_allow_html=True)
                                
                                with cols[1]:
                                    # Add save button for each word
                                    if st.button(f"Save", key=f"save_text_{i}"):
                                        # Auto-start session if needed
                                        if st.session_state.session_id is None:
                                            manage_session("start")
                                        
                                        # Save to vocabulary
                                        vocab_id = add_vocabulary_direct(
                                            word_original=word,
                                            word_translated=translated_word,
                                            language_translated=st.session_state.target_language,
                                            category="text",
                                            image_path=None
                                        )
                                        
                                        if vocab_id:
                                            success_message(f"Added '{word}' to vocabulary!")
                                            st.session_state.words_studied += 1
                                            st.session_state.words_learned += 1
                                        else:
                                            error_message(f"Failed to save '{word}'")
                                
                                st.markdown("---")
                    else:
                        info_message("No clear words detected in the image.")
                else:
                    info_message("No text detected. Try another image or adjust image clarity.")

elif app_mode == "My Vocabulary":
    style_title("📚 My Vocabulary")
    st.markdown("Review all the words you've learned so far.")
    
    # Get vocabulary from database
    vocabulary = get_all_vocabulary_direct()
    
    # Filter options
    col1, col2 = st.columns(2)
    with col1:
        filter_language = st.selectbox(
            "Filter by language:",
            ["All"] + list(languages.keys()),
            index=0
        )
    with col2:
        sort_by = st.selectbox(
            "Sort by:",
            ["Date added (newest first)", "Date added (oldest first)", "Proficiency (low to high)", "Proficiency (high to low)"]
        )
    
    # Apply filters
    filtered_vocab = []
    for word in vocabulary:
        # Skip any None entries or entries without required fields
        if word is None or 'language_translated' not in word:
            continue
            
        if filter_language == "All" or languages.get(filter_language) == word['language_translated']:
            # Make sure proficiency_level exists
            if 'proficiency_level' not in word or word['proficiency_level'] is None:
                word['proficiency_level'] = 0
                
            filtered_vocab.append(word)
    
    # Sort vocabulary
    if filtered_vocab:
        if sort_by == "Date added (newest first)":
            filtered_vocab.sort(key=lambda x: x.get('date_added', ''), reverse=True)
        elif sort_by == "Date added (oldest first)":
            filtered_vocab.sort(key=lambda x: x.get('date_added', ''))
        elif sort_by == "Proficiency (low to high)":
            filtered_vocab.sort(key=lambda x: x.get('proficiency_level', 0))
        elif sort_by == "Proficiency (high to low)":
            filtered_vocab.sort(key=lambda x: x.get('proficiency_level', 0), reverse=True)
    
    # Display vocabulary
    if filtered_vocab:
        st.markdown(f"**Found {len(filtered_vocab)} words in your vocabulary collection.**")
        
        # Create data for table view
        table_data = []
        for word in filtered_vocab:
            # Skip entries without required fields
            if not all(k in word for k in ['word_original', 'word_translated', 'language_translated']):
                continue
                
            # Get language name from code
            lang_code = word.get('language_translated', '')
            lang_name = next((k for k, v in languages.items() if v == lang_code), lang_code)
            
            # Format proficiency level
            proficiency = word.get('proficiency_level', 0) or 0
            proficiency_display = "⭐" * proficiency
            
            # Format date
            date_added = word.get('date_added', '')
            if date_added and isinstance(date_added, str):
                date_display = date_added.split()[0] if ' ' in date_added else date_added
            else:
                date_display = "Unknown"
            
            table_data.append({
                "Original": word.get('word_original', ''),
                "Translation": word.get('word_translated', ''),
                "Language": lang_name,
                "Proficiency": proficiency_display,
                "Date Added": date_display
            })
        
        # Display as a dataframe
        if table_data:
            st.dataframe(pd.DataFrame(table_data))
            
            # Detailed view
            st.subheader("Word Details")
            selected_word_index = st.selectbox(
                "Select a word to review:",
                range(len(filtered_vocab)),
                format_func=lambda i: f"{filtered_vocab[i].get('word_original', '')}{filtered_vocab[i].get('word_translated', '')}"
            )
            
            word = filtered_vocab[selected_word_index]
            
            # Display word details
            col1, col2 = st.columns([2, 3])
            with col1:
                st.markdown(f"**Original:** {word.get('word_original', '')}")
                st.markdown(f"**Translation:** {word.get('word_translated', '')}")
                
                # Language name from code
                lang_code = word.get('language_translated', '')
                lang_name = next((k for k, v in languages.items() if v == lang_code), lang_code)
                st.markdown(f"**Language:** {lang_name}")
                
                if word.get('category'):
                    st.markdown(f"**Category:** {word.get('category', '')}")
                
                # Generate pronunciation audio
                st.markdown("**Listen to pronunciation:**")
                audio_bytes = text_to_speech(word.get('word_translated', ''), word.get('language_translated', ''))
                if audio_bytes:
                    st.markdown(get_audio_html(audio_bytes), unsafe_allow_html=True)
                
                # Show proficiency if available
                proficiency = word.get('proficiency_level', 0) or 0
                st.markdown("**Learning progress:**")
                st.progress(proficiency / 5)
                review_count = word.get('review_count', 0) or 0
                st.markdown(f"Proficiency: {proficiency}/5 (based on {review_count} reviews)")
                
                # Add pronunciation helpers
                pronunciation_tips = get_pronunciation_guide(word.get('word_translated', ''), word.get('language_translated', ''))
                if pronunciation_tips:
                    st.markdown("**Pronunciation tips:**")
                    for tip in pronunciation_tips:
                        st.markdown(f"- {tip}")
            
            with col2:
                # Display image if available
                image_path = word.get('image_path', '')
                if image_path and os.path.exists(image_path):
                    try:
                        image = Image.open(image_path)
                        st.image(image, caption=f"Image for {word.get('word_original', '')}")
                    except Exception as e:
                        error_message(f"Error loading image: {e}")
                else:
                    st.markdown("*No image available for this word*")
                
                # Add pronunciation practice if available
                if has_pronunciation_practice:
                    try:
                        # Only initialize if not already initialized
                        if 'pronunciation_practice' not in st.session_state:
                            # Initialize the enhanced pronunciation practice module
                            st.session_state.pronunciation_practice = create_pronunciation_practice(
                                text_to_speech_func=text_to_speech, 
                                get_audio_html_func=get_audio_html,
                                translate_text_func=translate_text,
                                get_example_sentence_func=get_example_sentence
                            )
                            print("✅ Enhanced pronunciation practice initialized with AI feedback")

                            # Add pronunciation practice capabilities to session state
                            st.session_state.pronunciation_capabilities = {
                                'realtime_feedback': True,
                                'ai_analysis': True,
                                'visual_feedback': True,
                                'progress_tracking': True
                            }
                            
                        # Check for custom recorder availability
                        try:
                            from custom_audio_recorder import audio_recorder
                            st.session_state.pronunciation_practice.has_custom_recorder = True
                            print("✅ Custom audio recorder available")
                        except ImportError:
                            st.session_state.pronunciation_practice.has_custom_recorder = False
                            print("ℹ️ Using fallback recording methods")
                            # Handle saving pronunciation words to vocabulary
                            if 'save_pronunciation_word' in st.session_state:
                                word_data = st.session_state.save_pronunciation_word
                                
                                # Auto-start session if needed
                                if st.session_state.session_id is None:
                                    if manage_session("start"):
                                        success_message("Created a new learning session!")
                                
                                # Add to vocabulary
                                vocab_id = add_vocabulary_direct(
                                    word_original=word_data['original'],
                                    word_translated=word_data['translated'],
                                    language_translated=word_data['language'],
                                    category="pronunciation_practice",
                                    image_path=None
                                )
                                
                                if vocab_id:
                                    # Update session stats IMMEDIATELY
                                    st.session_state.words_studied += 1
                                    if word_data.get('score', 0) >= 70:  # Consider "learned" if good score
                                        st.session_state.words_learned += 1
                                    
                                    # Show success message
                                    success_message(f"✅ '{word_data['original']}' saved to vocabulary! Score: {word_data.get('score', 0):.0f}%")
                                    
                                    # Update database session stats immediately
                                    if st.session_state.session_id:
                                        try:
                                            conn = sqlite3.connect("language_learning.db")
                                            cursor = conn.cursor()
                                            cursor.execute(
                                                "UPDATE sessions SET words_studied = ?, words_learned = ? WHERE id = ?",
                                                (st.session_state.words_studied, st.session_state.words_learned, st.session_state.session_id)
                                            )
                                            conn.commit()
                                            conn.close()
                                        except Exception as e:
                                            print(f"Error updating session: {e}")
                                    
                                    # Check achievements
                                    try:
                                        gamification.check_achievements(
                                            "pronunciation_practice",
                                            word=word_data['original'],
                                            score=word_data['score']
                                        )
                                    except Exception as e:
                                        print(f"Gamification error: {e}")
                                
                                # Clear the save request
                                del st.session_state.save_pronunciation_word
                                
                                # Force UI update
                                st.rerun()

                            # Debug information (you can remove this later)
                            if st.checkbox("🔧 Show Debug Info", key="debug_pronunciation"):
                                st.write("**Session State Debug:**")
                                st.write(f"Session ID: {st.session_state.session_id}")
                                st.write(f"Words Studied: {st.session_state.words_studied}")
                                st.write(f"Words Learned: {st.session_state.words_learned}")
                                st.write(f"Has audio_data: {'audio_data' in st.session_state and st.session_state.audio_data is not None}")
                                st.write(f"Audio received: {st.session_state.get('audio_data_received', False)}")
                                if 'last_pronunciation_results' in st.session_state:
                                    st.write("**Last Results:**")
                                    st.write(st.session_state.last_pronunciation_results)
                            
                    except Exception as e:
                        print(f"❌ Error initializing pronunciation practice: {str(e)}")
                        has_pronunciation_practice = False
                        
                # Add example sentence directly (no expander)
                example = get_example_sentence(word.get('word_original', ''), word.get('language_translated', ''))
                st.markdown(f"**Example in context:**")
                st.markdown(f"**English:** {example['english']}")

                if example['translated']:
                    source = example.get('source', 'unknown')
                    source_name = source.replace('_', ' ').replace('api', 'API').title()
                    st.markdown(f"**{lang_name}:** {example['translated']}")
                    st.markdown(f"<small><i>Source: {source_name}</i></small>", unsafe_allow_html=True)
                    
                    # Only generate audio if there's text to speak
                    example_audio = text_to_speech(example['translated'], word.get('language_translated', ''))
                    if example_audio:
                        st.markdown(get_audio_html(example_audio), unsafe_allow_html=True)

        else:
            warning_message("There was an issue with the vocabulary data format.")
    else:
        info_message("No vocabulary words found with current filter. Go to Camera Mode to start learning new words!")

elif app_mode == "Quiz Mode":
    style_title("🎮 Quiz Mode")
    st.markdown("Test your vocabulary knowledge with interactive quizzes.")
    
    # Import the quiz system if not already imported
    if 'quiz_system' not in st.session_state:
        try:
            # Import quiz system
            from quiz_system import QuizSystem
            
            # Create a dictionary of database functions
            db_functions = {
                'get_all_vocabulary_direct': get_all_vocabulary_direct,
                'update_word_progress_direct': update_word_progress_direct
            }
            
            # Initialize the quiz system
            quiz_system = QuizSystem(
                db_functions=db_functions,
                text_to_speech=text_to_speech,
                get_audio_html=get_audio_html,
                get_example_sentence=get_example_sentence,
                get_pronunciation_guide=get_pronunciation_guide
            )
            
            # Store in session state
            st.session_state.quiz_system = quiz_system
            
            # Add gamification to session state for access by quiz system
            st.session_state.gamification = gamification
            
        except ImportError as e:
            error_message(f"Error loading quiz system: {e}")
            info_message("Please make sure quiz_system.py is in the same directory as main.py")
            st.stop()
    
    # Get the quiz system from session state
    quiz_system = st.session_state.quiz_system
    
    # Get vocabulary from database
    vocabulary = get_all_vocabulary_direct()
    
    # Quiz settings tab and quiz display tab
    if 'quiz_completed' not in st.session_state:
        st.session_state.quiz_completed = False
        
    if st.session_state.current_quiz_word and st.session_state.quiz_options:
        # Quiz is already in progress, display it
        quiz_system.display_quiz_question(languages, manage_session)
        
        # Display current score in sidebar
        st.sidebar.markdown(f"### Current Score: {st.session_state.quiz_score}/{st.session_state.quiz_total}")
        if st.session_state.quiz_total > 0:
            accuracy = (st.session_state.quiz_score / st.session_state.quiz_total) * 100
            st.sidebar.markdown(f"**Accuracy:** {accuracy:.1f}%")
            
    # Display quiz results if quiz is completed
    elif st.session_state.quiz_completed and st.session_state.quiz_total > 0:
        quiz_system.display_quiz_results()
        
    # Display quiz setup
    else:
        # Introduction
        st.markdown("""

        Choose your quiz settings below to test your vocabulary knowledge.

        The quiz will randomly include different types of questions:

        

        - 🔄 Translation (both directions)

        - 🖼️ Image recognition

        - 📝 Sentence completion

        - 🎯 Category matching

        - 📊 Related words identification

        - 🔊 Audio recognition

        

        Start with a small number of questions and work your way up!

        """)
        
        # Quiz settings in columns
        col1, col2, col3 = st.columns(3)
        
        with col1:
            quiz_language = st.selectbox(
                "Quiz language:",
                list(languages.keys()),
                index=list(languages.values()).index(st.session_state.target_language) if st.session_state.target_language in languages.values() else 0
            )
            quiz_lang_code = languages[quiz_language]
        
        with col2:
            num_questions = st.number_input("Number of questions:", min_value=1, max_value=20, value=5)
        
        with col3:
            # Get all categories from vocabulary
            categories = set()
            for word in vocabulary:
                if word and 'category' in word and word['category'] and word['category'] not in ['other', 'manual']:
                    categories.add(word['category'])
            
            if categories:
                category_filter = st.selectbox(
                    "Category filter (optional):",
                    ["All Categories"] + sorted(list(categories))
                )
            else:
                category_filter = "All Categories"
        
        # Filter vocabulary by selected language
        filtered_vocab = [word for word in vocabulary if word['language_translated'] == quiz_lang_code]
        
        # Further filter by category if selected
        if category_filter != "All Categories":
            filtered_vocab = [word for word in filtered_vocab if word.get('category') == category_filter]
        
        filtered_vocab = prepare_vocabulary_for_diverse_questions(filtered_vocab, languages)
        # Display information about available words
        if filtered_vocab:
            st.markdown(f"**{len(filtered_vocab)} words available** for your quiz in {quiz_language}" + 
                        (f" ({category_filter} category)" if category_filter != "All Categories" else ""))
            
            # Count words with images
            words_with_images = sum(1 for word in filtered_vocab 
                                  if word.get('image_path') and os.path.exists(word.get('image_path', '')))
            
            # Show details on available question types
            st.markdown(f"*{words_with_images} words have images for image recognition questions*")
            
            # Start quiz button with dynamic label
            start_label = "Start Quiz" if len(filtered_vocab) >= 4 else f"Need {4-len(filtered_vocab)} More Word(s)"
            if st.button(start_label, disabled=len(filtered_vocab) < 4):
                if quiz_system.start_new_quiz(filtered_vocab, languages, num_questions, manage_session):
                    st.rerun()
            
            # Show word preview 
            if st.checkbox("Preview Available Words"):
                # Create a simple table of words
                preview_data = []
                for word in filtered_vocab[:20]:  # Limit preview to 20 words
                    preview_data.append({
                        "Original": word.get('word_original', ''),
                        "Translation": word.get('word_translated', ''),
                        "Category": word.get('category', '')
                    })
                
                st.dataframe(pd.DataFrame(preview_data))
                
                if len(filtered_vocab) > 20:
                    st.markdown(f"*...and {len(filtered_vocab) - 20} more words*")
        else:
            warning_message(f"No vocabulary words found with current filter. Go to Camera Mode to add words in {quiz_language}" +
                      (f" for the {category_filter} category" if category_filter != "All Categories" else "") + ".")
            
            # Show a specific message for empty vocabulary
            if not vocabulary:
                info_message("Start by learning some words in Camera Mode to build your vocabulary!")
            elif not any(word['language_translated'] == quiz_lang_code for word in vocabulary):
                info_message(f"You don't have any words in {quiz_language} yet. Try selecting a different language or add some new words.")
            else:
                info_message(f"No words found in the {category_filter} category. Try selecting 'All Categories' or add words in this category.")

elif app_mode == "Statistics":
    style_title("📊 Learning Statistics")
    st.markdown("Track your progress and learning habits.")
    
    # Get session stats for the last 30 days
    stats = get_session_stats_direct(30)
    
    # Debug display for stats
    if st.checkbox("Show raw stats data"):
        st.write("Raw stats data from database:")
        st.write(stats)
    
    # Check if stats exist and have total_sessions
    if stats and stats.get('total_sessions'):
        # Display overall statistics
        st.subheader("Overall Statistics (Last 30 Days)")
        col1, col2, col3 = st.columns(3)
        
        with col1:
            st.metric("Total Sessions", stats.get('total_sessions', 0) or 0)
        with col2:
            st.metric("Words Studied", stats.get('total_words_studied', 0) or 0)
        with col3:
            st.metric("Words Learned", stats.get('total_words_learned', 0) or 0)
        
        # Learning efficiency
        st.subheader("Learning Efficiency")
        col1, col2 = st.columns(2)
        
        with col1:
            avg_words = stats.get('avg_words_per_session', 0) or 0
            st.metric("Avg Words per Session", f"{avg_words:.1f}")
        
        with col2:
            avg_time = stats.get('avg_session_minutes', 0) or 0
            st.metric("Avg Session Length", f"{avg_time:.1f} min")
        
        # Vocabulary distribution by language
        st.subheader("Vocabulary by Language")
        
        # Get all vocabulary items
        vocabulary = get_all_vocabulary_direct()
        
        # Count words per language
        language_counts = {}
        for word in vocabulary:
            if word is None or 'language_translated' not in word:
                continue
                
            lang = word['language_translated']
            if lang in language_counts:
                language_counts[lang] += 1
            else:
                language_counts[lang] = 1
        
        # Convert language codes to names
        language_names = {}
        for name, code in languages.items():
            if code in language_counts:
                language_names[name] = language_counts[code]
        
        # Create chart data
        if language_names:
            chart_data = pd.DataFrame({
                'Language': list(language_names.keys()),
                'Word Count': list(language_names.values())
            })
            
            # Plot bar chart
            fig, ax = plt.subplots(figsize=(10, 6))
            bars = ax.bar(chart_data['Language'], chart_data['Word Count'], color='skyblue')
            
            # Add count labels on top of bars
            for bar in bars:
                height = bar.get_height()
                ax.text(bar.get_x() + bar.get_width()/2., height + 0.1,
                        f'{int(height)}', ha='center', va='bottom')
            
            ax.set_xlabel('Language')
            ax.set_ylabel('Number of Words')
            ax.set_title('Vocabulary Distribution by Language')
            plt.xticks(rotation=45)
            plt.tight_layout()
            
            st.pyplot(fig)
        
        # Get proficiency distribution
        st.subheader("Proficiency Level Distribution")
        
        proficiency_counts = {0: 0, 1: 0, 2: 0, 3: 0, 4: 0, 5: 0}
        for word in vocabulary:
            if word is None:
                continue
            level = word.get('proficiency_level', 0) or 0
            proficiency_counts[level] += 1
        
        # Create proficiency chart
        prof_data = pd.DataFrame({
            'Level': [f"Level {lvl}" for lvl in proficiency_counts.keys()],
            'Words': list(proficiency_counts.values())
        })
        
        if sum(proficiency_counts.values()) > 0:
            fig, ax = plt.subplots(figsize=(10, 6))
            colors = ['#FFCCCC', '#FFE5CC', '#FFFFCC', '#E5FFCC', '#CCFFCC', '#CCFFEF']
            bars = ax.bar(prof_data['Level'], prof_data['Words'], color=colors)
            
            # Add count labels
            for bar in bars:
                height = bar.get_height()
                if height > 0:
                    ax.text(bar.get_x() + bar.get_width()/2., height + 0.1,
                            f'{int(height)}', ha='center', va='bottom')
            
            ax.set_xlabel('Proficiency Level')
            ax.set_ylabel('Number of Words')
            ax.set_title('Word Distribution by Proficiency Level')
            plt.tight_layout()
            
            st.pyplot(fig)
            
            # Add explanation of proficiency levels
            st.markdown("""

            **Proficiency Level Guide:**

            - **Level 0**: New words or words answered incorrectly multiple times

            - **Level 1**: Basic recognition (20% correct answers)

            - **Level 2**: Beginning to remember (40% correct answers)

            - **Level 3**: Moderate proficiency (60% correct answers)

            - **Level 4**: Good proficiency (80% correct answers)

            - **Level 5**: Mastered (90-100% correct answers)

            """)
        
        # Learning suggestions section
        st.subheader("Learning Suggestions")
        st.markdown("""

        Based on your learning patterns, here are some suggestions:

        

        1. **Words to Review**: Focus on lower proficiency words

        2. **Optimal Session Length**: Aim for 10-15 minute learning sessions

        3. **Learning Frequency**: Try to complete at least one session per day

        """)
        
    else:
        info_message("No learning statistics available yet. Complete some learning sessions to see your progress!")
        
        if st.button("Generate Sample Statistics (Demo)"):
            # Create sample data for demonstration
            st.subheader("Sample Statistics (Demo)")
            
            col1, col2, col3 = st.columns(3)
            with col1:
                st.metric("Total Sessions", 5)
            with col2:
                st.metric("Words Studied", 42)
            with col3:
                st.metric("Words Learned", 38)
                
            # Sample chart
            sample_data = pd.DataFrame({
                'Language': ['Spanish', 'French', 'German', 'Italian'],
                'Word Count': [15, 12, 8, 7]
            })
            
            fig, ax = plt.subplots(figsize=(10, 6))
            ax.bar(sample_data['Language'], sample_data['Word Count'], color='lightgray')
            ax.set_xlabel('Language')
            ax.set_ylabel('Number of Words (Sample Data)')
            ax.set_title('Example: Vocabulary Distribution by Language')
            plt.tight_layout()
            
            st.pyplot(fig)
            
            st.markdown("*This is sample data. Start learning with Camera Mode to begin tracking your real progress!*")
elif app_mode == "My Progress":
    try:
        gamification.render_dashboard()
    except Exception as e:
        error_message("There was an error displaying the Progress. The system might be initializing.")
        info_message("Please try again in a moment or add some vocabulary first to initialize the system.")
        print(f"Dashboard error: {e}")

elif app_mode == "Pronunciation Practice":
    style_title("🤖 AI-Powered Pronunciation Practice")
    st.markdown("Practice your pronunciation with real-time AI feedback and comprehensive analysis.")

    # Session management
    col1, col2 = st.columns(2)
    with col1:
        if st.session_state.session_id is None:
            if st.button("Start Learning Session", key="start_pron_session"):
                if manage_session("start"):
                    st.rerun()
        else:
            info_message(f"Session in progress - Words studied: {st.session_state.words_studied}")
    with col2:
        if st.session_state.session_id is not None:
            if st.button("End Session", key="end_pron_session"):
                if manage_session("end"):
                    st.rerun()
    
    if has_pronunciation_practice:
        try:
            # Initialize pronunciation practice if not already initialized
            if 'pronunciation_practice' not in st.session_state:
                # Initialize the enhanced pronunciation practice module
                st.session_state.pronunciation_practice = create_pronunciation_practice(
                    text_to_speech_func=text_to_speech, 
                    get_audio_html_func=get_audio_html,
                    translate_text_func=translate_text,
                    get_example_sentence_func=get_example_sentence
                )
                print("✅ Enhanced pronunciation practice initialized with AI feedback")

                # Add pronunciation practice capabilities to session state
                st.session_state.pronunciation_capabilities = {
                    'realtime_feedback': True,
                    'ai_analysis': True,
                    'visual_feedback': True,
                    'progress_tracking': True
                }
            
            # Check for custom recorder availability
            try:
                from custom_audio_recorder import audio_recorder
                st.session_state.pronunciation_practice.has_custom_recorder = True
                print("✅ Custom audio recorder available")
            except ImportError:
                st.session_state.pronunciation_practice.has_custom_recorder = False
                print("ℹ️ Using fallback recording methods")
            # Enhanced pronunciation practice interface
            st.markdown("""

            ### 🎯 Features Available:

            - **Real-time feedback** during recording

            - **AI-powered analysis** of your pronunciation 

            - **Visual spectrograms** showing sound patterns

            - **Progress tracking** across practice sessions

            - **Language-specific tips** for difficult sounds

            """)
            
            # Get vocabulary from database
            vocabulary = get_all_vocabulary_direct()
            
            # Language selection
            practice_language = st.selectbox(
                "Select practice language:",
                list(languages.keys()),
                index=list(languages.values()).index(st.session_state.target_language) 
                    if st.session_state.target_language in languages.values() else 0,
                key="pron_lang_select"
            )
            practice_language_code = languages[practice_language]
            
            # Filter vocabulary for the selected language
            filtered_vocab = [word for word in vocabulary if word['language_translated'] == practice_language_code]
            
            if filtered_vocab:
                # Practice mode selection
                practice_mode = st.radio(
                    "Choose practice mode:",
                    [
                        "📚 Individual Word Practice", 
                        "🎯 Focused Practice Session",
                        "🏆 Challenge Mode"
                    ],
                    key="practice_mode_select"
                )
                
                if practice_mode == "📚 Individual Word Practice":
                    # Individual word practice
                    word_index = st.selectbox(
                        "Select a word to practice:",
                        range(len(filtered_vocab)),
                        format_func=lambda i: f"{filtered_vocab[i].get('word_translated', '')} ({filtered_vocab[i].get('word_original', '')})",
                        key="word_select"
                    )
                    
                    selected_word = filtered_vocab[word_index]
                    st.session_state.pronunciation_practice.render_practice_ui(selected_word)
                
                elif practice_mode == "🎯 Focused Practice Session":
                    # Focused session mode
                    if 'practice_session_words' not in st.session_state:
                        session_size = st.slider("Number of words to practice:", 3, 10, 5)
                        
                        if st.button("🚀 Start Focused Session", type="primary"):
                            import random
                            st.session_state.practice_session_words = random.sample(
                                filtered_vocab, min(session_size, len(filtered_vocab))
                            )
                            st.session_state.current_session_index = 0
                            st.session_state.session_scores = []
                            st.rerun()
                    else:
                        # Session in progress
                        current_index = st.session_state.current_session_index
                        total_words = len(st.session_state.practice_session_words)
                        
                        # Progress display
                        progress = current_index / total_words
                        st.progress(progress)
                        st.markdown(f"**Word {current_index + 1} of {total_words}**")
                        
                        if current_index < total_words:
                            current_word = st.session_state.practice_session_words[current_index]
                            st.session_state.pronunciation_practice.render_practice_ui(current_word)
                            
                            # Session navigation
                            col1, col2, col3 = st.columns(3)
                            with col1:
                                if current_index > 0:
                                    if st.button("⬅️ Previous Word"):
                                        st.session_state.current_session_index -= 1
                                        st.rerun()
                            
                            with col2:
                                if st.button("⏭️ Skip Word"):
                                    st.session_state.current_session_index += 1
                                    st.rerun()
                            
                            with col3:
                                if current_index < total_words - 1:
                                    if st.button("➡️ Next Word"):
                                        st.session_state.current_session_index += 1
                                        st.rerun()
                        else:
                            # Session completed
                            st.success("🎉 Practice session completed!")
                            
                            # Show session results
                            if 'session_scores' in st.session_state and st.session_state.session_scores:
                                avg_score = sum(st.session_state.session_scores) / len(st.session_state.session_scores)
                                st.metric("Average Score", f"{avg_score:.0f}%")
                                
                                # Progress chart
                                fig, ax = plt.subplots(figsize=(8, 4))
                                ax.plot(range(1, len(st.session_state.session_scores) + 1), 
                                       st.session_state.session_scores, 
                                       marker='o', linestyle='-')
                                ax.set_xlabel('Word Number')
                                ax.set_ylabel('Score (%)')
                                ax.set_title('Session Progress')
                                ax.grid(True, alpha=0.3)
                                st.pyplot(fig)
                            
                            # Reset session
                            if st.button("🔄 Start New Session"):
                                for key in ['practice_session_words', 'current_session_index', 'session_scores']:
                                    if key in st.session_state:
                                        del st.session_state[key]
                                st.rerun()
                
                elif practice_mode == "🏆 Challenge Mode":
                    # Challenge mode - rapid pronunciation assessment
                    st.markdown("### 🏆 Pronunciation Challenge")
                    st.markdown("Quick-fire pronunciation assessment - get scored on speed and accuracy!")
                    
                    if 'challenge_mode' not in st.session_state:
                        difficulty = st.selectbox(
                            "Select difficulty:",
                            ["🟢 Easy (3 words)", "🟡 Medium (5 words)", "🔴 Hard (8 words)"]
                        )
                        
                        word_count = {"🟢 Easy (3 words)": 3, "🟡 Medium (5 words)": 5, "🔴 Hard (8 words)": 8}[difficulty]
                        
                        if st.button("🚀 Start Challenge!", type="primary"):
                            import random
                            st.session_state.challenge_words = random.sample(
                                filtered_vocab, min(word_count, len(filtered_vocab))
                            )
                            st.session_state.challenge_index = 0
                            st.session_state.challenge_scores = []
                            st.session_state.challenge_start_time = time.time()
                            st.session_state.challenge_mode = True
                            st.rerun()
                    else:
                        # Challenge in progress
                        current_index = st.session_state.challenge_index
                        total_words = len(st.session_state.challenge_words)
                        
                        if current_index < total_words:
                            # Timer display
                            elapsed_time = time.time() - st.session_state.challenge_start_time
                            col1, col2 = st.columns(2)
                            with col1:
                                st.metric("Time Elapsed", f"{elapsed_time:.1f}s")
                            with col2:
                                st.metric("Words Remaining", total_words - current_index)
                            
                            # Current word practice
                            current_word = st.session_state.challenge_words[current_index]
                            st.markdown(f"### Challenge Word: {current_word.get('word_translated', '')}")
                            
                            # Quick practice interface
                            st.session_state.pronunciation_practice.render_practice_ui(current_word)
                            
                        else:
                            # Challenge completed
                            total_time = time.time() - st.session_state.challenge_start_time
                            st.success(f"🏆 Challenge completed in {total_time:.1f} seconds!")
                            
                            # Challenge results
                            if st.session_state.challenge_scores:
                                avg_score = sum(st.session_state.challenge_scores) / len(st.session_state.challenge_scores)
                                speed_bonus = max(0, 100 - total_time)  # Bonus for speed
                                final_score = (avg_score * 0.8) + (speed_bonus * 0.2)
                                
                                col1, col2, col3 = st.columns(3)
                                with col1:
                                    st.metric("Average Accuracy", f"{avg_score:.0f}%")
                                with col2:
                                    st.metric("Speed Bonus", f"{speed_bonus:.0f}")
                                with col3:
                                    st.metric("Final Score", f"{final_score:.0f}%")
                                
                                # Achievement badges
                                if final_score >= 90:
                                    st.markdown("🏆 **PRONUNCIATION MASTER!**")
                                elif final_score >= 80:
                                    st.markdown("🥇 **EXCELLENT PERFORMANCE!**")
                                elif final_score >= 70:
                                    st.markdown("🥈 **GREAT JOB!**")
                                else:
                                    st.markdown("🥉 **KEEP PRACTICING!**")
                            
                            # Reset challenge
                            if st.button("🔄 New Challenge"):
                                for key in ['challenge_words', 'challenge_index', 'challenge_scores', 
                                          'challenge_start_time', 'challenge_mode']:
                                    if key in st.session_state:
                                        del st.session_state[key]
                                st.rerun()
            else:
                warning_message(f"No vocabulary words found for {practice_language}. Go to Camera Mode to add words first.")
            
        except Exception as e:
            error_message(f"Error in pronunciation practice: {str(e)}")
            st.info("Try refreshing the page or check the pronunciation practice module.")
    else:
        # Show what's available vs what's missing
        st.warning("🎤 Some pronunciation features require additional packages.")
        
        # Basic pronunciation practice without advanced features
        st.markdown("### 🎯 Basic Pronunciation Practice")
        st.markdown("You can still practice pronunciation with the available features:")
        
        # Get vocabulary
        vocabulary = get_all_vocabulary_direct()
        practice_language = st.selectbox(
            "Select practice language:",
            list(languages.keys()),
            index=list(languages.values()).index(st.session_state.target_language) 
                if st.session_state.target_language in languages.values() else 0,
            key="basic_pron_lang_select"
        )
        practice_language_code = languages[practice_language]
        
        # Filter vocabulary
        filtered_vocab = [word for word in vocabulary if word['language_translated'] == practice_language_code]
        
        if filtered_vocab:
            word_index = st.selectbox(
                "Select a word to practice:",
                range(len(filtered_vocab)),
                format_func=lambda i: f"{filtered_vocab[i].get('word_translated', '')} ({filtered_vocab[i].get('word_original', '')})",
                key="basic_word_select"
            )
            
            selected_word = filtered_vocab[word_index]
            word_translated = selected_word.get('word_translated', '')
            
            st.markdown(f"### Practice: {word_translated}")
            
            # Play pronunciation
            st.markdown("**🔊 Listen and repeat:**")
            audio_bytes = text_to_speech(word_translated, practice_language_code)
            if audio_bytes:
                st.markdown(get_audio_html(audio_bytes), unsafe_allow_html=True)
            
            # Show pronunciation tips
            pronunciation_tips = get_pronunciation_guide(word_translated, practice_language_code)
            if pronunciation_tips:
                st.markdown("**💡 Pronunciation Tips:**")
                for tip in pronunciation_tips:
                    st.markdown(f"- {tip}")
            
            # File upload for basic feedback
            st.markdown("**📁 Upload your recording for basic analysis:**")
            uploaded_audio = st.file_uploader(
                "Record yourself saying the word and upload the audio file", 
                type=["wav", "mp3", "ogg", "m4a"],
                key="basic_audio_upload"
            )
            
            if uploaded_audio:
                # Basic analysis without advanced features
                st.audio(uploaded_audio)
                
                # Simple feedback
                st.markdown("### 📝 Basic Feedback")
                st.success("✅ Audio received! Keep practicing by:")
                st.markdown("- 🔄 Comparing your pronunciation with the correct audio")
                st.markdown("- 📚 Focusing on the pronunciation tips above")
                st.markdown("- 🎯 Recording multiple attempts to improve")
                
                # Save to vocabulary option
                if st.button("💾 Save to Vocabulary", key="basic_save_vocab"):
                    if st.session_state.session_id is None:
                        manage_session("start")
                    
                    vocab_id = add_vocabulary_direct(
                        word_original=selected_word.get('word_original', ''),
                        word_translated=word_translated,
                        language_translated=practice_language_code,
                        category="pronunciation_practice",
                        image_path=None
                    )
                    
                    if vocab_id:
                        st.success("✅ Word saved to vocabulary!")
                        st.session_state.words_studied += 1
                        st.session_state.words_learned += 1
        else:
            warning_message(f"No vocabulary words found for {practice_language}. Go to Camera Mode to add words first.")
        
        # Installation guide
        st.markdown("### 🛠️ For Advanced AI Feedback")
        st.markdown("Install these packages for real-time AI pronunciation analysis:")
        st.code("pip install streamlit-webrtc speech-recognition librosa python-Levenshtein av")

st.sidebar.markdown("---")
st.sidebar.markdown("### Session Info")
if st.session_state.session_id:
    st.sidebar.success(f"Session active")
    st.sidebar.info(f"Words studied: {st.session_state.words_studied}")
    st.sidebar.info(f"Words learned: {st.session_state.words_learned}")
else:
    st.sidebar.warning("No active session")
    st.sidebar.markdown("*Start a session in Camera Mode to track progress*")

add_footer()