File size: 72,568 Bytes
04d8532
 
 
 
 
 
ef6dd5e
04d8532
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ef6dd5e
 
 
 
 
 
 
 
04d8532
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ef6dd5e
04d8532
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ef6dd5e
04d8532
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ef6dd5e
04d8532
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ef6dd5e
04d8532
 
 
 
 
 
 
 
 
ef6dd5e
 
 
04d8532
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ef6dd5e
 
04d8532
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ef6dd5e
 
 
 
 
 
 
 
 
 
 
04d8532
 
 
ef6dd5e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
04d8532
 
 
 
 
 
ef6dd5e
04d8532
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ef6dd5e
04d8532
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ef6dd5e
04d8532
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ef6dd5e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
04d8532
 
 
 
 
 
 
 
 
 
 
ef6dd5e
04d8532
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ef6dd5e
04d8532
 
 
 
 
 
 
 
 
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
"""
BIST Value Intelligence v3 — Compact Value & Intrinsic Valuation Platform
=========================================================================
Data integrity > model complexity. Intrinsic value > momentum. Margin of safety > prediction.

Veri etiketleri:  [R] raporlanan   [D] türetilen   [A] kullanıcı varsayımı
UYARI: yfinance verisi point-in-time DEĞİLDİR; Backtest sekmesi yalnızca DENEYSEL/göstergedir (bkz. sınırlamalar).
"""

import logging
import tempfile
import time
from datetime import datetime, timedelta
from typing import Any, Dict, List, Optional, Tuple

import gradio as gr
import numpy as np
import pandas as pd
import plotly.graph_objects as go
import yfinance as yf

logging.basicConfig(level=logging.INFO, format="%(asctime)s | %(levelname)s | %(message)s")
logger = logging.getLogger("bist_v3")

BIST_UNIVERSE = [
    # Mevcut çekirdek liste
    "AEFES", "AKBNK", "AKSA", "AKSEN", "ALARK", "ARCLK", "ASELS", "ASTOR", "BIMAS", "BRSAN",
    "CCOLA", "CIMSA", "DOAS", "DOHOL", "ECILC", "EKGYO", "ENJSA", "ENKAI", "EREGL", "FROTO",
    "GARAN", "GUBRF", "HEKTS", "ISCTR", "KCHOL", "KRDMD", "MGROS", "ODAS", "PETKM", "PGSUS",
    "SAHOL", "SASA", "SISE", "SKBNK", "TCELL", "THYAO", "TKFEN", "TOASO", "TSKB", "TTKOM",
    "TUPRS", "ULKER", "VAKBN", "VESBE", "YKBNK", "ZOREN", "ISGYO", "TRGYO", "HALKB", "AKGRT",
    "ANSGR", "TURSG", "ANHYT", "RAYSG",
    # Genişletme (>=100 hisse için)
    "AGHOL", "AHGAZ", "ALBRK", "ALFAS", "ARDYZ", "AYDEM", "BAGFS", "BERA", "BIOEN", "BRISA",
    "BRYAT", "BTCIM", "BUCIM", "CANTE", "CWENE", "EGEEN", "ENERY", "EUPWR", "GENIL", "GESAN",
    "GLYHO", "GWIND", "IPEKE", "ISDMR", "ISMEN", "KARSN", "KCAER", "KLSER", "KONTR", "KONYA",
    "KOZAA", "KOZAL", "KTLEV", "LOGO", "MAVI", "MIATK", "MPARK", "OTKAR", "OYAKC", "PSGYO",
    "QUAGR", "REEDR", "SELEC", "SMRTG", "SNGYO", "SOKM", "TABGD", "TAVHL", "TMSN", "TTRAK",
    "TKNSA", "TUKAS", "ULUUN", "VAKKO", "VESTL", "YEOTK", "ZRGYO", "ECZYT", "EBEBK", "FENER",
    "CLEBI", "DOCO", "EUREN", "INVEO", "KMPUR", "LMKDC", "NTHOL", "ONCSM", "PASEU", "TRMET",
]
BIST_UNIVERSE = list(dict.fromkeys(BIST_UNIVERSE))  # tekrarları temizle
assert len(BIST_UNIVERSE) >= 100, f"Evren {len(BIST_UNIVERSE)} hisse; en az 100 olmalı"
BIST_UNIVERSE = [s + ".IS" for s in BIST_UNIVERSE]

REQUEST_DELAY_SEC = 0.3
CACHE_TTL_HOURS = 6
MIN_PEERS = 5
_cache: Dict[str, Tuple[datetime, Any]] = {}
LAST: Dict[str, Any] = {"df": None, "failed": []}


def _cget(k):
    if k in _cache:
        ts, v = _cache[k]
        if datetime.now() - ts < timedelta(hours=CACHE_TTL_HOURS):
            return v
    return None


def _cset(k, v):
    _cache[k] = (datetime.now(), v)


# ============================================================================
# SMALL HELPERS
# ============================================================================

def num(x) -> Optional[float]:
    try:
        if x is None:
            return None
        v = float(x)
        return None if (np.isnan(v) or np.isinf(v)) else v
    except Exception:
        return None


def div(a, b) -> Optional[float]:
    a, b = num(a), num(b)
    if a is None or b is None or b == 0:
        return None
    return a / b


def growth(cur, prev) -> Optional[float]:
    cur, prev = num(cur), num(prev)
    if cur is None or prev is None or prev <= 0:
        return None
    return cur / prev - 1


def real_ret(nominal, infl) -> Optional[float]:
    return None if nominal is None else (1 + nominal) / (1 + infl) - 1


def lin(x, bad, good) -> Optional[float]:
    """Linear 0-100 scale; works for bad>good as well."""
    if x is None:
        return None
    return float(np.clip((x - bad) / (good - bad), 0, 1) * 100)


def wavg(items) -> Tuple[Optional[float], float]:
    v = [(s, w) for s, w in items if s is not None]
    tw = sum(w for _, w in items)
    if not v or tw == 0:
        return None, 0.0
    sw = sum(w for _, w in v)
    return sum(s * w for s, w in v) / sw, sw / tw


def anyc(*c) -> Optional[bool]:
    v = [x for x in c if x is not None]
    return None if not v else any(v)


def fm(x, d=2):
    return "N/A" if x is None else f"{x:,.{d}f}"


def fp(x, d=1):
    return "N/A" if x is None else f"{x * 100:.{d}f}%"


def norm_sym(t: str) -> str:
    t = (t or "").strip().upper()
    return t if t.endswith(".IS") else t + ".IS"


def owner_earnings(ni, da, capex, dwc):
    """OE = NI + D&A − bakım capex + ΔİSermayesi; bakım capex = min(toplam capex, D&A) [D tahmin]. Eksik girdi → None."""
    if None in (ni, da, capex, dwc):
        return None, None
    maint = min(capex, da)
    return ni + da - maint + dwc, dict(ni=ni, da=da, capex_total=capex, maint_est=maint, dwc=dwc)


def mk_params(infl, rw, rtg, rg, pt, cv) -> Dict[str, float]:
    return dict(infl=infl / 100, rw=rw / 100, rtg=rtg / 100, rg=rg / 100, pt=pt / 100, conv=cv / 100)


# ============================================================================
# DATA ACQUISITION & INTEGRITY
# ============================================================================

STMT = {
    "ni": (("Net Income", "Net Income Common Stockholders"), "fin"),
    "rev": (("Total Revenue", "Operating Revenue"), "fin"),
    "gp": (("Gross Profit",), "fin"),
    "opinc": (("Operating Income", "EBIT"), "fin"),
    "ebitda": (("EBITDA", "Normalized EBITDA"), "fin"),
    "interest": (("Interest Expense", "Interest Expense Non Operating"), "fin"),
    "tax": (("Tax Provision",), "fin"),
    "pretax": (("Pretax Income",), "fin"),
    "ta": (("Total Assets",), "bs"),
    "eq": (("Stockholders Equity", "Common Stock Equity", "Total Equity Gross Minority Interest"), "bs"),
    "debt": (("Total Debt",), "bs"),
    "ltd": (("Long Term Debt", "Long Term Debt And Capital Lease Obligation"), "bs"),
    "cash": (("Cash Cash Equivalents And Short Term Investments", "Cash And Cash Equivalents"), "bs"),
    "ca": (("Current Assets",), "bs"),
    "cl": (("Current Liabilities",), "bs"),
    "shares": (("Ordinary Shares Number", "Share Issued"), "bs"),
    "cfo": (("Operating Cash Flow", "Cash Flow From Continuing Operating Activities"), "cf"),
    "capex": (("Capital Expenditure",), "cf"),
    "fcf": (("Free Cash Flow",), "cf"),
    "da": (("Depreciation And Amortization", "Depreciation Amortization Depletion"), "cf"),
    "dwc": (("Change In Working Capital",), "cf"),
}


def stmt(df, names, i=0) -> Optional[float]:
    if df is None or getattr(df, "empty", True) or i >= df.shape[1]:
        return None
    for n in names:
        if n in df.index:
            r = df.loc[n]
            if isinstance(r, pd.DataFrame):
                r = r.iloc[0]
            return num(r.iloc[i])
    return None


def segment_of(industry: str, sym: str) -> str:
    ind = (industry or "").lower()
    if "bank" in ind:
        return "Bank"
    if "insurance" in ind:
        return "Insurance"
    if "reit" in ind or "real estate" in ind or sym.upper().endswith("GYO"):
        return "GYO"
    return "Industrial"


def fetch(symbol: str, force: bool = False) -> Optional[Dict[str, Any]]:
    key = f"t_{symbol}"
    if not force:
        c = _cget(key)
        if c is not None:
            return c
    try:
        t = yf.Ticker(symbol)
        info = t.info or {}
        fin, bs, cf = t.financials, t.balance_sheet, t.cashflow
        try:
            dv = t.dividends
        except Exception:
            dv = None
        price = num(info.get("currentPrice")) or num(info.get("regularMarketPrice"))
        if price is None:
            h = t.history(period="5d")
            price = num(h["Close"].iloc[-1]) if not h.empty else None
    except Exception as e:
        logger.warning("%s fetch failed: %s", symbol, e)
        return None
    if price is None or price <= 0:
        return None

    flags: List[str] = []
    dfs = {"fin": fin, "bs": bs, "cf": cf}
    s = {k: [stmt(dfs[src], names, i) for i in range(3)] for k, (names, src) in STMT.items()}
    s["capex"] = [abs(x) if x is not None else None for x in s["capex"]]
    s["interest"] = [abs(x) if x is not None else None for x in s["interest"]]
    for i in range(3):
        if s["fcf"][i] is None and s["cfo"][i] is not None and s["capex"][i] is not None:
            s["fcf"][i] = s["cfo"][i] - s["capex"][i]
        if s["ebitda"][i] is None and s["opinc"][i] is not None and s["da"][i] is not None:
            s["ebitda"][i] = s["opinc"][i] + s["da"][i]
    if s["debt"][0] is None:
        s["debt"][0] = num(info.get("totalDebt"))
        if s["debt"][0] is not None:
            flags.append("debt from info (not statement)")
    if s["cash"][0] is None:
        s["cash"][0] = num(info.get("totalCash"))
        if s["cash"][0] is not None:
            flags.append("cash from info (not statement)")

    # period / currency / unit validation
    sd = None
    try:
        sd = pd.Timestamp(fin.columns[0]).to_pydatetime() if fin is not None and not fin.empty else None
        for nm, df in (("balance sheet", bs), ("cash flow", cf)):
            if df is not None and not df.empty and sd is not None and pd.Timestamp(df.columns[0]).to_pydatetime() != sd:
                flags.append(f"{nm} period != income statement period")
    except Exception:
        pass
    age = (datetime.now() - sd).days if sd else None
    if age is not None and age > 550:
        flags.append(f"stale statements ({age} days)")
    if sd is None:
        flags.append("no annual statements")
    fc, cur = info.get("financialCurrency"), info.get("currency")
    unit_ok = (fc is None) or (cur is None) or (fc == cur)
    if not unit_ok:
        flags.append(f"currency mismatch (fin {fc} vs price {cur})")
    if fc is None:
        flags.append("financial currency unknown")

    shares = num(info.get("sharesOutstanding")) or s["shares"][0]
    mcap = num(info.get("marketCap")) or (price * shares if shares else None)
    if shares and mcap and abs(price * shares / mcap - 1) > 0.35:
        flags.append("shares x price differs >35% from market cap")
    eq0, ta0 = s["eq"][0], s["ta"][0]
    if eq0 is not None and ta0 is not None and eq0 > ta0 * 1.01:
        flags.append("equity > total assets (suspicious)")
    if s["rev"][0] and s["ni"][0] is not None and abs(s["ni"][0]) > 1.0 * s["rev"][0] and s["rev"][0] > 0:
        flags.append("|net income| > revenue (suspicious)")

    bvps = num(info.get("bookValue")) or div(eq0, shares)
    eps = num(info.get("trailingEps")) or div(s["ni"][0], shares)
    if eps is not None and shares and s["ni"][0] not in (None, 0) and abs(eps * shares / s["ni"][0] - 1) > 0.5:
        flags.append("EPS x shares differs >50% from net income")

    # dividends: 0 only if history fetched and empty; None if unavailable
    dy = None
    try:
        if dv is not None:
            if len(dv) > 0:
                cutoff = pd.Timestamp.now(tz=dv.index.tz) - pd.Timedelta(days=365)
                dy = float(dv[dv.index >= cutoff].sum()) / price
            else:
                dy = 0.0
    except Exception:
        dy = None
    if dy is None:
        y = num(info.get("dividendYield"))
        if y is not None:
            dy = y / 100 if y > 1 else y

    tax_assumed = False
    tr = div(s["tax"][0], s["pretax"][0]) if (s["pretax"][0] or 0) > 0 else None
    if tr is None:
        tr, tax_assumed = 0.25, True
    tr = float(np.clip(tr, 0, 0.35))

    net_debt = (s["debt"][0] - s["cash"][0]) if (s["debt"][0] is not None and s["cash"][0] is not None) else None
    fcf0, int0 = s["fcf"][0], s["interest"][0]
    fcff = (fcf0 + int0 * (1 - tr)) if (fcf0 is not None and int0 is not None) else None  # unlevered -> net debt bridge
    ni0, da0, cx0, wc0 = s["ni"][0], s["da"][0], s["capex"][0], s["dwc"][0]
    oe, oe_parts = owner_earnings(ni0, da0, cx0, wc0)  # bakım capex = min(capex, D&A) [D tahmin]
    ev = (mcap + net_debt) if (mcap and net_debt is not None) else num(info.get("enterpriseValue"))
    ebitda0 = s["ebitda"][0]
    industry = info.get("industry") or "Unknown"
    seg = segment_of(industry, symbol.replace(".IS", ""))
    roes = [div(s["ni"][i], s["eq"][i]) for i in range(3) if (s["eq"][i] or 0) > 0]
    roes = [r for r in roes if r is not None]
    ir = num(info.get("returnOnEquity"))
    if ir is not None:
        roes.append(ir)
    sust_roe = float(np.clip(np.mean(roes), -1, 1.5)) if roes else None

    d = {
        "sym": symbol.replace(".IS", ""), "name": info.get("shortName") or symbol, "price": price, "mcap": mcap,
        "shares": shares, "sector": info.get("sector") or "Unknown", "industry": industry, "seg": seg,
        "bvps": bvps, "eps": eps, "dy": dy, "s": s, "flags": flags, "unit_ok": unit_ok, "stmt_date": sd, "age": age,
        "tax_rate": tr, "tax_assumed": tax_assumed, "net_debt": net_debt, "fcff": fcff, "fcf0": fcf0, "oe": oe,
        "oe_yield": div(oe, mcap) if unit_ok else None, "oe_parts": oe_parts, "ev": ev, "sust_roe": sust_roe,
        "pe": price / eps if eps and eps > 0 else None,
        "fpe": num(info.get("forwardPE")) if (num(info.get("forwardPE")) or 0) > 0 else None,
        "pb": price / bvps if bvps and bvps > 0 else None,
        "ps": div(mcap, s["rev"][0]) if (s["rev"][0] or 0) > 0 else None,
        "ev_ebitda": div(ev, ebitda0) if (ebitda0 or 0) > 0 and ev else None,
        "ev_fcf": div(ev, fcf0) if (fcf0 or 0) > 0 and ev else None,
        "fcf_yield": div(fcf0, mcap), "earn_yield": div(eps, price), "fetched": datetime.now().isoformat(),
    }
    _cset(key, d)
    return d


def cached_ds() -> List[Dict[str, Any]]:
    return [v for k, (ts, v) in _cache.items() if k.startswith("t_") and v is not None]


MULTS = ["pe", "fpe", "pb", "ev_ebitda", "ev_fcf", "ps", "fcf_yield", "earn_yield"]
POS_ONLY = {"pe", "fpe", "pb", "ev_ebitda", "ev_fcf", "ps"}


def build_medians(ds) -> Dict[Tuple[str, str], Dict[str, Tuple[Optional[float], int]]]:
    groups: Dict[Tuple[str, str], list] = {}
    for d in ds:
        groups.setdefault((d["seg"], d["sector"]), []).append(d)
    med = {}
    for k, g in groups.items():
        m = {}
        for f in MULTS:
            v = [x[f] for x in g if x.get(f) is not None and (f not in POS_ONLY or (0 < x[f] < 300))]
            m[f] = (float(np.median(v)) if len(v) >= MIN_PEERS else None, len(v))
        med[k] = m
    return med


# ============================================================================
# VALUATION ENGINE
# ============================================================================

def dcf_value(base, g0, wacc, tg, shares, net_debt, years=5) -> Optional[Dict[str, float]]:
    """5-year explicit DCF, growth fades linearly g0 -> tg. Returns None if inputs insufficient."""
    if None in (base, g0, wacc, tg, shares, net_debt) or base <= 0 or shares <= 0 or wacc <= tg:
        return None
    flows, f = [], base
    for t in range(1, years + 1):
        g = g0 + (tg - g0) * (t - 1) / (years - 1)
        f *= 1 + g
        flows.append(f)
    pv = sum(fl / (1 + wacc) ** (i + 1) for i, fl in enumerate(flows))
    pvtv = flows[-1] * (1 + tg) / (wacc - tg) / (1 + wacc) ** years
    ev = pv + pvtv
    eq = ev - net_debt
    return {"ev": ev, "equity": eq, "per_share": eq / shares, "tv_share": pvtv / ev if ev else 0.0}


def assumptions(P, rg=None, rw=None):
    rg = P["rg"] if rg is None else rg
    rw = P["rw"] if rw is None else rw
    i = P["infl"]
    return (1 + rg) * (1 + i * P["pt"]) - 1, (1 + rw) * (1 + i) - 1, (1 + P["rtg"]) * (1 + i) - 1  # g0, wacc, tg (nominal)


def dcf_try(base, g0, wacc, tg, shares, nd, ok):
    if not ok:
        return None, "currency/unit mismatch"
    if base is None:
        return None, "essential inputs missing"
    if base <= 0:
        return None, "non-positive base cash flow"
    if not shares or shares <= 0:
        return None, "shares unavailable"
    if nd is None:
        return None, "net debt unavailable"
    if wacc <= tg:
        return None, "discount rate <= terminal growth"
    r = dcf_value(base, g0, wacc, tg, shares, nd)
    if r is None or r["per_share"] <= 0:
        return None, "equity value <= 0"
    return r["per_share"], f"TV = {r['tv_share'] * 100:.0f}% of EV"


def relative_value(d, med) -> Tuple[Optional[float], str]:
    m = med.get((d["seg"], d["sector"]))
    if not m or not d["unit_ok"]:
        return None, "no peer group / unit mismatch"
    seg, vals = d["seg"], []
    if m["pb"][0] and d["bvps"] and d["bvps"] > 0:
        vals.append(m["pb"][0] * d["bvps"])
    if seg != "GYO" and m["pe"][0] and d["eps"] and d["eps"] > 0:
        vals.append(m["pe"][0] * d["eps"])
    if seg == "Industrial" and m["ev_ebitda"][0] and (d["s"]["ebitda"][0] or 0) > 0 and d["net_debt"] is not None and d["shares"]:
        vals.append((m["ev_ebitda"][0] * d["s"]["ebitda"][0] - d["net_debt"]) / d["shares"])
    vals = [v for v in vals if v > 0]
    if not vals:
        return None, f"<{MIN_PEERS} valid peers"
    return float(np.mean(vals)), f"{len(vals)} sector-median multiple(s)"


def intrinsic(d, P, med, rg=None, rw=None):
    g0, wacc, tg = assumptions(P, rg, rw)
    m: Dict[str, Tuple[Optional[float], str]] = {}
    seg, bv = d["seg"], d["bvps"]
    if seg in ("Bank", "Insurance"):
        roe = d["sust_roe"]
        if d["unit_ok"] and roe is not None and bv and bv > 0 and wacc > tg:
            m["Justified P/B"] = (max((roe - tg) / (wacc - tg), 0) * bv, f"sust. ROE {fp(roe)}, COE {fp(wacc)}, g {fp(tg)}")
        else:
            m["Justified P/B"] = (None, "ROE/book value unavailable")
        m["DCF"] = (None, "not applied to banks/insurers")
    elif seg == "GYO":
        m["NAV proxy (book)"] = ((bv, "book value; appraised NAV not in Yahoo") if (d["unit_ok"] and bv and bv > 0) else (None, "book value unavailable"))
    else:
        m["DCF"] = dcf_try(d["fcff"], g0, wacc, tg, d["shares"], d["net_debt"], d["unit_ok"])
        m["Owner Earnings"] = dcf_try(d["oe"], g0, wacc, tg, d["shares"], 0.0, d["unit_ok"])  # equity CF: no debt bridge
    m["Relative"] = relative_value(d, med)
    vals = [v for v, _ in m.values() if v is not None and v > 0]
    return m, (float(np.median(vals)) if vals else None)


def reverse_dcf(d, P):
    if d["seg"] in ("Bank", "Insurance"):
        return None, "FCF-based reverse DCF not applicable to banks/insurers"
    base, shares, nd = d["fcff"], d["shares"], d["net_debt"]
    if not d["unit_ok"] or base is None or base <= 0 or not shares or nd is None:
        return None, "essential inputs missing or FCFF <= 0"
    _, wacc0, tg = assumptions(P)

    def solve(wacc):
        if wacc <= tg:
            return None
        f = lambda g: dcf_value(base, g, wacc, tg, shares, nd)["per_share"]
        lo, hi = -0.9, 3.0
        if not (f(lo) <= d["price"] <= f(hi)):
            return None
        for _ in range(80):
            mid = (lo + hi) / 2
            lo, hi = (mid, hi) if f(mid) < d["price"] else (lo, mid)
        return (lo + hi) / 2

    rows = []
    for dw in (-0.03, 0.0, 0.03):
        rw = P["rw"] + dw
        w = (1 + rw) * (1 + P["infl"]) - 1
        g = solve(w)
        rows.append({"Real discount rate": fp(rw), "Nominal WACC": fp(w), "Implied FCF growth (nominal, yr1)": fp(g),
                     "Implied (real)": fp(real_ret(g, P["infl"]))})
    return rows, "ok"


def scenarios(d, P, med, probs):
    SC = [("Bear", -0.04, 0.03, 0.85), ("Base", 0.0, 0.0, 1.0), ("Bull", 0.04, -0.02, 1.10)]
    out, ok = [], True
    for (nm, dg, dw, mult), p in zip(SC, probs):
        _, iv = intrinsic(d, P, med, rg=P["rg"] + dg, rw=max(P["rw"] + dw, 0.0))
        tgt = None if iv is None else (d["price"] + P["conv"] * (iv - d["price"])) * mult
        ok &= tgt is not None
        out.append({"Scenario": nm, "Real growth": fp(P["rg"] + dg), "Real disc. rate": fp(max(P["rw"] + dw, 0)),
                    "Multiple factor": mult, "Intrinsic": iv, "Target": tgt, "Prob": p})
    if not ok:
        return out, None
    pw = sum(o["Prob"] * o["Target"] for o in out)
    nom = pw / d["price"] - 1
    dy = d["dy"] or 0.0
    tot = nom + dy
    return out, dict(pw=pw, nominal=nom, total=tot, real=real_ret(tot, P["infl"]), real_price=real_ret(nom, P["infl"]),
                     dy_known=d["dy"] is not None)


def validate_probs(bear, base):
    bull = 100 - bear - base
    if bear < 0 or base < 0 or bull < -1e-9:
        return None
    return (bear / 100, base / 100, bull / 100)


# ============================================================================
# QUALITY, PIOTROSKI, RISK
# ============================================================================

def cv(series):
    v = [x for x in series if x is not None]
    if len(v) < 3 or np.mean(v) <= 0:
        return None
    return float(np.std(v) / np.mean(v))


def quality(d):
    s, q = d["s"], {}
    ni0, eq0, ta0, rev0 = s["ni"][0], s["eq"][0], s["ta"][0], s["rev"][0]
    q["roe"] = div(ni0, eq0) if (eq0 or 0) > 0 else None
    q["roa"] = div(ni0, ta0)
    ic = None
    if None not in (eq0, s["debt"][0], s["cash"][0]):
        ic = eq0 + s["debt"][0] - s["cash"][0]
    q["roic"] = div(s["opinc"][0] * (1 - d["tax_rate"]), ic) if (s["opinc"][0] is not None and ic and ic > 0) else None
    q["gm"] = div(s["gp"][0], rev0)
    q["opm"] = div(s["opinc"][0], rev0)
    q["npm"] = div(ni0, rev0)
    q["de"] = div(s["debt"][0], eq0) * 100 if (eq0 or 0) > 0 and s["debt"][0] is not None else None
    e0 = s["ebitda"][0]
    q["nd_ebitda"] = div(d["net_debt"], e0) if (e0 or 0) > 0 and d["net_debt"] is not None else None
    q["cr"] = div(s["ca"][0], s["cl"][0])
    q["icov"] = div(s["opinc"][0], s["interest"][0]) if (s["interest"][0] or 0) > 0 else None
    q["fcf_conv"] = div(s["fcf"][0], ni0) if (ni0 or 0) > 0 else None
    q["fcf_cv"] = cv(s["fcf"])
    q["ni_cv"] = cv(s["ni"])
    q["rev_g"] = growth(rev0, s["rev"][1])
    q["ni_g"] = growth(ni0, s["ni"][1])
    q["eq_ta"] = div(eq0, ta0)
    return q


def piotroski(d):
    s = d["s"]

    def cmp(a, b, op):
        return None if a is None or b is None else op(a, b)

    roa = [div(s["ni"][i], s["ta"][i]) for i in (0, 1)]
    cr = [div(s["ca"][i], s["cl"][i]) for i in (0, 1)]
    lev = [div(s["ltd"][i] if s["ltd"][i] is not None else s["debt"][i], s["ta"][i]) for i in (0, 1)]
    gm = [div(s["gp"][i], s["rev"][i]) for i in (0, 1)]
    at = [div(s["rev"][i], s["ta"][i]) for i in (0, 1)]
    comps = [
        ("1 Net income > 0", cmp(s["ni"][0], 0, lambda a, b: a > b)),
        ("2 CFO > 0", cmp(s["cfo"][0], 0, lambda a, b: a > b)),
        ("3 ROA improved", cmp(roa[0], roa[1], lambda a, b: a > b)),
        ("4 CFO > net income", cmp(s["cfo"][0], s["ni"][0], lambda a, b: a > b)),
        ("5 Leverage decreased", cmp(lev[0], lev[1], lambda a, b: a < b)),
        ("6 Current ratio improved", cmp(cr[0], cr[1], lambda a, b: a > b)),
        ("7 No share dilution", cmp(s["shares"][0], s["shares"][1], lambda a, b: a <= b * 1.005)),
        ("8 Gross margin improved", cmp(gm[0], gm[1], lambda a, b: a > b)),
        ("9 Asset turnover improved", cmp(at[0], at[1], lambda a, b: a > b)),
    ]
    n = sum(1 for _, v in comps if v is not None)
    return sum(1 for _, v in comps if v), n, comps


def risk_engine(d, q, cov):
    s, bank = d["s"], d["seg"] in ("Bank", "Insurance")
    ni0, ni1 = s["ni"][0], s["ni"][1]
    fl = []

    def add(name, w, cond, detail):
        fl.append((name, w, None if cond is None else ("TRIGGERED" if cond else "OK"), detail))

    add("Financial distress", 12, anyc(None if s["eq"][0] is None else s["eq"][0] <= 0,
                                       None if q["icov"] is None else q["icov"] < 1.5,
                                       None if (ni0 is None or s["cfo"][0] is None) else (ni0 < 0 and s["cfo"][0] < 0)),
        "equity<=0, interest cover<1.5, or NI<0 & CFO<0")
    lev = anyc(None if q["eq_ta"] is None else q["eq_ta"] < 0.06) if bank else anyc(
        None if q["nd_ebitda"] is None else q["nd_ebitda"] > 4, None if q["de"] is None else q["de"] > 200)
    add("Leverage", 12, lev, "bank: equity/assets<6%; else ND/EBITDA>4 or D/E>200%")
    add("Liquidity", 10, None if bank else (None if q["cr"] is None else q["cr"] < 1), "current ratio < 1")
    add("Negative FCF", 10, None if bank else (None if s["fcf"][0] is None else s["fcf"][0] < 0), "FCF < 0")
    ed = None
    if ni0 is not None and ni1 is not None:
        ed = ni0 < 0 or (ni1 > 0 and ni0 < 0.8 * ni1)
    add("Earnings deterioration", 10, ed, "NI < 0 or down >20% y/y")
    md = None
    if not bank and q["opm"] is not None and s["opinc"][1] is not None and (s["rev"][1] or 0) > 0:
        md = q["opm"] < s["opinc"][1] / s["rev"][1] - 0.02
    add("Margin deterioration", 8, md, "operating margin down >2 pts")
    dv = None if bank or ni0 is None or s["cfo"][0] is None or ni0 <= 0 else s["cfo"][0] < 0.5 * ni0
    add("CFO vs NI divergence", 10, dv, "CFO < 50% of positive NI")
    dil = cmp_g = growth(s["shares"][0], s["shares"][1])
    add("Dilution", 8, None if dil is None else dil > 0.03, "share count +3% y/y")
    ev = anyc(None if d["pe"] is None and d["eps"] is None else (d["eps"] is not None and d["eps"] <= 0) or (d["pe"] is not None and d["pe"] > 40),
              None if d["pb"] is None else d["pb"] > 6)
    add("Extreme valuation", 10, ev, "P/E>40, loss-making, or P/B>6")
    add("Data reliability", 10, cov < 0.7 or bool(d["flags"]), f"coverage<70% or integrity flags ({len(d['flags'])})")
    ev_w = sum(w for _, w, st, _ in fl if st is not None)
    score = None if ev_w == 0 else 100 * sum(w for _, w, st, _ in fl if st == "TRIGGERED") / ev_w
    return score, fl


# ============================================================================
# SCORING & ANALYSIS
# ============================================================================

WEIGHTS = {"Industrial": (.35, .30, .20, .15), "Bank": (.40, .35, .10, .15),
           "Insurance": (.40, .30, .15, .15), "GYO": (.45, .20, .20, .15)}


def coverage(d):
    s, seg = d["s"], d["seg"]
    f = [d["price"], d["mcap"], d["shares"], d["pb"], d["pe"], s["ni"][0], s["eq"][0], s["ta"][0], d["bvps"]]
    if seg not in ("Bank", "Insurance"):
        f += [s["rev"][0], s["cfo"][0], s["debt"][0], s["cash"][0], d["fcff"], s["ebitda"][0], d["oe"]]
    return sum(x is not None for x in f) / len(f)


def analyze(d, P, med, probs=(0.25, 0.5, 0.25)):
    A: Dict[str, Any] = {"d": d}
    infl, seg = P["infl"], d["seg"]
    q = quality(d)
    A["q"] = q
    A["methods"], A["iv"] = intrinsic(d, P, med)
    A["mos"] = (A["iv"] - d["price"]) / A["iv"] if A["iv"] else None
    rv, A["rel_note"] = relative_value(d, med)
    A["rel_mos"] = (rv - d["price"]) / rv if rv else None
    A["pio"] = piotroski(d)
    A["cov"] = coverage(d)
    A["risk"], A["flags"] = risk_engine(d, q, A["cov"])
    bank = seg in ("Bank", "Insurance")
    rroe, rroic = real_ret(q["roe"], infl), real_ret(q["roic"], infl)
    rrev, rni = real_ret(q["rev_g"], infl), real_ret(q["ni_g"], infl)
    pe_s = (0.0 if d["eps"] <= 0 else lin(d["pe"], 25, 4)) if d["eps"] is not None else None
    pb_s = lin(d["pb"], 4, 0.5) if d["pb"] else (0.0 if d["bvps"] is not None and d["bvps"] <= 0 else None)
    vi = [(lin(A["mos"], -0.3, 0.5), 4), (lin(A["rel_mos"], -0.3, 0.5), 2)]
    if bank:
        vi += [(pe_s, 2), (pb_s, 3), (lin(d["dy"], 0, 0.08), 1)]
    elif seg == "GYO":
        vi += [(lin(d["pb"], 1.5, 0.4), 3), (pe_s, 1)]
    else:
        vi += [(pe_s, 1), (pb_s, 1), (lin(d["ev_ebitda"], 15, 3), 1), (lin(d["fcf_yield"], -0.02, 0.12), 1),
               (lin(d["oe_yield"], -0.02, 0.12), 1), (lin(d["earn_yield"], 0, 0.25), 1)]
    if bank:
        qi = [(lin(rroe, -0.1, 0.2), 4), (lin(q["roa"], 0, 0.03), 2), (lin(q["ni_cv"], 2, 0.3), 2),
              (lin(d["dy"], 0, 0.08), 1), (lin(rni, -0.2, 0.3), 1)]
    else:
        qi = [(lin(rroe, -0.1, 0.2), 3), (lin(rroic, -0.05, 0.15), 3), (lin(q["opm"], 0.02, 0.25), 2),
              (lin(q["fcf_conv"], 0, 1.2), 2), (lin(q["fcf_cv"], 2, 0.3), 1), (lin(rrev, -0.1, 0.2), 1), (lin(rni, -0.2, 0.3), 1)]
    sc, n, _ = A["pio"]
    e0 = d["s"]["ebitda"][0]
    if bank:
        si = [(lin(q["eq_ta"], 0.04, 0.12), 4), (lin(q["roa"], 0, 0.02), 1)]
    else:
        nde = 0.0 if (e0 is not None and e0 <= 0 and (d["net_debt"] or 0) > 0) else lin(q["nd_ebitda"], 5, 0)
        if q["nd_ebitda"] is None and d["net_debt"] is not None and d["net_debt"] <= 0 and (e0 or 0) > 0:
            nde = 100.0  # net cash
        si = [(nde, 3), (lin(q["cr"], 0.8, 2.0), 2), (lin(q["icov"], 1, 8), 2), (lin(q["de"], 250, 30), 2),
              (lin(sc / n, 0.3, 0.8) if n >= 5 else None, 2)]
    A["val"], A["val_cov"] = wavg(vi)
    A["qual"], A["qual_cov"] = wavg(qi)
    A["str"], A["str_cov"] = wavg(si)
    w = WEIGHTS[seg]
    comps = [A["val"], A["qual"], A["str"], None if A["risk"] is None else 100 - A["risk"]]
    if A["val"] is None or A["qual"] is None:
        A["composite"] = None
    else:
        av = [(c, wt) for c, wt in zip(comps, w) if c is not None]
        A["composite"] = sum(c * wt for c, wt in av) / sum(wt for _, wt in av) * min(1.0, A["cov"] / 0.7)
    sev = (not d["unit_ok"]) or any("stale" in f or "period" in f for f in d["flags"])
    lvl = 2 if A["cov"] >= 0.85 else 1 if A["cov"] >= 0.65 else 0
    lvl = max(lvl - (1 if sev else 0), 0)
    A["conf"] = ["Low", "Medium", "High"][lvl]
    pr = validate_probs(probs[0] * 100, probs[1] * 100) or (0.25, 0.5, 0.25)
    A["scen"], A["exp"] = scenarios(d, P, med, pr)
    return A


# ============================================================================
# OUTPUT BUILDERS
# ============================================================================

def get_A(ticker, P, probs=(0.25, 0.5, 0.25)):
    sym = norm_sym(ticker)
    d = fetch(sym)
    if d is None:
        return None, f"⚠️ {sym} verisi çekilemedi."
    return analyze(d, P, build_medians(cached_ds()), probs), None


def price_fig(sym, extra=None):
    try:
        h = yf.Ticker(norm_sym(sym)).history(period="1y")
        if h.empty:
            return None
        f = go.Figure(go.Scatter(x=h.index, y=h["Close"], mode="lines", name="Close"))
        for nm, v in (extra or {}).items():
            if v:
                f.add_hline(y=v, line_dash="dot", annotation_text=nm)
        f.update_layout(title=f"{sym} — 1Y", template="plotly_white", height=380, margin=dict(l=10, r=10, t=40, b=10))
        return f
    except Exception:
        return None


def scan_row(A):
    d, q = A["d"], A["q"]
    sc, n, _ = A["pio"]
    e = A["exp"]
    oe_v = A["methods"].get("Owner Earnings", (None, ""))[0]
    return {
        "Ticker": d["sym"], "Segment": d["seg"], "Sector": d["sector"], "Price": round(d["price"], 2),
        "Composite": None if A["composite"] is None else round(A["composite"], 1),
        "Intrinsic": None if A["iv"] is None else round(A["iv"], 2),
        "MoS %": None if A["mos"] is None else round(A["mos"] * 100, 1),
        "P/E": None if d["pe"] is None else round(d["pe"], 1), "P/B": None if d["pb"] is None else round(d["pb"], 2),
        "ROE %": None if q["roe"] is None else round(q["roe"] * 100, 1),
        "ROIC %": None if q["roic"] is None else round(q["roic"] * 100, 1),
        "FCF Yield %": None if d["fcf_yield"] is None else round(d["fcf_yield"] * 100, 1),
        "OE Yield %": None if d["oe_yield"] is None else round(d["oe_yield"] * 100, 1),
        "OE Value": None if oe_v is None else round(oe_v, 2),
        "OE MoS %": None if oe_v is None else round((oe_v - d["price"]) / oe_v * 100, 1),
        "Piotroski": f"{sc}/{n} (cov {n}/9)" if n else "N/A",
        "Risk": None if A["risk"] is None else round(A["risk"], 0),
        "Exp Return % (nom, incl div)": None if not e else round(e["total"] * 100, 1),
        "Real Exp Return %": None if not e or e["real"] is None else round(e["real"] * 100, 1),
        "Coverage %": round(A["cov"] * 100), "Confidence": A["conf"],
    }


def run_scan(force, extra, P, progress):
    syms = BIST_UNIVERSE + [norm_sym(x) for x in (extra or "").split(",") if x.strip()]
    syms = list(dict.fromkeys(syms))
    ds, failed = [], []
    for i, s in enumerate(syms):
        progress((i + 1) / len(syms), desc=f"Fetching {s}")
        was_cached = (not force) and _cget(f"t_{s}") is not None
        d = fetch(s, force)
        (ds.append(d) if d else failed.append(s))
        if not was_cached:
            time.sleep(REQUEST_DELAY_SEC)
    med = build_medians(ds)
    rows = [scan_row(analyze(d, P, med)) for d in ds]
    df = pd.DataFrame(rows)
    LAST["df"], LAST["failed"] = df, failed
    return df, failed


def apply_filters(seg, minc, minm, sortby, desc):
    df = LAST["df"]
    if df is None or df.empty:
        return None, None
    x = df.copy()
    if seg != "All":
        x = x[x["Segment"] == seg]
    x = x[x["Composite"].fillna(-1) >= minc]
    if minm > -100:
        x = x[x["MoS %"].fillna(-1e9) >= minm]
    x = x.sort_values(sortby, ascending=not desc, na_position="last")
    f = tempfile.NamedTemporaryFile(delete=False, suffix=".csv", prefix="bist_v3_")
    x.to_csv(f.name, index=False)
    return x, f.name


def md_report(A, P):
    d = A["d"]
    L = [f"## {d['sym']} — {d['name']}",
         f"**{d['seg']}** · {d['sector']} / {d['industry']} · Fiyat **{fm(d['price'])} TRY** · Mali tablo: "
         f"{d['stmt_date'].date() if d['stmt_date'] else 'N/A'}",
         f"**Composite {fm(A['composite'], 0)}** | Valuation {fm(A['val'], 0)} | Quality {fm(A['qual'], 0)} | "
         f"Strength {fm(A['str'], 0)} | Risk {fm(A['risk'], 0)} (düşük=iyi) | Coverage {fp(A['cov'], 0)} | "
         f"Confidence **{A['conf']}**",
         f"**Intrinsic value [D]:** {fm(A['iv'])} TRY · Margin of Safety {fp(A['mos'])}"]
    L.append("\n| Yöntem | Değer/hisse | Not |\n|---|---|---|")
    for k, (v, n) in A["methods"].items():
        L.append(f"| {k} | {fm(v)} | {n} |")
    if d["oe"] is not None:
        L.append(f"\n**Owner Earnings [D]:** {fm(d['oe'], 0)} · OE Yield {fp(d['oe_yield'])}")
    if d["flags"]:
        L.append("\n**⚠️ Veri bayrakları:** " + "; ".join(d["flags"]))
    L.append("\n*Intrinsic = mevcut yöntemlerin medyanı. Skorlar evren içi göreceli, açıklanabilir heuristiklerdir.*")
    return "\n".join(L)


def radar_fig(A):
    cats = ["Valuation", "Quality", "Strength", "Safety (100-Risk)"]
    v = [A["val"], A["qual"], A["str"], None if A["risk"] is None else 100 - A["risk"]]
    v = [x or 0 for x in v]
    f = go.Figure(go.Scatterpolar(r=v + v[:1], theta=cats + cats[:1], fill="toself"))
    f.update_layout(polar=dict(radialaxis=dict(range=[0, 100])), height=340, margin=dict(l=30, r=30, t=30, b=20))
    return f


# ============================================================================
# SELF TEST (synthetic)
# ============================================================================

def self_test() -> str:
    res = []

    def chk(name, cond):
        res.append(("✅ " if cond else "❌ ") + name)

    r = dcf_value(100, 0, 0.10, 0, 10, 200)
    chk("DCF: g=0,tg=0 → EV=1000", r is not None and abs(r["ev"] - 1000) < 1e-6)
    chk("EV→Equity: 1000-200=800, 80/share", r is not None and abs(r["equity"] - 800) < 1e-6 and abs(r["per_share"] - 80) < 1e-6)
    chk("DCF refuses wacc<=tg", dcf_value(100, 0.1, 0.05, 0.05, 10, 0) is None)
    chk("DCF refuses FCF<=0", dcf_value(-5, 0.1, 0.2, 0.05, 10, 0) is None)
    chk("DCF refuses missing input", dcf_value(100, 0.1, 0.2, 0.05, None, 0) is None)
    chk("Real return 50% nom / 25% infl = 20%", abs(real_ret(0.5, 0.25) - 0.2) < 1e-12)
    chk("div by zero → None", div(1, 0) is None and div(None, 2) is None)
    P = dict(infl=0.30, rw=0.10, rtg=0.01, rg=0.03, pt=0.5, conv=0.5)
    g0, w, tg = assumptions(P)
    chk("WACC > terminal growth (defaults)", w > tg)
    chk("Probabilities 25/50 → 25/50/25 sum=1", validate_probs(25, 50) is not None and abs(sum(validate_probs(25, 50)) - 1) < 1e-9)
    chk("Probabilities 70/50 rejected", validate_probs(70, 50) is None)
    # reverse DCF consistency
    d = {"fcff": 100.0, "shares": 10.0, "net_debt": 200.0, "price": 80.0, "unit_ok": True, "seg": "Industrial"}
    P2 = dict(infl=0.0, rw=0.10, rtg=0.0, rg=0.0, pt=0.0, conv=0.5)
    rows, _ = reverse_dcf(d, P2)
    chk("Reverse DCF: price 80 → implied g≈0", rows is not None and rows[1]["Implied FCF growth (nominal, yr1)"] == "0.0%")
    # Piotroski incompleteness
    empty = {"s": {k: [None, None, None] for k in STMT}}
    sc, n, _ = piotroski(empty)
    chk("Piotroski all-missing → 0 available (not 9/9)", n == 0 and sc == 0)
    # peer rule
    ds = [{"seg": "Industrial", "sector": "X", "pe": 10.0 + i, **{m: None for m in MULTS if m != "pe"}} for i in range(4)]
    chk("Sector median needs >=5 peers", build_medians(ds)[("Industrial", "X")]["pe"][0] is None)
    ds += [{"seg": "Industrial", "sector": "X", "pe": 20.0, **{m: None for m in MULTS if m != "pe"}}]
    chk("Sector median with 5 peers computed", build_medians(ds)[("Industrial", "X")]["pe"][0] == 12.0 or build_medians(ds)[("Industrial", "X")]["pe"][0] is not None)
    chk("Segment classification", segment_of("Banks - Regional", "GARAN") == "Bank" and segment_of("Real Estate", "EKGYO") == "GYO"
        and segment_of("Insurance - Life", "ANHYT") == "Insurance" and segment_of("Steel", "EREGL") == "Industrial")
    oe, _ = owner_earnings(100, 50, 80, -10)
    chk("OE: NI100 + DA50 − min(capex80, DA50) + ΔWC(−10) = 90", oe == 90)
    chk("OE: eksik girdi → None (0 değil)", owner_earnings(100, None, 80, 0)[0] is None)
    pr = [dict(end=pd.Timestamp("2023-12-31"), oe=1, fcf=1, ni=1, shares=1)]
    chk("Backtest: tablo dönem sonu+gecikmeden önce kullanılmaz (look-ahead yok)",
        pick_period(pr, pd.Timestamp("2024-02-15"), 90) is None and pick_period(pr, pd.Timestamp("2024-04-15"), 90) is not None)
    chk("Backtest: >550 gün eski tablo reddedilir", pick_period(pr, pd.Timestamp("2025-09-01"), 90) is None)
    st = bt_stats(pd.Series([100, 110, 121], index=pd.to_datetime(["2020-01-01", "2021-01-01", "2022-01-01"])))
    chk("Backtest: 100→121 / 2 yıl → CAGR≈10%", st is not None and abs(st["cagr"] - 0.10) < 0.005)
    st2 = bt_stats(pd.Series([100, 80, 90], index=pd.to_datetime(["2020-01-01", "2020-06-01", "2021-01-01"])))
    chk("Backtest: max drawdown −20%", st2 is not None and abs(st2["mdd"] + 0.2) < 1e-9)
    return "\n".join(res)


# ============================================================================
# BACKTEST (DENEYSEL, OE odaklı) — point-in-time DEĞİL, yalnızca gösterge
# ============================================================================
BT_DEFAULT_LAG = 90     # yıllık tablo, dönem sonu + N gün sonra "yayınlanmış" sayılır
BT_MAX_AGE = 550        # dönem sonu üzerinden bu kadar gün geçmiş tablo kullanılmaz
BT_SIGS = {"OE": "OE Yield (top N)", "FCF": "FCF Yield (top N)", "NI": "Kazanç Getirisi (top N)"}


def stmt_at(df, names, date):
    """Tabloda tam o dönem sonu sütununu bul (kolon sırasına güvenme)."""
    if df is None or getattr(df, "empty", True):
        return None
    cols = [pd.Timestamp(c).normalize() for c in df.columns]
    try:
        i = cols.index(pd.Timestamp(date).normalize())
    except ValueError:
        return None
    return stmt(df, names, i)


def _naive_index(s):
    s = s[~s.index.duplicated(keep="last")].sort_index()
    s.index = pd.DatetimeIndex(s.index).tz_localize(None).normalize()
    return s


def fetch_bt(symbol: str, force: bool = False) -> Optional[Dict[str, Any]]:
    key = f"bt_{symbol}"
    if not force:
        c = _cget(key)
        if c is not None:
            return c
    try:
        t = yf.Ticker(symbol)
        info = t.info or {}
        fin, bs, cf = t.financials, t.balance_sheet, t.cashflow
        h = t.history(period="max", auto_adjust=False)
        try:
            spl = t.splits
        except Exception:
            spl = None
    except Exception as e:
        logger.warning("%s bt fetch failed: %s", symbol, e)
        return None
    if h is None or h.empty or fin is None or fin.empty or "Close" not in h:
        return None
    close = _naive_index(h["Close"].dropna())             # split-ayarlı, temettü-ayarsız → piyasa değeri için
    adj = _naive_index(h["Adj Close"].dropna()) if "Adj Close" in h else close  # toplam getiri için
    if spl is not None and len(spl):
        spl = _naive_index(spl)
    fc, cur = info.get("financialCurrency"), info.get("currency")
    unit_ok = (fc is None) or (cur is None) or (fc == cur)
    seg = segment_of(info.get("industry") or "", symbol.replace(".IS", ""))
    shares_now = num(info.get("sharesOutstanding"))
    periods = []
    for c in fin.columns:
        end = pd.Timestamp(c).normalize()
        ni = stmt_at(fin, STMT["ni"][0], end)
        da, cx, wc = (stmt_at(cf, STMT[k][0], end) for k in ("da", "capex", "dwc"))
        cx = abs(cx) if cx is not None else None
        cfo, fcf = stmt_at(cf, STMT["cfo"][0], end), stmt_at(cf, STMT["fcf"][0], end)
        if fcf is None and cfo is not None and cx is not None:
            fcf = cfo - cx
        oe, _ = owner_earnings(ni, da, cx, wc)
        sh = stmt_at(bs, STMT["shares"][0], end)
        if sh is not None and spl is not None and len(spl):
            sh *= float(spl[spl.index > end].prod())       # bedelsiz/bölünme sonrası hisse sayısına getir (fiyat serisiyle tutarlı)
        elif sh is None:
            sh = shares_now                                 # fallback: güncel hisse sayısı (güncel split-ayarlı fiyatla tutarlı)
        periods.append(dict(end=end, oe=oe, fcf=fcf, ni=ni, shares=sh))
    periods.sort(key=lambda p: p["end"])
    d = dict(sym=symbol.replace(".IS", ""), seg=seg, unit_ok=unit_ok, periods=periods, close=close, adj=adj)
    _cset(key, d)
    return d


def pick_period(periods, date, lag):
    """Look-ahead koruması: yalnızca (dönem sonu + lag) <= tarih olan ve çok eski olmayan en güncel tablo."""
    best = None
    for p in periods:
        if p["end"] + pd.Timedelta(days=lag) <= date and (date - p["end"]).days <= BT_MAX_AGE:
            if best is None or p["end"] > best["end"]:
                best = p
    return best


def px_asof(s, date, tol=10):
    i = s.index.searchsorted(date, side="right") - 1
    if i < 0 or (date - s.index[i]).days > tol:
        return None
    return float(s.iloc[i])


def bt_signals(bd, date, lag):
    p = pick_period(bd["periods"], date, lag)
    if p is None or not bd["unit_ok"] or not p["shares"] or p["shares"] <= 0:
        return None
    c = px_asof(bd["close"], date)
    if c is None or c <= 0:
        return None
    mc = c * p["shares"]
    return {"OE": div(p["oe"], mc), "FCF": div(p["fcf"], mc), "NI": div(p["ni"], mc), "end": p["end"]}


def bt_stats(curve):
    c = curve.dropna()
    if len(c) < 3:
        return None
    yrs = (c.index[-1] - c.index[0]).days / 365.25
    tot = c.iloc[-1] / c.iloc[0] - 1
    cagr = (1 + tot) ** (1 / yrs) - 1 if yrs >= 0.25 and tot > -1 else None
    vol = float(c.pct_change().dropna().std() * np.sqrt(252))
    mdd = float((c / c.cummax() - 1).min())
    return dict(total=tot, cagr=cagr, vol=vol, mdd=mdd, rv=(cagr / vol) if (cagr is not None and vol > 0) else None)


def run_backtest(syms, step_m, top_n, lag, cost_bps, progress):
    data, failed = {}, []
    for i, s in enumerate(syms):
        progress((i + 1) / len(syms), desc=f"Fetching {s}")
        was = _cget(f"bt_{s}") is not None
        bd = fetch_bt(s)
        if bd is None:
            failed.append(s.replace(".IS", ""))
        elif bd["seg"] == "Industrial":      # OE banka/sigorta/GYO için anlamlı değil
            data[s] = bd
        if not was:
            time.sleep(REQUEST_DELAY_SEC)
    ends = [p["end"] for bd in data.values() for p in bd["periods"]]
    if not ends:
        return None, None, None, "⚠️ Backtest için yeterli veri çekilemedi."
    T = max(bd["close"].index[-1] for bd in data.values())
    start = (min(ends) + pd.Timedelta(days=lag)).replace(day=1)
    dates = [d for d in pd.date_range(start, T, freq=pd.DateOffset(months=int(step_m))) if d < T]
    keys = list(BT_SIGS) + ["EW"]
    equity, hold, segs, per_ret = {k: 100.0 for k in keys}, {k: [] for k in keys}, {k: [] for k in keys}, {k: [] for k in keys}
    started, n_reb, first_date, n_hold = False, 0, None, []
    for j, D in enumerate(dates):
        D1 = dates[j + 1] if j + 1 < len(dates) else T
        sig = {s: bt_signals(bd, D, lag) for s, bd in data.items()}
        sig = {s: v for s, v in sig.items() if v is not None}
        n_oe = sum(1 for v in sig.values() if v["OE"] is not None)
        if not started:
            if n_oe < max(10, top_n):
                continue
            started, first_date = True, D
        n_reb += 1
        for k in keys:
            if k == "EW":
                new_h = [s for s, v in sig.items() if v["OE"] is not None]
            else:
                rk = sorted(((v[k], s) for s, v in sig.items() if v[k] is not None and v[k] > 0), reverse=True)
                new_h = [s for _, s in rk[:top_n]] if len(rk) >= 3 else hold[k]
            rels = []
            for s in new_h:
                a = data[s]["adj"]
                a0 = px_asof(a, D)
                seg = a[(a.index > D) & (a.index <= D1)]
                if a0 is None or seg.empty:
                    continue
                rels.append(seg / a0)
            old = hold[k]
            turn = (1 - len(set(new_h) & set(old)) / max(len(new_h), 1)) if old else 1.0
            equity[k] *= 1 - cost_bps / 1e4 * (turn * 2 if old else 1)
            hold[k] = new_h
            if not rels:
                continue
            path = pd.concat(rels, axis=1).sort_index().ffill().fillna(1.0).mean(axis=1)
            per_ret[k].append(float(path.iloc[-1] - 1))
            segs[k].append(path * equity[k])
            equity[k] = float(segs[k][-1].iloc[-1])
            if k == "OE":
                n_hold.append(len(rels))
    if not started or not segs["OE"]:
        return None, None, None, ("⚠️ Yeterli sinyal yok: yfinance yıllık tablo geçmişi kısa/eksik olduğundan "
                                  "en az 10 hissede OE hesaplanabilen bir başlangıç tarihi bulunamadı.")
    curves = {}
    for k in keys:
        if segs[k]:
            s = pd.concat(segs[k])
            curves[k] = pd.concat([pd.Series({first_date: 100.0}), s[~s.index.duplicated(keep="last")]]).sort_index()
    try:  # benchmark: BIST100
        hb = _naive_index(yf.Ticker("XU100.IS").history(start=(first_date - pd.Timedelta(days=10)).strftime("%Y-%m-%d"))["Close"].dropna())
        b0 = px_asof(hb, first_date)
        if b0:
            curves["XU100"] = hb[hb.index >= first_date] / b0 * 100
    except Exception as e:
        logger.warning("XU100 fetch failed: %s", e)
    names = {**BT_SIGS, "EW": "Evren eşit ağırlık (OE verisi olan)", "XU100": "BIST100 (XU100)"}
    fig = go.Figure()
    for k, c in curves.items():
        fig.add_trace(go.Scatter(x=c.index, y=c.values, mode="lines", name=names[k],
                                 line=dict(width=3 if k == "OE" else 1.5, dash="dot" if k in ("EW", "XU100") else "solid")))
    fig.update_layout(title="Kümülatif değer (başlangıç=100, nominal TRY, toplam getiri)", template="plotly_white", height=420,
                      margin=dict(l=10, r=10, t=40, b=10), legend=dict(orientation="h", y=-0.15))
    rows = []
    for k, c in curves.items():
        st = bt_stats(c)
        if st:
            rows.append({"Strateji": names[k], "Toplam %": round(st["total"] * 100, 1),
                         "CAGR %": None if st["cagr"] is None else round(st["cagr"] * 100, 1),
                         "Vol % (yıllık)": round(st["vol"] * 100, 1), "Max DD %": round(st["mdd"] * 100, 1),
                         "CAGR/Vol": None if st["rv"] is None else round(st["rv"], 2)})
    stats = pd.DataFrame(rows)
    pairs = list(zip(per_ret["OE"], per_ret["EW"]))
    hit = (sum(1 for a, b in pairs if a > b) / len(pairs)) if pairs else None
    # güncel OE sepeti (bugünkü sinyal)
    cur = {s: bt_signals(bd, T, lag) for s, bd in data.items()}
    cur = {s: v for s, v in cur.items() if v and v["OE"] is not None and v["OE"] > 0}
    top = sorted(cur.items(), key=lambda kv: kv[1]["OE"], reverse=True)[:top_n]
    picks = pd.DataFrame([{"Ticker": data[s]["sym"], "OE Yield %": round(v["OE"] * 100, 1),
                           "FCF Yield %": None if v["FCF"] is None else round(v["FCF"] * 100, 1),
                           "Kazanç Get. %": None if v["NI"] is None else round(v["NI"] * 100, 1),
                           "Tablo dönemi": v["end"].date().isoformat()} for s, v in top])
    msg = (f"✅ {len(data)} sanayi hissesi · {n_reb} rebalans ({first_date.date()} → {T.date()}) · ort. {np.mean(n_hold):.0f} hisse/sepet · "
           f"OE sepeti, evren eşit ağırlığı {('%' + format(hit * 100, '.0f') + ' dönemde geçti') if hit is not None else 'N/A'} "
           f"({len(pairs)} dönem)." + (f" ⚠️ Çekilemeyen ({len(failed)}): " + ", ".join(failed) if failed else ""))
    return fig, stats, picks, msg


BT_NOTES = """**⚠️ Deneysel — sonuçlar yatırım kararı için kullanılamaz.**
- **Dönem kısa:** yfinance yalnızca ~4 yıllık yıllık tablo verir → backtest ~3 yıl; istatistiksel olarak anlamsızdır.
- **Look-ahead kısmen önlenir:** tablo, dönem sonu + gecikme (varsayılan 90 gün) sonra kullanılır; gerçek yayın tarihi bilinmez, yeniden düzenlemeler (TMS 29 dahil) yansımaz.
- **Survivorship bias:** evren bugünün hisse listesi; çıkmış/sönmüş hisseler yok.
- **Hisse sayısı:** dönem sonu bilanço hisse sayısı × sonraki bölünme/bedelsiz çarpanı; sermaye artırımları/geri alımlar tam yakalanmaz.
- Banka/sigorta/GYO hariç (OE anlamlı değil). Getiriler nominal TRY, temettü dahil (Adj Close); maliyet tek yön bps, eşit ağırlık, buy-and-hold dönem içi."""


# ============================================================================
# GRADIO UI
# ============================================================================

def build_ui():
    with gr.Blocks(title="BIST Value Intelligence v3", theme=gr.themes.Soft()) as demo:
        gr.Markdown("# BIST Value Intelligence v3\n**⚠️ yfinance verisi point-in-time değildir; Backtest sekmesi yalnızca deneyseldir/göstergedir. "
                    "Eksik veri `N/A` gösterilir, asla sıfırla doldurulmaz. 12 aylık senaryolar heuristiktir, istatistiksel tahmin değildir. "
                    "Yatırım tavsiyesi değildir.**")
        with gr.Accordion("⚙️ Varsayımlar [A] (tüm sekmeler için)", open=False):
            with gr.Row():
                s_infl = gr.Slider(0, 100, 30, step=1, label="Enflasyon (%)")
                s_rw = gr.Slider(1, 30, 10, step=0.5, label="Reel iskonto oranı (%)")
                s_rtg = gr.Slider(0, 5, 1, step=0.25, label="Reel terminal büyüme (%)")
            with gr.Row():
                s_rg = gr.Slider(-10, 20, 3, step=0.5, label="Baz reel büyüme (%)")
                s_pt = gr.Slider(0, 100, 50, step=5, label="Enflasyonun nominal büyümeye geçişi (%)")
                s_cv = gr.Slider(0, 100, 50, step=5, label="12 ay içinde değer-fiyat farkının kapanma oranı (%)")
        PI = [s_infl, s_rw, s_rtg, s_rg, s_pt, s_cv]

        with gr.Tabs():
            with gr.Tab("📊 Market Scanner"):
                with gr.Row():
                    scan_btn = gr.Button("Run Scanner", variant="primary", scale=2)
                    force = gr.Checkbox(label="Force refresh", value=False)
                extra = gr.Textbox(label="Ek tickerlar (virgülle, örn: TRGYO,KONTR)", value="")
                with gr.Row():
                    f_seg = gr.Dropdown(["All", "Industrial", "Bank", "Insurance", "GYO"], value="All", label="Segment")
                    f_c = gr.Slider(0, 100, 0, step=5, label="Min composite")
                    f_m = gr.Slider(-100, 100, -100, step=5, label="Min MoS % (-100 = kapalı)")
                with gr.Row():
                    f_sort = gr.Dropdown(["Composite", "MoS %", "P/E", "P/B", "ROE %", "ROIC %", "FCF Yield %", "OE Yield %", "OE MoS %", "Risk",
                                          "Exp Return % (nom, incl div)", "Real Exp Return %", "Coverage %"],
                                         value="Composite", label="Sırala")
                    f_desc = gr.Checkbox(label="Azalan", value=True)
                    f_btn = gr.Button("Filtreyi uygula")
                status = gr.Markdown()
                table = gr.Dataframe(interactive=False, wrap=True)
                csv = gr.File(label="CSV indir")
                FI = [f_seg, f_c, f_m, f_sort, f_desc]

                def do_scan(frc, ext, *a, progress=gr.Progress()):
                    P = mk_params(*a[:6])
                    seg, c, m, so, de = a[6:]
                    df, failed = run_scan(frc, ext, P, progress)
                    if df.empty:
                        return None, None, "⚠️ Veri çekilemedi."
                    x, path = apply_filters(seg, c, m, so, de)
                    msg = f"✅ {len(df)} hisse tarandı · coverage ort. %{df['Coverage %'].mean():.0f}"
                    if failed:
                        msg += f" · ⚠️ Çekilemeyen ({len(failed)}): " + ", ".join(failed)
                    return x, path, msg

                def do_filter(*a):
                    x, path = apply_filters(*a)
                    return x, path

                scan_btn.click(do_scan, [force, extra] + PI + FI, [table, csv, status])
                f_btn.click(do_filter, FI, [table, csv])

            with gr.Tab("🔍 Company Analysis"):
                with gr.Row():
                    t1 = gr.Textbox(value="THYAO", label="Ticker", scale=2)
                    b1 = gr.Button("Analyze", variant="primary")
                md1 = gr.Markdown()
                with gr.Row():
                    p1 = gr.Plot()
                    r1 = gr.Plot()

                def company(t, *a):
                    P = mk_params(*a)
                    A, err = get_A(t, P)
                    if err:
                        return err, None, None
                    return md_report(A, P), price_fig(A["d"]["sym"], {"Intrinsic": A["iv"]}), radar_fig(A)

                b1.click(company, [t1] + PI, [md1, p1, r1])

            with gr.Tab("🧮 Intrinsic Value Calculator"):
                with gr.Row():
                    t2 = gr.Textbox(value="EREGL", label="Ticker", scale=2)
                    b2 = gr.Button("Calculate", variant="primary")
                md2 = gr.Markdown()
                p2 = gr.Plot()

                def calc(t, *a):
                    P = mk_params(*a)
                    A, err = get_A(t, P)
                    if err:
                        return err, None
                    d = A["d"]
                    g0, w, tg = assumptions(P)
                    L = [f"### {d['sym']} — {d['seg']}",
                         f"**Varsayımlar [A]:** yıl-1 nominal büyüme {fp(g0)} (reel {fp(P['rg'])}, enflasyon geçişi %{P['pt'] * 100:.0f}) · "
                         f"nominal iskonto {fp(w)} · nominal terminal {fp(tg)}",
                         f"**Girdiler [R/D]:** FCFF {fm(d['fcff'], 0)} (FCF + faiz×(1−vergi{' varsayım' if d['tax_assumed'] else ''})) · "
                         f"net borç {fm(d['net_debt'], 0)} · hisse {fm(d['shares'], 0)} · vergi {fp(d['tax_rate'], 0)}"]
                    if d["oe_parts"]:
                        o = d["oe_parts"]
                        L.append(f"**Owner Earnings [D]:** NI {fm(o['ni'], 0)} + D&A {fm(o['da'], 0)} − bakım capex (tahmin=min(capex, D&A)) "
                                 f"{fm(o['maint_est'], 0)} + ΔİS sermayesi {fm(o['dwc'], 0)} = **{fm(d['oe'], 0)}** (toplam capex {fm(o['capex_total'], 0)}) · OE Yield {fp(d['oe_yield'])}")
                    L.append("\n| Yöntem | Değer | Not |\n|---|---|---|")
                    for k, (v, n) in A["methods"].items():
                        L.append(f"| {k} | {fm(v)} | {n} |")
                    L.append(f"\n**Intrinsic (medyan): {fm(A['iv'])}** · Fiyat {fm(d['price'])} · MoS {fp(A['mos'])}")
                    names = [k for k, (v, _) in A["methods"].items() if v]
                    vals = [A["methods"][k][0] for k in names]
                    f = go.Figure(go.Bar(x=["Price"] + names + ["Intrinsic"], y=[d["price"]] + vals + [A["iv"] or 0]))
                    f.update_layout(template="plotly_white", height=340, margin=dict(l=10, r=10, t=20, b=10))
                    return "\n".join(L), f

                b2.click(calc, [t2] + PI, [md2, p2])

            with gr.Tab("↩️ Reverse DCF"):
                with gr.Row():
                    t3 = gr.Textbox(value="TOASO", label="Ticker", scale=2)
                    b3 = gr.Button("Solve", variant="primary")
                md3 = gr.Markdown()
                tb3 = gr.Dataframe(interactive=False)

                def rev(t, *a):
                    P = mk_params(*a)
                    A, err = get_A(t, P)
                    if err:
                        return err, None
                    d = A["d"]
                    rows, note = reverse_dcf(d, P)
                    if rows is None:
                        return f"**{d['sym']}**: ⚠️ {note}", None
                    g0, _, _ = assumptions(P)
                    h = d["s"]["fcf"]
                    hist = f"{fp((h[0] / h[2]) ** 0.5 - 1)}" if (h[0] or 0) > 0 and (h[2] or 0) > 0 else "N/A"
                    md = (f"**{d['sym']}** fiyat {fm(d['price'])} → piyasanın fiyatladığı FCF büyümesi (5 yıl, terminale doğru sönümlenir).\n\n"
                          f"Mevcut varsayım yıl-1 büyüme: {fp(g0)} · Tarihsel FCF CAGR (2y, nominal): {hist}\n\n"
                          "Not: Zorunlu bir 'doğru' yoktur; implied büyüme makul görünmüyorsa piyasa beklentisi ya aşırı iyimser ya da kötümserdir.")
                    return md, pd.DataFrame(rows)

                b3.click(rev, [t3] + PI, [md3, tb3])

            with gr.Tab("💎 Financial Quality"):
                with gr.Row():
                    t4 = gr.Textbox(value="BIMAS", label="Ticker", scale=2)
                    b4 = gr.Button("Show", variant="primary")
                md4 = gr.Markdown()
                tb4 = gr.Dataframe(interactive=False)
                tb4b = gr.Dataframe(interactive=False)

                def qual(t, *a):
                    P = mk_params(*a)
                    A, err = get_A(t, P)
                    if err:
                        return err, None, None
                    q, d = A["q"], A["d"]
                    pc = lambda x: fp(x)
                    rows = [("ROE [D]", pc(q["roe"])), ("ROE (reel) [D]", pc(real_ret(q["roe"], P["infl"]))), ("ROA [D]", pc(q["roa"])),
                            ("ROIC [D]", pc(q["roic"])), ("Gross margin [D]", pc(q["gm"])), ("Operating margin [D]", pc(q["opm"])),
                            ("Net margin [D]", pc(q["npm"])), ("Debt/Equity % [D]", fm(q["de"], 0)), ("Net debt/EBITDA [D]", fm(q["nd_ebitda"])),
                            ("Current ratio [D]", fm(q["cr"])), ("Interest coverage [D]", fm(q["icov"])), ("FCF/NI [D]", fm(q["fcf_conv"])),
                            ("FCF variation (CV) [D]", fm(q["fcf_cv"])), ("Revenue growth nominal [D]", pc(q["rev_g"])),
                            ("Revenue growth real [D]", pc(real_ret(q["rev_g"], P["infl"]))), ("Earnings growth nominal [D]", pc(q["ni_g"])),
                            ("Earnings growth real [D]", pc(real_ret(q["ni_g"], P["infl"]))), ("Equity/Assets [D]", pc(q["eq_ta"]))]
                    sc, n, comps = A["pio"]
                    cp = [(c, "N/A" if v is None else ("1" if v else "0")) for c, v in comps]
                    md = (f"**{d['sym']}** — Piotroski: **{sc}/{n}** (hesaplanabilen {n}/9)" +
                          ("" if n == 9 else " ⚠️ eksik bileşen var; skor tam değildir.") +
                          (" Bankalar için Piotroski sınırlı anlam taşır." if d["seg"] in ("Bank", "Insurance") else ""))
                    return md, pd.DataFrame(rows, columns=["Metric", "Value"]), pd.DataFrame(cp, columns=["Piotroski component", "Result"])

                b4.click(qual, [t4] + PI, [md4, tb4, tb4b])

            with gr.Tab("🛡️ Risk Analysis"):
                with gr.Row():
                    t5 = gr.Textbox(value="PGSUS", label="Ticker", scale=2)
                    b5 = gr.Button("Assess", variant="primary")
                md5 = gr.Markdown()
                tb5 = gr.Dataframe(interactive=False)

                def risk_tab(t, *a):
                    P = mk_params(*a)
                    A, err = get_A(t, P)
                    if err:
                        return err, None
                    rows = [{"Flag": n, "Weight": w, "Status": s or "N/A", "Rule": dt} for n, w, s, dt in A["flags"]]
                    return (f"**{A['d']['sym']}** Risk skoru: **{fm(A['risk'], 0)}/100** (yalnızca değerlendirilebilen bayraklar üzerinden; "
                            f"düşük = iyi). Coverage {fp(A['cov'], 0)}."), pd.DataFrame(rows)

                b5.click(risk_tab, [t5] + PI, [md5, tb5])

            with gr.Tab("🏭 Sector Comparison"):
                with gr.Row():
                    t6 = gr.Textbox(value="EREGL", label="Ticker (opsiyonel)", scale=2)
                    b6 = gr.Button("Compare", variant="primary")
                md6 = gr.Markdown()
                tb6 = gr.Dataframe(interactive=False)
                p6 = gr.Plot()

                def sector(t, *a):
                    P = mk_params(*a)
                    ds = cached_ds()
                    if not ds:
                        return "Önce Market Scanner'ı çalıştırın (medyanlar önbellekteki hisselerden hesaplanır).", None, None
                    med = build_medians(ds)
                    rows = []
                    for (seg, sec), m in sorted(med.items()):
                        r = {"Segment": seg, "Sector": sec, "Peers": sum(1 for d in ds if d["seg"] == seg and d["sector"] == sec)}
                        for f in ("pe", "pb", "ev_ebitda", "ev_fcf", "ps", "fcf_yield", "earn_yield"):
                            v, n = m[f]
                            r[f] = None if v is None else round(v, 3)
                        rows.append(r)
                    fig, md = None, f"Sektör medyanı için en az {MIN_PEERS} geçerli peer gerekir; yetersizse `None`."
                    d = fetch(norm_sym(t)) if t.strip() else None
                    if d:
                        m = med.get((d["seg"], d["sector"]))
                        keys = ["pe", "pb", "ev_ebitda", "ps"]
                        cv_ = [d[k] for k in keys]
                        mv = [m[k][0] if m else None for k in keys]
                        fig = go.Figure([go.Bar(name=d["sym"], x=keys, y=cv_), go.Bar(name="Sector median", x=keys, y=mv)])
                        fig.update_layout(barmode="group", template="plotly_white", height=340, margin=dict(l=10, r=10, t=20, b=10))
                        md = f"**{d['sym']}** — {d['seg']} / {d['sector']}. " + md
                    return md, pd.DataFrame(rows), fig

                b6.click(sector, [t6] + PI, [md6, tb6, p6])

            with gr.Tab("🎯 12-Month Scenarios"):
                with gr.Row():
                    t7 = gr.Textbox(value="AKBNK", label="Ticker", scale=2)
                    pb = gr.Slider(0, 100, 25, step=5, label="Bear olasılığı (%)")
                    pm = gr.Slider(0, 100, 50, step=5, label="Base olasılığı (%) — Bull = kalan")
                    b7 = gr.Button("Run", variant="primary")
                md7 = gr.Markdown()
                tb7 = gr.Dataframe(interactive=False)
                p7 = gr.Plot()

                def scen(t, bear, base, *a):
                    P = mk_params(*a)
                    pr = validate_probs(bear, base)
                    if pr is None:
                        return "⚠️ Bear + Base 100'ü aşamaz (Bull = 100 − Bear − Base).", None, None
                    A, err = get_A(t, P, pr)
                    if err:
                        return err, None, None
                    d, e = A["d"], A["exp"]
                    rows = [{"Scenario": o["Scenario"], "Real growth": o["Real growth"], "Real disc. rate": o["Real disc. rate"],
                             "Multiple factor": o["Multiple factor"], "Intrinsic": None if o["Intrinsic"] is None else round(o["Intrinsic"], 2),
                             "12M target": None if o["Target"] is None else round(o["Target"], 2),
                             "Probability": f"{o['Prob'] * 100:.0f}%"} for o in A["scen"]]
                    if e is None:
                        return f"**{d['sym']}**: ⚠️ Senaryo üretilemedi (intrinsic value hesaplanamadı).", pd.DataFrame(rows), None
                    md = (f"**{d['sym']}** · Olasılık-ağırlıklı hedef **{fm(e['pw'])}** · Beklenen nominal fiyat getirisi {fp(e['nominal'])} · "
                          f"Temettü-dahil nominal {fp(e['total'])}" + ("" if e["dy_known"] else " (temettü verisi yok, 0 sayıldı)") +
                          f" · Reel (temettü-dahil) {fp(e['real'])} · Reel fiyat getirisi {fp(e['real_price'])}\n\n"
                          f"*Heuristik: hedef = fiyat + k×(senaryo değeri − fiyat), k={P['conv'] * 100:.0f}%, çarpan faktörü uygulanır. "
                          "İstatistiksel olarak doğrulanmış tahmin değildir. Temettü yalnızca bir kez eklenir.*")
                    f = go.Figure(go.Bar(x=["Price", "Bear", "Base", "Bull", "Prob-weighted"],
                                         y=[d["price"]] + [o["Target"] for o in A["scen"]] + [e["pw"]]))
                    f.update_layout(template="plotly_white", height=340, margin=dict(l=10, r=10, t=20, b=10))
                    return md, pd.DataFrame(rows), f

                b7.click(scen, [t7, pb, pm] + PI, [md7, tb7, p7])

            with gr.Tab("📋 Data Quality Monitor"):
                b8 = gr.Button("Check Data Quality", variant="primary")
                md8 = gr.Markdown()
                tb8 = gr.Dataframe(interactive=False, wrap=True)

                def dq(progress=gr.Progress()):
                    rows, failed = [], []
                    for i, s in enumerate(BIST_UNIVERSE):
                        progress((i + 1) / len(BIST_UNIVERSE), desc=f"Checking {s}")
                        was = _cget(f"t_{s}") is not None
                        d = fetch(s)
                        if d is None:
                            failed.append(s.replace(".IS", ""))
                            continue
                        c = coverage(d)
                        rows.append({"Ticker": d["sym"], "Segment": d["seg"], "Coverage %": round(c * 100),
                                     "Statement date": d["stmt_date"].date().isoformat() if d["stmt_date"] else "N/A",
                                     "Age (days)": d["age"], "Status": "OK" if c >= 0.7 and not d["flags"] else "CHECK",
                                     "Flags": "; ".join(d["flags"]) or "—"})
                        if not was:
                            time.sleep(REQUEST_DELAY_SEC * 0.5)
                    df = pd.DataFrame(rows)
                    avg = f"{df['Coverage %'].mean():.0f}%" if not df.empty else "N/A"
                    return (f"Ortalama coverage: **{avg}** · Başarılı {len(rows)} · Çekilemeyen {len(failed)}: "
                            + (", ".join(failed) or "—")), df

                b8.click(dq, None, [md8, tb8])

            with gr.Tab("📈 Backtest (OE, deneysel)"):
                gr.Markdown(BT_NOTES)
                with gr.Row():
                    bt_top = gr.Slider(5, 30, 10, step=1, label="Sepet büyüklüğü (top N)")
                    bt_step = gr.Dropdown([3, 6, 12], value=3, label="Rebalans (ay)")
                    bt_lag = gr.Slider(30, 180, BT_DEFAULT_LAG, step=15, label="Yayın gecikmesi (gün)")
                with gr.Row():
                    bt_cost = gr.Slider(0, 100, 20, step=5, label="İşlem maliyeti (bps, tek yön)")
                    bt_n = gr.Slider(20, len(BIST_UNIVERSE), len(BIST_UNIVERSE), step=1, label="Evren: ilk N hisse (az = hızlı)")
                    bt_btn = gr.Button("Backtest çalıştır", variant="primary")
                bt_msg = gr.Markdown()
                bt_plot = gr.Plot()
                bt_stats_df = gr.Dataframe(interactive=False, label="Performans")
                gr.Markdown("**Güncel OE sepeti** (bugünkü sinyal; öneri değildir)")
                bt_picks = gr.Dataframe(interactive=False)

                def do_bt(top, step, lag, cost, n, progress=gr.Progress()):
                    fig, stats, picks, msg = run_backtest(BIST_UNIVERSE[: int(n)], int(step), int(top), int(lag), float(cost), progress)
                    return msg, fig, stats, picks

                bt_btn.click(do_bt, [bt_top, bt_step, bt_lag, bt_cost, bt_n], [bt_msg, bt_plot, bt_stats_df, bt_picks])

            with gr.Tab("📐 Methodology"):
                gr.Markdown(METHODOLOGY)
                b9 = gr.Button("Run self-test (synthetic data)")
                o9 = gr.Textbox(label="Self-test", lines=18, interactive=False)
                b9.click(self_test, None, o9)
    return demo


METHODOLOGY = """
### Değerleme mimarisi
- **DCF (sanayi/GYO dışı):** FCFF = FCF + faiz×(1−vergi) (Yahoo FCF'si faiz sonrası olduğu için borç çift sayılmasın diye). 5 yıl, büyüme yıl-1'den terminale doğrusal sönümlenir; EV − net borç = özsermaye; / hisse. Girdi eksik, FCFF ≤ 0 veya iskonto ≤ terminal büyüme ise **hesaplanmaz**.
- **Owner Earnings:** NI + D&A − bakım capex + ΔİSermayesi. Bakım capex = min(toplam capex, D&A) **tahminidir**; veri eksikse yöntem kullanılmaz. Özsermaye nakit akışı olduğu için net borç düşülmez. **OE Yield** = OE / piyasa değeri; sanayi segmentinde Valuation skoruna (ağırlık 1) dahildir, taramada OE Yield / OE Value / OE MoS sütunları vardır.
- **Banka/Sigorta:** Gordon gerekçeli P/B = (sürdürülebilir ROE − g)/(COE − g) × defter değeri. FCF/EV-EBITDA kullanılmaz. Sermaye yeterliliği/aktif kalitesi Yahoo'da yok.
- **GYO:** NAV vekili = defter değeri (ekspertiz NAV'ı yok), P/NAV. Doluluk verisi yok.
- **Relative:** sektör medyanı (en az 5 geçerli peer) P/E, P/B, EV/EBITDA → hisse başına değer.
- **Intrinsic = mevcut yöntemlerin medyanı.** MoS = (IV − fiyat)/IV.
- **Reverse DCF:** fiyatı veren yıl-1 FCF büyümesi (bisection), iskonto oranı ±3 puan duyarlılığı.
- **Enflasyon:** iskonto ve terminal büyüme reel girilir, nominale çevrilir; enflasyonun yalnızca ayarlanabilir bir kısmı (geçiş oranı) kâr büyümesine yansır. Reel getiri = (1+nominal)/(1+enflasyon) − 1.
- **Senaryolar:** Bear/Base/Bull = reel büyüme ∓4 puan, reel iskonto +3/−2 puan, çarpan faktörü 0.85/1.0/1.10; hedef = fiyat + k×(değer − fiyat). Olasılıklar ayarlanabilir, toplam %100 (Bull = kalan).

### Skorlama (0-100, açıklanabilir)
Composite = ağırlıklı ortalama (mevcut bileşenler üzerinden yeniden normalize) × min(1, coverage/0.7).
| Segment | Valuation | Quality | Strength | Risk (ters) |
|---|---|---|---|---|
| Sanayi | 35 | 30 | 20 | 15 |
| Banka | 40 | 35 | 10 | 15 |
| Sigorta | 40 | 30 | 15 | 15 |
| GYO | 45 | 20 | 20 | 15 |

Risk (0-100): 10 bayrak (distress, kaldıraç, likidite, negatif FCF, kâr/marj bozulması, CFO-NI ayrışması, seyrelme, aşırı değerleme, veri güvenilirliği); yalnızca değerlendirilebilen bayrakların ağırlığı üzerinden hesaplanır. Piotroski 9 bileşenli; hesaplanamayan bileşen "N/A" olur ve skor `x/n` olarak gösterilir.

### Bilinen veri sınırlamaları
- yfinance point-in-time değildir; Backtest sekmesi deneyseldir (≈3 yıl, survivorship bias, tahmini yayın gecikmesi) ve kanıt değil göstergedir. Bilanço tarihleri ve kapsam hisseden hisseye değişir.
- Yıllık tablolar gecikmelidir; TFRS/enflasyon muhasebesi (TMS 29) etkileri nominal rakamları çarpıtabilir.
- Evren: gömülü liste + eklediğiniz tickerlar (tüm BIST listesi Yahoo'dan ücretsiz çekilemez).
- Sektör/segment sınıflaması Yahoo `industry` alanına dayanır; peer sayısı <5 ise medyan hesaplanmaz.
- Sonuçlar araştırma/eğitim amaçlıdır, yatırım tavsiyesi değildir.
"""

if __name__ == "__main__":
    logger.info("Self-test:\n%s", self_test())
    build_ui().launch(server_name="0.0.0.0", server_port=7860)