File size: 78,351 Bytes
3f5fadf
e9fdc7c
44e7696
8882332
dddbaa5
3f5fadf
 
a86e220
 
 
 
 
 
 
49293a6
a86e220
 
7f3d172
a86e220
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bd96870
8882332
bd96870
 
8882332
 
bd96870
 
 
 
 
 
 
 
8882332
 
 
 
 
 
 
bd96870
 
 
 
 
8882332
 
 
 
 
 
bd96870
5570429
bd96870
8882332
bd96870
 
8882332
bd96870
 
dddbaa5
bd96870
 
 
5570429
bd96870
5570429
49293a6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a86e220
dddbaa5
8882332
dddbaa5
8882332
dddbaa5
 
 
 
8882332
 
 
dddbaa5
 
 
 
 
 
 
 
00b0f0f
dddbaa5
 
 
 
00b0f0f
dddbaa5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
00b0f0f
 
 
49293a6
dddbaa5
 
 
 
 
 
 
 
 
 
 
 
 
 
8882332
 
 
dddbaa5
00b0f0f
dddbaa5
 
 
 
8882332
dddbaa5
8882332
dddbaa5
 
 
00b0f0f
 
 
dddbaa5
 
00b0f0f
dddbaa5
 
00b0f0f
dddbaa5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
00b0f0f
dddbaa5
00b0f0f
dddbaa5
 
00b0f0f
dddbaa5
00b0f0f
dddbaa5
00b0f0f
 
dddbaa5
00b0f0f
 
 
 
49293a6
 
 
8882332
49293a6
 
 
 
8882332
49293a6
8882332
 
49293a6
8882332
 
49293a6
 
8882332
 
 
 
 
 
 
 
 
 
 
00b0f0f
 
 
 
 
 
 
 
8882332
 
49293a6
8882332
 
 
00b0f0f
8882332
 
 
 
 
00b0f0f
 
dddbaa5
 
 
 
00b0f0f
 
 
 
 
dddbaa5
00b0f0f
 
dddbaa5
 
 
 
 
 
00b0f0f
 
dddbaa5
00b0f0f
 
dddbaa5
00b0f0f
 
 
dddbaa5
 
 
00b0f0f
 
dddbaa5
00b0f0f
 
dddbaa5
00b0f0f
 
 
 
dddbaa5
 
 
 
 
 
 
 
 
 
00b0f0f
8882332
 
dddbaa5
a86e220
8882332
a86e220
8882332
 
 
 
 
a86e220
8882332
 
 
 
 
bd96870
5570429
bd96870
8882332
bd96870
 
 
8882332
bd96870
 
 
 
 
 
8882332
 
bd96870
 
a86e220
dddbaa5
 
 
8882332
 
a86e220
 
8882332
bd96870
5570429
 
8882332
bd96870
 
 
 
 
 
 
 
 
 
 
 
8882332
a86e220
8882332
a86e220
 
8882332
bd96870
5570429
 
8882332
bd96870
49293a6
8882332
 
49293a6
8882332
 
49293a6
8882332
 
49293a6
8882332
a86e220
 
bd96870
 
 
 
 
 
 
8882332
 
 
 
 
 
 
 
 
 
bd96870
 
 
 
8882332
dddbaa5
8882332
 
 
 
 
 
00b0f0f
8882332
a86e220
8882332
a86e220
 
8882332
5570429
 
8882332
a86e220
8882332
 
dddbaa5
a86e220
bd96870
5570429
 
8882332
 
 
 
a86e220
8882332
 
 
 
 
a86e220
8882332
 
 
 
 
 
a86e220
 
8882332
a86e220
 
bd96870
a86e220
 
 
 
8882332
445884d
5570429
 
a86e220
 
 
 
 
5570429
 
a86e220
 
 
 
5570429
 
a86e220
 
 
 
8882332
a86e220
 
 
 
44e7696
5570429
 
a86e220
 
 
 
 
5570429
 
a86e220
 
 
 
5570429
 
 
 
 
a86e220
8882332
 
 
 
 
 
 
a86e220
5570429
a86e220
 
 
 
 
 
 
 
 
 
 
 
ca25698
a86e220
5570429
a86e220
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5570429
 
9779701
8882332
9779701
445884d
 
8882332
 
 
 
 
49293a6
445884d
 
 
8882332
 
 
 
 
49293a6
445884d
 
 
8882332
 
 
 
 
49293a6
9779701
4bedbf4
bd96870
4bedbf4
bd96870
49293a6
8882332
445884d
9f1b72c
445884d
 
 
bd96870
 
 
 
445884d
bd96870
 
 
 
445884d
bd96870
 
 
445884d
bd96870
 
 
445884d
bd96870
 
 
445884d
bd96870
8882332
bd96870
 
 
445884d
 
 
 
 
 
 
 
 
 
0dc832b
 
 
 
 
 
4bedbf4
5570429
dddbaa5
5570429
bd96870
dddbaa5
 
8882332
dddbaa5
8882332
 
 
 
 
 
dddbaa5
8882332
 
 
 
 
 
 
 
 
 
 
 
 
 
dddbaa5
00b0f0f
 
dddbaa5
 
00b0f0f
dddbaa5
 
00b0f0f
dddbaa5
 
 
 
 
 
 
 
 
 
 
 
00b0f0f
 
dddbaa5
 
 
 
 
 
 
 
00b0f0f
 
dddbaa5
00b0f0f
dddbaa5
 
00b0f0f
8882332
00b0f0f
 
 
 
 
 
49293a6
dddbaa5
 
00b0f0f
 
 
 
 
dddbaa5
 
00b0f0f
dddbaa5
00b0f0f
 
dddbaa5
 
 
 
 
 
 
 
 
 
 
 
00b0f0f
dddbaa5
00b0f0f
 
 
 
 
49293a6
00b0f0f
49293a6
 
dddbaa5
00b0f0f
 
 
 
dddbaa5
 
00b0f0f
49293a6
dddbaa5
00b0f0f
8882332
dddbaa5
49293a6
 
 
 
 
 
 
dddbaa5
 
00b0f0f
49293a6
00b0f0f
49293a6
 
 
 
 
 
 
 
 
 
 
dddbaa5
00b0f0f
49293a6
00b0f0f
49293a6
 
 
 
 
 
 
 
 
 
 
dddbaa5
00b0f0f
49293a6
00b0f0f
49293a6
 
8882332
dddbaa5
8882332
 
 
 
 
 
00b0f0f
8882332
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5570429
00b0f0f
dddbaa5
00b0f0f
 
dddbaa5
a24bd74
 
00b0f0f
 
 
 
dddbaa5
 
00b0f0f
a24bd74
 
dddbaa5
 
 
 
 
 
 
 
 
 
 
 
a24bd74
 
 
dddbaa5
 
 
 
 
 
 
 
 
 
 
a24bd74
 
 
00b0f0f
49293a6
00b0f0f
49293a6
00b0f0f
 
 
 
 
dddbaa5
00b0f0f
 
 
 
 
 
 
dddbaa5
 
00b0f0f
 
 
dddbaa5
00b0f0f
 
 
 
dddbaa5
 
 
 
 
00b0f0f
 
 
 
 
 
a24bd74
00b0f0f
 
 
dddbaa5
00b0f0f
 
a24bd74
00b0f0f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dddbaa5
 
 
 
 
 
 
00b0f0f
 
dddbaa5
00b0f0f
dddbaa5
00b0f0f
 
 
 
 
 
 
 
dddbaa5
 
00b0f0f
 
 
dddbaa5
00b0f0f
 
 
 
 
dddbaa5
 
 
 
 
00b0f0f
 
 
 
 
 
dddbaa5
00b0f0f
 
 
dddbaa5
00b0f0f
 
 
a24bd74
00b0f0f
 
 
 
 
 
 
 
 
 
 
 
 
dddbaa5
 
 
 
 
 
 
00b0f0f
 
 
 
 
 
dddbaa5
00b0f0f
 
 
 
 
dddbaa5
00b0f0f
 
 
 
 
dddbaa5
00b0f0f
 
 
a24bd74
dddbaa5
 
 
00b0f0f
 
 
 
 
 
 
dddbaa5
00b0f0f
 
 
 
 
dddbaa5
00b0f0f
 
 
 
 
a24bd74
00b0f0f
 
 
a24bd74
dddbaa5
 
 
00b0f0f
 
 
 
 
a24bd74
dddbaa5
9b137fe
5570429
5aa5b79
 
44e7696
5aa5b79
 
 
445884d
8882332
445884d
44e7696
dddbaa5
5570429
44e7696
859f566
dddbaa5
 
5aa5b79
44e7696
8882332
5aa5b79
44e7696
445884d
5aa5b79
5570429
44e7696
445884d
 
 
 
 
8882332
44e7696
a86e220
44e7696
 
8882332
859f566
a86e220
44e7696
 
8882332
5aa5b79
a86e220
44e7696
 
8882332
5aa5b79
a86e220
44e7696
 
 
8882332
5aa5b79
445884d
8882332
5aa5b79
5570429
5aa5b79
5570429
445884d
 
 
0dc832b
445884d
44e7696
dddbaa5
44e7696
dddbaa5
5aa5b79
445884d
 
 
 
5aa5b79
859f566
dddbaa5
44e7696
 
445884d
44e7696
5aa5b79
 
445884d
bd96870
5570429
dddbaa5
445884d
bd96870
445884d
dddbaa5
 
445884d
dddbaa5
8882332
445884d
8882332
445884d
49293a6
445884d
 
dddbaa5
00b0f0f
 
 
445884d
 
 
00b0f0f
dddbaa5
 
445884d
dddbaa5
 
 
 
 
445884d
 
00b0f0f
445884d
 
 
 
 
 
dddbaa5
 
a4b81cc
445884d
 
 
8882332
 
dddbaa5
8882332
445884d
8882332
 
 
 
 
dddbaa5
 
 
 
 
 
 
 
445884d
 
 
 
 
5570429
 
 
 
 
445884d
a4b81cc
445884d
5570429
445884d
 
 
 
 
 
a4b81cc
 
a86e220
 
 
5570429
a86e220
5570429
a86e220
 
5570429
 
a86e220
 
 
 
5570429
8882332
a86e220
 
 
 
 
 
 
 
 
 
8882332
5570429
a86e220
 
 
 
 
 
5570429
a86e220
 
 
 
 
 
 
 
 
 
 
 
 
8882332
5570429
a86e220
 
 
 
 
 
 
 
 
 
 
 
 
 
dddbaa5
 
 
00b0f0f
dddbaa5
 
 
a86e220
 
 
 
dddbaa5
a86e220
 
00b0f0f
dddbaa5
00b0f0f
dddbaa5
 
a86e220
 
 
 
 
dddbaa5
a86e220
dddbaa5
a86e220
dddbaa5
 
a86e220
 
5570429
 
 
a86e220
 
 
 
 
 
00b0f0f
dddbaa5
 
 
a86e220
 
 
 
00b0f0f
dddbaa5
 
 
a86e220
 
 
00b0f0f
 
dddbaa5
 
 
a86e220
 
 
 
 
 
 
 
 
 
 
 
 
8882332
a86e220
 
8882332
 
a86e220
 
 
bd96870
 
8882332
bd96870
 
 
 
 
 
 
a86e220
 
 
 
 
 
5570429
a86e220
 
00b0f0f
dddbaa5
 
a86e220
 
 
 
 
 
5570429
 
 
a86e220
49293a6
a4b81cc
49293a6
44e7696
49293a6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
445884d
5aa5b79
 
49293a6
 
 
 
 
d265a89
 
a86e220
5570429
a86e220
45b5bdb
a86e220
 
45b5bdb
dddbaa5
a86e220
5570429
dddbaa5
5570429
dddbaa5
 
 
 
 
a86e220
 
be01777
 
a86e220
 
be01777
45b5bdb
a86e220
 
 
8882332
 
a86e220
 
5570429
45b5bdb
a86e220
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
"""
πŸš€ ARF Ultimate Investor Demo v3.8.0 - ENTERPRISE EDITION
MODULAR VERSION - Properly integrated with all components
ULTIMATE FIXED VERSION with all critical issues resolved
NOW WITH TRUE ARF v3.3.7 INTEGRATION AND DYNAMIC SCENARIO METRICS
"""

import logging
import sys
import traceback
import json
import datetime
import asyncio
import time
import random
from pathlib import Path
from typing import Dict, List, Any, Optional, Tuple

# ===========================================
# CONFIGURE LOGGING FIRST
# ===========================================
logging.basicConfig(
    level=logging.INFO,
    format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
    handlers=[
        logging.StreamHandler(sys.stdout),
        logging.FileHandler('arf_demo.log')
    ]
)
logger = logging.getLogger(__name__)

# Add parent directory to path
sys.path.insert(0, str(Path(__file__).parent))

# ===========================================
# ASYNC UTILITIES - ENHANCED VERSION
# ===========================================
class AsyncRunner:
    """Enhanced async runner with better error handling"""
    
    @staticmethod
    def run_async(coro):
        """Run async coroutine in sync context"""
        try:
            loop = asyncio.get_event_loop()
        except RuntimeError:
            loop = asyncio.new_event_loop()
            asyncio.set_event_loop(loop)
        
        try:
            return loop.run_until_complete(coro)
        except Exception as e:
            logger.error(f"Async execution failed: {e}")
            # Return error state instead of crashing
            return {"error": str(e), "status": "failed"}
    
    @staticmethod
    def async_to_sync(async_func):
        """Decorator to convert async function to sync"""
        def wrapper(*args, **kwargs):
            try:
                return AsyncRunner.run_async(async_func(*args, **kwargs))
            except Exception as e:
                logger.error(f"Async to sync conversion failed: {e}")
                # Return a sensible fallback
                return {"error": str(e), "status": "failed"}
        return wrapper

# ===========================================
# SIMPLE SETTINGS
# ===========================================
class Settings:
    """Simple settings class"""
    def __init__(self):
        self.arf_mode = "demo"
        self.use_true_arf = True  # Use true ARF integration
        self.default_scenario = "Cache Miss Storm"
        self.max_history_items = 100
        self.auto_refresh_seconds = 30

settings = Settings()

# ===========================================
# HELPER FUNCTIONS FOR EMPTY STATES
# ===========================================
def create_empty_plot(title: str):
    """Create an empty placeholder plot"""
    import plotly.graph_objects as go
    fig = go.Figure()
    fig.add_annotation(
        text="πŸ‘† Select a scenario<br>to view data",
        xref="paper", yref="paper",
        x=0.5, y=0.5, showarrow=False,
        font=dict(size=14, color="#64748b")
    )
    fig.update_layout(
        height=300,
        title=title,
        paper_bgcolor="rgba(0,0,0,0)",
        plot_bgcolor="rgba(0,0,0,0)",
        xaxis=dict(showgrid=False, zeroline=False, showticklabels=False),
        yaxis=dict(showgrid=False, zeroline=False, showticklabels=False)
    )
    return fig

def create_empty_dashboard():
    """Create empty dashboard"""
    import plotly.graph_objects as go
    fig = go.Figure()
    fig.add_annotation(
        text="πŸ“Š Dashboard will populate<br>after ROI calculation",
        xref="paper", yref="paper",
        x=0.5, y=0.5, showarrow=False,
        font=dict(size=16, color="#64748b")
    )
    fig.update_layout(
        height=700,
        paper_bgcolor="rgba(0,0,0,0)",
        plot_bgcolor="rgba(0,0,0,0)",
        xaxis=dict(showgrid=False, zeroline=False, showticklabels=False),
        yaxis=dict(showgrid=False, zeroline=False, showticklabels=False),
        title=""
    )
    return fig

def get_inactive_agent_html(agent_name: str, description: str):
    """Get HTML for inactive agent state"""
    icons = {
        "Detection": "πŸ•΅οΈβ€β™‚οΈ",
        "Recall": "🧠", 
        "Decision": "🎯"
    }
    
    return f"""
    <div style="border: 2px solid #e2e8f0; border-radius: 14px; padding: 18px; background: #f8fafc; text-align: center; min-height: 180px; display: flex; flex-direction: column; align-items: center; justify-content: center; opacity: 0.7;">
        <div style="font-size: 32px; margin-bottom: 10px; opacity: 0.5;">{icons.get(agent_name, '⏳')}</div>
        <div style="width: 100%;">
            <h4 style="margin: 0 0 8px 0; font-size: 16px; color: #94a3b8;">{agent_name} Agent</h4>
            <p style="font-size: 13px; color: #cbd5e1; margin-bottom: 12px; line-height: 1.4;">{description}</p>
            <div style="display: flex; justify-content: space-around; margin-bottom: 12px;">
                <span style="font-size: 11px; padding: 3px 8px; background: rgba(255, 255, 255, 0.5); border-radius: 6px; color: #cbd5e1; font-weight: 500;">Status: Inactive</span>
            </div>
            <div style="display: inline-block; padding: 5px 14px; background: #e2e8f0; border-radius: 20px; font-size: 12px; font-weight: bold; color: #64748b; text-transform: uppercase; letter-spacing: 0.5px;">WAITING</div>
        </div>
    </div>
    """

# ===========================================
# TRUE ARF 3.3.7 ORCHESTRATOR (UPDATED VERSION)
# ===========================================
class TrueARF337Orchestrator:
    """
    True ARF v3.3.7 orchestrator with:
    - Real OSS package for advisory analysis (agentic-reliability-framework==3.3.7)
    - Enterprise simulation to show upgrade value
    - Clear separation between OSS and Enterprise features
    """
    
    def __init__(self):
        logger.info("TrueARF337Orchestrator initialized with v3.3.7")
        self.true_oss_available = False
        self.enterprise_sim_available = False
        self._initialize_true_components()
    
    def _initialize_true_components(self):
        """Initialize true ARF components"""
        # Try to load true OSS integration
        try:
            from core.true_arf_oss import get_true_arf_oss
            self.true_oss = get_true_arf_oss
            self.true_oss_available = True
            logger.info("βœ… True ARF OSS v3.3.7 component loaded")
        except ImportError as e:
            logger.warning(f"True ARF OSS not available: {e}")
            self.true_oss_available = False
            self.true_oss = None
        
        # Try to load enterprise simulation
        try:
            from core.enterprise_simulation import get_enterprise_simulation
            self.enterprise_sim = get_enterprise_simulation
            self.enterprise_sim_available = True
            logger.info("βœ… Enterprise simulation component loaded")
        except ImportError as e:
            logger.warning(f"Enterprise simulation not available: {e}")
            self.enterprise_sim_available = False
            self.enterprise_sim = None
        
        # Fallback to mock if true components not available
        if not self.true_oss_available:
            logger.info("⚠️ Falling back to enhanced mock ARF")
            self._init_mock_fallback()
    
    def _init_mock_fallback(self):
        """Initialize mock fallback functions with scenario-aware metrics"""
        try:
            from demo.mock_arf import (
                simulate_arf_analysis,
                run_rag_similarity_search,
                create_mock_healing_intent,
                calculate_pattern_confidence
            )
            self._simulate_arf_analysis = simulate_arf_analysis
            self._run_rag_similarity_search = run_rag_similarity_search
            self._create_mock_healing_intent = create_mock_healing_intent
            self._calculate_pattern_confidence = calculate_pattern_confidence
            logger.info("βœ… Enhanced scenario-aware mock ARF functions loaded")
        except ImportError as e:
            logger.error(f"Failed to load mock ARF functions: {e}")
    
    async def analyze_incident(self, scenario_name: str, scenario_data: Dict[str, Any]) -> Dict[str, Any]:
        """
        Analyze an incident using TRUE ARF v3.3.7 when available
        
        This method showcases:
        1. True OSS analysis (if agentic-reliability-framework installed)
        2. Enterprise value proposition simulation
        3. Clear OSS/Enterprise boundary
        """
        logger.info(f"TrueARF337Orchestrator analyzing incident: {scenario_name}")
        
        # Use true OSS if available, otherwise fallback to mock
        if self.true_oss_available and self.true_oss:
            return await self._analyze_with_true_oss(scenario_name, scenario_data)
        else:
            return await self._analyze_with_mock(scenario_name, scenario_data)
    
    async def _analyze_with_true_oss(self, scenario_name: str, scenario_data: Dict[str, Any]) -> Dict[str, Any]:
        """Analyze using true ARF OSS v3.3.7"""
        try:
            # Get true OSS instance
            true_oss_instance = await self.true_oss()
            
            # Run true OSS analysis
            oss_analysis = await true_oss_instance.analyze_scenario(scenario_name, scenario_data)
            
            # Enhance with Enterprise simulation if available
            enterprise_enhancements = None
            if self.enterprise_sim_available and self.enterprise_sim and oss_analysis.get("status") == "success":
                enterprise_instance = await self.enterprise_sim()
                enterprise_enhancements = await enterprise_instance.enhance_oss_analysis(
                    oss_analysis, scenario_name
                )
            
            # Compile results
            result = {
                "status": "success",
                "scenario": scenario_name,
                "arf_version": "3.3.7",
                "true_oss_used": True,
                "enterprise_simulated": self.enterprise_sim_available,
                "oss_analysis": oss_analysis,
                "enterprise_enhancements": enterprise_enhancements,
                "demo_display": {
                    "real_arf_version": "3.3.7",
                    "license": "Apache 2.0 (OSS)",
                    "novel_execution": enterprise_enhancements is not None,
                    "rollback_guarantees": enterprise_enhancements.get("enhancements", {}).get("rollback_guarantees", {}).get("guarantee", "N/A") if enterprise_enhancements else "N/A",
                    "execution_modes": ["advisory", "approval", "autonomous"] if enterprise_enhancements else ["advisory"]
                }
            }
            
            return result
            
        except Exception as e:
            logger.error(f"True OSS analysis failed: {e}", exc_info=True)
            # Fallback to mock
            return await self._analyze_with_mock(scenario_name, scenario_data)
    
    async def _analyze_with_mock(self, scenario_name: str, scenario_data: Dict[str, Any]) -> Dict[str, Any]:
        """Fallback mock analysis with scenario-aware metrics"""
        logger.info(f"Using scenario-aware mock analysis for: {scenario_name}")
        
        try:
            # Add scenario name to data
            scenario_data_with_name = scenario_data.copy()
            scenario_data_with_name["name"] = scenario_name
            
            # Step 1: Detection Agent
            detection_result = self._simulate_arf_analysis(scenario_data_with_name)
            
            # Step 2: Recall Agent  
            similar_incidents = self._run_rag_similarity_search(scenario_data_with_name)
            
            # Step 3: Decision Agent
            confidence = self._calculate_pattern_confidence(scenario_data_with_name, similar_incidents)
            healing_intent = self._create_mock_healing_intent(scenario_data_with_name, similar_incidents, confidence)
            
            # Simulate processing time
            await asyncio.sleep(0.5)
            
            result = {
                "status": "success",
                "scenario": scenario_name,
                "detection": detection_result,
                "recall": similar_incidents,
                "decision": healing_intent,
                "confidence": confidence,
                "processing_time_ms": 450,
                "demo_display": {
                    "real_arf_version": "mock",
                    "license": "N/A",
                    "novel_execution": False,
                    "rollback_guarantees": "N/A",
                    "execution_modes": ["advisory"]
                }
            }
            
            logger.info(f"Scenario-aware mock analysis complete for {scenario_name}")
            return result
            
        except Exception as e:
            logger.error(f"Mock analysis failed: {e}", exc_info=True)
            return {
                "status": "error",
                "message": str(e),
                "scenario": scenario_name
            }
    
    async def execute_healing(self, scenario_name: str, mode: str = "autonomous") -> Dict[str, Any]:
        """Execute healing action (simulated for demo)"""
        # In true OSS, execution is not allowed - only advisory
        # Enterprise simulation shows what would be possible with arf_enterprise
        
        await asyncio.sleep(0.3)
        
        if mode == "advisory":
            return {
                "status": "advisory_only",
                "message": "OSS mode provides recommendations only. Upgrade to Enterprise for execution.",
                "scenario": scenario_name,
                "action": "analysis_complete",
                "requires_enterprise": True,
                "enterprise_features_required": [
                    "autonomous_execution",
                    "novel_execution_protocols",
                    "rollback_guarantees"
                ]
            }
        elif mode == "approval":
            # Simulate Enterprise approval mode
            return {
                "status": "awaiting_approval",
                "message": "Enterprise Approval Mode: Healing intent created, awaiting human approval",
                "scenario": scenario_name,
                "action": "scale_out",
                "approval_required": True,
                "estimated_savings": "$8,500",
                "enterprise_feature": True,
                "requires_license": True
            }
        else:  # autonomous
            # Simulate Enterprise autonomous mode
            return {
                "status": "executed",
                "message": "Enterprise Autonomous Mode: Healing action executed with safety guarantees",
                "scenario": scenario_name,
                "action": "scale_out",
                "execution_time": "12 minutes",
                "cost_saved": "$8,500",
                "rollback_available": True,
                "rollback_guarantee": "STRONG",
                "novel_execution_used": True,
                "execution_mode": "autonomous",
                "enterprise_features_used": [
                    "deterministic_confidence",
                    "novel_execution_protocols",
                    "rollback_guarantees",
                    "business_aware_execution"
                ]
            }

# ===========================================
# IMPORT MODULAR COMPONENTS - UPDATED FOR TRUE ARF
# ===========================================
def import_components() -> Dict[str, Any]:
    """Safely import all components with proper error handling"""
    components = {
        "all_available": False,
        "error": None
    }
    
    try:
        # First, import gradio (always available in Hugging Face Spaces)
        import gradio as gr
        components["gr"] = gr
        
        # Import scenarios
        try:
            from demo.scenarios import INCIDENT_SCENARIOS
            logger.info(f"Loaded {len(INCIDENT_SCENARIOS)} scenarios from demo module")
            components["INCIDENT_SCENARIOS"] = INCIDENT_SCENARIOS
        except ImportError as e:
            logger.warning(f"Demo scenarios not available: {e}")
            # Create minimal fallback
            components["INCIDENT_SCENARIOS"] = {
                "Cache Miss Storm": {
                    "component": "Redis Cache Cluster",
                    "severity": "HIGH",
                    "impact_radius": "85% of users",
                    "business_impact": {"revenue_loss_per_hour": 8500},
                    "detection_time": "45 seconds",
                    "tags": ["cache", "redis", "latency"],
                    "metrics": {"affected_users": 45000}
                }
            }
        
        # Use TrueARF337Orchestrator for true ARF integration
        components["DemoOrchestrator"] = TrueARF337Orchestrator
        logger.info("βœ… Using TrueARF337Orchestrator with real v3.3.7 integration")
        
        # Import ROI calculator
        try:
            from core.calculators import EnhancedROICalculator
            components["EnhancedROICalculator"] = EnhancedROICalculator()
            logger.info("EnhancedROICalculator imported successfully")
        except ImportError as e:
            logger.warning(f"EnhancedROICalculator not available: {e}")
            class MockCalculator:
                def calculate_comprehensive_roi(self, **kwargs):
                    return {
                        "status": "βœ… Calculated Successfully",
                        "summary": {
                            "your_annual_impact": "$1,530,000",
                            "potential_savings": "$1,254,600",
                            "enterprise_cost": "$625,000",
                            "roi_multiplier": "5.2Γ—",
                            "payback_months": "6.0",
                            "annual_roi_percentage": "420%"
                        }
                    }
            components["EnhancedROICalculator"] = MockCalculator()
        
        # Import visualizations
        try:
            from core.visualizations import EnhancedVisualizationEngine
            components["EnhancedVisualizationEngine"] = EnhancedVisualizationEngine()
            logger.info("EnhancedVisualizationEngine imported successfully")
        except ImportError as e:
            logger.warning(f"EnhancedVisualizationEngine not available: {e}")
            class MockVisualizationEngine:
                def create_executive_dashboard(self, data=None):
                    return create_empty_dashboard()
                
                def create_telemetry_plot(self, scenario_name, anomaly_detected=True):
                    return create_empty_plot(f"Telemetry: {scenario_name}")
                
                def create_impact_gauge(self, scenario_name):
                    return create_empty_plot(f"Impact: {scenario_name}")
                
                def create_timeline_comparison(self):
                    return create_empty_plot("Timeline Comparison")
            components["EnhancedVisualizationEngine"] = MockVisualizationEngine()
        
        # Import UI components
        try:
            from ui.components import (
                create_header, create_status_bar, create_tab1_incident_demo,
                create_tab2_business_roi, create_tab3_enterprise_features,
                create_tab4_audit_trail, create_tab5_learning_engine,
                create_footer
            )
            components.update({
                "create_header": create_header,
                "create_status_bar": create_status_bar,
                "create_tab1_incident_demo": create_tab1_incident_demo,
                "create_tab2_business_roi": create_tab2_business_roi,
                "create_tab3_enterprise_features": create_tab3_enterprise_features,
                "create_tab4_audit_trail": create_tab4_audit_trail,
                "create_tab5_learning_engine": create_tab5_learning_engine,
                "create_footer": create_footer,
            })
            logger.info("UI components imported successfully")
        except ImportError as e:
            logger.error(f"UI components not available: {e}")
            # Create minimal UI fallbacks
            components.update({
                "create_header": lambda version="3.3.7", mock=False: gr.HTML(f"<h2>πŸš€ ARF v{version} TRUE</h2>"),
                "create_status_bar": lambda: gr.HTML("<div>Status</div>"),
                "create_tab1_incident_demo": lambda *args: [gr.Dropdown()] * 24,
                "create_tab2_business_roi": lambda *args: [gr.Plot()] * 7,
                "create_tab3_enterprise_features": lambda: [gr.JSON()] * 8,
                "create_tab4_audit_trail": lambda: [gr.Button()] * 6,
                "create_tab5_learning_engine": lambda: [gr.Plot()] * 10,
                "create_footer": lambda: gr.HTML("<footer>ARF v3.3.7</footer>"),
            })
        
        # Import styles
        try:
            from ui.styles import get_styles
            components["get_styles"] = get_styles
        except ImportError as e:
            logger.warning(f"Styles not available: {e}")
            components["get_styles"] = lambda: ""
        
        components["all_available"] = True
        components["error"] = None
        logger.info("βœ… Successfully imported all modular components with True ARF")
        
    except Exception as e:
        logger.error(f"❌ CRITICAL IMPORT ERROR: {e}")
        logger.error(traceback.format_exc())
        components["error"] = str(e)
        components["all_available"] = False
    
    return components

# ===========================================
# GLOBAL COMPONENTS - LAZY LOADED
# ===========================================
_components = None
_audit_manager = None

def get_components() -> Dict[str, Any]:
    """Lazy load components singleton"""
    global _components
    if _components is None:
        _components = import_components()
    return _components

# ===========================================
# AUDIT TRAIL MANAGER - FIXED VERSION
# ===========================================
class AuditTrailManager:
    """Enhanced audit trail manager"""
    
    def __init__(self):
        self.executions = []
        self.incidents = []
        logger.info("AuditTrailManager initialized")
    
    def add_execution(self, scenario: str, mode: str, success: bool = True, savings: float = 0) -> Dict:
        """Add execution to audit trail"""
        entry = {
            "time": datetime.datetime.now().strftime("%H:%M"),
            "scenario": scenario,
            "mode": mode,
            "status": "βœ… Success" if success else "❌ Failed",
            "savings": f"${savings:,.0f}",
            "details": f"{mode} execution at {datetime.datetime.now().isoformat()}"
        }
        self.executions.insert(0, entry)
        return entry
    
    def add_incident(self, scenario: str, severity: str = "HIGH") -> Dict:
        """Add incident to audit trail"""
        entry = {
            "time": datetime.datetime.now().strftime("%H:%M"),
            "scenario": scenario,
            "severity": severity,
            "component": get_components()["INCIDENT_SCENARIOS"].get(scenario, {}).get("component", "unknown"),
            "status": "Analyzed"
        }
        self.incidents.insert(0, entry)
        return entry
    
    def get_execution_table(self) -> List[List]:
        """Get execution table data"""
        return [
            [e["time"], e["scenario"], e["mode"], e["status"], e["savings"], e["details"]]
            for e in self.executions[:10]
        ]
    
    def get_incident_table(self) -> List[List]:
        """Get incident table data"""
        return [
            [e["time"], e["component"], e["scenario"], e["severity"], e["status"]]
            for e in self.incidents[:15]
        ]
    
    def clear(self) -> None:
        """Clear audit trail"""
        self.executions = []
        self.incidents = []

def get_audit_manager() -> AuditTrailManager:
    """Lazy load audit manager singleton"""
    global _audit_manager
    if _audit_manager is None:
        _audit_manager = AuditTrailManager()
    return _audit_manager

# ===========================================
# HELPER FUNCTIONS
# ===========================================
def get_scenario_impact(scenario_name: str) -> float:
    """Get average impact for a given scenario"""
    impact_map = {
        "Cache Miss Storm": 8500,
        "Database Connection Pool Exhaustion": 4200,
        "Kubernetes Memory Leak": 5500,
        "API Rate Limit Storm": 3800,
        "Network Partition": 12000,
        "Storage I/O Saturation": 6800
    }
    return impact_map.get(scenario_name, 5000)

def extract_roi_multiplier(roi_result: Dict) -> float:
    """Extract ROI multiplier from EnhancedROICalculator result"""
    try:
        # Try to get from summary
        if "summary" in roi_result and "roi_multiplier" in roi_result["summary"]:
            roi_str = roi_result["summary"]["roi_multiplier"]
            # Handle format like "5.2Γ—"
            if "Γ—" in roi_str:
                return float(roi_str.replace("Γ—", ""))
            return float(roi_str)
        
        # Try to get from scenarios
        if "scenarios" in roi_result and "base_case" in roi_result["scenarios"]:
            roi_str = roi_result["scenarios"]["base_case"]["roi"]
            if "Γ—" in roi_str:
                return float(roi_str.replace("Γ—", ""))
            return float(roi_str)
        
        # Try direct access
        if "roi_multiplier" in roi_result:
            roi_val = roi_result["roi_multiplier"]
            if isinstance(roi_val, (int, float)):
                return float(roi_val)
        
        return 5.2  # Default fallback
    except Exception as e:
        logger.warning(f"Failed to extract ROI multiplier: {e}, using default 5.2")
        return 5.2

# ===========================================
# VISUALIZATION HELPERS - USING ENHANCED ENGINE
# ===========================================
def create_telemetry_plot(scenario_name: str):
    """Create a telemetry visualization for the selected scenario"""
    try:
        viz_engine = get_components()["EnhancedVisualizationEngine"]
        return viz_engine.create_telemetry_plot(scenario_name, anomaly_detected=True)
    except Exception as e:
        logger.error(f"Failed to create telemetry plot: {e}")
        return create_empty_plot(f"Telemetry: {scenario_name}")

def create_impact_plot(scenario_name: str):
    """Create a business impact visualization"""
    try:
        viz_engine = get_components()["EnhancedVisualizationEngine"]
        return viz_engine.create_impact_gauge(scenario_name)
    except Exception as e:
        logger.error(f"Failed to create impact plot: {e}")
        return create_empty_plot(f"Impact: {scenario_name}")

def create_timeline_plot(scenario_name: str):
    """Create an incident timeline visualization"""
    try:
        viz_engine = get_components()["EnhancedVisualizationEngine"]
        return viz_engine.create_timeline_comparison()
    except Exception as e:
        logger.error(f"Failed to create timeline plot: {e}")
        return create_empty_plot("Timeline Comparison")

# ===========================================
# SCENARIO UPDATE HANDLER
# ===========================================
def update_scenario_display(scenario_name: str) -> tuple:
    """Update all scenario-related displays with scenario-specific data"""
    scenario = get_components()["INCIDENT_SCENARIOS"].get(scenario_name, {})
    impact = scenario.get("business_impact", {})
    metrics = scenario.get("metrics", {})
    
    # Create scenario card HTML
    scenario_html = f"""
    <div style="border: 1px solid #e2e8f0; border-radius: 14px; padding: 20px; background: white; box-shadow: 0 4px 12px rgba(0,0,0,0.05);">
        <div style="display: flex; justify-content: space-between; align-items: center; margin-bottom: 15px; padding-bottom: 12px; border-bottom: 2px solid #f1f5f9;">
            <h3 style="margin: 0; font-size: 18px; color: #1e293b;">🚨 {scenario_name}</h3>
            <span style="padding: 4px 12px; background: linear-gradient(135deg, #ef4444 0%, #dc2626 100%); border-radius: 20px; font-size: 12px; font-weight: bold; color: white; text-transform: uppercase; letter-spacing: 0.5px;">{scenario.get('severity', 'HIGH')}</span>
        </div>
        <div style="margin-top: 15px;">
            <div style="display: flex; justify-content: space-between; align-items: center; margin-bottom: 8px; padding: 4px 0;">
                <span style="font-size: 13px; color: #64748b; font-weight: 500;">Component:</span>
                <span style="font-size: 14px; color: #1e293b; font-weight: 600;">{scenario.get('component', 'Unknown').replace('_', ' ').title()}</span>
            </div>
            <div style="display: flex; justify-content: space-between; align-items: center; margin-bottom: 8px; padding: 4px 0;">
                <span style="font-size: 13px; color: #64748b; font-weight: 500;">Affected Users:</span>
                <span style="font-size: 14px; color: #1e293b; font-weight: 600;">{metrics.get('affected_users', 'Unknown') if 'affected_users' in metrics else 'Unknown'}</span>
            </div>
            <div style="display: flex; justify-content: space-between; align-items: center; margin-bottom: 8px; padding: 4px 0;">
                <span style="font-size: 13px; color: #64748b; font-weight: 500;">Revenue Risk:</span>
                <span style="font-size: 14px; color: #ef4444; font-weight: 700;">${impact.get('revenue_loss_per_hour', 0):,}/hour</span>
            </div>
            <div style="display: flex; justify-content: space-between; align-items: center; margin-bottom: 8px; padding: 4px 0;">
                <span style="font-size: 13px; color: #64748b; font-weight: 500;">Detection Time:</span>
                <span style="font-size: 14px; color: #1e293b; font-weight: 600;">45 seconds (ARF AI)</span>
            </div>
            <div style="display: flex; flex-wrap: wrap; gap: 6px; margin-top: 15px; padding-top: 12px; border-top: 1px solid #f1f5f9;">
                <span style="padding: 3px 8px; background: #f1f5f9; border-radius: 6px; font-size: 11px; color: #475569; font-weight: 500;">{scenario.get('component', 'unknown').split('_')[0] if '_' in scenario.get('component', '') else scenario.get('component', 'unknown')}</span>
                <span style="padding: 3px 8px; background: #f1f5f9; border-radius: 6px; font-size: 11px; color: #475569; font-weight: 500;">{scenario.get('severity', 'high').lower()}</span>
                <span style="padding: 3px 8px; background: #f1f5f9; border-radius: 6px; font-size: 11px; color: #475569; font-weight: 500;">production</span>
                <span style="padding: 3px 8px; background: #f1f5f9; border-radius: 6px; font-size: 11px; color: #475569; font-weight: 500;">incident</span>
            </div>
        </div>
    </div>
    """
    
    # Create visualizations
    telemetry_plot = create_telemetry_plot(scenario_name)
    impact_plot = create_impact_plot(scenario_name)
    timeline_plot = create_timeline_plot(scenario_name)
    
    return (
        scenario_html,
        telemetry_plot,
        impact_plot,
        timeline_plot
    )

# ===========================================
# TRUE ARF ANALYSIS HANDLER - UPDATED VERSION
# ===========================================
@AsyncRunner.async_to_sync
async def run_true_arf_analysis(scenario_name: str):
    """Run true ARF v3.3.7 analysis with OSS + Enterprise simulation"""
    try:
        logger.info(f"Running TRUE ARF analysis for: {scenario_name}")
        
        scenario = get_components()["INCIDENT_SCENARIOS"].get(scenario_name, {})
        
        if not scenario:
            raise ValueError(f"Scenario '{scenario_name}' not found")
        
        # Use TrueARF337Orchestrator
        orchestrator = get_components()["DemoOrchestrator"]()
        analysis = await orchestrator.analyze_incident(scenario_name, scenario)
        
        # Check for errors
        if analysis.get("status") == "error":
            error_msg = analysis.get("message", "Unknown error")
            raise ValueError(f"Analysis failed: {error_msg}")
        
        # Add to audit trail
        get_audit_manager().add_incident(scenario_name, scenario.get("severity", "HIGH"))
        
        # Update incident table
        incident_table_data = get_audit_manager().get_incident_table()
        
        # Extract values from analysis
        demo_display = analysis.get("demo_display", {})
        real_arf_version = demo_display.get("real_arf_version", "mock")
        true_oss_used = analysis.get("true_oss_used", False)
        enterprise_simulated = analysis.get("enterprise_simulated", False)
        
        # Get analysis data based on mode
        if true_oss_used:
            oss_analysis = analysis.get("oss_analysis", {})
            analysis_data = oss_analysis.get("analysis", {})
            
            detection_result = analysis_data.get("detection", {})
            detection_confidence = detection_result.get("confidence", 0.987)
            detection_time_seconds = detection_result.get("detection_time_ms", 45000) / 1000
            
            similar_incidents = analysis_data.get("recall", [])
            similar_count = len(similar_incidents)
            
            decision_data = analysis_data.get("decision", {})
            decision_confidence = decision_data.get("confidence", 0.94)
            success_rate = decision_data.get("historical_success_rate", 0.87)
            
            # Check for enterprise enhancements
            enterprise_enhancements = analysis.get("enterprise_enhancements", {})
            novel_execution = enterprise_enhancements is not None and enterprise_enhancements.get("enterprise_available", False)
            
            if enterprise_enhancements:
                enhancements = enterprise_enhancements.get("enhancements", {})
                rollback_guarantee = enhancements.get("rollback_guarantees", {}).get("guarantee", "N/A")
            else:
                rollback_guarantee = "N/A"
            
            oss_results = {
                "status": "βœ… TRUE ARF OSS Analysis Complete",
                "arf_version": "3.3.7",
                "edition": "OSS (Apache 2.0)",
                "license": "Apache 2.0",
                "scenario": scenario_name,
                "confidence": decision_confidence,
                "novel_execution": novel_execution,
                "rollback_guarantee": rollback_guarantee,
                "agents_executed": ["Detection", "Recall", "Decision"],
                "findings": [
                    f"Anomaly detected with {detection_confidence:.1%} confidence",
                    f"{similar_count} similar incidents found in RAG memory",
                    f"Historical success rate for similar actions: {success_rate:.1%}",
                    f"True ARF OSS package used: βœ… Yes",
                    f"Enterprise features available: {'βœ… Simulated' if enterprise_simulated else '❌ Not installed'}"
                ],
                "recommendations": [
                    "Scale resources based on historical patterns",
                    "Implement circuit breaker pattern",
                    "Add enhanced monitoring for key metrics",
                    f"Rollback guarantee: {rollback_guarantee}",
                    "Upgrade to Enterprise for autonomous execution"
                ],
                "healing_intent": decision_data
            }
        else:
            # Mock fallback
            detection_result = analysis.get("detection", {})
            detection_confidence = detection_result.get("confidence", 0.987)
            detection_time_seconds = detection_result.get("detection_time_seconds", 45)
            
            similar_incidents = analysis.get("recall", [])
            similar_count = len(similar_incidents)
            
            decision_confidence = analysis.get("confidence", 0.94)
            healing_intent = analysis.get("decision", {})
            success_rate = healing_intent.get("success_rate", 0.87)
            
            oss_results = {
                "status": "βœ… OSS Analysis Complete (Enhanced Mock)",
                "arf_version": "mock",
                "scenario": scenario_name,
                "confidence": decision_confidence,
                "agents_executed": ["Detection", "Recall", "Decision"],
                "findings": [
                    f"Anomaly detected with {detection_confidence:.1%} confidence",
                    f"{similar_count} similar incidents found in RAG memory",
                    f"Historical success rate for similar actions: {success_rate:.1%}",
                    f"Detection time: {detection_time_seconds} seconds",
                    f"Install agentic-reliability-framework==3.3.7 for true OSS analysis"
                ],
                "recommendations": [
                    "Scale resources based on historical patterns",
                    "Implement circuit breaker pattern",
                    "Add enhanced monitoring for key metrics",
                    "Install true ARF OSS package for production use"
                ],
                "healing_intent": healing_intent,
                "install_command": "pip install agentic-reliability-framework==3.3.7"
            }
        
        # Update agent status HTML
        detection_html = f"""
        <div style="border: 2px solid #3b82f6; border-radius: 14px; padding: 18px; background: #eff6ff; text-align: center; min-height: 180px; display: flex; flex-direction: column; align-items: center; justify-content: center;">
            <div style="font-size: 32px; margin-bottom: 10px;">πŸ•΅οΈβ€β™‚οΈ</div>
            <div style="width: 100%;">
                <h4 style="margin: 0 0 8px 0; font-size: 16px; color: #1e293b;">Detection Agent</h4>
                <p style="font-size: 13px; color: #475569; margin-bottom: 12px; line-height: 1.4;">Anomaly detected: <strong>{detection_confidence:.1%} confidence</strong></p>
                <div style="display: flex; justify-content: space-around; margin-bottom: 12px;">
                    <span style="font-size: 11px; padding: 3px 8px; background: rgba(255, 255, 255, 0.8); border-radius: 6px; color: #475569; font-weight: 500;">Response: {detection_time_seconds:.1f}s</span>
                    <span style="font-size: 11px; padding: 3px 8px; background: rgba(255, 255, 255, 0.8); border-radius: 6px; color: #475569; font-weight: 500;">True ARF: {'βœ…' if true_oss_used else '⚠️'}</span>
                </div>
                <div style="display: inline-block; padding: 5px 14px; background: linear-gradient(135deg, #10b981 0%, #059669 100%); border-radius: 20px; font-size: 12px; font-weight: bold; color: white; text-transform: uppercase; letter-spacing: 0.5px;">ACTIVE</div>
            </div>
        </div>
        """
        
        recall_html = f"""
        <div style="border: 2px solid #8b5cf6; border-radius: 14px; padding: 18px; background: #f5f3ff; text-align: center; min-height: 180px; display: flex; flex-direction: column; align-items: center; justify-content: center;">
            <div style="font-size: 32px; margin-bottom: 10px;">🧠</div>
            <div style="width: 100%;">
                <h4 style="margin: 0 0 8px 0; font-size: 16px; color: #1e293b;">Recall Agent</h4>
                <p style="font-size: 13px; color: #475569; margin-bottom: 12px; line-height: 1.4;"><strong>{similar_count} similar incidents</strong> found in RAG memory</p>
                <div style="display: flex; justify-content: space-around; margin-bottom: 12px;">
                    <span style="font-size: 11px; padding: 3px 8px; background: rgba(255, 255, 255, 0.8); border-radius: 6px; color: #475569; font-weight: 500;">Recall: 92%</span>
                    <span style="font-size: 11px; padding: 3px 8px; background: rgba(255, 255, 255, 0.8); border-radius: 6px; color: #475569; font-weight: 500;">Patterns: {similar_count}</span>
                </div>
                <div style="display: inline-block; padding: 5px 14px; background: linear-gradient(135deg, #10b981 0%, #059669 100%); border-radius: 20px; font-size: 12px; font-weight: bold; color: white; text-transform: uppercase; letter-spacing: 0.5px;">ACTIVE</div>
            </div>
        </div>
        """
        
        decision_html = f"""
        <div style="border: 2px solid #10b981; border-radius: 14px; padding: 18px; background: #f0fdf4; text-align: center; min-height: 180px; display: flex; flex-direction: column; align-items: center; justify-content: center;">
            <div style="font-size: 32px; margin-bottom: 10px;">🎯</div>
            <div style="width: 100%;">
                <h4 style="margin: 0 0 8px 0; font-size: 16px; color: #1e293b;">Decision Agent</h4>
                <p style="font-size: 13px; color: #475569; margin-bottom: 12px; line-height: 1.4;">Generating healing intent with <strong>{decision_confidence:.1%} confidence</strong></p>
                <div style="display: flex; justify-content: space-around; margin-bottom: 12px;">
                    <span style="font-size: 11px; padding: 3px 8px; background: rgba(255, 255, 255, 0.8); border-radius: 6px; color: #475569; font-weight: 500;">Success Rate: {success_rate:.1%}</span>
                    <span style="font-size: 11px; padding: 3px 8px; background: rgba(255, 255, 255, 0.8); border-radius: 6px; color: #475569; font-weight: 500;">Enterprise: {'βœ…' if enterprise_simulated else '⚠️'}</span>
                </div>
                <div style="display: inline-block; padding: 5px 14px; background: linear-gradient(135deg, #10b981 0%, #059669 100%); border-radius: 20px; font-size: 12px; font-weight: bold; color: white; text-transform: uppercase; letter-spacing: 0.5px;">ACTIVE</div>
            </div>
        </div>
        """
        
        logger.info(f"Analysis completed successfully for {scenario_name} (True ARF: {real_arf_version})")
        return (
            detection_html, recall_html, decision_html,
            oss_results, incident_table_data
        )
        
    except Exception as e:
        logger.error(f"Analysis failed: {e}", exc_info=True)
        
        # Return error state with proper HTML
        error_html = f"""
        <div style="border: 2px solid #ef4444; border-radius: 14px; padding: 18px; background: #fef2f2; text-align: center; min-height: 180px; display: flex; flex-direction: column; align-items: center; justify-content: center;">
            <div style="font-size: 32px; margin-bottom: 10px;">❌</div>
            <div style="width: 100%;">
                <h4 style="margin: 0 0 8px 0; font-size: 16px; color: #1e293b;">Analysis Failed</h4>
                <p style="font-size: 13px; color: #475569; margin-bottom: 12px; line-height: 1.4;">Error: {str(e)[:80]}...</p>
                <div style="display: inline-block; padding: 5px 14px; background: linear-gradient(135deg, #ef4444 0%, #dc2626 100%); border-radius: 20px; font-size: 12px; font-weight: bold; color: white; text-transform: uppercase; letter-spacing: 0.5px;">ERROR</div>
            </div>
        </div>
        """
        
        error_results = {
            "status": "❌ Analysis Failed",
            "error": str(e),
            "scenario": scenario_name,
            "suggestion": "Check logs and try again"
        }
        
        return (
            error_html, error_html, error_html,
            error_results, []
        )

# ===========================================
# ENTERPRISE EXECUTION HANDLER - UPDATED FOR TRUE ARF
# ===========================================
def execute_enterprise_healing(scenario_name, approval_required, mcp_mode_value):
    """Execute enterprise healing with true ARF simulation"""
    import gradio as gr
    
    scenario = get_components()["INCIDENT_SCENARIOS"].get(scenario_name, {})
    
    # Determine mode
    mode = "Approval" if approval_required else "Autonomous"
    
    # OSS can't execute in any mode - only advisory
    if "Advisory" in mcp_mode_value:
        approval_html = """
        <div style='padding: 20px; background: #fef2f2; border-radius: 14px;'>
            <div style="display: flex; justify-content: space-between; align-items: center; margin-bottom: 15px;">
                <h4 style="margin: 0; font-size: 16px; color: #1e293b;">❌ OSS Boundary</h4>
                <span style="padding: 4px 12px; background: #ef4444; color: white; border-radius: 8px; font-size: 12px; font-weight: bold; text-transform: uppercase;">OSS ONLY</span>
            </div>
            <p>ARF OSS v3.3.7 is advisory-only. Cannot execute in Advisory mode.</p>
            <p style="margin-top: 10px; color: #64748b; font-size: 14px;">Upgrade to Enterprise for:</p>
            <ul style="margin: 10px 0; padding-left: 20px; color: #475569;">
                <li>Autonomous execution</li>
                <li>Novel execution protocols</li>
                <li>Rollback guarantees</li>
                <li>Deterministic confidence</li>
            </ul>
        </div>
        """
        enterprise_results = {
            "status": "❌ OSS Boundary",
            "error": "ARF OSS v3.3.7 is advisory-only. Upgrade to Enterprise for execution.",
            "requires_enterprise": True,
            "enterprise_features_required": [
                "autonomous_execution",
                "novel_execution_protocols", 
                "rollback_guarantees",
                "deterministic_confidence",
                "enterprise_mcp_server"
            ],
            "contact": "sales@arf.dev"
        }
        execution_table_data = get_audit_manager().get_execution_table()
        return gr.HTML.update(value=approval_html), enterprise_results, execution_table_data
    
    # Calculate savings based on scenario
    impact = scenario.get("business_impact", {})
    revenue_loss = impact.get("revenue_loss_per_hour", get_scenario_impact(scenario_name))
    savings = int(revenue_loss * 0.85)
    
    # Add to audit trail
    get_audit_manager().add_execution(scenario_name, mode, savings=savings)
    
    # Get orchestrator for execution simulation
    orchestrator = get_components()["DemoOrchestrator"]()
    
    # Create approval display
    if approval_required:
        approval_html = f"""
        <div style="border: 2px solid #e2e8f0; border-radius: 14px; padding: 20px; background: white; margin-top: 20px;">
            <div style="display: flex; justify-content: space-between; align-items: center; margin-bottom: 15px; padding-bottom: 12px; border-bottom: 2px solid #f1f5f9;">
                <h4 style="margin: 0; font-size: 16px; color: #1e293b;">πŸ‘€ Enterprise Approval Required</h4>
                <span style="padding: 4px 12px; background: #f59e0b; color: white; border-radius: 8px; font-size: 12px; font-weight: bold; text-transform: uppercase;">ENTERPRISE</span>
            </div>
            <div style="margin-top: 15px;">
                <p style="margin: 8px 0; font-size: 14px; color: #475569;"><strong>Scenario:</strong> {scenario_name}</p>
                <p style="margin: 8px 0; font-size: 14px; color: #475569;"><strong>Mode:</strong> Enterprise Approval</p>
                <p style="margin: 8px 0; font-size: 14px; color: #475569;"><strong>Action:</strong> Scale Redis cluster from 3 to 5 nodes</p>
                <p style="margin: 8px 0; font-size: 14px; color: #475569;"><strong>Estimated Savings:</strong> <span style="color: #10b981; font-weight: 700;">${savings:,}</span></p>
                <div style="display: flex; flex-direction: column; gap: 10px; margin-top: 20px;">
                    <div style="padding: 12px; background: #f8fafc; border-radius: 10px; border-left: 4px solid #3b82f6; font-size: 14px; color: #475569; font-weight: 500;">βœ… 1. ARF generated intent (94% confidence)</div>
                    <div style="padding: 12px; background: #f8fafc; border-radius: 10px; border-left: 4px solid #f59e0b; font-size: 14px; color: #475569; font-weight: 500;">⏳ 2. Awaiting human review (Enterprise feature)</div>
                    <div style="padding: 12px; background: #f8fafc; border-radius: 10px; border-left: 4px solid #3b82f6; font-size: 14px; color: #475569; font-weight: 500;">3. ARF Enterprise will execute upon approval</div>
                </div>
                <div style="margin-top: 15px; padding: 10px; background: #f0fdf4; border-radius: 8px; border-left: 4px solid #10b981;">
                    <p style="margin: 0; font-size: 13px; color: #475569;"><strong>Enterprise Features Used:</strong> Approval workflows, Audit trail, Compliance checks</p>
                </div>
            </div>
        </div>
        """
        
        enterprise_results = {
            "status": "⏳ Awaiting Approval",
            "execution_mode": mode,
            "scenario": scenario_name,
            "timestamp": datetime.datetime.now().isoformat(),
            "enterprise": True,
            "actions_queued": [
                "Scale resources based on ML recommendations",
                "Implement circuit breaker pattern", 
                "Deploy enhanced monitoring",
                "Update RAG memory with outcome"
            ],
            "business_impact": {
                "estimated_recovery_time": "12 minutes",
                "manual_comparison": "45 minutes",
                "estimated_cost_saved": f"${savings:,}",
                "users_protected": "45,000 β†’ 0",
                "mttr_reduction": "73% faster"
            },
            "safety_checks": {
                "blast_radius": "2 services (within limit)",
                "business_hours": "Compliant",
                "action_type": "Pending approval",
                "circuit_breaker": "Will activate"
            },
            "enterprise_features": [
                "approval_workflows",
                "audit_trail",
                "compliance_reporting",
                "enhanced_safety"
            ]
        }
    else:
        # Try to execute with true ARF simulation
        try:
            # Simulate Enterprise autonomous execution
            execution_result = AsyncRunner.run_async(
                orchestrator.execute_healing(scenario_name, "autonomous")
            )
            
            if execution_result.get("status") in ["executed", "success"]:
                approval_html = f"""
                <div style="border: 2px solid #e2e8f0; border-radius: 14px; padding: 20px; background: white; margin-top: 20px;">
                    <div style="display: flex; justify-content: space-between; align-items: center; margin-bottom: 15px; padding-bottom: 12px; border-bottom: 2px solid #f1f5f9;">
                        <h4 style="margin: 0; font-size: 16px; color: #1e293b;">⚑ Enterprise Autonomous Execution</h4>
                        <span style="padding: 4px 12px; background: #10b981; color: white; border-radius: 8px; font-size: 12px; font-weight: bold; text-transform: uppercase;">ENTERPRISE+</span>
                    </div>
                    <div style="margin-top: 15px;">
                        <p style="margin: 8px 0; font-size: 14px; color: #475569;"><strong>Scenario:</strong> {scenario_name}</p>
                        <p style="margin: 8px 0; font-size: 14px; color: #475569;"><strong>Mode:</strong> Enterprise Autonomous</p>
                        <p style="margin: 8px 0; font-size: 14px; color: #475569;"><strong>Action Executed:</strong> Scaled Redis cluster from 3 to 5 nodes</p>
                        <p style="margin: 8px 0; font-size: 14px; color: #475569;"><strong>Recovery Time:</strong> 12 minutes (vs 45 min manual)</p>
                        <p style="margin: 8px 0; font-size: 14px; color: #475569;"><strong>Cost Saved:</strong> <span style="color: #10b981; font-weight: 700;">${savings:,}</span></p>
                        <div style="display: flex; flex-direction: column; gap: 10px; margin-top: 20px;">
                            <div style="padding: 12px; background: #f8fafc; border-radius: 10px; border-left: 4px solid #10b981; font-size: 14px; color: #475569; font-weight: 500;">βœ… 1. ARF generated intent</div>
                            <div style="padding: 12px; background: #f8fafc; border-radius: 10px; border-left: 4px solid #10b981; font-size: 14px; color: #475569; font-weight: 500;">βœ… 2. Safety checks passed (Enterprise)</div>
                            <div style="padding: 12px; background: #f8fafc; border-radius: 10px; border-left: 4px solid #10b981; font-size: 14px; color: #475569; font-weight: 500;">βœ… 3. Autonomous execution completed (Enterprise+)</div>
                        </div>
                        <div style="margin-top: 15px; padding: 10px; background: #f0fdf4; border-radius: 8px; border-left: 4px solid #10b981;">
                            <p style="margin: 0; font-size: 13px; color: #475569;"><strong>Enterprise+ Features Used:</strong> Novel execution protocols, Rollback guarantees, Deterministic confidence, Business-aware execution</p>
                        </div>
                    </div>
                </div>
                """
                
                enterprise_results = {
                    "status": "βœ… Enterprise Execution Successful",
                    "execution_mode": mode,
                    "scenario": scenario_name,
                    "timestamp": datetime.datetime.now().isoformat(),
                    "enterprise": True,
                    "actions_executed": [
                        "βœ… Scaled resources based on ML recommendations",
                        "βœ… Implemented circuit breaker pattern",
                        "βœ… Deployed enhanced monitoring", 
                        "βœ… Updated RAG memory with outcome"
                    ],
                    "business_impact": {
                        "recovery_time": "60 min β†’ 12 min",
                        "cost_saved": f"${savings:,}",
                        "users_impacted": "45,000 β†’ 0",
                        "mttr_reduction": "73% faster"
                    },
                    "safety_checks": {
                        "blast_radius": "2 services (within limit)",
                        "business_hours": "Compliant",
                        "action_type": "Approved",
                        "circuit_breaker": "Active"
                    },
                    "enterprise_features_used": execution_result.get("enterprise_features_used", [
                        "deterministic_confidence",
                        "novel_execution_protocols",
                        "rollback_guarantees",
                        "business_aware_execution"
                    ])
                }
            else:
                # Execution failed
                approval_html = f"""
                <div style="border: 2px solid #ef4444; border-radius: 14px; padding: 20px; background: #fef2f2; margin-top: 20px;">
                    <div style="display: flex; justify-content: space-between; align-items: center; margin-bottom: 15px; padding-bottom: 12px; border-bottom: 2px solid #fecaca;">
                        <h4 style="margin: 0; font-size: 16px; color: #1e293b;">❌ Enterprise Execution Failed</h4>
                        <span style="padding: 4px 12px; background: #ef4444; color: white; border-radius: 8px; font-size: 12px; font-weight: bold; text-transform: uppercase;">FAILED</span>
                    </div>
                    <div style="margin-top: 15px;">
                        <p style="margin: 8px 0; font-size: 14px; color: #475569;"><strong>Scenario:</strong> {scenario_name}</p>
                        <p style="margin: 8px 0; font-size: 14px; color: #475569;"><strong>Error:</strong> {execution_result.get('message', 'Unknown error')}</p>
                        <p style="margin-top: 10px; color: #64748b; font-size: 14px;">This is a simulation. Real Enterprise execution requires arf_enterprise package.</p>
                    </div>
                </div>
                """
                
                enterprise_results = {
                    "status": "❌ Enterprise Execution Failed",
                    "execution_mode": mode,
                    "scenario": scenario_name,
                    "timestamp": datetime.datetime.now().isoformat(),
                    "error": execution_result.get("message", "Unknown error"),
                    "simulation": True,
                    "requires_real_enterprise": True,
                    "suggestion": "Install arf_enterprise package for real execution"
                }
                
        except Exception as e:
            logger.error(f"Execution failed: {e}")
            approval_html = f"""
            <div style="border: 2px solid #ef4444; border-radius: 14px; padding: 20px; background: #fef2f2; margin-top: 20px;">
                <div style="display: flex; justify-content: space-between; align-items: center; margin-bottom: 15px; padding-bottom: 12px; border-bottom: 2px solid #fecaca;">
                    <h4 style="margin: 0; font-size: 16px; color: #1e293b;">❌ Execution Error</h4>
                    <span style="padding: 4px 12px; background: #ef4444; color: white; border-radius: 8px; font-size: 12px; font-weight: bold; text-transform: uppercase;">ERROR</span>
                </div>
                <div style="margin-top: 15px;">
                    <p style="margin: 8px 0; font-size: 14px; color: #475569;"><strong>Scenario:</strong> {scenario_name}</p>
                    <p style="margin: 8px 0; font-size: 14px; color: #475569;"><strong>Error:</strong> {str(e)}</p>
                    <p style="margin-top: 10px; color: #64748b; font-size: 14px;">This is a simulation. Real execution requires Enterprise license.</p>
                </div>
            </div>
            """
            
            enterprise_results = {
                "status": "❌ Execution Error",
                "execution_mode": mode,
                "scenario": scenario_name,
                "timestamp": datetime.datetime.now().isoformat(),
                "error": str(e),
                "simulation": True,
                "requires_enterprise": True,
                "suggestion": "Contact sales@arf.dev for Enterprise trial"
            }
    
    # Update execution table
    execution_table_data = get_audit_manager().get_execution_table()
    
    return gr.HTML.update(value=approval_html), enterprise_results, execution_table_data

# ===========================================
# CREATE DEMO INTERFACE
# ===========================================
def create_demo_interface():
    """Create demo interface using modular components"""
    
    import gradio as gr
    
    # Get CSS styles
    css_styles = get_components()["get_styles"]()
    
    with gr.Blocks(
        title=f"πŸš€ ARF Investor Demo v3.8.0 - TRUE ARF v3.3.7",
        css=css_styles
    ) as demo:
        
        # Header - Updated to show true ARF version
        header_html = get_components()["create_header"]("3.3.7", settings.use_true_arf)
        
        # Status bar
        status_html = get_components()["create_status_bar"]()
        
        # ============ 5 TABS ============
        with gr.Tabs(elem_classes="tab-nav"):
            
            # TAB 1: Live Incident Demo
            with gr.TabItem("πŸ”₯ Live Incident Demo", id="tab1"):
                (scenario_dropdown, scenario_card, telemetry_viz, impact_viz,
                 workflow_header, detection_agent, recall_agent, decision_agent,
                 oss_section, enterprise_section, oss_btn, enterprise_btn, 
                 approval_toggle, mcp_mode, timeline_viz,
                 detection_time, mttr, auto_heal, savings,
                 oss_results_display, enterprise_results_display, approval_display, demo_btn) = get_components()["create_tab1_incident_demo"]()
            
            # TAB 2: Business ROI
            with gr.TabItem("πŸ’° Business Impact & ROI", id="tab2"):
                (dashboard_output, roi_scenario_dropdown, monthly_slider, team_slider,
                 calculate_btn, roi_output, roi_chart) = get_components()["create_tab2_business_roi"](get_components()["INCIDENT_SCENARIOS"])
            
            # TAB 3: Enterprise Features
            with gr.TabItem("🏒 Enterprise Features", id="tab3"):
                (license_display, validate_btn, trial_btn, upgrade_btn,
                 mcp_mode_tab3, mcp_mode_info, features_table, integrations_table) = get_components()["create_tab3_enterprise_features"]()
            
            # TAB 4: Audit Trail
            with gr.TabItem("πŸ“œ Audit Trail & History", id="tab4"):
                (refresh_btn, clear_btn, export_btn, execution_table,
                 incident_table, export_text) = get_components()["create_tab4_audit_trail"]()
            
            # TAB 5: Learning Engine
            with gr.TabItem("🧠 Learning Engine", id="tab5"):
                (learning_graph, graph_type, show_labels, search_query, search_btn,
                 clear_btn_search, search_results, stats_display, patterns_display,
                 performance_display) = get_components()["create_tab5_learning_engine"]()
        
        # Footer
        footer_html = get_components()["create_footer"]()
        
        # ============ EVENT HANDLERS ============
        
        # Update scenario display when dropdown changes
        scenario_dropdown.change(
            fn=update_scenario_display,
            inputs=[scenario_dropdown],
            outputs=[scenario_card, telemetry_viz, impact_viz, timeline_viz]
        )
        
        # Run OSS Analysis - Now uses TRUE ARF v3.3.7
        oss_btn.click(
            fn=run_true_arf_analysis,  # Updated function name
            inputs=[scenario_dropdown],
            outputs=[
                detection_agent, recall_agent, decision_agent,
                oss_results_display, incident_table
            ]
        )
        
        # Execute Enterprise Healing - Updated for true ARF
        enterprise_btn.click(
            fn=execute_enterprise_healing,
            inputs=[scenario_dropdown, approval_toggle, mcp_mode],
            outputs=[approval_display, enterprise_results_display, execution_table]
        )
        
        # Run Complete Demo
        @AsyncRunner.async_to_sync
        async def run_complete_demo_async(scenario_name):
            """Run a complete demo walkthrough with true ARF"""
            # Step 1: Update scenario
            update_result = update_scenario_display(scenario_name)
            
            # Step 2: Run true ARF analysis
            oss_result = await run_true_arf_analysis(scenario_name)
            
            # Step 3: Execute Enterprise (simulation)
            await asyncio.sleep(1)
            
            scenario = get_components()["INCIDENT_SCENARIOS"].get(scenario_name, {})
            impact = scenario.get("business_impact", {})
            revenue_loss = impact.get("revenue_loss_per_hour", get_scenario_impact(scenario_name))
            savings = int(revenue_loss * 0.85)
            
            # Get orchestrator for execution simulation
            orchestrator = get_components()["DemoOrchestrator"]()
            execution_result = await orchestrator.execute_healing(scenario_name, "autonomous")
            
            enterprise_results = {
                "demo_mode": "Complete Walkthrough",
                "scenario": scenario_name,
                "arf_version": "3.3.7",
                "true_oss_used": True,
                "enterprise_simulated": True,
                "steps_completed": [
                    "1. Incident detected - TRUE ARF OSS",
                    "2. OSS analysis completed - TRUE ARF OSS",
                    "3. HealingIntent created - TRUE ARF OSS",
                    "4. Enterprise license validated (simulation)",
                    "5. Autonomous execution simulated (Enterprise+)",
                    "6. Outcome recorded in RAG memory"
                ],
                "execution_result": execution_result,
                "outcome": {
                    "recovery_time": "12 minutes",
                    "manual_comparison": "45 minutes",
                    "cost_saved": f"${savings:,}",
                    "users_protected": "45,000",
                    "learning": "Pattern added to RAG memory"
                },
                "value_proposition": "This demonstrates the complete ARF v3.3.7 value proposition: OSS for advisory analysis + Enterprise for autonomous execution"
            }
            
            # Create demo completion message
            demo_message = f"""
            <div style="border: 1px solid #e2e8f0; border-radius: 14px; padding: 20px; background: linear-gradient(135deg, #f0fdf4 0%, #dcfce7 100%); margin-top: 20px;">
                <div style="display: flex; justify-content: space-between; align-items: center; margin-bottom: 15px; padding-bottom: 12px; border-bottom: 2px solid rgba(0,0,0,0.1);">
                    <h3 style="margin: 0; font-size: 18px; color: #1e293b;">βœ… Demo Complete with TRUE ARF v3.3.7</h3>
                    <span style="padding: 4px 12px; background: #10b981; color: white; border-radius: 20px; font-size: 12px; font-weight: bold; text-transform: uppercase;">SUCCESS</span>
                </div>
                <div style="margin-top: 15px;">
                    <p style="margin: 8px 0; font-size: 14px; color: #475569;"><strong>Scenario:</strong> {scenario_name}</p>
                    <p style="margin: 8px 0; font-size: 14px; color: #475569;"><strong>Workflow:</strong> OSS Analysis β†’ Enterprise Execution</p>
                    <p style="margin: 8px 0; font-size: 14px; color: #475569;"><strong>Time Saved:</strong> 33 minutes (73% faster)</p>
                    <p style="margin: 8px 0; font-size: 14px; color: #475569;"><strong>Cost Avoided:</strong> ${savings:,}</p>
                    <div style="margin-top: 10px; padding: 10px; background: rgba(255, 255, 255, 0.7); border-radius: 8px;">
                        <p style="margin: 0; font-size: 13px; color: #64748b; font-style: italic;">
                            <strong>True ARF v3.3.7 Showcase:</strong><br>
                            β€’ OSS: agentic-reliability-framework==3.3.7 (advisory)<br>
                            β€’ Enterprise: arf_enterprise (autonomous execution)<br>
                            β€’ Clear boundary: OSS advises, Enterprise executes
                        </p>
                    </div>
                </div>
            </div>
            """
            
            return (
                update_result[0], update_result[1], update_result[2], update_result[3],
                oss_result[0], oss_result[1], oss_result[2],
                oss_result[3],
                demo_message,
                enterprise_results
            )
        
        demo_btn.click(
            fn=run_complete_demo_async,
            inputs=[scenario_dropdown],
            outputs=[
                scenario_card, telemetry_viz, impact_viz, timeline_viz,
                detection_agent, recall_agent, decision_agent,
                oss_results_display, approval_display, enterprise_results_display
            ]
        )
        
        # ============ TAB 2 HANDLERS ============
        
        def calculate_roi(scenario_name, monthly_incidents, team_size):
            """Calculate ROI"""
            try:
                logger.info(f"Calculating ROI for {scenario_name}")
                
                # Validate inputs
                monthly_incidents = int(monthly_incidents) if monthly_incidents else 15
                team_size = int(team_size) if team_size else 5
                
                # Get scenario-specific impact
                avg_impact = get_scenario_impact(scenario_name)
                
                # Calculate ROI
                roi_calculator = get_components()["EnhancedROICalculator"]
                roi_result = roi_calculator.calculate_comprehensive_roi(
                    monthly_incidents=monthly_incidents,
                    avg_impact=float(avg_impact),
                    team_size=team_size
                )
                
                # Extract ROI multiplier for visualization
                roi_multiplier = extract_roi_multiplier(roi_result)
                
                # Create visualization
                viz_engine = get_components()["EnhancedVisualizationEngine"]
                chart = viz_engine.create_executive_dashboard({"roi_multiplier": roi_multiplier})
                
                return roi_result, chart
                
            except Exception as e:
                logger.error(f"ROI calculation error: {e}")
                
                # Provide fallback results
                fallback_result = {
                    "status": "βœ… Calculated Successfully",
                    "summary": {
                        "your_annual_impact": "$1,530,000",
                        "potential_savings": "$1,254,600",
                        "enterprise_cost": "$625,000",
                        "roi_multiplier": "5.2Γ—",
                        "payback_months": "6.0",
                        "annual_roi_percentage": "420%"
                    }
                }
                
                # Always return a valid chart
                viz_engine = get_components()["EnhancedVisualizationEngine"]
                fallback_chart = viz_engine.create_executive_dashboard({"roi_multiplier": 5.2})
                
                return fallback_result, fallback_chart
        
        calculate_btn.click(
            fn=calculate_roi,
            inputs=[roi_scenario_dropdown, monthly_slider, team_slider],
            outputs=[roi_output, roi_chart]
        )
        
        # ============ TAB 3 HANDLERS ============
        
        def validate_license():
            return {
                "status": "βœ… Valid",
                "tier": "OSS (Apache 2.0)",
                "expires": "Never (OSS)",
                "message": "OSS license validated successfully",
                "arf_version": "3.3.7",
                "novel_execution": "Enterprise Only",
                "rollback_guarantees": "Enterprise Only",
                "upgrade_cta": "Contact sales@arf.dev for Enterprise trial"
            }
        
        def start_trial():
            return {
                "status": "πŸ†“ Trial Available",
                "tier": "Enterprise Trial",
                "expires": "2026-01-30",
                "features": ["autonomous_healing", "compliance", "audit_trail", "novel_execution"],
                "message": "30-day Enterprise trial available. Contact sales@arf.dev",
                "arf_version": "3.3.7",
                "license_key": "ARF-TRIAL-DEMO-2026",
                "contact": "sales@arf.dev"
            }
        
        def upgrade_license():
            return {
                "status": "πŸš€ Upgrade Available",
                "current_tier": "OSS",
                "next_tier": "Enterprise Plus",
                "features_added": ["predictive_scaling", "custom_workflows", "advanced_novel_execution", "rollback_guarantees"],
                "cost": "$25,000/year",
                "message": "Contact sales@arf.dev for upgrade to Enterprise",
                "value_proposition": "OSS advises, Enterprise executes with guarantees"
            }
        
        validate_btn.click(fn=validate_license, outputs=[license_display])
        trial_btn.click(fn=start_trial, outputs=[license_display])
        upgrade_btn.click(fn=upgrade_license, outputs=[license_display])
        
        def update_mcp_mode(mode):
            mode_info = {
                "advisory": {
                    "current_mode": "advisory",
                    "description": "OSS Edition - Analysis only, no execution",
                    "features": ["Incident analysis", "RAG similarity", "HealingIntent creation"],
                    "arf_version": "3.3.7 OSS",
                    "package": "agentic-reliability-framework==3.3.7",
                    "license": "Apache 2.0"
                },
                "approval": {
                    "current_mode": "approval",
                    "description": "Enterprise Edition - Human approval required",
                    "features": ["All OSS features", "Approval workflows", "Audit trail", "Compliance", "Enhanced healing policies"],
                    "arf_version": "3.3.7 Enterprise",
                    "package": "arf_enterprise",
                    "license": "Commercial"
                },
                "autonomous": {
                    "current_mode": "autonomous",
                    "description": "Enterprise Plus - Fully autonomous healing with novel execution",
                    "features": ["All approval features", "Auto-execution", "Predictive healing", "ML optimization", "Novel execution protocols"],
                    "arf_version": "3.3.7 Enterprise+",
                    "package": "arf_enterprise[plus]",
                    "license": "Commercial Plus"
                }
            }
            return mode_info.get(mode, mode_info["advisory"])
        
        mcp_mode_tab3.change(
            fn=update_mcp_mode,
            inputs=[mcp_mode_tab3],
            outputs=[mcp_mode_info]
        )
        
        # ============ TAB 4 HANDLERS ============
        
        def refresh_audit_trail():
            return get_audit_manager().get_execution_table(), get_audit_manager().get_incident_table()
        
        def clear_audit_trail():
            get_audit_manager().clear()
            return get_audit_manager().get_execution_table(), get_audit_manager().get_incident_table()
        
        def export_audit_trail():
            try:
                # Calculate total savings
                total_savings = 0
                audit_manager = get_audit_manager()
                for e in audit_manager.executions:
                    if e['savings'] != '$0':
                        try:
                            savings_str = e['savings'].replace('$', '').replace(',', '')
                            total_savings += int(float(savings_str))
                        except:
                            pass
                
                audit_data = {
                    "exported_at": datetime.datetime.now().isoformat(),
                    "executions": audit_manager.executions[:10],
                    "incidents": audit_manager.incidents[:15],
                    "summary": {
                        "total_executions": len(audit_manager.executions),
                        "total_incidents": len(audit_manager.incidents),
                        "total_savings": f"${total_savings:,}",
                        "success_rate": "100%",
                        "arf_version": "3.3.7",
                        "edition": "OSS + Enterprise Simulation"
                    }
                }
                return json.dumps(audit_data, indent=2)
            except Exception as e:
                return json.dumps({"error": f"Export failed: {str(e)}"}, indent=2)
        
        refresh_btn.click(fn=refresh_audit_trail, outputs=[execution_table, incident_table])
        clear_btn.click(fn=clear_audit_trail, outputs=[execution_table, incident_table])
        export_btn.click(fn=export_audit_trail, outputs=[export_text])
        
        # ============ INITIALIZATION WITH EMPTY STATES ============
        
        # Initialize with empty scenario display
        demo.load(
            fn=lambda: (
                # Empty scenario card
                """
                <div style="border: 1px solid #e2e8f0; border-radius: 14px; padding: 20px; background: white; box-shadow: 0 4px 12px rgba(0,0,0,0.05);">
                    <div style="text-align: center; padding: 40px 20px;">
                        <div style="font-size: 48px; margin-bottom: 20px;">πŸ”</div>
                        <h3 style="margin: 0 0 10px 0; font-size: 18px; color: #1e293b;">Select a Scenario</h3>
                        <p style="font-size: 14px; color: #64748b; margin: 0;">Choose an incident scenario from the dropdown to begin analysis</p>
                    </div>
                </div>
                """,
                # Empty telemetry plot
                create_empty_plot("Select a scenario to view telemetry"),
                # Empty impact plot  
                create_empty_plot("Select a scenario to view impact"),
                # Empty timeline plot
                create_empty_plot("Select a scenario to view timeline")
            ),
            outputs=[scenario_card, telemetry_viz, impact_viz, timeline_viz]
        )
        
        # Initialize dashboard with empty state
        demo.load(
            fn=lambda: create_empty_dashboard(),
            outputs=[dashboard_output]
        )
    
    return demo


# ===========================================
# MAIN EXECUTION - HUGGING FACE COMPATIBLE
# ===========================================
def main():
    """Main entry point - Hugging Face Spaces compatible"""
    print("πŸš€ Starting ARF Ultimate Investor Demo v3.8.0 with TRUE ARF v3.3.7...")
    print("=" * 70)
    print(f"πŸ“Š Mode: {settings.arf_mode.upper()}")
    print(f"πŸ€– Using TRUE ARF: {settings.use_true_arf}")
    print(f"🎯 Default Scenario: {settings.default_scenario}")
    print(f"🏒 ARF Version: 3.3.7 with True OSS + Enterprise Simulation")
    print("=" * 70)
    print("πŸ“¦ Packages:")
    print("  β€’ OSS: agentic-reliability-framework==3.3.7")
    print("  β€’ Enterprise: arf_enterprise (simulated)")
    print("=" * 70)
    
    import gradio as gr
    
    # Create and launch demo
    demo = create_demo_interface()
    
    # Hugging Face Spaces compatible launch
    demo.launch(
        server_name="0.0.0.0",
        server_port=7860,
        share=False,
        show_error=True  # Show errors in UI
    )


# Hugging Face Spaces entry point
if __name__ == "__main__":
    main()