File size: 46,045 Bytes
33d8b39
 
 
 
 
f9bcb56
1a1c26f
33d8b39
 
9a6eae1
33d8b39
 
 
 
 
 
 
f9bcb56
468cfed
d769770
869f1a1
 
 
9a6eae1
 
 
 
869f1a1
 
 
 
 
9a6eae1
 
 
33d8b39
869f1a1
33d8b39
 
 
9a6eae1
33d8b39
f9bcb56
 
 
d769770
 
869f1a1
 
 
 
 
d769770
869f1a1
 
33d8b39
869f1a1
1a1c26f
 
51dd7a9
869f1a1
1a1c26f
869f1a1
 
 
 
 
 
1a1c26f
 
869f1a1
 
1a1c26f
 
 
 
 
 
 
 
 
 
 
 
1773769
869f1a1
1773769
869f1a1
 
51dd7a9
 
 
 
a942df4
 
 
51dd7a9
b9785dc
 
51dd7a9
 
 
1773769
e24dd93
f888f5d
51dd7a9
 
 
 
 
 
f888f5d
51dd7a9
 
 
 
 
 
 
f888f5d
 
 
 
 
51dd7a9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f888f5d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
51dd7a9
1773769
51dd7a9
1773769
51dd7a9
 
 
 
 
 
1773769
51dd7a9
 
 
1773769
 
51dd7a9
 
 
 
8a49dbc
 
 
51dd7a9
93661e2
 
 
51dd7a9
 
 
 
1773769
 
51dd7a9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
93661e2
 
 
 
 
 
 
 
 
51dd7a9
 
 
93661e2
 
 
 
 
 
 
 
 
 
51dd7a9
 
 
8a49dbc
51dd7a9
 
 
 
 
 
 
 
 
8a49dbc
4847f96
 
8a49dbc
 
4847f96
8a49dbc
4847f96
8a49dbc
51dd7a9
 
8a49dbc
51dd7a9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4847f96
51dd7a9
 
 
 
 
 
 
 
 
 
 
 
1773769
51dd7a9
 
 
 
 
 
1773769
51dd7a9
1773769
51dd7a9
1773769
51dd7a9
 
 
1773769
51dd7a9
 
1773769
51dd7a9
 
 
1773769
1a1c26f
 
 
 
a942df4
 
 
1a1c26f
 
a942df4
1a1c26f
edcea58
1a1c26f
869f1a1
a942df4
 
1773769
a942df4
ceaa272
 
 
 
 
 
 
 
 
 
 
a942df4
 
 
 
856fa70
a942df4
 
ceaa272
edcea58
1a1c26f
869f1a1
1a1c26f
edcea58
1a1c26f
 
 
869f1a1
ceaa272
1a1c26f
 
 
 
 
869f1a1
1a1c26f
869f1a1
1a1c26f
 
 
869f1a1
1a1c26f
 
869f1a1
1a1c26f
 
 
 
 
 
 
 
869f1a1
1a1c26f
869f1a1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1a1c26f
 
869f1a1
 
1a1c26f
 
 
 
 
2d4eabc
1a1c26f
869f1a1
 
 
 
 
 
2d4eabc
869f1a1
 
 
 
 
 
 
 
 
 
 
 
2d4eabc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
869f1a1
 
 
 
 
 
 
 
 
 
 
 
 
 
2d4eabc
869f1a1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1a1c26f
869f1a1
 
 
 
 
 
1a1c26f
 
 
869f1a1
 
 
1a1c26f
869f1a1
1a1c26f
869f1a1
1a1c26f
869f1a1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1a1c26f
 
 
 
869f1a1
 
1a1c26f
 
 
33d8b39
 
 
d769770
869f1a1
33d8b39
 
 
 
1a1c26f
33d8b39
 
 
 
 
1a1c26f
33d8b39
 
 
 
 
 
 
 
 
 
 
 
 
 
 
869f1a1
 
 
33d8b39
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1a1c26f
33d8b39
 
51dd7a9
33d8b39
 
869f1a1
33d8b39
 
 
 
 
 
869f1a1
 
 
33d8b39
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
48fbd23
33d8b39
 
 
48fbd23
33d8b39
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
51dd7a9
 
 
48fbd23
 
869f1a1
 
48fbd23
33d8b39
 
 
 
 
 
 
 
 
 
 
869f1a1
48fbd23
 
33d8b39
 
 
869f1a1
48fbd23
 
33d8b39
 
 
 
9082c5a
33d8b39
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
869f1a1
33d8b39
 
51dd7a9
33d8b39
 
 
 
 
 
 
869f1a1
9a6eae1
 
 
 
 
 
33d8b39
 
48fbd23
869f1a1
 
 
48fbd23
 
 
 
 
 
 
 
 
33d8b39
 
 
 
 
 
 
 
 
869f1a1
 
 
33d8b39
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
869f1a1
 
 
33d8b39
 
 
 
 
 
 
 
48fbd23
33d8b39
1a1c26f
33d8b39
1a1c26f
 
 
33d8b39
 
48fbd23
9082c5a
48fbd23
33d8b39
 
48fbd23
869f1a1
51dd7a9
 
 
 
 
 
869f1a1
33d8b39
 
 
 
 
48fbd23
33d8b39
 
 
 
48fbd23
33d8b39
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9a6eae1
33d8b39
 
 
 
1a1c26f
33d8b39
 
51dd7a9
33d8b39
 
 
869f1a1
33d8b39
 
 
 
 
 
48fbd23
869f1a1
33d8b39
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f9bcb56
 
 
 
 
33d8b39
1a1c26f
48fbd23
 
51dd7a9
48fbd23
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1a1c26f
33d8b39
 
51dd7a9
33d8b39
 
 
d769770
48fbd23
 
33d8b39
 
48fbd23
9a6eae1
51dd7a9
6d974a3
869f1a1
9a6eae1
6d974a3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9a6eae1
 
 
 
 
 
 
33d8b39
6d974a3
 
 
 
33d8b39
6d974a3
 
 
 
 
 
 
 
13233ee
6d974a3
 
 
 
 
 
468cfed
6d974a3
 
 
 
 
 
 
 
 
 
 
f3ff324
 
6d974a3
 
 
 
 
 
51dd7a9
 
 
6d974a3
 
 
5824387
 
 
6d974a3
5824387
6d974a3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9a6eae1
6d974a3
48fbd23
6d974a3
9a6eae1
6d974a3
9a6eae1
6d974a3
 
 
9a6eae1
 
33d8b39
48fbd23
 
72acd30
 
 
 
 
 
 
 
 
 
 
 
 
48fbd23
33d8b39
 
9a6eae1
869f1a1
 
33d8b39
 
 
 
 
48fbd23
 
1a1c26f
 
 
869f1a1
48fbd23
1a1c26f
48fbd23
33d8b39
 
 
 
48fbd23
 
 
 
 
 
 
 
 
 
 
 
33d8b39
 
48fbd23
 
33d8b39
 
 
 
 
48fbd23
 
33d8b39
 
48fbd23
33d8b39
869f1a1
 
 
33d8b39
 
 
 
48fbd23
 
33d8b39
 
 
 
9a6eae1
 
869f1a1
9a6eae1
 
 
 
 
 
 
 
 
33d8b39
9a6eae1
 
869f1a1
 
 
 
 
9a6eae1
869f1a1
9a6eae1
 
869f1a1
 
 
 
 
 
 
 
 
 
 
 
 
9a6eae1
 
33d8b39
 
 
f9bcb56
 
 
869f1a1
f9bcb56
 
 
 
869f1a1
f9bcb56
 
 
 
 
 
 
 
 
 
869f1a1
 
 
f9bcb56
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33d8b39
 
 
 
 
48fbd23
33d8b39
869f1a1
33d8b39
 
869f1a1
9a6eae1
 
 
 
 
 
 
 
869f1a1
 
 
 
 
9a6eae1
869f1a1
9a6eae1
 
 
 
 
 
 
 
 
 
 
 
869f1a1
 
 
 
9a6eae1
869f1a1
9a6eae1
 
 
33d8b39
 
 
 
d769770
33d8b39
d769770
869f1a1
48fbd23
33d8b39
1a1c26f
869f1a1
 
 
33d8b39
 
 
 
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
import os
import io
import zipfile
import re
import difflib
import tempfile
import uuid
from typing import List, Optional, Dict, Any

from fastapi import FastAPI, UploadFile, File, HTTPException, Form, Header
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel

import torch
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
from langdetect import detect
from transformers import MarianMTModel, MarianTokenizer
from openai import OpenAI

# ---- Postgres ----
import psycopg2
from psycopg2 import sql as pgsql

# ---- Supabase ----
from supabase import create_client, Client

SUPABASE_URL = "https://bnvmqgjawtaslczewqyd.supabase.co"
SUPABASE_ANON_KEY = (
    "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6ImJudm1x"
    "Z2phd3Rhc2xjemV3cXlkIiwicm9sZSI6ImFub24iLCJpYXQiOjE3NjQ0NjM5NDAsImV4cCI6MjA4"
    "MDAzOTk0MH0.9zkyqrsm-QOSwMTUPZEWqyFeNpbbuar01rB7pmObkUI"
)

supabase: Client = create_client(SUPABASE_URL, SUPABASE_ANON_KEY)

# ======================================================
# 0) Configuración general de paths / modelo / OpenAI
# ======================================================

MODEL_DIR = os.getenv("MODEL_DIR", "stvnnnnnn/t5-large-nl2sql-spider")
DEVICE = torch.device("cpu")

OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
openai_client = OpenAI(api_key=OPENAI_API_KEY) if OPENAI_API_KEY else None

# DSN de Supabase Postgres – EJEMPLO:
# postgresql://postgres:[email protected]:5432/postgres
POSTGRES_DSN = os.getenv("POSTGRES_DSN")

if not POSTGRES_DSN:
    raise RuntimeError(
        "⚠️ POSTGRES_DSN no está definido. "
        "Configúralo en los secrets del Space con la cadena de conexión de Supabase."
    )

# ======================================================
# 1) Gestor de conexiones dinámicas: Postgres (Neon)
# ======================================================


class PostgresManager:
    """
    Cada upload crea un *schema* aislado en Neon.
    connections[connection_id] = {
        "label": str,        # nombre de archivo original
        "engine": "postgres",
        "schema": str        # nombre del schema en Neon
    }
    """

    def __init__(self, dsn: str):
        self.dsn = dsn
        self.connections: Dict[str, Dict[str, Any]] = {}

    # ---------- utilidades internas ----------

    def _new_connection_id(self) -> str:
        return f"db_{uuid.uuid4().hex[:8]}"

    def _get_info(self, connection_id: str) -> Dict[str, Any]:
        if connection_id not in self.connections:
            raise KeyError(f"connection_id '{connection_id}' no registrado")
        return self.connections[connection_id]

    def _get_conn(self, autocommit: bool = True):
        conn = psycopg2.connect(self.dsn)
        conn.autocommit = autocommit
        return conn

    # ---------- helpers de sanitización de dumps ----------

    def _rewrite_line_for_schema(self, line: str, schema_name: str) -> str:
        """
        Versión simplificada:
        - Solo elimina líneas que modifican el search_path.
        - NO reescribe public./pagila. → dejamos que el dump use su propio schema.
        """
        if "search_path" in line.lower():
            return ""
        return line

    def _should_skip_statement(self, stmt: str) -> bool:
        """
        Devuelve True si el statement NO debe ejecutarse (grants, owner, create db, domains, etc.).
        Filtro universal para dumps PostgreSQL (Neon, Pagila, etc.).
        """
        if not stmt:
            return True

        upper = stmt.upper().strip()

        # 1) Statements globales / de administración que SIEMPRE ignoramos
        skip_prefixes = (
            "SET ",
            "RESET ",
            "SELECT PG_CATALOG.SET_CONFIG",
            "COMMENT ON EXTENSION",
            "COMMENT ON SCHEMA",
            "COMMENT ON DATABASE",
            "COMMENT ON COLLATION",
            "COMMENT ON CONVERSION",
            "COMMENT ON LANGUAGE",
            "COMMENT ON TEXT SEARCH",
            "COMMENT ON FOREIGN",
            "CREATE DATABASE",
            "ALTER DATABASE",
            "DROP DATABASE",
            "CREATE EXTENSION",
            "ALTER EXTENSION",
            "DROP EXTENSION",
            "REVOKE ",
            "GRANT ",
            "ALTER ROLE",
            "CREATE ROLE",
            "DROP ROLE",
            "CREATE USER",
            "ALTER USER",
            "DROP USER",
            "ALTER DEFAULT PRIVILEGES",
            "SECURITY LABEL",
            "BEGIN",
            "COMMIT",
            "ROLLBACK",
        )
        if upper.startswith(skip_prefixes):
            return True

        # 2) Cualquier cosa que toque OWNER / AUTHORIZATION la ignoramos
        owner_markers = (
            " OWNER TO ",
            " OWNER ",
            "AUTHORIZATION POSTGRES",
            "AUTHORIZATION PUBLIC",
            "AUTHORIZATION CURRENT_USER",
            "AUTHORIZATION \"POSTGRES\"",
        )
        if any(marker in upper for marker in owner_markers):
            return True

        # 3) Grants / revokes explícitos a postgres o public (aunque no empiecen por GRANT/REVOKE)
        if " TO POSTGRES" in upper or " FROM POSTGRES" in upper:
            return True
        if " TO PUBLIC" in upper or " FROM PUBLIC" in upper:
            return True

        return False

    def _execute_sanitized_pg_dump(
        self, cur, sql_text: str, schema_name: str
    ) -> None:
        """
        Ejecuta un dump de PostgreSQL dentro de un schema de sesión,
        aplicando sanitización y soportando COPY ... FROM stdin;.

        - Reescribe public./pagila. -> schema_name.
        - Respeta funciones con $$...$$ (no corta por ';' internos).
        - Ignora statements peligrosos via _should_skip_statement().
        """

        in_copy = False
        copy_sql = ""
        copy_lines: list[str] = []

        buffer = ""       # statement acumulado
        in_dollar = False # estamos dentro de $$...$$ ?
        dollar_tag = ""   # por ej. "$func$"

        in_domain_block = False  # 👈 estamos dentro de un bloque CREATE DOMAIN ?
        in_function_block = False  # 👈 estamos dentro de un CREATE FUNCTION ?

        def flush_statement():
            nonlocal buffer
            stmt = buffer.strip()
            buffer = ""
            if not stmt:
                return
            if self._should_skip_statement(stmt):
                return
            try:
                cur.execute(stmt)
            except Exception as e:
                msg = str(e).lower()
                # Ignoramos errores típicos de dumps que no son fatales
                if "already exists" in msg or "duplicate key value" in msg:
                    print("[WARN] Ignorando error no crítico:", e)
                    return
                raise

        # Procesar línea por línea
        for raw_line in sql_text.splitlines():
            line = raw_line.rstrip("\n")
            stripped = line.strip()

            # ====== BLOQUE CREATE FUNCTION (lo ignoramos entero) ======
            if in_function_block:
                # Cerramos cuando vemos algo tipo "$_$;" o "$func$;"
                if re.search(r"\$[A-Za-z0-9_]*\$;", stripped):
                    in_function_block = False
                continue

            # Comentarios y líneas vacías (fuera de COPY / DOMAIN / FUNCTION)
            if not in_copy and not in_domain_block:
                if not stripped or stripped.startswith("--"):
                    continue

                upper_line = stripped.upper()
                if (
                    upper_line.startswith("CREATE FUNCTION")
                    or upper_line.startswith("CREATE OR REPLACE FUNCTION")
                    or upper_line.startswith("ALTER FUNCTION")
                ):
                    # Ignoramos toda la función (cabecera + cuerpo)
                    in_function_block = True
                    continue

            # ====== BLOQUE COPY ... FROM stdin ======
            if in_copy:
                if stripped == r"\.":
                    # fin de COPY
                    data = "\n".join(copy_lines) + "\n"
                    cur.copy_expert(copy_sql, io.StringIO(data))
                    in_copy = False
                    copy_sql = ""
                    copy_lines.clear()
                else:
                    copy_lines.append(line)
                continue

            # Reescribimos la línea según el schema de sesión
            line = self._rewrite_line_for_schema(line, schema_name)
            stripped = line.strip()
            if not stripped:
                continue

            # Detectar inicio de COPY ahora que la línea ya está reescrita
            if stripped.upper().startswith("COPY ") and "FROM stdin" in stripped.upper():
                # Ejecutar lo que haya pendiente antes del COPY
                flush_statement()
                in_copy = True
                copy_sql = stripped  # ya reescrita
                copy_lines = []
                continue

            # Escanear la línea caracter a caracter para detectar $tag$ y ';'
            i = 0
            start_seg = 0
            length = len(line)

            while i < length:
                ch = line[i]

                # Manejo de delimitadores $tag$
                if ch == "$":
                    # ¿Inicio o fin de bloque dollar-quoted?
                    j = i + 1
                    while j < length and (line[j].isalnum() or line[j] == "_"):
                        j += 1
                    if j < length and line[j] == "$":
                        tag = line[i : j + 1]  # incluye ambos '$'
                        if not in_dollar:
                            in_dollar = True
                            dollar_tag = tag
                        else:
                            if tag == dollar_tag:
                                in_dollar = False
                                dollar_tag = ""
                        i = j + 1
                        continue

                # Fin de statement: ';' fuera de bloque dollar-quoted
                if ch == ";" and not in_dollar:
                    segment = line[start_seg : i + 1]
                    buffer += segment + "\n"
                    flush_statement()
                    start_seg = i + 1
                    i += 1
                    continue

                i += 1

            # Resto de la línea (después del último ';' o toda la línea si no hubo ';')
            if start_seg < length:
                buffer += line[start_seg:] + "\n"

        # Ejecutar lo que quede pendiente
        flush_statement()

        # Por seguridad, aseguramos que no haya COPY abierto sin cerrar
        if in_copy:
            raise RuntimeError("Dump SQL inválido: COPY sin terminación '\\.'")

    # ---------- creación de BD desde dump ----------

    def create_database_from_dump(self, label: str, sql_text: str) -> str:
        """
        Restaura un dump de Postgres (schema + datos) en la BD Neon.
        NO crea schemas de sesión: deja que el dump use sus propios schemas
        (public, pagila, etc.). Luego detecta el schema con más tablas.
        """
        connection_id = self._new_connection_id()
        schema_name: str | None = None

        conn = self._get_conn()
        try:
            with conn.cursor() as cur:
                # 1) Ejecutar el dump tal cual (solo limpiamos search_path)
                self._execute_sanitized_pg_dump(cur, sql_text, schema_name="public")

                # 2) Detectar el schema REAL donde quedaron las tablas del dump
                cur.execute(
                    """
                    SELECT table_schema, COUNT(*) AS n
                    FROM information_schema.tables
                    WHERE table_type = 'BASE TABLE'
                      AND table_schema NOT IN ('pg_catalog','information_schema')
                    GROUP BY table_schema
                    ORDER BY n DESC;
                    """
                )
                rows = cur.fetchall()
                if not rows:
                    raise RuntimeError(
                        "El dump se ejecutó pero no se encontraron tablas de usuario."
                    )

                # Tomamos el schema con más tablas (pagila, public, etc.)
                schema_name = rows[0][0]

        except Exception as e:
            conn.close()
            raise RuntimeError(f"Error ejecutando dump SQL en Postgres: {e}")
        finally:
            conn.close()

        self.connections[connection_id] = {
            "label": label,
            "engine": "postgres",
            "schema": schema_name,   # 👈 ahora es el schema REAL con tablas
        }
        return connection_id

    # ---------- ejecución segura de SQL ----------

    def execute_sql(self, connection_id: str, sql_text: str) -> Dict[str, Any]:
        """
        Ejecuta un SELECT dentro del schema asociado al connection_id.
        Bloquea operaciones destructivas por seguridad.
        """
        info = self._get_info(connection_id)
        schema = info["schema"]

        forbidden = ["drop ", "delete ", "update ", "insert ", "alter ", "replace "]
        sql_low = sql_text.lower()
        if any(tok in sql_low for tok in forbidden):
            return {
                "ok": False,
                "error": "Query bloqueada por seguridad (operación destructiva).",
                "rows": None,
                "columns": [],
            }

        conn = self._get_conn()
        try:
            with conn.cursor() as cur:
                # usar el schema de la sesión
                cur.execute(
                    pgsql.SQL("SET search_path TO {}").format(
                        pgsql.Identifier(schema)
                    )
                )
                cur.execute(sql_text)

                if cur.description:
                    rows = cur.fetchall()
                    cols = [d[0] for d in cur.description]
                else:
                    rows, cols = [], []

            return {
                "ok": True,
                "error": None,
                "rows": [list(r) for r in rows],
                "columns": cols,
            }
        except Exception as e:
            return {"ok": False, "error": str(e), "rows": None, "columns": []}
        finally:
            conn.close()

    # ---------- introspección de esquema ----------

    def get_schema(self, connection_id: str) -> Dict[str, Any]:
        info = self._get_info(connection_id)
        schema = info["schema"]  # schema "ideal" que registramos

        conn = self._get_conn()
        try:
            tables_info: Dict[str, Dict[str, Any]] = {}
            foreign_keys: List[Dict[str, Any]] = []

            with conn.cursor() as cur:
                # 1) Intentamos solo con el schema registrado
                cur.execute(
                    """
                    SELECT table_name
                    FROM information_schema.tables
                    WHERE table_schema = %s
                      AND table_type = 'BASE TABLE'
                    ORDER BY table_name;
                    """,
                    (schema,),
                )
                tables = [r[0] for r in cur.fetchall()]

                # 2) 🔁 Fallback: si no hay tablas en ese schema,
                #    buscamos en TODOS los schemas de usuario
                if not tables:
                    cur.execute(
                        """
                        SELECT table_schema, table_name
                        FROM information_schema.tables
                        WHERE table_type = 'BASE TABLE'
                          AND table_schema NOT IN ('pg_catalog','information_schema')
                        ORDER BY table_schema, table_name;
                        """
                    )
                    rows = cur.fetchall()

                    if not rows:
                        # No hay tablas en ningún schema de usuario
                        return {
                            "tables": {},
                            "foreign_keys": [],
                        }

                    # Schemas candidatos que sí tienen tablas
                    schemas = sorted({s for (s, _) in rows})

                    # Preferimos:
                    # 1) el schema ya registrado (si por alguna razón tiene tablas)
                    # 2) 'pagila'
                    # 3) 'public'
                    # 4) el primero que aparezca
                    target_schema = None
                    if schema in schemas:
                        target_schema = schema
                    elif "pagila" in schemas:
                        target_schema = "pagila"
                    elif "public" in schemas:
                        target_schema = "public"
                    else:
                        target_schema = schemas[0]

                    print(
                        f"[WARN] Schema '{schema}' sin tablas; usando schema real '{target_schema}'"
                    )

                    # Actualizamos el schema asociado a esta conexión
                    schema = target_schema
                    info["schema"] = schema

                    tables = [t for (s, t) in rows if s == schema]

                # 3) Columnas por tabla del schema final seleccionado
                for t in tables:
                    cur.execute(
                        """
                        SELECT column_name
                        FROM information_schema.columns
                        WHERE table_schema = %s
                          AND table_name = %s
                        ORDER BY ordinal_position;
                        """,
                        (schema, t),
                    )
                    cols = [r[0] for r in cur.fetchall()]
                    tables_info[t] = {"columns": cols}

                # 4) Foreign keys del schema final
                cur.execute(
                    """
                    SELECT
                        tc.table_name      AS from_table,
                        kcu.column_name    AS from_column,
                        ccu.table_name     AS to_table,
                        ccu.column_name    AS to_column
                    FROM information_schema.table_constraints AS tc
                    JOIN information_schema.key_column_usage AS kcu
                      ON tc.constraint_name = kcu.constraint_name
                     AND tc.table_schema   = kcu.table_schema
                    JOIN information_schema.constraint_column_usage AS ccu
                      ON ccu.constraint_name = tc.constraint_name
                     AND ccu.table_schema   = tc.table_schema
                    WHERE tc.constraint_type = 'FOREIGN KEY'
                      AND tc.table_schema    = %s;
                    """,
                    (schema,),
                )
                for ft, fc, tt, tc2 in cur.fetchall():
                    foreign_keys.append(
                        {
                            "from_table": ft,
                            "from_column": fc,
                            "to_table": tt,
                            "to_column": tc2,
                        }
                    )

            return {
                "tables": tables_info,
                "foreign_keys": foreign_keys,
            }
        finally:
            conn.close()

    # ---------- preview de tabla ----------

    def get_preview(
        self, connection_id: str, table: str, limit: int = 20
    ) -> Dict[str, Any]:
        info = self._get_info(connection_id)
        schema = info["schema"]

        conn = self._get_conn()
        try:
            with conn.cursor() as cur:
                cur.execute(
                    pgsql.SQL("SET search_path TO {}").format(
                        pgsql.Identifier(schema)
                    )
                )
                query = pgsql.SQL("SELECT * FROM {} LIMIT %s").format(
                    pgsql.Identifier(table)
                )
                cur.execute(query, (int(limit),))
                rows = cur.fetchall()
                cols = [d[0] for d in cur.description] if cur.description else []

            return {
                "columns": cols,
                "rows": [list(r) for r in rows],
            }
        finally:
            conn.close()


# Instancia global de PostgresManager
sql_manager = PostgresManager(POSTGRES_DSN)

# ======================================================
# 2) Inicialización de FastAPI
# ======================================================

app = FastAPI(
    title="NL2SQL Backend",
    version="3.0.0",
)

app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_methods=["*"],
    allow_headers=["*"],
)

# ======================================================
# 3) Modelo NL→SQL y traductor ES→EN
# ======================================================

t5_tokenizer = None
t5_model = None
mt_tokenizer = None
mt_model = None


def load_nl2sql_model():
    """Carga el modelo NL→SQL (T5-large fine-tuned en Spider) desde HF Hub."""
    global t5_tokenizer, t5_model
    if t5_model is not None:
        return
    print(f"🔁 Cargando modelo NL→SQL desde: {MODEL_DIR}")
    t5_tokenizer = AutoTokenizer.from_pretrained(MODEL_DIR, use_fast=True)
    t5_model = AutoModelForSeq2SeqLM.from_pretrained(
        MODEL_DIR, torch_dtype=torch.float32
    )
    t5_model.to(DEVICE)
    t5_model.eval()
    print("✅ Modelo NL→SQL listo en memoria.")


def load_es_en_translator():
    """Carga el modelo Helsinki-NLP para traducción ES→EN (solo una vez)."""
    global mt_tokenizer, mt_model
    if mt_model is not None:
        return
    model_name = "Helsinki-NLP/opus-mt-es-en"
    print(f"🔁 Cargando traductor ES→EN: {model_name}")
    mt_tokenizer = MarianTokenizer.from_pretrained(model_name)
    mt_model = MarianMTModel.from_pretrained(model_name)
    mt_model.to(DEVICE)
    mt_model.eval()
    print("✅ Traductor ES→EN listo.")


def detect_language(text: str) -> str:
    try:
        return detect(text)
    except Exception:
        return "unknown"


def translate_es_to_en(text: str) -> str:
    """
    Usa Marian ES→EN solo si el texto se detecta como español ('es').
    Si no, devuelve el texto tal cual.
    """
    lang = detect_language(text)
    if lang != "es":
        return text
    if mt_model is None:
        load_es_en_translator()
    inputs = mt_tokenizer(text, return_tensors="pt", truncation=True).to(DEVICE)
    with torch.no_grad():
        out = mt_model.generate(**inputs, max_length=256)
    return mt_tokenizer.decode(out[0], skip_special_tokens=True)


# ======================================================
# 4) Capa de reparación de SQL (usa el schema real)
# ======================================================


def _normalize_name_for_match(name: str) -> str:
    s = name.lower()
    s = s.replace('"', "").replace("`", "")
    s = s.replace("_", "")
    if s.endswith("s") and len(s) > 3:
        s = s[:-1]
    return s


def _build_schema_indexes(
    tables_info: Dict[str, Dict[str, List[str]]]
) -> Dict[str, Dict[str, List[str]]]:
    table_index: Dict[str, List[str]] = {}
    column_index: Dict[str, List[str]] = {}

    for t, info in tables_info.items():
        tn = _normalize_name_for_match(t)
        table_index.setdefault(tn, [])
        if t not in table_index[tn]:
            table_index[tn].append(t)

        for c in info.get("columns", []):
            cn = _normalize_name_for_match(c)
            column_index.setdefault(cn, [])
            if c not in column_index[cn]:
                column_index[cn].append(c)

    return {"table_index": table_index, "column_index": column_index}


def _best_match_name(missing: str, index: Dict[str, List[str]]) -> Optional[str]:
    if not index:
        return None

    key = _normalize_name_for_match(missing)
    if key in index and index[key]:
        return index[key][0]

    candidates = difflib.get_close_matches(key, list(index.keys()), n=1, cutoff=0.7)
    if not candidates:
        return None
    best_key = candidates[0]
    if index[best_key]:
        return index[best_key][0]
    return None


DOMAIN_SYNONYMS_TABLE = {
    "song": "track",
    "songs": "track",
    "tracks": "track",
    "artist": "artist",
    "artists": "artist",
    "album": "album",
    "albums": "album",
    "order": "invoice",
    "orders": "invoice",
}

DOMAIN_SYNONYMS_COLUMN = {
    "song": "name",
    "songs": "name",
    "track": "name",
    "title": "name",
    "length": "milliseconds",
    "duration": "milliseconds",
}


def try_repair_sql(
    sql: str, error: str, schema_meta: Dict[str, Any]
) -> Optional[str]:
    """
    Intenta reparar nombres de tablas/columnas basándose en el esquema real.
    Compatible con mensajes de Postgres y también con los de SQLite
    (por si algún día reusamos la lógica).
    """
    tables_info = schema_meta["tables"]
    idx = _build_schema_indexes(tables_info)
    table_index = idx["table_index"]
    column_index = idx["column_index"]

    repaired_sql = sql
    changed = False

    missing_table = None
    missing_column = None

    m_t = re.search(r'relation "([\w\.]+)" does not exist', error, re.IGNORECASE)
    if not m_t:
        m_t = re.search(r"no such table: ([\w\.]+)", error)
    if m_t:
        missing_table = m_t.group(1)

    m_c = re.search(r'column "([\w\.]+)" does not exist', error, re.IGNORECASE)
    if not m_c:
        m_c = re.search(r"no such column: ([\w\.]+)", error)
    if m_c:
        missing_column = m_c.group(1)

    if missing_table:
        short = missing_table.split(".")[-1]
        syn = DOMAIN_SYNONYMS_TABLE.get(short.lower())
        target = None
        if syn:
            target = _best_match_name(syn, table_index) or syn
        if not target:
            target = _best_match_name(short, table_index)

        if target:
            pattern = r"\b" + re.escape(short) + r"\b"
            new_sql = re.sub(pattern, target, repaired_sql)
            if new_sql != repaired_sql:
                repaired_sql = new_sql
                changed = True

    if missing_column:
        short = missing_column.split(".")[-1]
        syn = DOMAIN_SYNONYMS_COLUMN.get(short.lower())
        target = None
        if syn:
            target = _best_match_name(syn, column_index) or syn
        if not target:
            target = _best_match_name(short, column_index)

        if target:
            pattern = r"\b" + re.escape(short) + r"\b"
            new_sql = re.sub(pattern, target, repaired_sql)
            if new_sql != repaired_sql:
                repaired_sql = new_sql
                changed = True

    if not changed:
        return None
    return repaired_sql


# ======================================================
# 5) Prompt NL→SQL + re-ranking
# ======================================================


def build_prompt(question_en: str, db_id: str, schema_str: str) -> str:
    return (
        f"translate to SQL: {question_en} | "
        f"db: {db_id} | schema: {schema_str} | "
        f"note: use JOIN when foreign keys link tables"
    )


def normalize_score(raw: float) -> float:
    """Normaliza el score logit del modelo a un porcentaje 0-100."""
    norm = (raw + 20) / 25
    norm = max(0, min(1, norm))
    return round(norm * 100, 2)


def nl2sql_with_rerank(question: str, conn_id: str) -> Dict[str, Any]:
    if conn_id not in sql_manager.connections:
        raise HTTPException(
            status_code=404, detail=f"connection_id '{conn_id}' no registrado"
        )

    meta = sql_manager.get_schema(conn_id)
    tables_info = meta["tables"]

    parts = []
    for t, info in tables_info.items():
        cols = info.get("columns", [])
        parts.append(f"{t}(" + ", ".join(cols) + ")")
    schema_str = " ; ".join(parts) if parts else "(empty_schema)"

    detected = detect_language(question)
    question_en = translate_es_to_en(question) if detected == "es" else question

    prompt = build_prompt(question_en, db_id=conn_id, schema_str=schema_str)

    if t5_model is None:
        load_nl2sql_model()

    inputs = t5_tokenizer(
        [prompt], return_tensors="pt", truncation=True, max_length=768
    ).to(DEVICE)
    num_beams = 6
    num_return = 6

    with torch.no_grad():
        out = t5_model.generate(
            **inputs,
            max_length=220,
            num_beams=num_beams,
            num_return_sequences=num_return,
            return_dict_in_generate=True,
            output_scores=True,
        )

    sequences = out.sequences
    scores = out.sequences_scores
    if scores is not None:
        scores = scores.cpu().tolist()
    else:
        scores = [0.0] * sequences.size(0)

    candidates: List[Dict[str, Any]] = []
    best = None
    best_exec = False
    best_score = -1e9

    for i in range(sequences.size(0)):
        raw_sql = t5_tokenizer.decode(
            sequences[i], skip_special_tokens=True
        ).strip()
        cand: Dict[str, Any] = {
            "sql": raw_sql,
            "score": float(scores[i]),
            "repaired_from": None,
            "repair_note": None,
            "raw_sql_model": raw_sql,
        }

        exec_info = sql_manager.execute_sql(conn_id, raw_sql)

        err_lower = (exec_info["error"] or "").lower()
        if (not exec_info["ok"]) and (
            "no such table" in err_lower
            or "no such column" in err_lower
            or "does not exist" in err_lower
        ):
            current_sql = raw_sql
            last_error = exec_info["error"] or ""
            for step in range(1, 4):
                repaired_sql = try_repair_sql(current_sql, last_error, meta)
                if not repaired_sql or repaired_sql == current_sql:
                    break
                exec_info2 = sql_manager.execute_sql(conn_id, repaired_sql)
                cand["repaired_from"] = (
                    current_sql
                    if cand["repaired_from"] is None
                    else cand["repaired_from"]
                )
                cand["repair_note"] = (
                    f"auto-repair (table/column name, step {step})"
                )
                cand["sql"] = repaired_sql
                exec_info = exec_info2
                current_sql = repaired_sql
                if exec_info2["ok"]:
                    break
                last_error = exec_info2["error"] or ""

        cand["exec_ok"] = exec_info["ok"]
        cand["exec_error"] = exec_info["error"]
        cand["rows_preview"] = (
            exec_info["rows"][:5] if exec_info["ok"] and exec_info["rows"] else None
        )
        cand["columns"] = exec_info["columns"]

        candidates.append(cand)

        if exec_info["ok"]:
            if (not best_exec) or cand["score"] > best_score:
                best_exec = True
                best_score = cand["score"]
                best = cand
        elif not best_exec and cand["score"] > best_score:
            best_score = cand["score"]
            best = cand

    if best is None and candidates:
        best = candidates[0]

    return {
        "question_original": question,
        "detected_language": detected,
        "question_en": question_en,
        "connection_id": conn_id,
        "schema_summary": schema_str,
        "best_sql": best["sql"],
        "best_exec_ok": best.get("exec_ok", False),
        "best_exec_error": best.get("exec_error"),
        "best_rows_preview": best.get("rows_preview"),
        "best_columns": best.get("columns", []),
        "candidates": candidates,
        "score_percent": normalize_score(best["score"]),
    }


# ======================================================
# 6) Schemas Pydantic
# ======================================================


class UploadResponse(BaseModel):
    connection_id: str
    label: str
    db_path: str
    note: Optional[str] = None


class ConnectionInfo(BaseModel):
    connection_id: str
    label: str
    engine: Optional[str] = None
    db_name: Optional[str] = None  # ya no usamos archivo, pero mantenemos campo


class SchemaResponse(BaseModel):
    connection_id: str
    schema_summary: str
    tables: Dict[str, Dict[str, List[str]]]


class PreviewResponse(BaseModel):
    connection_id: str
    table: str
    columns: List[str]
    rows: List[List[Any]]


class InferRequest(BaseModel):
    connection_id: str
    question: str


class InferResponse(BaseModel):
    question_original: str
    detected_language: str
    question_en: str
    connection_id: str
    schema_summary: str
    best_sql: str
    best_exec_ok: bool
    best_exec_error: Optional[str]
    best_rows_preview: Optional[List[List[Any]]]
    best_columns: List[str]
    candidates: List[Dict[str, Any]]


class SpeechInferResponse(BaseModel):
    transcript: str
    result: InferResponse


# ======================================================
# 7) Helpers para /upload (.sql y .zip)
# ======================================================


def _combine_sql_files_from_zip(zip_bytes: bytes) -> str:
    """
    Lee un ZIP, se queda solo con los .sql y los concatena.
    Orden:
      1) archivos con 'schema' o 'structure' en el nombre
      2) el resto (data, etc.)
    """
    try:
        with zipfile.ZipFile(io.BytesIO(zip_bytes)) as zf:
            names = [info.filename for info in zf.infolist() if not info.is_dir()]
            sql_names = [n for n in names if n.lower().endswith(".sql")]

            if not sql_names:
                raise ValueError("El ZIP no contiene archivos .sql utilizables.")

            def sort_key(name: str) -> int:
                nl = name.lower()
                if "schema" in nl or "structure" in nl:
                    return 0
                return 1

            sql_names_sorted = sorted(sql_names, key=sort_key)

            parts: List[str] = []
            for name in sql_names_sorted:
                with zf.open(name) as f:
                    text = f.read().decode("utf-8", errors="ignore")
                parts.append(f"-- FILE: {name}\n{text}\n")

            return "\n\n".join(parts)
    except zipfile.BadZipFile:
        raise ValueError("Archivo ZIP inválido o corrupto.")


# ======================================================
# 8) Endpoints FastAPI
# ======================================================


@app.on_event("startup")
async def startup_event():
    load_nl2sql_model()
    print("✅ Backend NL2SQL inicializado.")
    print(f"MODEL_DIR={MODEL_DIR}, DEVICE={DEVICE}")
    print(f"Conexiones activas al inicio: {len(sql_manager.connections)}")


@app.post("/upload", response_model=UploadResponse)
async def upload_database(
    mode: str = Form("full"),  # "full" | "schema_data" | "zip"
    db_files: List[UploadFile] = File(...),  # uno o varios archivos
    authorization: Optional[str] = Header(None),
):
    """
    Sube uno o varios archivos SQL/ZIP según el modo:

    - mode = "full":
        * Espera EXACTAMENTE 1 archivo .sql
        * El .sql trae esquema + datos juntos (dump de PostgreSQL)

    - mode = "schema_data":
        * Espera EXACTAMENTE 2 archivos .sql
        * Uno de esquema y otro de datos (el orden lo resolvemos nosotros)

    - mode = "zip":
        * Espera EXACTAMENTE 1 archivo .zip
        * Dentro del zip buscamos SOLO archivos .sql (ignoramos el resto)
    """
    if authorization is None:
        raise HTTPException(401, "Missing Authorization header")

    jwt = authorization.replace("Bearer ", "")
    user = supabase.auth.get_user(jwt)
    if not user or not user.user:
        raise HTTPException(401, "Invalid Supabase token")

    if not db_files:
        raise HTTPException(400, "No se recibió ningún archivo.")

    mode = mode.lower().strip()

    # =======================
    # MODO 1: FULL (.sql único)
    # =======================
    if mode == "full":
        if len(db_files) != 1:
            raise HTTPException(
                400, "Modo FULL requiere exactamente 1 archivo .sql."
            )

        file = db_files[0]
        filename = file.filename or ""
        if not filename.lower().endswith(".sql"):
            raise HTTPException(400, "Modo FULL solo acepta archivos .sql.")

        contents = await file.read()
        sql_text = contents.decode("utf-8", errors="ignore")

    # ====================================
    # MODO 2: ESQUEMA + DATOS (2 archivos)
    # ====================================
    elif mode == "schema_data":
        if len(db_files) != 2:
            raise HTTPException(
                400,
                "Modo esquema+datos requiere exactamente 2 archivos .sql.",
            )

        print("FILES RECEIVED:", [f.filename for f in db_files])

        files_info: List[tuple[str, str]] = []
        for f in db_files:
            fname = f.filename or ""
            if not fname.lower().endswith(".sql"):
                raise HTTPException(400, "Todos los archivos deben ser .sql.")
            contents = await f.read()
            files_info.append(
                (fname, contents.decode("utf-8", errors="ignore"))
            )

        # Intentamos poner primero el esquema y luego los datos
        def weight(name: str) -> int:
            nl = name.lower().replace("-", "_").replace(" ", "_")
        
            if any(x in nl for x in ["schema", "structure", "ddl"]):
                return 0
            if any(x in nl for x in ["data", "dml", "insert", "rows"]):
                return 1
            return 2

        files_info_sorted = sorted(files_info, key=lambda x: weight(x[0]))

        sql_parts: List[str] = []
        for fname, text in files_info_sorted:
            sql_parts.append(f"-- FILE: {fname}\n{text}\n")

        sql_text = "\n\n".join(sql_parts)
        # usamos el nombre del primer archivo como label "principal"
        filename = files_info_sorted[0][0]

    # ==================
    # MODO 3: ZIP (.zip)
    # ==================
    elif mode == "zip":
        if len(db_files) != 1:
            raise HTTPException(
                400, "Modo ZIP requiere exactamente 1 archivo .zip."
            )

        file = db_files[0]
        filename = file.filename or ""
        if not filename.lower().endswith(".zip"):
            raise HTTPException(400, "Modo ZIP solo acepta archivos .zip.")

        contents = await file.read()
        # tu helper ya ignora carpetas y solo concatena .sql
        sql_text = _combine_sql_files_from_zip(contents)

    else:
        raise HTTPException(400, f"Modo no soportado: {mode}")

    # --- crear schema dinámico en Postgres (Neon) ---
    try:
        conn_id = sql_manager.create_database_from_dump(
            label=filename, sql_text=sql_text
        )
    except Exception as e:
        raise HTTPException(400, f"Error creando BD: {e}")

    meta = sql_manager.connections[conn_id]

    # --- guardar metadatos en Supabase (sin romper el upload si falla) ---
    try:
        supabase.table("databases").insert(
            {
                "user_id": user.user.id,
                "filename": filename,
                "engine": meta["engine"],
                "connection_id": conn_id,
            }
        ).execute()
    except Exception as e:
        # Solo logeamos, pero NO rompemos el endpoint
        print("[WARN] No se pudieron guardar metadatos en Supabase:", repr(e))

    return UploadResponse(
        connection_id=conn_id,
        label=filename,
        db_path=f"{meta['engine']}://schema/{meta['schema']}",
        note="Database schema created in Neon and indexed in Supabase.",
    )


@app.get("/connections", response_model=List[ConnectionInfo])
async def list_connections():
    return [
        ConnectionInfo(
            connection_id=cid,
            label=meta.get("label", ""),
            engine=meta.get("engine"),
            db_name=meta.get("schema"),  # usamos schema como "nombre"
        )
        for cid, meta in sql_manager.connections.items()
    ]


@app.get("/schema/{connection_id}", response_model=SchemaResponse)
async def get_schema(connection_id: str):
    if connection_id not in sql_manager.connections:
        raise HTTPException(status_code=404, detail="connection_id no encontrado")

    meta = sql_manager.get_schema(connection_id)
    tables = meta["tables"]

    parts = []
    for t, info in tables.items():
        cols = info.get("columns", [])
        parts.append(f"{t}(" + ", ".join(cols) + ")")
    schema_str = " ; ".join(parts) if parts else "(empty_schema)"

    return SchemaResponse(
        connection_id=connection_id,
        schema_summary=schema_str,
        tables=tables,
    )


@app.get("/preview/{connection_id}/{table}", response_model=PreviewResponse)
async def preview_table(connection_id: str, table: str, limit: int = 20):
    if connection_id not in sql_manager.connections:
        raise HTTPException(status_code=404, detail="connection_id no encontrado")

    try:
        preview = sql_manager.get_preview(connection_id, table, limit)
    except Exception as e:
        raise HTTPException(
            status_code=400, detail=f"Error al leer tabla '{table}': {e}"
        )

    return PreviewResponse(
        connection_id=connection_id,
        table=table,
        columns=preview["columns"],
        rows=preview["rows"],
    )


@app.post("/infer", response_model=InferResponse)
async def infer_sql(
    req: InferRequest,
    authorization: Optional[str] = Header(None),
):
    if authorization is None:
        raise HTTPException(401, "Missing Authorization header")

    jwt = authorization.replace("Bearer ", "")
    user = supabase.auth.get_user(jwt)
    if not user or not user.user:
        raise HTTPException(401, "Invalid Supabase token")

    result = nl2sql_with_rerank(req.question, req.connection_id)
    score = normalize_score(result["candidates"][0]["score"])

    db_row = (
        supabase.table("databases")
        .select("id")
        .eq("connection_id", req.connection_id)
        .eq("user_id", user.user.id)
        .execute()
    )
    db_id = db_row.data[0]["id"] if db_row.data else None

    supabase.table("queries").insert(
        {
            "user_id": user.user.id,
            "db_id": db_id,
            "nl": result["question_original"],
            "sql_generated": result["best_sql"],
            "sql_repaired": result["candidates"][0]["sql"],
            "execution_ok": result["best_exec_ok"],
            "error": result["best_exec_error"],
            "rows_preview": result["best_rows_preview"],
            "score": score,
        }
    ).execute()

    result["score_percent"] = score
    return InferResponse(**result)


@app.post("/speech-infer", response_model=SpeechInferResponse)
async def speech_infer(
    connection_id: str = Form(...),
    audio: UploadFile = File(...),
):
    if openai_client is None:
        raise HTTPException(
            status_code=500,
            detail="OPENAI_API_KEY no está configurado en el backend.",
        )

    if audio.content_type is None:
        raise HTTPException(status_code=400, detail="Archivo de audio inválido.")

    try:
        with tempfile.NamedTemporaryFile(delete=False, suffix=".webm") as tmp:
            tmp.write(await audio.read())
            tmp_path = tmp.name
    except Exception:
        raise HTTPException(
            status_code=500, detail="No se pudo procesar el audio recibido."
        )

    try:
        with open(tmp_path, "rb") as f:
            transcription = openai_client.audio.transcriptions.create(
                model="gpt-4o-transcribe",
                file=f,
            )
        transcript_text: str = transcription.text
    except Exception as e:
        raise HTTPException(status_code=500, detail=f"Error al transcribir audio: {e}")

    result_dict = nl2sql_with_rerank(transcript_text, connection_id)
    infer_result = InferResponse(**result_dict)

    return SpeechInferResponse(
        transcript=transcript_text,
        result=infer_result,
    )


@app.get("/health")
async def health():
    return {
        "status": "ok",
        "model_loaded": t5_model is not None,
        "connections": len(sql_manager.connections),
        "device": str(DEVICE),
        "engine": "postgres",
    }


@app.get("/history")
def get_history(authorization: Optional[str] = Header(None)):
    if authorization is None:
        raise HTTPException(401, "Missing Authorization")

    jwt = authorization.replace("Bearer ", "")
    user = supabase.auth.get_user(jwt)

    rows = (
        supabase.table("queries")
        .select("*")
        .eq("user_id", user.user.id)
        .order("created_at", desc=True)
        .execute()
    )

    return rows.data


@app.get("/my-databases")
def get_my_databases(authorization: Optional[str] = Header(None)):
    if authorization is None:
        raise HTTPException(401, "Missing Authorization")

    jwt = authorization.replace("Bearer ", "")
    user = supabase.auth.get_user(jwt)

    rows = (
        supabase.table("databases")
        .select("*")
        .eq("user_id", user.user.id)
        .execute()
    )

    return rows.data


@app.get("/")
async def root():
    return {
        "message": "NL2SQL T5-large backend running.",
        "endpoints": [
            "POST /upload            (subir .sql o .zip con .sql → crea schema en Supabase)",
            "GET  /connections       (listar BDs subidas en esta instancia)",
            "GET  /schema/{id}       (esquema resumido)",
            "GET  /preview/{id}/{t}  (preview de tabla)",
            "POST /infer             (NL→SQL + ejecución en BD)",
            "POST /speech-infer      (voz → NL→SQL + ejecución)",
            "GET  /history           (historial de consultas en Supabase)",
            "GET  /my-databases      (BDs del usuario en Supabase)",
            "GET  /health            (estado del backend)",
            "GET  /docs              (OpenAPI UI)",
        ],
    }