File size: 49,394 Bytes
12eff8e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Gradio event handlers for MorphSQL."""

from __future__ import annotations

import json
import re
import tempfile
import zipfile
from pathlib import Path

import plotly.graph_objects as go

from morphsql.assistant.copilot import MigrationCopilot
from morphsql.dbt_generator.decomposer import decompose_to_dbt, format_dbt_project, is_dbt_target
from morphsql.intelligence.lineage_viz import lineage_to_plotly
from morphsql.intelligence.rationalization import generate_rationalization
from morphsql.intelligence.runbook import generate_executive_summary, generate_runbook
from morphsql.lineage.builder import build_lineage_graph
from morphsql.models import Dialect, MigrationObject, MigrationReport, ObjectType
from morphsql.pipeline import MigrationPipeline
from morphsql.translator.engine import translate_sql
from morphsql.translator.pandas_codegen import is_pandas_target
from morphsql.translator.pyspark_codegen import is_pyspark_target
from demo.theme import C_MUTED, C_PANEL, C_TEXT

EXAMPLES_DIR = Path(__file__).parent.parent / "examples" / "vertica_legacy"
_copilot = MigrationCopilot()


def _sanitize_project(name: str) -> str:
    return re.sub(r"[^a-zA-Z0-9_]", "_", name.lower()).strip("_") or "migration_project"


def _apply_dark_layout(fig: go.Figure, height: int = 260) -> go.Figure:
    fig.update_layout(
        template="plotly_dark",
        height=height,
        margin=dict(t=40, b=40, l=30, r=20),
        paper_bgcolor=C_PANEL,
        plot_bgcolor=C_PANEL,
        font={"color": C_TEXT, "family": "Inter, system-ui, sans-serif"},
        xaxis={"gridcolor": "#334155", "zerolinecolor": "#334155", "color": C_TEXT},
        yaxis={"gridcolor": "#334155", "zerolinecolor": "#334155", "color": C_TEXT},
    )
    return fig


def _risk_gauge(score: int) -> go.Figure:
    score = max(0, min(100, int(score or 0)))
    color = "#22c55e" if score < 30 else "#eab308" if score < 60 else "#ef4444"
    label = "Low" if score < 30 else "Medium" if score < 60 else "High"
    fig = go.Figure(
        go.Indicator(
            mode="gauge+number",
            value=score,
            number={"suffix": " / 100", "font": {"color": C_TEXT, "size": 28}},
            title={"text": f"Portfolio risk Β· {label}", "font": {"color": C_MUTED, "size": 13}},
            gauge={
                "axis": {"range": [0, 100], "tickcolor": C_MUTED, "tickfont": {"color": C_MUTED}},
                "bar": {"color": color, "thickness": 0.8},
                "bgcolor": "#0f172a",
                "bordercolor": "#334155",
                "steps": [
                    {"range": [0, 30], "color": "#14532d"},
                    {"range": [30, 60], "color": "#713f12"},
                    {"range": [60, 100], "color": "#7f1d1d"},
                ],
            },
        )
    )
    return _apply_dark_layout(fig, height=280)


def _distribution_chart(report: MigrationReport) -> go.Figure:
    d = report.dashboard
    fig = go.Figure(
        go.Bar(
            x=["Auto-migrate", "Review", "Redesign", "Retire"],
            y=[
                d.auto_migratable,
                d.requires_review,
                d.requires_redesign,
                d.recommended_retirement,
            ],
            marker_color=["#22c55e", "#eab308", "#ef4444", "#64748b"],
            text=[
                d.auto_migratable,
                d.requires_review,
                d.requires_redesign,
                d.recommended_retirement,
            ],
            textposition="outside",
            textfont={"color": C_TEXT, "size": 14},
        )
    )
    fig.update_layout(title={"text": "Object distribution", "font": {"color": C_MUTED, "size": 13}})
    return _apply_dark_layout(fig, height=280)


def _distribution_chart_empty() -> go.Figure:
    fig = go.Figure(
        go.Bar(
            x=["Auto-migrate", "Review", "Redesign", "Retire"],
            y=[0, 0, 0, 0],
            marker_color=["#22c55e", "#eab308", "#ef4444", "#64748b"],
            text=[0, 0, 0, 0],
            textposition="outside",
            textfont={"color": C_TEXT},
        )
    )
    fig.update_layout(
        title={"text": "Object distribution (run scan)", "font": {"color": C_MUTED, "size": 13}},
        yaxis={"range": [0, 5]},
    )
    return _apply_dark_layout(fig, height=280)


def figure_to_html(fig: go.Figure) -> str:
    """Reliable Plotly HTML embed for Gradio (avoids blank Plot widgets)."""
    return fig.to_html(
        include_plotlyjs="cdn",
        full_html=False,
        config={"displayModeBar": False, "responsive": True},
    )


def empty_lineage_plot() -> go.Figure:
    return lineage_to_plotly(build_lineage_graph([], Dialect.VERTICA))


def _resolve_repo_path(upload_file, use_sample: bool) -> Path | None:
    if use_sample:
        return EXAMPLES_DIR if EXAMPLES_DIR.exists() else None
    if not upload_file:
        return None

    # Gradio File may return str path, Path, or tempfile-like object
    if isinstance(upload_file, (list, tuple)) and upload_file:
        upload_file = upload_file[0]
    path = Path(str(upload_file.name if hasattr(upload_file, "name") else upload_file))
    if not path.exists():
        return None
    if path.suffix.lower() == ".zip":
        tmp = Path(tempfile.mkdtemp(prefix="morphsql_"))
        with zipfile.ZipFile(path) as zf:
            zf.extractall(tmp)
        return tmp
    if path.is_dir():
        return path
    return None


def _report_to_dict(report: MigrationReport | None) -> dict | None:
    if report is None:
        return None
    return report.model_dump(mode="json")


def _dict_to_report(data: dict | MigrationReport | None) -> MigrationReport | None:
    if data is None:
        return None
    if isinstance(data, MigrationReport):
        return data
    if isinstance(data, dict):
        try:
            return MigrationReport.model_validate(data)
        except Exception:
            return None
    return None


def run_migration_workbench(
    upload_file,
    use_sample: bool,
    source: str,
    target: str,
) -> tuple:
    """Full migration intelligence pipeline for workbench tab."""
    empty_msg = (
        "Enable **Use sample repository** or upload a zip file, "
        "then click **Run migration intelligence**."
    )
    empty_risk = _risk_gauge(0)
    empty_dist = _distribution_chart_empty()
    empty_lineage = empty_lineage_plot()
    empty = (
        empty_msg,
        "No objects yet.",
        "No rationalization yet.",
        "No runbook yet.",
        "No dbt preview yet.",
        "No validation results yet.",
        "Portfolio risk: β€”",
        empty_risk,
        empty_dist,
        empty_lineage,
        json.dumps({"status": "waiting_for_input"}, indent=2),
        None,
    )

    try:
        repo_path = _resolve_repo_path(upload_file, bool(use_sample))
    except Exception as exc:
        return (
            f"Failed to open repository: {exc}",
            *empty[1:],
        )

    if repo_path is None:
        return empty

    try:
        source_d = Dialect(source)
        wants_dbt = is_dbt_target(target)
        if wants_dbt:
            target_d = Dialect.SNOWFLAKE
        elif is_pandas_target(target):
            target_d = Dialect.PANDAS
        elif is_pyspark_target(target):
            target_d = Dialect.PYSPARK
        else:
            target_d = Dialect(target)

        pipeline = MigrationPipeline(source=source_d, target=target_d)
        report = pipeline.analyze(str(repo_path))
        report = pipeline.convert(report)
        report = pipeline.validate(report)
        if wants_dbt:
            report.target_dialect = Dialect.DBT_SNOWFLAKE

        exec_summary = generate_executive_summary(report)
        runbook = generate_runbook(report)
        rationalization = generate_rationalization(report)

        dbt_preview = "No suitable object for dbt decomposition."
        candidates = sorted(report.objects, key=lambda o: o.complexity_score, reverse=True)
        dbt_parts = ["### dbt project preview (Snowflake)\n"]
        shown = 0
        for obj in candidates:
            if obj.object_type.value not in ("stored_procedure", "sql_script", "view"):
                continue
            files = decompose_to_dbt(obj, source_d, project_name=_sanitize_project(obj.name))
            dbt_parts.append(f"#### `{obj.name}` β†’ {len(files)} files\n")
            # Show model SQL files first
            model_files = [p for p in sorted(files) if p.startswith("models/") and p.endswith(".sql")]
            for rel in model_files[:5]:
                dbt_parts.append(f"**{rel}**\n```sql\n{files[rel][:1200]}\n```\n")
            shown += 1
            if shown >= 2:
                break
        if shown:
            dbt_preview = "\n".join(dbt_parts)
        elif not wants_dbt:
            dbt_preview = (
                "Target is not **dbt-snowflake**. "
                "Select **dbt-snowflake** as the target to generate staging / intermediate / mart models, "
                "or open Architecture (dbt) after a dbt-snowflake run."
            )

        val_lines = ["### Validation results\n"]
        passed = sum(1 for r in report.validation_results if r.passed)
        total = len(report.validation_results)
        val_lines.append(f"**{passed}/{total}** checks passed\n")
        for r in report.validation_results[:20]:
            status = "PASS" if r.passed else "FAIL"
            val_lines.append(f"- **[{status}]** {r.object_name} Β· {r.check_name}")
            if r.root_cause:
                val_lines.append(f"  - {r.root_cause}")
            if r.recommendation and not r.passed:
                val_lines.append(f"  - Fix: {r.recommendation}")
        validation_md = "\n".join(val_lines) if total else "No validation checks generated."

        obj_lines = [
            "### Discovered objects\n",
            "| Object | Type | Complexity | Risk | Confidence | Category |",
            "|--------|------|------------|------|------------|----------|",
        ]
        for obj in report.objects:
            obj_lines.append(
                f"| {obj.name} | {obj.object_type.value} | {obj.complexity_score} "
                f"| {obj.risk_level.value} | {obj.conversion_confidence:.0f}% "
                f"| {obj.migration_category.value.replace('_', ' ')} |"
            )
        if not report.objects:
            obj_lines.append("| β€” | β€” | β€” | β€” | β€” | No objects found |")

        metrics_md = (
            f"**Portfolio risk:** {int(report.dashboard.migration_risk_score)} / 100  Β·  "
            f"**Objects:** {report.dashboard.total_objects}  Β·  "
            f"**Auto-migrate:** {report.dashboard.auto_migratable}  Β·  "
            f"**Review:** {report.dashboard.requires_review}  Β·  "
            f"**Redesign:** {report.dashboard.requires_redesign}  Β·  "
            f"**Retire:** {report.dashboard.recommended_retirement}"
        )

        graph = build_lineage_graph(report.objects, source_d)
        lineage_fig = lineage_to_plotly(graph, report.objects)
        risk_fig = _risk_gauge(int(report.dashboard.migration_risk_score))
        dist_fig = _distribution_chart(report)

        export = json.dumps(
            {
                "repository": str(repo_path),
                "source": source,
                "target": target,
                "dashboard": report.dashboard.model_dump(mode="json"),
                "objects": [
                    {
                        "name": o.name,
                        "type": o.object_type.value,
                        "complexity": o.complexity_score,
                        "risk": o.risk_level.value,
                        "confidence": o.conversion_confidence,
                        "category": o.migration_category.value,
                    }
                    for o in report.objects
                ],
            },
            indent=2,
        )

        return (
            exec_summary + "\n\n" + metrics_md,
            "\n".join(obj_lines),
            rationalization,
            runbook,
            dbt_preview,
            validation_md,
            metrics_md,
            risk_fig,
            dist_fig,
            lineage_fig,
            export,
            _report_to_dict(report),
        )
    except Exception as exc:
        return (
            f"**Workbench error:** {type(exc).__name__}: {exc}",
            *empty[1:],
        )


def analyze_sql_object(sql: str, source: str, target: str) -> tuple[str, go.Figure, str, str, str]:
    """Assess + convert a single SQL object.

    Returns: analysis_md, risk_fig, badge, converted_sql_or_dbt, notes_md
    """
    if not (sql or "").strip():
        return (
            "Paste SQL above, then click **Assess & Convert**.",
            _risk_gauge(0),
            "β€”",
            "",
            "Waiting for SQL input.",
        )

    try:
        from morphsql.parser.sql_parser import count_sql_complexity, detect_unsupported_features
        from morphsql.risk.scorer import extract_business_rules, score_object
        from morphsql.validation.reconciliation import generate_incremental_strategy

        source_d = Dialect(source)
        wants_dbt = is_dbt_target(target)
        wants_pandas = is_pandas_target(target)
        wants_pyspark = is_pyspark_target(target)
        if wants_dbt:
            target_d = Dialect.SNOWFLAKE
        elif wants_pandas:
            target_d = Dialect.PANDAS
        elif wants_pyspark:
            target_d = Dialect.PYSPARK
        else:
            target_d = Dialect(target)

        # Infer object type for better dbt decomposition
        obj_type = ObjectType.SQL_SCRIPT
        if re.search(r"\bCREATE\s+(?:OR\s+REPLACE\s+)?PROCEDURE\b", sql, re.I):
            obj_type = ObjectType.STORED_PROCEDURE
        elif re.search(r"\bCREATE\s+(?:OR\s+REPLACE\s+)?VIEW\b", sql, re.I):
            obj_type = ObjectType.VIEW

        score_target = (
            Dialect.SNOWFLAKE if (wants_pandas or wants_pyspark) else target_d
        )
        obj = MigrationObject(name="input_object", object_type=obj_type, source_sql=sql)
        obj = score_object(obj, source_d, score_target)
        complexity = count_sql_complexity(sql, source_d)
        unsupported = detect_unsupported_features(sql, source_d, score_target)
        rules = extract_business_rules(sql)
        incremental = generate_incremental_strategy(sql)

        converted, conf, auto, review = translate_sql(sql, source_d, target_d)
        obj.target_sql = converted
        obj.conversion_confidence = conf
        obj.auto_converted = auto
        obj.requires_review = review

        output_sql = converted
        dbt_note = ""
        if wants_dbt:
            files = decompose_to_dbt(obj, source_d, project_name="input_object")
            output_sql = format_dbt_project(files)
            dbt_note = (
                f"**dbt project generated:** {len(files)} files "
                f"({len(obj.dbt_models)} models) β€” staging / intermediate / marts\n\n"
            )

        lines = [
            "### Object assessment",
            "",
            "| Field | Value |",
            "|-------|-------|",
            f"| Complexity | {obj.complexity_score} / 100 |",
            f"| Risk | {obj.risk_level.value.title()} |",
            f"| Category | {obj.migration_category.value.replace('_', ' ').title()} |",
            f"| Conversion confidence | {conf:.0f}% |",
            f"| Output | {'dbt models (Snowflake)' if wants_dbt else target} |",
            f"| Lines | {complexity.get('lines', 0)} |",
            f"| CTEs | {complexity.get('ctes', 0)} |",
            f"| Joins | {complexity.get('joins', 0)} |",
            f"| Temp tables | {complexity.get('temp_tables', 0)} |",
            f"| Window functions | {complexity.get('window_functions', 0)} |",
            "",
        ]
        if wants_dbt and obj.dbt_models:
            lines += ["**dbt models**", ""] + [f"- `{m}`" for m in obj.dbt_models] + [""]
        if obj.risk_factors:
            lines += ["**Risk factors**", ""] + [f"- {rf.description}" for rf in obj.risk_factors] + [""]
        if unsupported:
            lines += ["**Unsupported syntax**", ""] + [f"- {u}" for u in unsupported] + [""]
        if rules:
            lines += ["**Business rules**", ""] + [f"- {r}" for r in rules[:5]] + [""]
        if incremental:
            lines += ["**Incremental pattern**", ""]
            for k, v in incremental.items():
                lines.append(f"- {k.replace('_', ' ').title()}: {v}")

        notes = [
            f"**Route:** {source} β†’ {target}",
            f"**Confidence:** {conf:.0f}%",
            "",
        ]
        if dbt_note:
            notes.append(dbt_note)
        if auto:
            notes.append("**SQL transformations**")
            notes.extend(f"- {a}" for a in auto[:10])
            notes.append("")
        if review:
            notes.append("**Manual review**")
            notes.extend(f"- {r}" for r in review[:10])

        badge = f"{obj.complexity_score} / 100 Β· {obj.risk_level.value.title()} Β· {conf:.0f}% conf"
        return (
            "\n".join(lines),
            _risk_gauge(obj.complexity_score),
            badge,
            output_sql,
            "\n".join(notes),
        )
    except Exception as exc:
        return (
            f"**Assessment error:** {type(exc).__name__}: {exc}",
            _risk_gauge(0),
            "Error",
            "",
            str(exc),
        )


def get_sample_workbench() -> tuple:
    """Precompute sample workbench outputs so UI is never blank on load."""
    return run_migration_workbench(None, True, "vertica", "snowflake")


def copilot_chat(
    message: str,
    history: list | None,
    report_data: dict | MigrationReport | None,
    sql: str,
    source: str,
    target: str,
) -> tuple[list, str]:
    history = list(history or [])
    if not (message or "").strip():
        return history, ""

    report = _dict_to_report(report_data)
    normalized: list[dict] = []
    for turn in history:
        if isinstance(turn, dict) and "role" in turn and "content" in turn:
            normalized.append({"role": turn["role"], "content": str(turn["content"])})
        elif isinstance(turn, (list, tuple)) and len(turn) == 2:
            if turn[0]:
                normalized.append({"role": "user", "content": str(turn[0])})
            if turn[1]:
                normalized.append({"role": "assistant", "content": str(turn[1])})

    reply = _copilot.respond(message.strip(), normalized, report, sql or "", source, target)
    normalized.append({"role": "user", "content": message.strip()})
    normalized.append({"role": "assistant", "content": reply})
    return normalized, ""


def report_to_context(report_data: dict | MigrationReport | None) -> str:
    report = _dict_to_report(report_data)
    if report is None:
        return (
            "No repository scan loaded yet. "
            "Open Migration Workbench, run a scan, then return here β€” "
            "the copilot will use that context."
        )
    ctx = _copilot.build_context(report)
    # Avoid markdown code fences that can go white-on-white in Gradio themes
    safe = ctx[:2500].replace("`", "'")
    return f"### Active scan context\n\n{safe}"


HERO_EXAMPLE = (
    "SELECT customer_id, COALESCE(order_amount, 0) AS order_amount, "
    "COALESCE(discount, 0) AS discount FROM staging.orders "
    "WHERE order_date >= CURRENT_DATE - 30"
)

FEATURE_SQL_PATH = Path(__file__).parent.parent / "examples" / "ml_features" / "churn_feature_sql.sql"

# Human-readable labels (defined before convert helpers)
# Snowflake first β€” most common warehouse for DS/AI users
SOURCE_LABELS = {
    "snowflake": "Snowflake",
    "bigquery": "BigQuery",
    "redshift": "Redshift",
    "oracle": "Oracle",
    "vertica": "Vertica",
}
TARGET_LABELS = {
    "pandas": "Python (pandas)",
    "pyspark": "Python (PySpark)",
    "snowflake": "Snowflake SQL",
    "bigquery": "BigQuery SQL",
    "dbt-snowflake": "dbt project (Snowflake)",
}
SOURCE_DROPDOWN = [(label, key) for key, label in SOURCE_LABELS.items()]
TARGET_DROPDOWN = [
    ("Python (pandas) β€” recommended for notebooks", "pandas"),
    ("Python (PySpark) β€” Spark DataFrame API", "pyspark"),
    ("Snowflake SQL", "snowflake"),
    ("BigQuery SQL", "bigquery"),
    ("dbt project (Snowflake)", "dbt-snowflake"),
]


def run_hero_agent(sql: str, source: str, target: str) -> tuple[str, str, str, str]:
    """Convert SQL and return (notes_md, output, status, share_md)."""
    from morphsql.risk.scorer import score_object

    sql = sql or HERO_EXAMPLE
    source_d = Dialect(source)
    wants_dbt = is_dbt_target(target)
    wants_pandas = is_pandas_target(target)
    wants_pyspark = is_pyspark_target(target)
    if wants_dbt:
        target_d = Dialect.SNOWFLAKE
    elif wants_pandas:
        target_d = Dialect.PANDAS
    elif wants_pyspark:
        target_d = Dialect.PYSPARK
    else:
        target_d = Dialect(target)

    converted, conf, auto, review = translate_sql(sql, source_d, target_d)
    score_target = (
        Dialect.SNOWFLAKE if (wants_pandas or wants_pyspark) else target_d
    )
    obj = MigrationObject(
        name="hero_query",
        object_type=ObjectType.SQL_SCRIPT,
        source_sql=sql,
        target_sql=converted,
    )
    obj = score_object(obj, source_d, score_target)

    output = converted
    if wants_dbt:
        files = decompose_to_dbt(obj, source_d, project_name="morphsql_dbt")
        output = format_dbt_project(files, max_files=12)

    source_label = SOURCE_LABELS.get(source, source)
    target_label = TARGET_LABELS.get(target, target)
    if wants_pandas:
        kind = "Python (pandas)"
        next_steps = [
            "1. Review the generated Python on the right.",
            "2. Check the **sample preview** (synthetic tables).",
            "3. Download the `.py` file or open the notebook starter cell.",
            "4. Point `tables['…']` at your real DataFrames (`read_parquet` / `read_sql`).",
        ]
    elif wants_pyspark:
        kind = "Python (PySpark)"
        next_steps = [
            "1. Review the generated PySpark code on the right.",
            "2. Check the **sample preview** (same query logic on synthetic tables).",
            "3. Download the `.py` file or open the Spark notebook starter.",
            "4. Provide `tables['…']` or replace with `spark.table` / `spark.read`.",
        ]
    elif wants_dbt:
        kind = "dbt project"
        next_steps = [
            "1. Copy the generated dbt files.",
            "2. Check the **sample preview** for query-shape sanity.",
            "3. Drop models into a dbt project and run `dbt run`.",
            "4. Review models marked for manual checks.",
        ]
    else:
        kind = f"{target_label} SQL"
        next_steps = [
            "1. Copy the converted SQL on the right.",
            "2. Check the **sample preview** (same query logic on synthetic tables).",
            "3. Run the SQL on the target warehouse.",
            "4. Check any items under Needs review.",
        ]

    changes: list[str] = []
    for a in auto[:8]:
        if any(
            k in a
            for k in (
                "β†’",
                "Removed",
                "Converted",
                "JOIN",
                "WHERE",
                "GROUP",
                "SELECT",
                "pandas",
                "Dialect",
                "FROM dual",
            )
        ):
            changes.append(f"- {a}")

    notes = [
        f"### {source_label} β†’ {target_label}",
        "",
        f"**{conf:.0f}%** confidence Β· output: **{kind}**",
        "",
        "**Next steps**",
        *next_steps,
        "",
    ]
    if changes:
        notes.append("**What changed**")
        notes.extend(changes)
        notes.append("")
    if review:
        notes.append("**Needs review**")
        notes.extend(f"- {r}" for r in review[:5])
        notes.append("")

    status = f"{conf:.0f}% Β· {kind}"
    space_url = "https://huggingface.co/spaces/dgvj-work/morphsql"
    share = (
        f"MorphSQL converted **{source_label} β†’ {target_label}** "
        f"({conf:.0f}% confidence).\n\n"
        f"[Open Space]({space_url}) Β· "
        f"[GitHub](https://github.com/dgvj-work/morphsql)"
    )
    return "\n".join(notes), output, status, share


def _infer_columns_from_sql(sql: str) -> list[str]:
    cols: list[str] = []
    # AS aliases
    cols.extend(re.findall(r"\bAS\s+([A-Za-z_][\w]*)", sql or "", flags=re.I))
    # bare identifiers after SELECT / commas (best-effort)
    cols.extend(re.findall(r"(?:SELECT|,)\s*(?:[\w.]+\.)?([A-Za-z_][\w]*)\b", sql or "", flags=re.I))
    # common warehouse columns used in demos
    cols.extend(
        [
            "customer_id",
            "user_id",
            "order_amount",
            "discount",
            "order_date",
            "amount",
            "name",
            "id",
            "a",
            "b",
            "x",
            "dt",
            "dept",
            "event_value",
            "event_ts",
            "event_type",
            "value",
            "score",
        ]
    )
    # de-dupe preserving order, drop SQL keywords
    skip = {
        "select",
        "from",
        "where",
        "group",
        "by",
        "order",
        "limit",
        "as",
        "and",
        "or",
        "on",
        "join",
        "left",
        "right",
        "inner",
        "outer",
        "case",
        "when",
        "then",
        "else",
        "end",
        "null",
        "not",
        "is",
        "in",
        "distinct",
        "count",
        "sum",
        "avg",
        "min",
        "max",
        "coalesce",
        "nvl",
        "zeroifnull",
        "ifnull",
        "current_date",
        "sysdate",
        "getdate",
    }
    out: list[str] = []
    for c in cols:
        if c.lower() in skip:
            continue
        if c not in out:
            out.append(c)
    return out[:24]


def build_sample_tables(code: str, sql: str = "") -> dict:
    """Build tiny demo DataFrames so generated pandas can execute in the Space."""
    import pandas as pd

    keys = list(dict.fromkeys(re.findall(r"tables\[['\"]([^'\"]+)['\"]\]", code or "")))
    if not keys and sql:
        # Fallback: FROM / JOIN identifiers in the source SQL
        keys = list(
            dict.fromkeys(
                re.findall(
                    r"(?:FROM|JOIN)\s+([A-Za-z_][\w]*(?:\.[A-Za-z_][\w]*)?)",
                    sql,
                    flags=re.I,
                )
            )
        )
        keys = [k for k in keys if k.lower() not in {"dual", "select"}]
    cols = _infer_columns_from_sql(sql)
    # Include columns referenced in generated code (e.g. procedure params used as cols)
    for c in re.findall(r"\[['\"]([^'\"]+)['\"]\]", code or ""):
        if "." in c or c in keys:
            continue  # table keys like staging.orders
        if c not in cols:
            cols.append(c)
    if not cols:
        cols = ["id", "value"]

    n = 5
    today = pd.Timestamp.today().normalize()
    frames: dict = {}
    for key in keys:
        data: dict = {}
        for i, col in enumerate(cols):
            cl = col.lower()
            if any(t in cl for t in ("date", "ts", "time", "dt")):
                data[col] = [today - pd.Timedelta(days=j) for j in range(n)]
            elif any(t in cl for t in ("id", "count", "nunique", "flag", "label")):
                data[col] = list(range(1, n + 1))
            elif any(t in cl for t in ("amount", "value", "score", "avg", "sum", "discount")):
                data[col] = [None if j == 0 else float(10 * (j + 1)) for j in range(n)]
            elif any(t in cl for t in ("name", "type", "dept")):
                data[col] = [f"item_{j}" for j in range(n)]
            else:
                data[col] = [j + 1 for j in range(n)]
        frames[key] = pd.DataFrame(data)
    return frames


def _preview_code_for_target(output: str, target: str, sql: str, source: str) -> tuple[str, str]:
    """
    Return (python_code, via_label) used to produce a sample DataFrame preview.

    Pandas targets execute the generated code. All other targets reuse the same
    source SQL β†’ pandas path so preview works without Spark / a warehouse.
    """
    from morphsql.translator.pandas_codegen import sql_to_pandas

    code = output or ""
    if is_pandas_target(target) and "import pandas" in code:
        return code, "generated pandas"

    if not (sql or "").strip():
        return "", ""

    try:
        source_d = Dialect(source)
    except ValueError:
        source_d = Dialect.SNOWFLAKE

    preview_code, _conf, _auto, _review = sql_to_pandas(sql, source_d)
    label = TARGET_LABELS.get(target, target)
    return preview_code, f"pandas runtime Β· same query logic (output above is {label})"


def run_sample_preview(
    output: str,
    target: str,
    sql: str = "",
    source: str = "snowflake",
) -> tuple:
    """
    Execute query logic against synthetic tables for a sample DataFrame preview.

    Works for every Convert target: pandas runs the generated code; PySpark / SQL /
    dbt reuse a pandas translation of the source SQL so the Space can show rows
    without Spark or a warehouse connection.
    Returns (dataframe_or_none, note_md).
    """
    import pandas as pd

    preview_code, via = _preview_code_for_target(output, target, sql, source)
    if not preview_code or "import pandas" not in preview_code:
        return None, "_Could not build a sample preview for this input._"

    tables = build_sample_tables(preview_code, sql)
    if not tables and "tables[" in preview_code:
        return None, "_Could not infer input tables for a live preview._"
    if not tables:
        tables = {}

    ns: dict = {"pd": pd, "np": __import__("numpy"), "tables": tables}
    try:
        exec(preview_code, ns, ns)  # noqa: S102 β€” intentional demo sandbox for generated code
    except Exception as exc:
        return None, f"_Preview could not run automatically:_ `{exc}`"

    result = ns.get("result")
    if not isinstance(result, pd.DataFrame):
        # Procedures may leave the last SELECT under result_N
        for key, val in reversed(list(ns.items())):
            if key.startswith("result") and isinstance(val, pd.DataFrame):
                result = val
                break
    if not isinstance(result, pd.DataFrame):
        return None, "_Preview ran, but no `result` DataFrame was produced._"

    note = (
        f"**Sample preview** Β· {via} on {len(tables)} synthetic table(s) "
        f"β†’ `{result.shape[0]}` rows Γ— `{result.shape[1]}` cols. "
        "Replace inputs with your real data when you run the converted output."
    )
    return result.head(20), note


def write_output_download(
    output: str,
    target: str,
    stem: str | None = None,
) -> str:
    """Write converted output to a downloadable temp file; return path."""
    safe = re.sub(r"[^\w.\-]+", "_", (stem or "morphsql").strip())[:80] or "morphsql"
    if is_pandas_target(target):
        suffix, kind = ".py", "pandas"
    elif is_pyspark_target(target):
        suffix, kind = ".py", "pyspark"
    elif is_dbt_target(target):
        suffix, kind = ".txt", "dbt"
    else:
        suffix, kind = ".sql", str(target).replace("-", "_")
    path = Path(tempfile.gettempdir()) / f"{safe}_{kind}{suffix}"
    # Avoid collisions when converting multiple files in one session
    if path.exists():
        path = Path(tempfile.gettempdir()) / f"{safe}_{kind}_{abs(hash(output)) % 100_000}{suffix}"
    path.write_text(output or "", encoding="utf-8")
    return str(path)


def _resolve_upload_path(upload_file) -> Path | None:
    if not upload_file:
        return None
    if isinstance(upload_file, (list, tuple)) and upload_file:
        upload_file = upload_file[0]
    path = Path(str(getattr(upload_file, "name", upload_file)))
    return path if path.exists() else None


def _collect_sql_files(upload_file) -> list[Path]:
    """Return SQL/text files from an upload (.sql/.txt or .zip of those)."""
    path = _resolve_upload_path(upload_file)
    if path is None:
        return []
    suffix = path.suffix.lower()
    if suffix in {".sql", ".txt", ".ddl", ".prc", ".pkb", ".pks"}:
        return [path]
    if suffix == ".zip":
        tmp = Path(tempfile.mkdtemp(prefix="morphsql_upload_"))
        with zipfile.ZipFile(path) as zf:
            zf.extractall(tmp)
        files = sorted(
            p
            for p in tmp.rglob("*")
            if p.is_file() and p.suffix.lower() in {".sql", ".txt", ".ddl", ".prc"}
        )
        return files
    # Unknown extension β€” try reading as text SQL
    return [path]


def load_sql_from_upload(upload_file, current_sql: str = "") -> str:
    """Load uploaded SQL into the input box (first file if a zip). Keeps current text if empty."""
    files = _collect_sql_files(upload_file)
    if not files:
        return current_sql or ""
    try:
        return files[0].read_text(encoding="utf-8", errors="replace")
    except OSError:
        return current_sql or ""


def convert_upload_for_ui(upload_file, sql: str, source: str, target: str):
    """
    Convert pasted SQL or an uploaded .sql/.txt/.zip.

    Single file β†’ normal Convert payload with a named download.
    Zip with multiple SQL files β†’ convert each and return a zip download.
    Returns: (sql_in, notes, output, status, share, preview, download, notebook, api)
    """
    files = _collect_sql_files(upload_file)

    if not files:
        text = (sql or "").strip()
        if not text:
            empty = convert_for_ui("", source, target)
            return ("", *empty)
        notes, output, status, share, preview, download, nb, api = convert_for_ui(
            text, source, target
        )
        return text, notes, output, status, share, preview, download, nb, api

    if len(files) == 1:
        text = files[0].read_text(encoding="utf-8", errors="replace")
        notes, output, status, share, preview, _old_dl, nb, api = convert_for_ui(
            text, source, target
        )
        download = write_output_download(output, target, stem=files[0].stem)
        note = (
            f"\n**Upload** Β· converted `{files[0].name}` β†’ download "
            f"`{Path(download).name}`.\n"
        )
        return text, notes + note, output, status, share, preview, download, nb, api

    # Multi-file zip β†’ convert each and package
    out_dir = Path(tempfile.mkdtemp(prefix="morphsql_batch_"))
    converted_paths: list[Path] = []
    previews = []
    combined_notes = [
        f"### Batch upload Β· {len(files)} SQL file(s) β†’ **{TARGET_LABELS.get(target, target)}**",
        "",
    ]
    first_output = ""
    first_sql = ""
    statuses: list[str] = []

    for fp in files:
        try:
            text = fp.read_text(encoding="utf-8", errors="replace")
        except OSError as exc:
            combined_notes.append(f"- `{fp.name}`: read error ({exc})")
            continue
        if not first_sql:
            first_sql = text
        notes, output, status, _share, preview, _dl, _nb, _api = convert_for_ui(
            text, source, target
        )
        if not first_output:
            first_output = output
            if preview is not None:
                previews.append(preview)
        if is_pandas_target(target):
            dest = out_dir / f"{fp.stem}_pandas.py"
        elif is_pyspark_target(target):
            dest = out_dir / f"{fp.stem}_pyspark.py"
        elif is_dbt_target(target):
            dest = out_dir / f"{fp.stem}_dbt.txt"
        else:
            dest = out_dir / f"{fp.stem}_{target.replace('-', '_')}.sql"
        dest.write_text(output or "", encoding="utf-8")
        converted_paths.append(dest)
        statuses.append(status)
        combined_notes.append(f"- `{fp.name}` β†’ `{dest.name}` Β· {status}")

    zip_path = Path(tempfile.gettempdir()) / f"morphsql_batch_{target.replace('-', '_')}.zip"
    with zipfile.ZipFile(zip_path, "w", compression=zipfile.ZIP_DEFLATED) as zf:
        for p in converted_paths:
            zf.write(p, arcname=p.name)

    preview = previews[0] if previews else None
    status = statuses[0] if statuses else "Batch convert"
    share = (
        f"Converted **{len(converted_paths)}** file(s) to `{target}`. "
        f"Download the zip and drop files into your notebook / warehouse / dbt project.\n\n"
        f"[Space](https://huggingface.co/spaces/dgvj-work/morphsql) Β· "
        f"[GitHub](https://github.com/dgvj-work/morphsql)"
    )
    nb = notebook_cell(first_output, target)
    api = hf_pipeline_snippet(first_sql or HERO_EXAMPLE, source, target)
    notes = "\n".join(combined_notes) + (
        f"\n\n**Download** the zip (`{zip_path.name}`) for all converted files.\n"
    )
    return (
        first_sql,
        notes,
        first_output or "(see zip for all converted files)",
        status,
        share,
        preview,
        str(zip_path),
        nb,
        api,
    )


def notebook_cell(output: str, target: str) -> str:
    """Short copy-paste cell for Jupyter / Colab / Databricks."""
    if is_pyspark_target(target):
        return (
            "# MorphSQL β†’ PySpark (paste into a Databricks / Spark notebook)\n"
            "from pyspark.sql import SparkSession, functions as F, Window\n\n"
            "# spark = SparkSession.builder.getOrCreate()\n"
            "# 1) Provide input frames (or replace with spark.table / spark.read)\n"
            "# tables = {\n"
            "#     'staging.orders': spark.table('staging.orders'),\n"
            "# }\n\n"
            "# 2) Paste the generated MorphSQL code below (or %run the downloaded .py)\n"
            "# 3) Use `result` as your Spark DataFrame\n"
        )
    if not is_pandas_target(target):
        return (
            "# Output is SQL/dbt β€” paste into your warehouse client or dbt project.\n"
            "# Tip: switch Convert to β†’ Python (pandas) or Python (PySpark).\n"
        )
    return (
        "# MorphSQL β†’ pandas (paste into a notebook cell)\n"
        "import pandas as pd\n"
        "import numpy as np\n\n"
        "# 1) Load your real data (parquet / sql / HF datasets)\n"
        "# tables = {\n"
        "#     'staging.orders': pd.read_parquet('orders.parquet'),\n"
        "# }\n\n"
        "# 2) Paste the generated MorphSQL code below (or %run the downloaded .py)\n"
        "# 3) Use `result` as features for sklearn / XGBoost / embeddings pipelines\n"
    )


def hf_pipeline_snippet(sql: str, source: str, target: str) -> str:
    """Hugging Face-style API snippet for AI / ML users."""
    sql_short = (sql or "").strip().replace("\\", "\\\\").replace('"""', "'''")
    if len(sql_short) > 180:
        sql_short = sql_short[:177] + "..."
    return (
        "from morphsql.ai import pipeline\n\n"
        "# Same API style as transformers.pipeline\n"
        'pipe = pipeline("sql-migration")\n'
        "out = pipe(\n"
        f'    """{sql_short}""",\n'
        f'    source="{source}",\n'
        f'    target="{target}",\n'
        ")\n"
        'print(out["converted_sql"][:500])  # pandas code or SQL\n'
        'print(out.get("risk"))             # optional risk head\n'
    )


def convert_for_ui(sql: str, source: str, target: str):
    """Full Convert-tab payload for the Space UI."""
    notes, output, status, share = run_hero_agent(sql, source, target)
    preview, preview_note = run_sample_preview(
        output, target, sql=sql or "", source=source or "snowflake"
    )
    download = write_output_download(output, target)
    nb = notebook_cell(output, target)
    api = hf_pipeline_snippet(sql or HERO_EXAMPLE, source, target)
    if preview_note:
        notes = notes + "\n" + preview_note + "\n"
    return notes, output, status, share, preview, download, nb, api


PLAYGROUND_EXAMPLE_LABELS = [
    "DS: Snowflake orders β†’ pandas (fillna / filters)",
    "DS: Snowflake orders β†’ PySpark (F.coalesce / filter)",
    "AI: Feature aggregates β†’ pandas (training features)",
    "DS: Vertica ZEROIFNULL β†’ pandas",
    "DS: Oracle dual constants β†’ pandas",
    "DS: Redshift window slice β†’ pandas",
    "DS: BigQuery null handling β†’ pandas",
    "Warehouse: Vertica β†’ Snowflake SQL",
    "Warehouse: Vertica procedure β†’ dbt",
]

PLAYGROUND_EXAMPLES = [
    [
        "SELECT customer_id, COALESCE(order_amount, 0) AS order_amount, COALESCE(discount, 0) AS discount FROM staging.orders WHERE order_date >= CURRENT_DATE - 30",
        "snowflake",
        "pandas",
    ],
    [
        "SELECT customer_id, COALESCE(order_amount, 0) AS order_amount, COALESCE(discount, 0) AS discount FROM staging.orders WHERE order_date >= CURRENT_DATE - 30",
        "snowflake",
        "pyspark",
    ],
    [
        "SELECT user_id, COUNT(*) AS event_count_90d, SUM(ZEROIFNULL(event_value)) AS value_sum_90d, AVG(ZEROIFNULL(event_value)) AS value_avg_90d FROM staging.product_events WHERE event_ts >= CURRENT_DATE - 90 GROUP BY user_id",
        "vertica",
        "pandas",
    ],
    [
        "SELECT customer_id, ZEROIFNULL(order_amount) AS order_amount, NVL(discount, 0) AS discount FROM staging.orders WHERE order_date >= CURRENT_DATE - 30",
        "vertica",
        "pandas",
    ],
    [
        "SELECT NVL(amount, 0) AS amount, SYSDATE AS ts FROM dual",
        "oracle",
        "pandas",
    ],
    [
        "SELECT GETDATE() AS ts, name FROM users WHERE id > 1 LIMIT 5",
        "redshift",
        "pandas",
    ],
    [
        "SELECT IFNULL(a, 0) AS a, b FROM t WHERE a IS NOT NULL",
        "bigquery",
        "pandas",
    ],
    [
        "SELECT customer_id, ZEROIFNULL(order_amount) AS order_amount FROM staging.orders",
        "vertica",
        "snowflake",
    ],
    [
        "CREATE OR REPLACE PROCEDURE p(load_date DATE) AS $$ BEGIN CREATE LOCAL TEMP TABLE tmp ON COMMIT PRESERVE ROWS AS SELECT customer_id, ZEROIFNULL(amount) AS amount FROM staging.orders WHERE order_date = load_date; INSERT INTO analytics.daily SELECT * FROM tmp; END; $$;",
        "vertica",
        "dbt-snowflake",
    ],
]


def load_playground_example(index: int) -> tuple[str, str, str]:
    """Return (sql, source, target) for a preset example index."""
    idx = max(0, min(int(index), len(PLAYGROUND_EXAMPLES) - 1))
    sql, source, target = PLAYGROUND_EXAMPLES[idx]
    return sql, source, target


def load_and_convert_example(index: int) -> tuple[str, str, str, str, str, str, str]:
    """Load a preset and immediately convert (legacy 7-tuple)."""
    sql, source, target = load_playground_example(index)
    explain, output, badge, share = run_hero_agent(sql, source, target)
    return sql, source, target, explain, output, badge, share


def on_example_selected(label: str | None):
    """Dropdown helper used by the Convert tab (extended UI payload)."""
    if not label or label not in PLAYGROUND_EXAMPLE_LABELS:
        label = PLAYGROUND_EXAMPLE_LABELS[0]
    sql, source, target = load_playground_example(PLAYGROUND_EXAMPLE_LABELS.index(label))
    notes, output, status, share, preview, download, nb, api = convert_for_ui(
        sql, source, target
    )
    return sql, source, target, notes, output, status, share, preview, download, nb, api


AGENT_PROMPTS = [
    (
        "Convert this SQL to pandas",
        PLAYGROUND_EXAMPLES[0][0],
        "snowflake",
        "pandas",
    ),
    (
        "Convert this SQL to PySpark",
        PLAYGROUND_EXAMPLES[1][0],
        "snowflake",
        "pyspark",
    ),
    (
        "Convert feature SQL to pandas",
        PLAYGROUND_EXAMPLES[2][0],
        "vertica",
        "pandas",
    ),
    (
        "Emit a dbt project from this procedure",
        PLAYGROUND_EXAMPLES[8][0],
        "vertica",
        "dbt-snowflake",
    ),
]


def load_agent_example(index: int) -> tuple[str, str, str, str]:
    idx = max(0, min(int(index), len(AGENT_PROMPTS) - 1))
    return AGENT_PROMPTS[idx]


def run_eval_suite(limit: int, category: str) -> tuple[str, str, dict]:
    """Run eval suite and return markdown summary, detail table, metrics dict."""
    from morphsql.eval.metrics import run_eval
    from morphsql.eval.pairs import ensure_pairs_file

    ensure_pairs_file()
    cats = None if category in ("all", "", None) else [category]
    limit_i = int(limit) if limit else 50
    results, summary = run_eval(limit=limit_i, categories=cats)

    lines = [
        "### Eval suite results",
        "",
        f"| Metric | Score |",
        f"|--------|-------|",
        f"| Pairs | {summary['n_pairs']} |",
        f"| Exact match | {100 * summary['exact_match']:.1f}% |",
        f"| Token F1 | {100 * summary['token_f1']:.1f}% |",
        f"| Fuzzy (Dice) | {100 * summary['fuzzy']:.1f}% |",
        f"| Pass rate | {100 * summary['pass_rate']:.1f}% |",
        "",
        "#### By category",
        "",
        "| Category | N | Exact | Token F1 | Pass |",
        "|----------|---|-------|----------|------|",
    ]
    for cat, stats in summary.get("by_category", {}).items():
        lines.append(
            f"| {cat} | {stats['n']} | {100 * stats['exact_match']:.0f}% "
            f"| {100 * stats['token_f1']:.0f}% | {100 * stats['pass_rate']:.0f}% |"
        )

    detail = [
        "### Sample predictions",
        "",
        "| ID | Pass | F1 | Exact |",
        "|----|------|----|-------|",
    ]
    for r in results[:25]:
        detail.append(
            f"| {r.pair_id} | {'yes' if r.passed else 'no'} "
            f"| {100 * r.token_f1:.0f}% | {100 * r.exact_match:.0f}% |"
        )
    return "\n".join(lines), "\n".join(detail), summary


def submit_eval_score(name: str, summary: dict | None) -> str:
    from morphsql.eval.leaderboard import format_leaderboard_md, submit_score

    if not summary or not summary.get("n_pairs"):
        return format_leaderboard_md() + "\n\n_Run the eval suite before submitting._"
    board = submit_score(
        name=name or "anonymous",
        exact_match=summary.get("exact_match", 0),
        token_f1=summary.get("token_f1", 0),
        fuzzy=summary.get("fuzzy", 0),
        pass_rate=summary.get("pass_rate", 0),
        n_pairs=summary.get("n_pairs", 0),
        notes="MorphSQL hybrid translator",
    )
    return format_leaderboard_md(board)


def run_behavior_rag(query: str, source: str, target: str) -> str:
    from morphsql.intelligence.rag import get_rag

    return get_rag().answer(query or "NULL empty string timezone", source, target)


def run_feature_migration(target: str) -> tuple[str, str]:
    """Convert ML feature SQL and optionally emit dbt feature mart."""
    sql = FEATURE_SQL_PATH.read_text(encoding="utf-8") if FEATURE_SQL_PATH.exists() else HERO_EXAMPLE
    wants_dbt = is_dbt_target(target) or target == "dbt-snowflake"
    target_d = Dialect.SNOWFLAKE
    converted, conf, auto, review = translate_sql(sql, Dialect.VERTICA, target_d)
    obj = MigrationObject(
        name="churn_features",
        object_type=ObjectType.SQL_SCRIPT,
        source_sql=sql,
        target_sql=converted,
    )
    if wants_dbt:
        files = decompose_to_dbt(obj, Dialect.VERTICA, project_name="ml_feature_mart")
        out = format_dbt_project(files, max_files=14)
    else:
        out = converted

    md = [
        "### ML / DS feature SQL migration",
        "",
        "Legacy Vertica feature engineering SQL β†’ Snowflake"
        + (" dbt feature mart" if wants_dbt else ""),
        "",
        f"**Confidence:** {conf:.0f}%",
        "",
        "This path is for **data scientists / ML engineers** migrating training-feature SQL "
        "into warehouse-native, versioned dbt models.",
        "",
        "**Transforms**",
    ]
    md.extend(f"- {a}" for a in auto[:10])
    if review:
        md.append("")
        md.append("**Review**")
        md.extend(f"- {r}" for r in review[:6])
    return "\n".join(md), out


def analyze_sql_object_ui(sql: str, source: str, target: str) -> tuple:
    """Gradio-friendly object assess (Plotly β†’ HTML)."""
    analysis, risk_fig, badge, output, notes = analyze_sql_object(sql, source, target)
    return analysis, figure_to_html(risk_fig), badge, output, notes


def run_workbench_ui(upload_file, use_sample: bool, source: str, target: str) -> tuple:
    """Gradio-friendly workbench (Plotly figs β†’ HTML)."""
    (
        summary,
        objects,
        rationalization,
        runbook,
        dbt_preview,
        validation,
        metrics,
        risk_fig,
        dist_fig,
        lineage_fig,
        export,
        report_data,
    ) = run_migration_workbench(upload_file, use_sample, source, target)
    return (
        summary,
        objects,
        rationalization,
        runbook,
        dbt_preview,
        validation,
        metrics,
        figure_to_html(risk_fig) if isinstance(risk_fig, go.Figure) else str(risk_fig),
        figure_to_html(dist_fig) if isinstance(dist_fig, go.Figure) else str(dist_fig),
        figure_to_html(lineage_fig) if isinstance(lineage_fig, go.Figure) else str(lineage_fig),
        export,
        report_data,
    )


def get_leaderboard_md() -> str:
    from morphsql.eval.leaderboard import format_leaderboard_md

    return format_leaderboard_md()