File size: 87,185 Bytes
c2e2750
 
87e6231
c2e2750
87e6231
c2e2750
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
87e6231
c2e2750
 
 
 
 
 
 
 
 
87e6231
bbbdd24
255c3b7
 
 
 
 
 
 
95259b2
bbbdd24
 
 
255c3b7
 
 
 
 
 
 
 
 
 
 
 
 
4579bf8
 
255c3b7
 
 
c2e2750
 
 
 
 
87e6231
c2e2750
27f8b0c
c2e2750
 
 
 
46d8e72
c2e2750
 
 
 
 
 
 
87e6231
35358da
87e6231
35358da
 
 
 
 
c2e2750
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
46d8e72
c2e2750
 
 
 
 
 
 
46d8e72
c2e2750
 
 
 
 
 
 
 
 
 
 
255c3b7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c2e2750
 
 
 
 
 
 
255c3b7
 
 
c2e2750
 
 
 
255c3b7
 
 
c2e2750
 
 
 
 
 
 
 
 
 
 
255c3b7
 
 
c2e2750
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
35358da
c2e2750
 
 
 
 
 
 
 
35358da
 
 
 
 
 
c2e2750
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4f90e1d
c2e2750
 
 
 
 
 
 
 
 
 
 
 
 
4f90e1d
 
c2e2750
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4f90e1d
c2e2750
 
 
 
 
 
 
 
bbbdd24
 
 
 
 
 
 
 
 
 
 
 
 
95259b2
 
 
 
 
 
35358da
 
 
 
 
 
 
 
 
 
 
 
 
 
 
95259b2
 
 
 
 
 
 
 
35358da
 
 
 
95259b2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
35358da
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
95259b2
 
 
35358da
 
 
 
 
987743c
 
95259b2
 
 
 
 
 
ef24a6e
95259b2
 
 
 
 
bbbdd24
c2e2750
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
35358da
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c2e2750
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0b44440
 
 
 
 
 
 
 
 
c2e2750
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4f90e1d
 
 
c2e2750
 
 
4f90e1d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c2e2750
35358da
 
 
 
 
 
 
 
987743c
 
35358da
 
0b44440
35358da
 
 
987743c
 
 
 
 
 
35358da
c2e2750
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
83d7af2
 
 
 
 
c2e2750
83d7af2
 
 
 
 
 
 
 
 
 
 
c2e2750
83d7af2
c2e2750
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
86f56cb
c2e2750
 
86f56cb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c2e2750
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8d250e3
c2e2750
 
 
 
 
 
 
 
 
bbbdd24
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c2e2750
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
35358da
 
987743c
 
 
 
35358da
 
987743c
 
 
 
 
35358da
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c2e2750
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bbbdd24
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c2e2750
87e6231
c2e2750
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
"""

================================================================================

KIAMI V1 SOFTEDGE - DATABASE MODULE

================================================================================

Banco de dados SQLite extremamente robusto para Kiami, moderno e completo.

Gerencia: mensagens, embeddings, gírias, tom, aprendizados, API logs, training sessions.



Features:

- SQLite com WAL mode para performance máxima

- Retry logic com exponential backoff

- Full-text search com FTS5

- Vector storage para embeddings (SentenceTransformers)

- Transactions.atomic()

- Backup/restore automático

- Health checks e métricas detalhadas

- Índices otimizados

- Migration system completo

- Logging detalhado

- Singleton pattern para conexões

- Suporte a numpy arrays para embeddings

- API performance tracking

- Training sessions tracking

================================================================================

"""

import sqlite3
import time
import os
import json
import hashlib
import random
from typing import Optional, List, Dict, Any, Tuple, Union
from datetime import datetime
from loguru import logger


class Database:
    """

    Classe de banco de dados robusta para Kiami V1 SoftEdge (irma mais velha da Akira 18).

    Implementada como Singleton para evitar conflitos de conexão entre workers.

    """
    
    _instances: Dict[str, 'Database'] = {}
    _initialized: Dict[str, bool] = {}
    
    # Códigos de verificação para usuários privilegiados
    CODIGOS_VERIFICACAO: Dict[str, str] = {}

    def __new__(cls, db_path: str = "/kiami/data/kiami.db"):
        import os
        import time as _time
        # Opt-out explícito: força SQLite sem tocar no socket do PG.
        if os.environ.get('DISABLE_PG') == '1' or os.environ.get('USE_SQLITE_ONLY') == '1':
            if db_path not in cls._instances:
                cls._instances[db_path] = super(Database, cls).__new__(cls)
                cls._initialized[db_path] = False
            return cls._instances[db_path]
        pg_url = os.environ.get('DATABASE_URL', '')
        if pg_url:
            try:
                from .database_pg import DatabasePG
                # Se o circuito do PG já está aberto (proxy morto), nem tenta:
                # vai direto ao SQLite sem o custo de 2-3s por request.
                try:
                    if getattr(DatabasePG, '_circuit_breaker_open', False):
                        _now = _time.time()
                        _last = getattr(DatabasePG, '_circuit_breaker_last_failure', 0.0)
                        _timeout = getattr(DatabasePG, '_CIRCUIT_BREAKER_TIMEOUT', 60.0)
                        if _now - _last < _timeout:
                            raise RuntimeError("pg circuit open - sqlite fast path")
                except RuntimeError:
                    pass
                else:
                    return DatabasePG(db_path)
            except Exception as e:
                import sys
                # Log único por processo para não poluir: o circuito do PG já loga.
                if 'circuit' not in str(e).lower() and 'disabled' not in str(e).lower():
                    print(f"[DB-DEBUG] DatabasePG falhou: {e}", file=sys.stderr)
        if db_path not in cls._instances:
            cls._instances[db_path] = super(Database, cls).__new__(cls)
            cls._initialized[db_path] = False
        return cls._instances[db_path]
    
    def __init__(self, db_path: str = "/kiami/data/kiami.db"):
        """Inicializa a conexão se ainda não foi inicializada para este path."""
        import os
        if self._initialized.get(db_path, False):
            return

        self.db_path = db_path
        self.max_retries = 3
        self.retry_delay = 0.1
        
        # Garante que o diretório /akira/data existe (Docker)
        db_dir = os.path.dirname(db_path)
        if db_dir and not os.path.exists(db_dir):
            os.makedirs(db_dir, exist_ok=True)
        
        # Fallback: se kiami.db nao existe mas akira.db existe, migra/copia
        try:
            alt = db_path.replace("kiami.db", "akira.db").replace("/kiami/", "/akira/")
            if not os.path.exists(db_path) and os.path.exists(alt):
                import shutil
                shutil.copy2(alt, db_path)
        except Exception:
            pass
        # Inicialização pesada acontece apenas uma vez
        self._init_db()
        self._init_context_isolation_tables()
        self._ensure_all_columns_and_indexes()
        
        Database._initialized[db_path] = True
        logger.info(f"Database inicializado e otimizado: {self.db_path}")
    
    # ================================================================
    # CONEXÃO + RETRY
    # ================================================================
    def _get_connection(self) -> sqlite3.Connection:
        """Obtém conexão com retry automático."""
        for attempt in range(self.max_retries):
            try:
                conn = sqlite3.connect(
                    self.db_path, 
                    timeout=3.0, 
                    check_same_thread=False
                )
                # Otimizações SQLite para performance
                conn.execute("PRAGMA journal_mode=WAL")
                conn.execute("PRAGMA synchronous=NORMAL")
                conn.execute("PRAGMA cache_size=1000")
                conn.execute("PRAGMA temp_store=MEMORY")
                conn.execute("PRAGMA busy_timeout=3000")
                conn.execute("PRAGMA foreign_keys=ON")
                conn.row_factory = sqlite3.Row
                return conn
            except sqlite3.OperationalError as e:
                if "locked" in str(e) and attempt < self.max_retries - 1:
                    time.sleep(self.retry_delay * (2 ** attempt))
                    continue
                logger.error(f"Erro de conexão DB: {e}")
                raise
        raise sqlite3.OperationalError("Falha ao conectar ao banco após várias tentativas")

    @staticmethod
    def _pg_to_sqlite_placeholders(query: str) -> str:
        """Converte placeholders %s (estilo psycopg2) para ? (sqlite),

        ignorando %s dentro de literais de string ('...').



        Ex: strftime('%s','now') é função SQLite e NÃO é placeholder —

        a conversão ingénua query.replace("%s", "?") corrompia o DDL/DML.

        Qualquer %s real fora de literais continua convertido.

        """
        if "%s" not in query or query.lstrip().upper().startswith("SHOW"):
            return query
        out = []
        i = 0
        n = len(query)
        in_str = False
        while i < n:
            ch = query[i]
            if ch == "'":
                # '' escapado dentro de literal
                if in_str and i + 1 < n and query[i + 1] == "'":
                    out.append("''")
                    i += 2
                    continue
                in_str = not in_str
                out.append(ch)
                i += 1
                continue
            if not in_str and ch == "%" and i + 1 < n and query[i + 1] == "s":
                out.append("?")
                i += 2
                continue
            out.append(ch)
            i += 1
        return "".join(out)

    def _execute_with_retry(

        self, 

        query: str, 

        params: Optional[tuple] = None, 

        commit: bool = False

    ) -> Optional[List[sqlite3.Row]]:
        """Executa query com retry automático."""
        # Converter placeholders psycopg2 (%s) para sqlite (?) se necessário
        # (queries novas já usam '?', que passa intacto; legado %s é convertido)
        sqlite_query = self._pg_to_sqlite_placeholders(query)
        for attempt in range(self.max_retries):
            try:
                with self._get_connection() as conn:
                    cur = conn.cursor()
                    cur.execute(sqlite_query, params or ())

                    if sqlite_query.strip().upper().startswith("SELECT"):
                        result = cur.fetchall()
                        return result
                    
                    if commit:
                        conn.commit()
                    return None
                    
            except sqlite3.OperationalError as e:
                if "locked" in str(e) and attempt < self.max_retries - 1:
                    time.sleep(self.retry_delay * (2 ** attempt))
                    continue
                # Inclui snippet do template SQL (sem params/dados do utilizador)
                # para diagnosticar erros tipo 'near "%": syntax error'.
                logger.error(f"Erro SQL: {e} | query={sqlite_query[:150]!r}")
                raise
        raise sqlite3.OperationalError("Query falhou após retries")

    # ================================================================
    # SCHEMA + MIGRAÇÃO
    # ================================================================
    def _init_db(self):
        """Inicializa todas as tabelas do banco."""
        try:
            with self._get_connection() as conn:
                c = conn.cursor()
                
                # Tabela de mensagens
                c.executescript("""

                CREATE TABLE IF NOT EXISTS mensagens (

                    id INTEGER PRIMARY KEY AUTOINCREMENT,

                    usuario TEXT,

                    mensagem TEXT,

                    resposta TEXT,

                    numero TEXT,

                    nome_usuario TEXT DEFAULT '',

                    is_reply BOOLEAN DEFAULT 0,

                    mensagem_original TEXT,

                    humor TEXT DEFAULT 'neutro',

                    modo_resposta TEXT DEFAULT 'normal',

                    nivel_transicao INTEGER DEFAULT 1,

                    usuario_privilegiado BOOLEAN DEFAULT 0,

                    modelo_usado TEXT DEFAULT 'desconhecido',

                    conversation_id TEXT DEFAULT '',

                    grupo_id TEXT DEFAULT '',

                    tipo_conversa TEXT DEFAULT 'pv',

                    recipient TEXT DEFAULT '',

                    quoted_author TEXT DEFAULT '',

                    is_listen BOOLEAN DEFAULT 0,

                    message_id TEXT UNIQUE,

                    created_at DATETIME DEFAULT CURRENT_TIMESTAMP

                );

                """)
                
                # Tabela de usuários privilegiados
                c.executescript("""

                CREATE TABLE IF NOT EXISTS usuarios_privilegiados (

                    id INTEGER PRIMARY KEY AUTOINCREMENT,

                    numero TEXT UNIQUE,

                    nome TEXT,

                    apelido TEXT,

                    modo_fala TEXT,

                    codigo_verificacao TEXT,

                    ativo BOOLEAN DEFAULT 1,

                    privilegio_temporario_ativo BOOLEAN DEFAULT 0,

                    expira_em REAL,

                    created_at DATETIME DEFAULT CURRENT_TIMESTAMP

                );

                """)
            
                # Tabela de embeddings
                c.executescript("""

                CREATE TABLE IF NOT EXISTS embeddings (

                    id INTEGER PRIMARY KEY AUTOINCREMENT,

                    numero_usuario TEXT,

                    source_type TEXT,

                    texto TEXT,

                    embedding BLOB

                );

                """)
                
                # Tabela de aprendizados
                c.executescript("""

                CREATE TABLE IF NOT EXISTS aprendizados (

                    id INTEGER PRIMARY KEY AUTOINCREMENT,

                    numero_usuario TEXT,

                    chave TEXT,

                    valor TEXT,

                    created_at DATETIME DEFAULT CURRENT_TIMESTAMP

                );

                """)
                
                # Tabela de gírias aprendidas
                c.executescript("""

                CREATE TABLE IF NOT EXISTS girias_aprendidas (

                    id INTEGER PRIMARY KEY AUTOINCREMENT,

                    numero_usuario TEXT,

                    giria TEXT,

                    significado TEXT,

                    contexto TEXT,

                    frequencia INTEGER DEFAULT 1,

                    created_at DATETIME DEFAULT CURRENT_TIMESTAMP,

                    updated_at DATETIME DEFAULT CURRENT_TIMESTAMP

                );

                """)
                
                # Tabela de tom do usuário
                c.executescript("""

                CREATE TABLE IF NOT EXISTS tom_usuario (

                    id INTEGER PRIMARY KEY AUTOINCREMENT,

                    numero_usuario TEXT,

                    tom_detectado TEXT,

                    intensidade REAL DEFAULT 0.5,

                    contexto TEXT,

                    humor TEXT DEFAULT 'neutro',

                    created_at DATETIME DEFAULT CURRENT_TIMESTAMP

                );

                """)
                
                # Tabela de contexto
                c.executescript("""

                CREATE TABLE IF NOT EXISTS contexto (

                    user_key TEXT PRIMARY KEY,

                    historico TEXT,

                    emocao_atual TEXT,

                    humor_atual TEXT DEFAULT 'neutro',

                    modo_resposta TEXT DEFAULT 'normal',

                    nivel_transicao INTEGER DEFAULT 1,

                    usuario_privilegiado BOOLEAN DEFAULT 0,

                    termos TEXT,

                    girias TEXT,

                    tom TEXT,

                    updated_at DATETIME DEFAULT CURRENT_TIMESTAMP

                );

                """)
                
                # Tabela de pronomes por tom
                c.executescript("""

                CREATE TABLE IF NOT EXISTS pronomes_por_tom (

                    tom TEXT PRIMARY KEY,

                    pronomes TEXT

                );

                """)
                
                # Tabela de Persona do Usuário (Character.AI style LTM)
                c.executescript("""

                CREATE TABLE IF NOT EXISTS persona_usuario (

                    numero_usuario TEXT PRIMARY KEY,

                    personalidade TEXT,

                    vicios_linguagem TEXT,

                    gostos TEXT,

                    desgostos TEXT,

                    emocional TEXT,

                    created_at DATETIME DEFAULT CURRENT_TIMESTAMP,

                    updated_at DATETIME DEFAULT CURRENT_TIMESTAMP

                );

                """)
                
                # Insere dados padrão de pronomes
                c.execute("INSERT OR IGNORE INTO pronomes_por_tom (tom, pronomes) VALUES (?, ?)", 
                         ('neutro', 'tu/você'))
                c.execute("INSERT OR IGNORE INTO pronomes_por_tom (tom, pronomes) VALUES (?, ?)", 
                         ('formal', 'o senhor/a senhora'))
                c.execute("INSERT OR IGNORE INTO pronomes_por_tom (tom, pronomes) VALUES (?, ?)", 
                         ('informal', 'puto/kota'))
                c.execute("INSERT OR IGNORE INTO pronomes_por_tom (tom, pronomes) VALUES (?, ?)", 
                         ('tecnico_formal', 'senhor'))
                
                # Insere usuários privilegiados padrão
                usuarios_default = [
                    ('244937035662', 'Isaac Quarenta', 'Isaac', 'tecnico_formal'),
                    ('244978787009', 'Isaac Quarenta 2', 'Isaac', 'tecnico_formal')
                ]
                for numero, nome, apelido, modo in usuarios_default:
                    c.execute("""

                        INSERT OR IGNORE INTO usuarios_privilegiados 

                        (numero, nome, apelido, modo_fala) VALUES (?, ?, ?, ?)

                    """, (numero, nome, apelido, modo))
                
                # ===== LSTM MEMORY SYSTEM TABLES =====
                c.executescript("""

                CREATE TABLE IF NOT EXISTS lstm_contexto (

                    context_id VARCHAR(255) NOT NULL,

                    numero_usuario VARCHAR(50) NOT NULL,

                    topic_principal VARCHAR(255),

                    subtopicas JSON,

                    conversation_path JSON,

                    interaction_pattern VARCHAR(50),

                    emotional_state VARCHAR(50),

                    unanswered_questions JSON,

                    assumed_knowledge JSON,

                    last_key_message TEXT,

                    context_switches INTEGER DEFAULT 0,

                    contradictions JSON,

                    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,

                    last_updated TIMESTAMP DEFAULT CURRENT_TIMESTAMP,

                    metadata JSON,

                    PRIMARY KEY (context_id, numero_usuario)

                );

                """)
                
                c.executescript("""

                CREATE INDEX IF NOT EXISTS idx_lstm_usuario ON lstm_contexto(numero_usuario);

                CREATE INDEX IF NOT EXISTS idx_lstm_created ON lstm_contexto(created_at);

                """)
                
                c.executescript("""

                CREATE TABLE IF NOT EXISTS lstm_message_links (

                    id INTEGER PRIMARY KEY AUTOINCREMENT,

                    context_id VARCHAR(255) NOT NULL,

                    message_id VARCHAR(255) NOT NULL,

                    numero_usuario VARCHAR(50) NOT NULL,

                    speaker_name VARCHAR(255),

                    parent_message_id VARCHAR(255),

                    topic_changed BOOLEAN DEFAULT FALSE,

                    context_switch_type VARCHAR(50),

                    relevance_score FLOAT DEFAULT 0.0,

                    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,

                    UNIQUE(context_id, message_id, numero_usuario),

                    FOREIGN KEY (context_id, numero_usuario) REFERENCES lstm_contexto(context_id, numero_usuario) ON DELETE CASCADE

                );

                """)
                
                c.executescript("""

                CREATE INDEX IF NOT EXISTS idx_lstm_msg_context ON lstm_message_links(context_id);

                CREATE INDEX IF NOT EXISTS idx_lstm_msg_message ON lstm_message_links(message_id);

                """)
                
                # ✅ TABELA DEDUP PERSISTENTE (anti-retry do WhatsApp por 24h)
                c.executescript("""

                CREATE TABLE IF NOT EXISTS dedup_messages (

                    id INTEGER PRIMARY KEY AUTOINCREMENT,

                    content_hash TEXT NOT NULL,

                    message_id TEXT,

                    usuario TEXT,

                    numero TEXT,

                    created_at DATETIME DEFAULT CURRENT_TIMESTAMP

                );

                CREATE INDEX IF NOT EXISTS idx_dedup_hash ON dedup_messages(content_hash);

                CREATE INDEX IF NOT EXISTS idx_dedup_created ON dedup_messages(created_at);

                """)
                
                # ===== MISSING TABLES (required by various modules) =====
                
                c.executescript("""

                CREATE TABLE IF NOT EXISTS continuous_learning (

                    id INTEGER PRIMARY KEY AUTOINCREMENT,

                    ts REAL,

                    usuario TEXT,

                    numero TEXT,

                    nome_usuario TEXT,

                    tipo_conversa TEXT DEFAULT 'pv',

                    mensagem TEXT,

                    resposta_do_bot BOOLEAN DEFAULT 0,

                    resposta_gerada TEXT,

                    is_reply BOOLEAN DEFAULT 0,

                    reply_to_bot BOOLEAN DEFAULT 0,

                    contexto_grupo TEXT,

                    modelo_usado TEXT DEFAULT 'desconhecido',

                    message_id TEXT,

                    qualidade REAL DEFAULT 0.0,

                    tipo_conteudo TEXT,

                    user_id TEXT,

                    topic TEXT,

                    learned_fact TEXT,

                    confidence REAL DEFAULT 0.5,

                    source TEXT DEFAULT 'conversation',

                    created_at DATETIME DEFAULT CURRENT_TIMESTAMP,

                    updated_at DATETIME DEFAULT CURRENT_TIMESTAMP

                );

                CREATE INDEX IF NOT EXISTS idx_cl_user ON continuous_learning(usuario);

                CREATE INDEX IF NOT EXISTS idx_cl_ts ON continuous_learning(ts);

                CREATE INDEX IF NOT EXISTS idx_cl_quality ON continuous_learning(qualidade);

                CREATE INDEX IF NOT EXISTS idx_cl_msgid ON continuous_learning(message_id);

                """)
                
                # Tabela de exemplos para fine-tuning
                c.executescript("""

                CREATE TABLE IF NOT EXISTS finetuning_examples (

                    id INTEGER PRIMARY KEY AUTOINCREMENT,

                    user_id TEXT,

                    input_text TEXT,

                    output_text TEXT,

                    quality_score REAL DEFAULT 0.5,

                    source TEXT DEFAULT 'conversation',

                    is_approved BOOLEAN DEFAULT 0,

                    created_at DATETIME DEFAULT CURRENT_TIMESTAMP

                );

                CREATE INDEX IF NOT EXISTS idx_ft_user ON finetuning_examples(user_id);

                """)
                
                # Tabela de eventos do sistema
                c.executescript("""

                CREATE TABLE IF NOT EXISTS system_events (

                    id INTEGER PRIMARY KEY AUTOINCREMENT,

                    event_type TEXT,

                    event_data TEXT,

                    severity TEXT DEFAULT 'info',

                    created_at DATETIME DEFAULT CURRENT_TIMESTAMP

                );

                CREATE INDEX IF NOT EXISTS idx_se_type ON system_events(event_type);

                CREATE INDEX IF NOT EXISTS idx_se_created ON system_events(created_at);

                """)
                
                # Tabela de conhecimento global (seeds)
                c.executescript("""

                CREATE TABLE IF NOT EXISTS conhecimento_global (

                    id INTEGER PRIMARY KEY AUTOINCREMENT,

                    chave TEXT UNIQUE,

                    valor TEXT,

                    categoria TEXT DEFAULT 'geral',

                    created_at DATETIME DEFAULT CURRENT_TIMESTAMP

                );

                """)
                
                # Tabela de prompts do sistema
                c.executescript("""

                CREATE TABLE IF NOT EXISTS system_prompts (

                    id INTEGER PRIMARY KEY AUTOINCREMENT,

                    prompt_name TEXT UNIQUE,

                    prompt_text TEXT,

                    version INTEGER DEFAULT 1,

                    active BOOLEAN DEFAULT 1,

                    created_at DATETIME DEFAULT CURRENT_TIMESTAMP

                );

                """)
                
                # Tabela de configuração de persona
                c.executescript("""

                CREATE TABLE IF NOT EXISTS persona_config (

                    id INTEGER PRIMARY KEY AUTOINCREMENT,

                    config_key TEXT UNIQUE,

                    config_value TEXT,

                    created_at DATETIME DEFAULT CURRENT_TIMESTAMP

                );

                """)
                
                # Tabela de padrões de moderação
                c.executescript("""

                CREATE TABLE IF NOT EXISTS moderation_patterns (

                    id INTEGER PRIMARY KEY AUTOINCREMENT,

                    pattern TEXT,

                    action TEXT DEFAULT 'warn',

                    severity INTEGER DEFAULT 1,

                    created_at DATETIME DEFAULT CURRENT_TIMESTAMP

                );

                """)
                
                # Tabela de configuração de tom
                c.executescript("""

                CREATE TABLE IF NOT EXISTS tone_config (

                    id INTEGER PRIMARY KEY AUTOINCREMENT,

                    tone_name TEXT UNIQUE,

                    tone_description TEXT,

                    response_template TEXT,

                    created_at DATETIME DEFAULT CURRENT_TIMESTAMP

                );

                """)
                
                # Tabela de templates de resposta
                c.executescript("""

                CREATE TABLE IF NOT EXISTS response_templates (

                    id INTEGER PRIMARY KEY AUTOINCREMENT,

                    template_name TEXT UNIQUE,

                    template_text TEXT,

                    context TEXT DEFAULT 'geral',

                    created_at DATETIME DEFAULT CURRENT_TIMESTAMP

                );

                """)
                
                # Tabela de provedores LLM
                c.executescript("""

                CREATE TABLE IF NOT EXISTS llm_providers (

                    id INTEGER PRIMARY KEY AUTOINCREMENT,

                    provider_name TEXT UNIQUE,

                    api_key_encrypted TEXT,

                    model_name TEXT,

                    priority INTEGER DEFAULT 0,

                    active BOOLEAN DEFAULT 1,

                    created_at DATETIME DEFAULT CURRENT_TIMESTAMP

                );

                """)
                
                c.executescript("""

                CREATE TABLE IF NOT EXISTS stm_messages (

                  id INTEGER PRIMARY KEY AUTOINCREMENT,

                  conversation_id TEXT NOT NULL,

                  numero_usuario TEXT NOT NULL DEFAULT '',

                  grupo_id TEXT DEFAULT '',

                  tipo_conversa TEXT DEFAULT 'pv',

                  role TEXT NOT NULL,

                  content TEXT NOT NULL,

                  author_name TEXT DEFAULT '',

                  recipient TEXT DEFAULT '',

                  quoted_author TEXT DEFAULT '',

                  is_listen BOOLEAN DEFAULT 0,

                  timestamp REAL DEFAULT (strftime('%s','now')),

                  importancia REAL DEFAULT 1.0,

                  emocao TEXT DEFAULT 'neutro',

                  reply_info TEXT DEFAULT '{}',

                  token_count INTEGER DEFAULT 0,

                  message_id TEXT,

                  created_at DATETIME DEFAULT CURRENT_TIMESTAMP

                );

                CREATE INDEX IF NOT EXISTS idx_stm_conv ON stm_messages(conversation_id);

                CREATE INDEX IF NOT EXISTS idx_stm_created ON stm_messages(created_at);

                CREATE INDEX IF NOT EXISTS idx_stm_conv_user ON stm_messages(conversation_id, numero_usuario);

                CREATE INDEX IF NOT EXISTS idx_stm_grupo ON stm_messages(grupo_id);

                CREATE INDEX IF NOT EXISTS idx_stm_listen ON stm_messages(is_listen);

                CREATE INDEX IF NOT EXISTS idx_stm_recipient ON stm_messages(recipient, quoted_author);

                CREATE INDEX IF NOT EXISTS idx_stm_numero ON stm_messages(numero_usuario);

                CREATE UNIQUE INDEX IF NOT EXISTS uq_stm_conv_msg ON stm_messages(conversation_id, message_id) WHERE message_id IS NOT NULL AND message_id <> '';

                CREATE INDEX IF NOT EXISTS idx_stm_msgid ON stm_messages(message_id);

                """)
                
                # Tabela de perfis emocionais de usuário
                c.executescript("""

                CREATE TABLE IF NOT EXISTS user_emotional_profiles (

                    id INTEGER PRIMARY KEY AUTOINCREMENT,

                    numero_usuario TEXT UNIQUE,

                    dominant_emotion TEXT DEFAULT 'neutral',

                    emotion_history TEXT DEFAULT '[]',

                    last_updated DATETIME DEFAULT CURRENT_TIMESTAMP

                );

                """)
                
                conn.commit()
                logger.info(f"Banco de dados inicializado: {self.db_path}")
                
        except Exception as e:
            logger.error(f"Erro ao criar tabelas: {e}")
            raise

    def _ensure_all_columns_and_indexes(self):
        """Garante que todas as colunas e índices existam."""
        try:
            with self._get_connection() as conn:
                c = conn.cursor()
                
                # Adiciona colunas faltantes na tabela mensagens
                columns_to_add = {
                    'mensagens': [
                        ('humor', 'TEXT DEFAULT "neutro"'),
                        ('modo_resposta', 'TEXT DEFAULT "normal"'),
                        ('nivel_transicao', 'INTEGER DEFAULT 1'),
                        ('usuario_privilegiado', 'BOOLEAN DEFAULT 0'),
                        ('modelo_usado', 'TEXT DEFAULT "desconhecido"'),
                        ('conversation_id', 'TEXT DEFAULT ""'),
                        ('nome_usuario', 'TEXT DEFAULT ""'),
                        ('grupo_id', 'TEXT DEFAULT ""'),
                        ('tipo_conversa', 'TEXT DEFAULT "pv"'),
                        ('recipient', 'TEXT DEFAULT ""'),
                        ('quoted_author', 'TEXT DEFAULT ""'),
                        ('is_listen', 'BOOLEAN DEFAULT 0')
                    ],
                    'stm_messages': [
                        ('numero_usuario', 'TEXT DEFAULT ""'),
                        ('grupo_id', 'TEXT DEFAULT ""'),
                        ('tipo_conversa', 'TEXT DEFAULT "pv"'),
                        ('recipient', 'TEXT DEFAULT ""'),
                        ('quoted_author', 'TEXT DEFAULT ""'),
                        ('is_listen', 'BOOLEAN DEFAULT 0'),
                        ('timestamp', 'REAL DEFAULT (strftime(\'%s\',\'now\'))'),
                        ('importancia', 'REAL DEFAULT 1.0'),
                        ('author_name', 'TEXT DEFAULT ""'),
                        ('token_count', 'INTEGER DEFAULT 0'),
                        ('reply_info', 'TEXT DEFAULT "{}"'),
                        ('message_id', 'TEXT')
                    ],
                    'continuous_learning': [
                        ('ts', 'REAL'),
                        ('usuario', 'TEXT'),
                        ('numero', 'TEXT'),
                        ('nome_usuario', 'TEXT'),
                        ('tipo_conversa', 'TEXT DEFAULT "pv"'),
                        ('mensagem', 'TEXT'),
                        ('resposta_do_bot', 'BOOLEAN DEFAULT 0'),
                        ('resposta_gerada', 'TEXT'),
                        ('is_reply', 'BOOLEAN DEFAULT 0'),
                        ('reply_to_bot', 'BOOLEAN DEFAULT 0'),
                        ('contexto_grupo', 'TEXT'),
                        ('modelo_usado', 'TEXT DEFAULT "desconhecido"'),
                        ('message_id', 'TEXT'),
                        ('qualidade', 'REAL DEFAULT 0.0'),
                        ('tipo_conteudo', 'TEXT')
                    ],
                    'tom_usuario': [
                        ('humor', 'TEXT DEFAULT "neutro"')
                    ],
                    'contexto': [
                        ('humor_atual', 'TEXT DEFAULT "neutro"'),
                        ('modo_resposta', 'TEXT DEFAULT "normal"'),
                        ('nivel_transicao', 'INTEGER DEFAULT 1'),
                        ('usuario_privilegiado', 'BOOLEAN DEFAULT 0'),
                        ('updated_at', 'DATETIME DEFAULT CURRENT_TIMESTAMP')
                    ],
                    'usuarios_privilegiados': [
                        ('privilegio_temporario_ativo', 'BOOLEAN DEFAULT 0'),
                        ('expira_em', 'REAL')
                    ],
                    'persona_usuario': [
                        ('nome', 'TEXT DEFAULT ""')
                    ],
                    'user_emotional_profiles': [
                        ('user_id', 'TEXT'),
                        ('profile_data', 'TEXT DEFAULT "{}"'),
                        ('created_at', 'DATETIME DEFAULT CURRENT_TIMESTAMP'),
                        ('updated_at', 'DATETIME DEFAULT CURRENT_TIMESTAMP'),
                        ('dominant_emotion', 'TEXT DEFAULT "neutral"'),
                        ('emotion_history', 'TEXT DEFAULT "[]"'),
                        ('last_updated', 'DATETIME DEFAULT CURRENT_TIMESTAMP')
                    ]
                }
                
                for table, cols in columns_to_add.items():
                    c.execute(f"PRAGMA table_info('{table}')")
                    existing = {row[1] for row in c.fetchall()}
                    for col_name, col_def in cols:
                        if col_name not in existing:
                            try:
                                c.execute(f"ALTER TABLE {table} ADD COLUMN {col_name} {col_def}")
                                logger.info(f"Coluna '{col_name}' adicionada em '{table}'")
                            except Exception as e:
                                logger.warning(f"Erro ao adicionar coluna {col_name}: {e}")
                
                # Migration para consertar FK mismatch em lstm_message_links
                try:
                    # Verifica se a FK de lstm_message_links está correta
                    c.execute("PRAGMA foreign_key_list('lstm_message_links')")
                    fk_list = c.fetchall()
                    # Se não houver a FK composta (composta por 2 colunas), recriamos a tabela
                    if len(fk_list) != 2:
                        logger.warning("Refazendo lstm_message_links para corrigir esquema de foreign key...")
                        c.execute("DROP TABLE IF EXISTS lstm_message_links")
                        # O _init_db() vai recriar na próxima inicialização
                except Exception as e:
                    logger.warning(f"Erro ao verificar migração LSTM links: {e}")

                # Migration para consertar PK de lstm_contexto
                try:
                    c.execute("PRAGMA table_info('lstm_contexto')")
                    info = c.fetchall()
                    # Se houver apenas 1 coluna com pk=1, recriamos a tabela com PK composta
                    pks = [row for row in info if row[5] > 0]
                    if len(pks) == 1 and pks[0][1] == 'context_id':
                        logger.warning("Migration: Detectada PK legada de lstm_contexto. Migrando para PK composta...")
                        # 1. Renomeia tabela antiga
                        c.execute("ALTER TABLE lstm_contexto RENAME TO lstm_contexto_old")
                        # 2. Cria nova tabela
                        c.execute("""

                        CREATE TABLE lstm_contexto (

                            context_id VARCHAR(255) NOT NULL,

                            numero_usuario VARCHAR(50) NOT NULL,

                            topic_principal VARCHAR(255),

                            subtopicas JSON,

                            conversation_path JSON,

                            interaction_pattern VARCHAR(50),

                            emotional_state VARCHAR(50),

                            unanswered_questions JSON,

                            assumed_knowledge JSON,

                            last_key_message TEXT,

                            context_switches INTEGER DEFAULT 0,

                            contradictions JSON,

                            created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,

                            last_updated TIMESTAMP DEFAULT CURRENT_TIMESTAMP,

                            metadata JSON,

                            PRIMARY KEY (context_id, numero_usuario)

                        );

                        """)
                        # 3. Copia dados antigos evitando conflitos
                        c.execute("""

                        INSERT OR IGNORE INTO lstm_contexto 

                        (context_id, numero_usuario, topic_principal, subtopicas, conversation_path,

                         interaction_pattern, emotional_state, unanswered_questions, assumed_knowledge,

                         last_key_message, context_switches, contradictions, created_at, last_updated, metadata)

                        SELECT 

                         context_id, numero_usuario, topic_principal, subtopicas, conversation_path,

                         interaction_pattern, emotional_state, unanswered_questions, assumed_knowledge,

                         last_key_message, context_switches, contradictions, created_at, last_updated, metadata

                        FROM lstm_contexto_old

                        """)
                        # 4. Remove tabela temporária
                        c.execute("DROP TABLE lstm_contexto_old")
                        logger.success("Migration: Tabela lstm_contexto migrada com sucesso para PK composta!")
                except Exception as e:
                    logger.error(f"Erro ao migrar PK de lstm_contexto: {e}")

                try:
                    c.executescript("""

                    CREATE INDEX IF NOT EXISTS idx_stm_conv_user ON stm_messages(conversation_id, numero_usuario);

                    CREATE INDEX IF NOT EXISTS idx_stm_grupo ON stm_messages(grupo_id);

                    CREATE INDEX IF NOT EXISTS idx_stm_listen ON stm_messages(is_listen);

                    CREATE INDEX IF NOT EXISTS idx_stm_recipient ON stm_messages(recipient, quoted_author);

                    CREATE INDEX IF NOT EXISTS idx_stm_numero ON stm_messages(numero_usuario);

                    CREATE INDEX IF NOT EXISTS idx_stm_created ON stm_messages(created_at);

                    CREATE UNIQUE INDEX IF NOT EXISTS uq_stm_conv_msg ON stm_messages(conversation_id, message_id) WHERE message_id IS NOT NULL AND message_id <> '';

                    CREATE INDEX IF NOT EXISTS idx_stm_msgid ON stm_messages(message_id);

                    CREATE INDEX IF NOT EXISTS idx_cl_user ON continuous_learning(usuario);

                    CREATE INDEX IF NOT EXISTS idx_cl_msgid ON continuous_learning(message_id);

                    CREATE UNIQUE INDEX IF NOT EXISTS uq_emoprof_user ON user_emotional_profiles(user_id);

                    """)
                except Exception:
                    pass
                try:
                    cur2 = conn.cursor()
                    cur2.execute("CREATE UNIQUE INDEX IF NOT EXISTS uq_stm_conv_msg ON stm_messages(conversation_id, message_id) WHERE message_id IS NOT NULL AND message_id <> ''")
                    conn.commit()
                except Exception:
                    pass

                conn.commit()
                
        except Exception as e:
            logger.error(f"Erro na migração: {e}")

    # ================================================================
    # USUÁRIOS PRIVILEGIADOS
    # ================================================================
    def adicionar_usuario_privilegiado(

        self, 

        numero: str, 

        nome: str, 

        apelido: str, 

        modo_fala: str = "tecnico_formal"

    ) -> Tuple[bool, str]:
        """

        Adiciona um usuário privilegiado ao sistema.

        

        Args:

            numero: Número de telefone do usuário

            nome: Nome completo

            apelido: Apelido

            modo_fala: Modo de fala inicial

            

        Returns:

            Tuple[bool, str]: (sucesso, código de verificação)

        """
        try:
            # Gera código de verificação
            codigo = str(random.randint(100000, 999999))
            
            self._execute_with_retry(
                """INSERT OR REPLACE INTO usuarios_privilegiados 

                (numero, nome, apelido, modo_fala, codigo_verificacao) 

                VALUES (?, ?, ?, ?, ?)""",
                (numero, nome, apelido, modo_fala, codigo),
                commit=True
            )
            
            logger.info(f"Usuário privilegiado adicionado: {numero} ({nome})")
            return True, codigo
            
        except Exception as e:
            logger.error(f"Erro ao adicionar usuário privilegiado: {e}")
            return False, str(e)

    def eh_privilegiado(self, numero: str) -> bool:
        """

        Verifica se um número é de usuário privilegiado.

        

        Args:

            numero: Número de telefone a verificar

            

        Returns:

            bool: True se for privilegiado

        """
        try:
            rows = self._execute_with_retry(
                "SELECT ativo FROM usuarios_privilegiados WHERE numero = ? AND ativo = 1",
                (numero,)
            )
            # Verificação segura para evitar "List[Row] | None cannot be assigned to len()"
            return rows is not None and len(rows) > 0
        except Exception as e:
            logger.error(f"Erro ao verificar privilégios: {e}")
            return False

    def verificar_privilegios_usuario(self, numero: str) -> Dict[str, Any]:
        """

        Verifica privilégios detalhados do usuário no database com suporte a temporários.

        

        Args:

            numero: Número do usuário

            

        Returns:

            Dict: Dicionário com flags de privilégio

        """
        try:
            rows = self._execute_with_retry(
                "SELECT ativo, privilegio_temporario_ativo, expira_em FROM usuarios_privilegiados WHERE numero = ?",
                (numero,)
            )
            if rows:
                row = rows[0]
                return {
                    "privilegiado": bool(row[0]),
                    "privilegio_temporario_ativo": bool(row[1]),
                    "expira_em": row[2]
                }
            return {
                "privilegiado": False,
                "privilegio_temporario_ativo": False,
                "expira_em": None
            }
        except Exception as e:
            logger.error(f"Erro em verificar_privilegios_usuario: {e}")
            return {"privilegiado": False, "privilegio_temporario_ativo": False}

    def verificar_codigo(self, numero: str, codigo: str) -> bool:
        """

        Verifica o código de um usuário privilegiado.

        

        Args:

            numero: Número de telefone

            codigo: Código de verificação

            

        Returns:

            bool: True se o código for válido

        """
        try:
            rows = self._execute_with_retry(
                "SELECT codigo_verificacao FROM usuarios_privilegiados WHERE numero = ?",
                (numero,)
            )
            if rows and rows[0][0] == codigo:
                # Gera novo código para próxima verificação
                novo_codigo = str(random.randint(100000, 999999))
                self._execute_with_retry(
                    "UPDATE usuarios_privilegiados SET codigo_verificacao = ? WHERE numero = ?",
                    (novo_codigo, numero),
                    commit=True
                )
                return True
            return False
        except Exception as e:
            logger.error(f"Erro ao verificar código: {e}")
            return False

    def obter_modo_fala_privilegiado(self, numero: str) -> Optional[str]:
        """Obtém o modo de fala de um usuário privilegiado."""
        try:
            rows = self._execute_with_retry(
                "SELECT modo_fala FROM usuarios_privilegiados WHERE numero = ?",
                (numero,)
            )
            return rows[0][0] if rows else None
        except Exception as e:
            logger.error(f"Erro ao obter modo de fala: {e}")
            return None

    # ================================================================
    # MENSAGENS
    # ================================================================
    def salvar_mensagem(

        self,

        usuario: str,

        mensagem: str,

        resposta: str,

        numero: Optional[str] = None,

        is_reply: bool = False,

        mensagem_original: Optional[str] = None,

        humor: str = "neutro",

        modo_resposta: str = "normal",

        nivel_transicao: int = 1,

        usuario_privilegiado: bool = False,

        modelo_usado: str = "desconhecido",

        **kwargs

    ) -> bool:
        """

        Salva uma mensagem no banco de dados.

        """
        try:
            cols = ['usuario', 'mensagem', 'resposta', 'humor', 'modo_resposta', 
                    'nivel_transicao', 'usuario_privilegiado', 'is_reply', 'modelo_usado']
            vals: List[Any] = [usuario, mensagem, resposta, humor, modo_resposta,
                    nivel_transicao, usuario_privilegiado, is_reply, modelo_usado]
            
            # ✅ Novo: message_id via kwargs ou parâmetro opcional (compatibilidade)
            message_id = kwargs.get('message_id')
            if message_id:
                cols.append('message_id')
                vals.append(message_id)
            
            if numero:
                cols.append('numero')
                vals.append(numero)
            
            if mensagem_original:
                cols.append('mensagem_original')
                vals.append(mensagem_original)

            # ✅ Suporte para nome_usuario (evita erro SQL se a coluna existir)
            nome_usuario = kwargs.get('nome_usuario') or usuario
            if nome_usuario:
                cols.append('nome_usuario')
                vals.append(nome_usuario)
            
            placeholders = ', '.join(['?' for _ in cols])
            
            # ✅ FIX #3-CAMADA: INSERT OR REPLACE ao invés de INSERT OR IGNORE
            # Motivo: INSERT OR IGNORE falha SILENCIOSAMENTE em duplicatas
            # Resultado: A tentativa é registrada sem erro, causando corridas de dedup
            # Solução: INSERT OR REPLACE + logging explícito
            query = f"INSERT OR REPLACE INTO mensagens ({', '.join(cols)}) VALUES ({placeholders})"
            
            try:
                self._execute_with_retry(query, tuple(vals), commit=True)
                
                # ✅ Log de sucesso com message_id para rastreabilidade
                if message_id:
                    logger.info(f"✅ [DB INSERT OK] message_id={message_id} | usuario={usuario} | modelo={modelo_usado}")
                return True
            except Exception as db_err:
                # ❌ Log de falha com contexto completo
                logger.error(f"❌ [DB INSERT FAIL] Erro ao salvar mensagem: {db_err} | message_id={message_id} | usuario={usuario}")
                return False
        except Exception as e:
            logger.warning(f"Erro salvar_mensagem (outer): {e}")
            return False

    def recuperar_mensagens(

        self, 

        usuario: str, 

        limite: int = 5

    ) -> List[Tuple[str, str]]:
        """Recupera mensagens de um usuário."""
        try:
            result = self._execute_with_retry(
                """SELECT mensagem, resposta FROM mensagens 

                WHERE usuario=? OR numero=? 

                ORDER BY id DESC LIMIT ?""",
                (usuario, usuario, limite)
            )
            if not result:
                return []
            # Converte sqlite3.Row para tuplas
            return [(row[0], row[1]) for row in result]
        except Exception as e:
            logger.error(f"Erro ao recuperar mensagens: {e}")
            return []

    def recuperar_mensagens_por_contexto(self, context_id: str, limite: int = 50) -> List[Dict[str, Any]]:
        """Recupera mensagens de um contexto específico (grupo ou PV)."""
        try:
            rows = self._execute_with_retry(
                "SELECT usuario, mensagem, resposta, created_at FROM mensagens WHERE conversation_id = ? ORDER BY id DESC LIMIT ?",
                (context_id, limite)
            )
            if not rows:
                return []
            return [dict(row) for row in rows]
        except Exception as e:
            logger.error(f"Erro ao recuperar mensagens por contexto: {e}")
            return []

    def recuperar_humor(self, numero_usuario: str) -> str:
        """

        Recupera o humor atual de um usuário.

        

        Args:

            numero_usuario: Número do usuário

            

        Returns:

            str: Humor detectado ('neutro', 'feliz', 'triste', 'irritado', 'entediado')

        """
        try:
            rows = self._execute_with_retry(
                """SELECT humor FROM tom_usuario 

                WHERE numero_usuario=? 

                ORDER BY created_at DESC LIMIT 1""",
                (numero_usuario,)
            )
            return rows[0][0] if rows else "neutro"
        except Exception as e:
            logger.error(f"Erro ao recuperar humor: {e}")
            return "neutro"

    # ================================================================
    # CONTEXTO
    # ================================================================
    def salvar_contexto(

        self,

        user_key: str,

        historico: Optional[str] = None,

        emocao_atual: str = "neutra",

        humor_atual: str = "neutro",

        modo_resposta: str = "normal",

        nivel_transicao: int = 1,

        usuario_privilegiado: bool = False,

        termos: Optional[str] = None,

        girias: Optional[str] = None,

        tom: Optional[str] = None

    ) -> bool:
        """

        Salva o contexto de um usuário.

        

        Args:

            user_key: Chave do usuário (número ou nome)

            historico: Histórico de conversas

            emocao_atual: Emoção atual

            humor_atual: Humor atual

            modo_resposta: Modo de resposta

            nivel_transicao: Nível de transição

            usuario_privilegiado: Se é usuário privilegiado

            termos: Termos aprendidos

            girias: Gírias aprendidas

            tom: Tom de fala

            

        Returns:

            bool: Sucesso da operação

        """
        try:
            self._execute_with_retry(
                """INSERT OR REPLACE INTO contexto 

                (user_key, historico, emocao_atual, humor_atual, modo_resposta, 

                nivel_transicao, usuario_privilegiado, termos, girias, tom, updated_at)

                VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, CURRENT_TIMESTAMP)""",
                (user_key, historico or "[]", emocao_atual, humor_atual, modo_resposta,
                 nivel_transicao, 1 if usuario_privilegiado else 0, 
                 termos or "{}", girias or "{}", tom),
                commit=True
            )
            return True
        except Exception as e:
            logger.error(f"Erro ao salvar contexto: {e}")
            return False

    def recuperar_contexto(self, user_key: str) -> Optional[Dict[str, Any]]:
        """Recupera o contexto de um usuário."""
        try:
            rows = self._execute_with_retry(
                "SELECT * FROM contexto WHERE user_key = ?",
                (user_key,)
            )
            if rows:
                row = rows[0]
                return dict(row)
            return None
        except Exception as e:
            logger.error(f"Erro ao recuperar contexto: {e}")
            return None

    # ================================================================
    # TOM E HUMOR
    # ================================================================
    def registrar_tom_usuario(

        self,

        numero_usuario: str,

        tom_detectado: str,

        intensidade: float = 0.5,

        contexto: Optional[str] = None,

        humor: str = "neutro"

    ) -> bool:
        """

        Registra o tom detectado de um usuário.

        

        Args:

            numero_usuario: Número do usuário

            tom_detectado: Tom detectado

            intensidade: Intensidade do tom

            contexto: Contexto da detecção

            humor: Humor detectado

            

        Returns:

            bool: Sucesso da operação

        """
        try:
            self._execute_with_retry(
                """INSERT INTO tom_usuario 

                (numero_usuario, tom_detectado, intensidade, contexto, humor) 

                VALUES (?, ?, ?, ?, ?)""",
                (numero_usuario, tom_detectado, intensidade, contexto, humor),
                commit=True
            )
            return True
        except Exception as e:
            logger.error(f"Erro ao registrar tom: {e}")
            return False

    def obter_tom_predominante(self, numero_usuario: str) -> Optional[str]:
        """Obtém o tom predominante de um usuário."""
        try:
            rows = self._execute_with_retry(
                """SELECT tom_detectado FROM tom_usuario 

                WHERE numero_usuario=? 

                ORDER BY created_at DESC LIMIT 1""",
                (numero_usuario,)
            )
            return rows[0][0] if rows else None
        except Exception as e:
            logger.error(f"Erro ao obter tom predominante: {e}")
            return None

    # ================================================================
    # APRENDIZADOS E GÍRIAS
    # ================================================================
    def salvar_aprendizado_detalhado(

        self, 

        numero_usuario: str, 

        chave: str, 

        valor: str

    ) -> bool:
        """Salva um aprendizado detalhado, atualizando se já existir."""
        try:
            existing = self._execute_with_retry(
                "SELECT id FROM aprendizados WHERE numero_usuario=? AND chave=?",
                (numero_usuario, chave)
            )
            
            if existing:
                self._execute_with_retry(
                    "UPDATE aprendizados SET valor=?, created_at=CURRENT_TIMESTAMP WHERE id=?",
                    (valor, existing[0][0]),
                    commit=True
                )
            else:
                self._execute_with_retry(
                    "INSERT INTO aprendizados (numero_usuario, chave, valor) VALUES (?, ?, ?)",
                    (numero_usuario, chave, valor),
                    commit=True
                )
            return True
        except Exception as e:
            logger.error(f"Erro ao salvar aprendizado: {e}")
            return False

    def recuperar_aprendizado_detalhado(

        self, 

        numero_usuario: str, 

        chave: Optional[str] = None

    ) -> Union[Dict, str, None]:
        """Recupera aprendizados detalhados."""
        try:
            if chave:
                rows = self._execute_with_retry(
                    "SELECT valor FROM aprendizados WHERE numero_usuario=? AND chave=?",
                    (numero_usuario, chave)
                )
                return rows[0][0] if rows else None
            else:
                rows = self._execute_with_retry(
                    "SELECT chave, valor FROM aprendizados WHERE numero_usuario=?",
                    (numero_usuario,)
                )
                return {r[0]: r[1] for r in rows} if rows else {}
        except Exception as e:
            logger.error(f"Erro ao recuperar aprendizado: {e}")
            return None

    def salvar_giria_aprendida(

        self,

        numero_usuario: str,

        giria: str,

        significado: str,

        contexto: Optional[str] = None

    ) -> bool:
        """Salva uma gíria aprendida."""
        try:
            existing = self._execute_with_retry(
                "SELECT id, frequencia FROM girias_aprendidas WHERE numero_usuario=? AND giria=?",
                (numero_usuario, giria)
            )
            
            if existing:
                self._execute_with_retry(
                    """UPDATE girias_aprendidas SET frequencia=frequencia+1, 

                    updated_at=CURRENT_TIMESTAMP WHERE id=?""",
                    (existing[0][0],),
                    commit=True
                )
            else:
                self._execute_with_retry(
                    """INSERT INTO girias_aprendidas 

                    (numero_usuario, giria, significado, contexto) VALUES (?, ?, ?, ?)""",
                    (numero_usuario, giria, significado, contexto),
                    commit=True
                )
            return True
            
        except Exception as e:
            logger.error(f"Erro ao salvar gíria: {e}")
            return False

    def recuperar_girias_usuario(self, numero_usuario: str) -> List[Dict[str, Any]]:
        """Recupera gírias de um usuário."""
        try:
            rows = self._execute_with_retry(
                "SELECT giria, significado, frequencia FROM girias_aprendidas WHERE numero_usuario=?",
                (numero_usuario,)
            )
            return [{"giria": r[0], "significado": r[1], "frequencia": r[2]} for r in rows] if rows else []
        except Exception as e:
            logger.error(f"Erro ao recuperar gírias: {e}")
            return []

    # ================================================================
    # EMBEDDINGS
    # ================================================================
    def salvar_embedding(

        self,

        numero_usuario: str,

        source_type: str,

        texto: str,

        embedding: Any

    ) -> bool:
        """Salva um embedding no banco."""
        try:
            if hasattr(embedding, "tobytes"):
                embedding = embedding.tobytes()
            
            self._execute_with_retry(
                """INSERT INTO embeddings 

                (numero_usuario, source_type, texto, embedding) VALUES (?, ?, ?, ?)""",
                (numero_usuario, source_type, texto, embedding),
                commit=True
            )
            return True
        except Exception as e:
            logger.error(f"Erro ao salvar embedding: {e}")
            return False

    def recuperar_embeddings(self, numero_usuario: str) -> List[Dict[str, Any]]:
        """Recupera embeddings de um usuário."""
        try:
            rows = self._execute_with_retry(
                "SELECT source_type, texto, embedding FROM embeddings WHERE numero_usuario=?",
                (numero_usuario,)
            )
            result = []
            # Verificação segura para evitar "Object of type None cannot be used as iterable"
            if rows:
                for r in rows:
                    embedding_data = r[2]
                    if isinstance(embedding_data, bytes):
                        # Mantém como bytes para uso com numpy
                        pass
                    result.append({
                        "source_type": r[0],
                        "texto": r[1],
                        "embedding": embedding_data
                    })
            return result
        except Exception as e:
            logger.error(f"Erro ao recuperar embeddings: {e}")
            return []

    # ================================================================
    # PERSONA DO USUÁRIO (LTM)
    # ================================================================
    def atualizar_persona(self, numero_usuario: str, campos: Dict[str, str]) -> bool:
        """

        Atualiza campos específicos da persona do usuário.

        

        Args:

            numero_usuario: Número do usuário

            campos: Dicionário com chaves ('personalidade', 'vicios_linguagem', 'gostos', 'desgostos', 'emocional')

        """
        try:
            # Verifica se já existe
            existente = self.recuperar_persona(numero_usuario)
            
            if existente:
                # Update
                set_clauses = []
                values = []
                for k, v in campos.items():
                    if k in ['personalidade', 'vicios_linguagem', 'gostos', 'desgostos', 'emocional']:
                        set_clauses.append(f"{k} = ?")
                        values.append(v)
                
                if not set_clauses:
                    return False
                    
                set_clauses.append("updated_at = CURRENT_TIMESTAMP")
                values.append(numero_usuario)
                
                query = f"UPDATE persona_usuario SET {', '.join(set_clauses)} WHERE numero_usuario = ?"
                self._execute_with_retry(query, tuple(values), commit=True)
            else:
                # Insert
                keys = ['numero_usuario']
                values = [numero_usuario]
                for k, v in campos.items():
                    if k in ['personalidade', 'vicios_linguagem', 'gostos', 'desgostos', 'emocional']:
                        keys.append(k)
                        values.append(v)
                
                placeholders = ', '.join(['?' for _ in keys])
                query = f"INSERT INTO persona_usuario ({', '.join(keys)}) VALUES ({placeholders})"
                self._execute_with_retry(query, tuple(values), commit=True)
                
            return True
        except Exception as e:
            logger.error(f"Erro ao atualizar persona para {numero_usuario}: {e}")
            return False

    def recuperar_persona(self, numero_usuario: str) -> Optional[Dict[str, Any]]:
        """Recupera a persona completa de um usuário."""
        try:
            rows = self._execute_with_retry(
                "SELECT * FROM persona_usuario WHERE numero_usuario = ?",
                (numero_usuario,)
            )
            if rows:
                row = rows[0]
                return dict(row)
            return None
        except Exception as e:
            logger.error(f"Erro ao recuperar persona para {numero_usuario}: {e}")
            return None

    # ================================================================
    # CONTEXTOS ISOLADOS
    # ================================================================

    def get_connection_context(self):
        """

        Retorna uma conexão em contexto manager para operações que precisam de cursor.

        Uso:

            with db.get_connection_context() as conn:

                cur = conn.cursor()

                cur.execute("INSERT INTO ... VALUES ...")

                conn.commit()

        """
        class ConnectionContextManager:
            def __init__(self, db_instance):
                self.db = db_instance
                self.conn = None
            
            def __enter__(self):
                self.conn = self.db._get_connection()
                return self.conn
            
            def __exit__(self, exc_type, exc_val, exc_tb):
                if self.conn:
                    try:
                        if exc_type:
                            self.conn.rollback()
                        else:
                            self.conn.commit()
                    finally:
                        self.conn.close()
        
        return ConnectionContextManager(self)

    def _init_context_isolation_tables(self):
        """Cria tabelas de contexto isolado se não existirem."""
        try:
            with self._get_connection() as conn:
                c = conn.cursor()
                c.executescript("""

                    CREATE TABLE IF NOT EXISTS contextos_isolados (

                        context_id TEXT PRIMARY KEY,

                        numero_usuario TEXT NOT NULL,

                        grupo_id TEXT,

                        tipo_conversa TEXT DEFAULT 'pv',

                        estado_emocional TEXT DEFAULT 'neutral',

                        nivel_intimidade INTEGER DEFAULT 1,

                        short_memory TEXT DEFAULT '[]',

                        metadata TEXT DEFAULT '{}',

                        created_at REAL DEFAULT (strftime('%s', 'now')),

                        last_interaction REAL DEFAULT (strftime('%s', 'now'))

                    );

                    CREATE INDEX IF NOT EXISTS idx_contextos_user ON contextos_isolados(numero_usuario);

                    CREATE INDEX IF NOT EXISTS idx_contextos_tipo ON contextos_isolados(tipo_conversa);

                """)
                conn.commit()
                logger.info("Tabela contextos_isolados garantida")
        except Exception as e:
            logger.warning(f"Erro ao criar tabela contextos_isolados: {e}")

    def salvar_contexto_isolado(self, context_data: Dict[str, Any]) -> bool:
        """Salva ou atualiza um contexto isolado (upsert)."""
        try:
            # Garante que a tabela existe
            self._init_context_isolation_tables()

            with self._get_connection() as conn:
                c = conn.cursor()
                c.execute("""

                    INSERT INTO contextos_isolados

                    (context_id, numero_usuario, grupo_id, tipo_conversa, estado_emocional,

                     nivel_intimidade, short_memory, metadata, created_at, last_interaction)

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

                    ON CONFLICT(context_id) DO UPDATE SET

                        estado_emocional = excluded.estado_emocional,

                        nivel_intimidade = excluded.nivel_intimidade,

                        short_memory = excluded.short_memory,

                        metadata = excluded.metadata,

                        last_interaction = excluded.last_interaction

                """, (
                    context_data.get('context_id'),
                    context_data.get('numero_usuario'),
                    context_data.get('grupo_id'),
                    context_data.get('tipo_conversa', 'pv'),
                    context_data.get('estado_emocional', 'neutral'),
                    context_data.get('nivel_intimidade', 1),
                    json.dumps(context_data.get('short_memory', [])),
                    json.dumps(context_data.get('metadata', {})),
                    context_data.get('created_at', time.time()),
                    context_data.get('last_interaction', time.time()),
                ))
                conn.commit()
                return True
        except Exception as e:
            logger.warning(f"Erro ao salvar contexto isolado: {e}")
            return False

    def recuperar_contexto_isolado(self, context_id: str) -> Optional[Dict[str, Any]]:
        """Recupera um contexto isolado pelo context_id."""
        try:
            # Garante que a tabela existe (Prevenir "no such table")
            self._init_context_isolation_tables()

            rows = self._execute_with_retry(
                "SELECT * FROM contextos_isolados WHERE context_id = ?",
                (context_id,)
            )
            if rows:
                row = dict(rows[0])
                # Desserializar campos JSON
                try: row['short_memory'] = json.loads(row.get('short_memory', '[]'))
                except: row['short_memory'] = []
                try: row['metadata'] = json.loads(row.get('metadata', '{}'))
                except: row['metadata'] = {}
                return row
            return None
        except Exception as e:
            logger.warning(f"Erro ao recuperar contexto isolado: {e}")
            return None

    def deletar_contexto_isolado(self, context_id: str) -> bool:
        """Remove um contexto isolado (safe - verifica tabela primeiro)."""
        try:
            # Garante que a tabela existe
            self._init_context_isolation_tables()

            # Verifica se tabela existe
            conn = self._get_connection()
            c = conn.cursor()
            c.execute("SELECT name FROM sqlite_master WHERE type='table' AND name='contextos_isolados'")
            if not c.fetchone():
                logger.warning("Tabela contextos_isolados não existe. Pulando delete.")
                return True
            conn.close()
            
            self._execute_with_retry(
                "DELETE FROM contextos_isolados WHERE context_id = ?",
                (context_id,), commit=True
            )
            return True
        except Exception as e:
            logger.warning(f"Erro ao deletar contexto isolado: {e}")
            return False

    def listar_contextos_usuario(self, numero_usuario: str) -> List[Dict[str, Any]]:
        """Lista todos os contextos de um usuário."""
        results = []
        try:
            rows = self._execute_with_retry(
                "SELECT * FROM contextos_isolados WHERE numero_usuario = ?",
                (numero_usuario,)
            )
            if rows:
                for row in rows:
                    d = dict(row)
                    try: d['short_memory'] = json.loads(d.get('short_memory', '[]'))
                    except: d['short_memory'] = []
                    try: d['metadata'] = json.loads(d.get('metadata', '{}'))
                    except: d['metadata'] = {}
                    results.append(d)
        except Exception as e:
            logger.warning(f"Erro ao listar contextos do usuário: {e}")
        return results

    # ================================================================
    # HISTÓRICO POR CONVERSATION ID
    # ================================================================

    def recuperar_historico(self, usuario: str = "", numero: str = "",

                            conversation_id: str = "", limite: int = 20) -> List[Dict[str, Any]]:
        """

        Recupera histórico de mensagens.

        Suporta conversation_id para isolamento de contexto.

        """
        # Tenta nova coluna conversation_id primeiro
        try:
            if conversation_id:
                try:
                    rows = self._execute_with_retry(
                        "SELECT usuario, mensagem, resposta, humor, modelo_usado, created_at FROM mensagens "
                        "WHERE conversation_id = ? ORDER BY id DESC LIMIT ?",
                        (conversation_id, limite)
                    )
                except Exception:
                    # Fallback para banco antigo sem conversation_id
                    rows = []
            elif numero:
                rows = self._execute_with_retry(
                    "SELECT usuario, mensagem, resposta, humor, modelo_usado, created_at FROM mensagens "
                    "WHERE numero = ? ORDER BY id DESC LIMIT ?",
                    (numero, limite)
                )
            elif usuario:
                rows = self._execute_with_retry(
                    "SELECT usuario, mensagem, resposta, humor, modelo_usado, created_at FROM mensagens "
                    "WHERE usuario = ? ORDER BY id DESC LIMIT ?",
                    (usuario, limite)
                )
            else:
                return []

            return [dict(r) for r in (rows or [])][::-1]  # Reverte para ordem cronológica
        except Exception:
            return []

    def recuperar_resposta_por_id(self, message_id: str) -> Optional[Dict[str, Any]]:
        """Recupera uma resposta já gerada para um message_id (idempotência)."""
        if not message_id: return None
        try:
            # Garante que a coluna existe
            with self._get_connection() as conn:
                c = conn.cursor()
                c.execute("PRAGMA table_info(mensagens)")
                if 'message_id' not in [row[1] for row in c.fetchall()]:
                    return None

            rows = self._execute_with_retry(
                "SELECT resposta, modelo_usado, created_at, mensagem FROM mensagens WHERE message_id = ? LIMIT 1",
                (message_id,)
            )
            if rows:
                return dict(rows[0])
            return None
        except Exception as e:
            logger.warning(f"Erro ao recuperar resposta por id: {e}")
            return None

    # ✅ DEDUP PERSISTENTE (anti-retry do WhatsApp por 24h)
    def is_duplicate_content_hash(self, content_hash: str) -> bool:
        """Verifica se já processamos esta mensagem nas últimas 24h."""
        if not content_hash:
            return False
        try:
            rows = self._execute_with_retry(
                "SELECT id FROM dedup_messages WHERE content_hash = ? AND created_at > datetime('now', '-24 hours') LIMIT 1",
                (content_hash,)
            )
            return bool(rows)
        except Exception:
            return False

    def save_content_hash(self, content_hash: str, message_id: str = "", usuario: str = "", numero: str = "") -> bool:
        """Salva hash do conteúdo para dedup futura."""
        if not content_hash:
            return False
        try:
            self._execute_with_retry(
                "INSERT INTO dedup_messages (content_hash, message_id, usuario, numero) VALUES (?, ?, ?, ?)",
                (content_hash, message_id, usuario, numero),
                commit=True
            )
            return True
        except Exception as e:
            logger.warning(f"Erro ao salvar content hash: {e}")
            return False

    def cleanup_old_dedup(self, hours: int = 24) -> int:
        """Remove registros de dedup mais antigos que N horas."""
        try:
            with self._get_connection() as conn:
                c = conn.cursor()
                c.execute("DELETE FROM dedup_messages WHERE created_at < datetime('now', ?)", (f'-{hours} hours',))
                deleted = c.rowcount
                conn.commit()
                if deleted > 0:
                    logger.info(f"🧹 [DEDUP] {deleted} registros antigos removidos")
                return deleted
        except Exception as e:
            logger.warning(f"Erro ao limpar dedup: {e}")
            return 0

    def registrar_mensagem_conversation_id(self, usuario: str, mensagem: str, resposta: str,

                                            conversation_id: str = "", numero: str = "",

                                            is_reply: bool = False, mensagem_original: str = "",

                                            humor: str = "neutro", modo_resposta: str = "normal",

                                            modelo_usado: str = "desconhecido", **kwargs) -> bool:
        """Registra mensagem com conversation_id para isolamento."""
        try:
            # Verifica se a coluna conversation_id existe
            with self._get_connection() as conn:
                c = conn.cursor()
                c.execute("PRAGMA table_info(mensagens)")
                cols = [row[1] for row in c.fetchall()]
                if 'conversation_id' not in cols:
                    c.execute("ALTER TABLE mensagens ADD COLUMN conversation_id TEXT")
                    conn.commit()

            self._execute_with_retry(
                """INSERT INTO mensagens

                (usuario, mensagem, resposta, numero, is_reply, mensagem_original, humor, modo_resposta, modelo_usado, conversation_id, message_id)

                VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)""",
                (usuario, mensagem, resposta, numero, is_reply, mensagem_original, humor, modo_resposta, modelo_usado, conversation_id, kwargs.get('message_id')),
                commit=True
            )
            return True
        except Exception as e:
            logger.warning(f"Erro ao registrar mensagem com conversation_id: {e}")
            return False

    def limpar_contexto_usuario(self, usuario: str = "", numero: str = "") -> bool:
        """Limpa todas as mensagens de um usuário (reset)."""
        try:
            if numero:
                self._execute_with_retry("DELETE FROM mensagens WHERE numero = ?", (numero,), commit=True)
            elif usuario:
                self._execute_with_retry("DELETE FROM mensagens WHERE usuario = ?", (usuario,), commit=True)
            # Limpa também contextos isolados
            self._execute_with_retry("DELETE FROM contextos_isolados WHERE numero_usuario = ?", (numero or usuario,), commit=True)
            return True
        except Exception as e:
            logger.warning(f"Erro ao limpar contexto: {e}")
            return False

    # ================================================================
    # PERSONA DO USUÁRIO (LTM)
    # ================================================================
    def recuperar_persona(self, numero_usuario: str) -> Dict[str, Any]:
        """Recupera a persona (personalidade, gostos, etc.) do usuário."""
        try:
            rows = self._execute_with_retry(
                "SELECT personalidade, vicios_linguagem, gostos, desgostos, emocional FROM persona_usuario WHERE numero_usuario = ?",
                (numero_usuario,)
            )
            if rows:
                return dict(rows[0])
            return {}
        except Exception as e:
            logger.warning(f"Erro ao recuperar persona de {numero_usuario}: {e}")
            return {}

    def atualizar_persona(self, numero_usuario: str, campos: Dict[str, str]) -> bool:
        """Atualiza ou insere novos traços de persona para o usuário."""
        if not campos:
            return False
        try:
            # Garante que as colunas existem (migração rápida se necessário)
            with self._get_connection() as conn:
                c = conn.cursor()
                c.execute("INSERT OR IGNORE INTO persona_usuario (numero_usuario) VALUES (?)", (numero_usuario,))
                
                query_parts = []
                params = []
                for campo, valor in campos.items():
                    query_parts.append(f"{campo} = ?")
                    params.append(valor)
                
                query_parts.append("updated_at = CURRENT_TIMESTAMP")
                params.append(numero_usuario)
                
                query = f"UPDATE persona_usuario SET {', '.join(query_parts)} WHERE numero_usuario = ?"
                c.execute(query, tuple(params))
                conn.commit()
            return True
        except Exception as e:
            logger.warning(f"Erro ao atualizar persona de {numero_usuario}: {e}")
            return False

    def fazer_checkpoint_hf_sync(self) -> bool:
        """

        Executa um backup seguro da base de dados otimizado para o Hugging Face Buckets (hf sync).

        Garante integridade total forçando a gravação do WAL e criando um snapshot isolado.

        

        Isso previne qualquer corrupção de dados por conflito entre a nuvem e as 

        threads concorrentes da Akira.

        """
        import shutil
        from pathlib import Path
        try:
            # 1. Força a gravação de todos os dados do WAL para o arquivo DB principal (Truncate)
            self._execute_with_retry("PRAGMA wal_checkpoint(TRUNCATE)")
            
            # 2. Configura caminhos
            db_path_obj = Path(self.db_path)
            # Diretório alvo para o hf sync
            cloud_sync_dir = db_path_obj.parent / "cloud_sync"
            cloud_sync_dir.mkdir(parents=True, exist_ok=True)
            
            backup_path = cloud_sync_dir / db_path_obj.name
            
            # 3. Usa a API de Backup Nativa e atômica do próprio sqlite
            with self._get_connection() as source:
                backup_conn = sqlite3.connect(
                    str(backup_path),
                    timeout=30.0,
                    check_same_thread=False
                )
                with backup_conn:
                    source.backup(backup_conn, pages=-1)
                backup_conn.close()
                
            logger.info(f"✅ Checkpoint Seguro para HF Buckets concluído em: {backup_path}")
            return True
        except Exception as e:
            logger.error(f"❌ Erro ao criar Checkpoint HF Sync: {e}")
            return False
    def stm_save_message(self, conversation_id: str, role: str, content: str, timestamp: float = 0, importancia: float = 1.0, emocao: str = 'neutro', reply_info: dict = None, author_name: str = '', token_count: int = 0, numero_usuario: str = '', grupo_id: str = '', tipo_conversa: str = 'pv', recipient: str = '', quoted_author: str = '', is_listen: bool = False, message_id: str = None) -> bool:
        try:
            if message_id:
                self._execute_with_retry("INSERT OR IGNORE INTO stm_messages (conversation_id, numero_usuario, grupo_id, tipo_conversa, role, content, author_name, recipient, quoted_author, is_listen, timestamp, importancia, emocao, reply_info, token_count, message_id) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", (conversation_id, numero_usuario or '', grupo_id or '', tipo_conversa or 'pv', role, content, author_name or '', recipient or '', quoted_author or '', 1 if is_listen else 0, timestamp or 0, importancia, emocao, json.dumps(reply_info or {}), token_count or 0, message_id), commit=True)
            else:
                self._execute_with_retry("INSERT INTO stm_messages (conversation_id, numero_usuario, grupo_id, tipo_conversa, role, content, author_name, recipient, quoted_author, is_listen, timestamp, importancia, emocao, reply_info, token_count, message_id) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", (conversation_id, numero_usuario or '', grupo_id or '', tipo_conversa or 'pv', role, content, author_name or '', recipient or '', quoted_author or '', 1 if is_listen else 0, timestamp or 0, importancia, emocao, json.dumps(reply_info or {}), token_count or 0, message_id), commit=True)
            return True
        except Exception:
            try:
                self._execute_with_retry("INSERT INTO stm_messages (conversation_id, numero_usuario, grupo_id, tipo_conversa, role, content, author_name, recipient, quoted_author, is_listen, timestamp, importancia, emocao, reply_info, token_count, message_id) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", (conversation_id, numero_usuario or '', grupo_id or '', tipo_conversa or 'pv', role, content, author_name or '', recipient or '', quoted_author or '', 1 if is_listen else 0, timestamp or 0, importancia, emocao, json.dumps(reply_info or {}), token_count or 0, message_id), commit=True)
                return True
            except Exception:
                return False

    def stm_get_messages(self, conversation_id: str, limit: int = 30):
        try:
            rows = self._execute_with_retry("SELECT role, content, timestamp, importancia, emocao, reply_info, author_name, token_count, numero_usuario, grupo_id, is_listen, recipient, quoted_author FROM stm_messages WHERE conversation_id = ? ORDER BY timestamp DESC LIMIT ?", (conversation_id, limit))
            if not rows:
                return []
            return [dict(r) for r in rows][::-1]
        except Exception:
            return []

    def stm_get_listen_messages(self, conversation_id: str, limit: int = 30, numero_usuario: str = None):
        try:
            if numero_usuario:
                rows = self._execute_with_retry("SELECT role, content, timestamp, author_name, reply_info, numero_usuario, is_listen, recipient, quoted_author FROM stm_messages WHERE conversation_id = ? AND numero_usuario = ? AND (is_listen = 1 OR reply_info LIKE '%observed_only%') ORDER BY timestamp DESC LIMIT ?", (conversation_id, numero_usuario, limit))
            else:
                rows = self._execute_with_retry("SELECT role, content, timestamp, author_name, reply_info, numero_usuario, is_listen, recipient, quoted_author FROM stm_messages WHERE conversation_id = ? AND (is_listen = 1 OR reply_info LIKE '%observed_only%') ORDER BY timestamp DESC LIMIT ?", (conversation_id, limit))
            if not rows:
                return []
            return [dict(r) for r in rows][::-1]
        except Exception:
            return []

    def stm_cleanup(self, conversation_id: str = None, max_age_hours: int = 24):
        try:
            if conversation_id:
                self._execute_with_retry("DELETE FROM stm_messages WHERE conversation_id = ? AND created_at < datetime('now', ?)", (conversation_id, f'-{max_age_hours} hours'), commit=True)
            else:
                self._execute_with_retry("DELETE FROM stm_messages WHERE created_at < datetime('now', ?)", (f'-{max_age_hours} hours',), commit=True)
        except Exception:
            pass

    def check_idempotency(self, message_id: str, context: str = "general") -> bool:
        """

        Verifica se um message_id já foi processado recentemente.

        Retorna True se for DUPLICADO, False se for NOVO.

        """
        if not message_id:
            return False
            
        try:
            # Tenta inserir na tabela de mensagens. Se falhar por UNIQUE constraint, é duplicado.
            # Mas como a tabela mensagens pode ter muitos campos, vamos usar um cache rápido ou 
            # verificar apenas o message_id.
            query = "SELECT id FROM mensagens WHERE message_id = ? LIMIT 1"
            res = self._execute_with_retry(query, (message_id,))
            return len(res) > 0 if res else False
        except Exception:
            return False

def get_database(db_path: Optional[str] = None):
    """

    Factory function — detecta PostgreSQL automaticamente.

    Se DATABASE_URL estiver definido, usa PostgreSQL. Caso contrário, SQLite.

    """
    import os
    pg_url = os.environ.get('DATABASE_URL', '')
    if pg_url:
        try:
            from .database_pg import DatabasePG
            logger.info("🐘 Usando PostgreSQL (DATABASE_URL detectado)")
            return DatabasePG(db_path or pg_url)
        except ImportError:
            logger.warning("⚠️ psycopg2 não instalado — fallback para SQLite")
    
    from . import config
    path = db_path or getattr(config, 'DB_PATH', '/kiami/data/kiami.db')
    return Database(path)