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
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
import streamlit as st
import yfinance as yf
import plotly.graph_objects as go
import plotly.express as px
from plotly.subplots import make_subplots
import pandas as pd
import numpy as np
from datetime import datetime, timedelta
from fpdf import FPDF
import io
import base64
import json
import tempfile
import os
import json

# ─────────────────────────────────────────────
# PAGE CONFIG & THEME
# ─────────────────────────────────────────────
st.set_page_config(
    page_title="FinScope | Global Financial Dashboard",
    page_icon="πŸ“Š",
    layout="wide",
    initial_sidebar_state="expanded",
)

# ─────────────────────────────────────────────
# CUSTOM CSS
# ─────────────────────────────────────────────
st.markdown("""

<style>

    @import url('https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700;800;900&display=swap');



    /* Global */

    .stApp {

        font-family: 'Inter', sans-serif;

    }

    

    /* Header */

    .main-header {

        background: linear-gradient(135deg, #0f0c29, #302b63, #24243e);

        padding: 1.5rem 2rem;

        border-radius: 16px;

        margin-bottom: 1.5rem;

        box-shadow: 0 8px 32px rgba(0,0,0,0.3);

        border: 1px solid rgba(255,255,255,0.08);

    }

    .main-header h1 {

        color: #ffffff;

        font-size: 2rem;

        font-weight: 800;

        margin: 0;

        letter-spacing: -0.5px;

    }

    .main-header p {

        color: rgba(255,255,255,0.6);

        font-size: 0.9rem;

        margin: 0.25rem 0 0 0;

    }



    /* Metric cards */

    .metric-card {

        background: linear-gradient(145deg, #1a1a2e, #16213e);

        border: 1px solid rgba(255,255,255,0.06);

        border-radius: 14px;

        padding: 1.2rem 1.4rem;

        text-align: center;

        box-shadow: 0 4px 20px rgba(0,0,0,0.15);

        transition: transform 0.2s ease, box-shadow 0.2s ease;

    }

    .metric-card:hover {

        transform: translateY(-2px);

        box-shadow: 0 8px 30px rgba(0,0,0,0.25);

    }

    .metric-label {

        color: rgba(255,255,255,0.5);

        font-size: 0.72rem;

        font-weight: 600;

        text-transform: uppercase;

        letter-spacing: 1px;

        margin-bottom: 0.4rem;

    }

    .metric-value {

        color: #ffffff;

        font-size: 1.5rem;

        font-weight: 700;

        line-height: 1.2;

    }

    .metric-delta-pos { color: #00e676; font-size: 0.82rem; font-weight: 600; }

    .metric-delta-neg { color: #ff5252; font-size: 0.82rem; font-weight: 600; }



    /* Section headers */

    .section-header {

        color: #ffffff;

        font-size: 1.2rem;

        font-weight: 700;

        margin: 1.5rem 0 0.8rem 0;

        padding-bottom: 0.5rem;

        border-bottom: 2px solid rgba(255,255,255,0.06);

        letter-spacing: -0.3px;

    }



    /* Info box */

    .info-box {

        background: linear-gradient(145deg, #1a1a2e, #16213e);

        border: 1px solid rgba(255,255,255,0.06);

        border-radius: 12px;

        padding: 1.2rem;

        margin: 0.5rem 0;

    }

    .info-box .label {

        color: rgba(255,255,255,0.45);

        font-size: 0.72rem;

        font-weight: 600;

        text-transform: uppercase;

        letter-spacing: 0.8px;

    }

    .info-box .value {

        color: #ffffff;

        font-size: 0.95rem;

        font-weight: 500;

        margin-top: 0.15rem;

    }



    /* Recommendation badge */

    .rec-badge {

        display: inline-block;

        padding: 0.35rem 1rem;

        border-radius: 50px;

        font-size: 0.78rem;

        font-weight: 700;

        text-transform: uppercase;

        letter-spacing: 0.5px;

    }

    .rec-buy { background: rgba(0,230,118,0.15); color: #00e676; border: 1px solid rgba(0,230,118,0.3); }

    .rec-sell { background: rgba(255,82,82,0.15); color: #ff5252; border: 1px solid rgba(255,82,82,0.3); }

    .rec-hold { background: rgba(255,193,7,0.15); color: #ffc107; border: 1px solid rgba(255,193,7,0.3); }

    .rec-neutral { background: rgba(144,164,174,0.15); color: #90a4ae; border: 1px solid rgba(144,164,174,0.3); }



    /* Sidebar styling */

    section[data-testid="stSidebar"] {

        background: linear-gradient(180deg, #0d1117, #161b22);

        border-right: 1px solid rgba(255,255,255,0.06);

    }

    section[data-testid="stSidebar"] .stSelectbox label,

    section[data-testid="stSidebar"] .stMultiSelect label,

    section[data-testid="stSidebar"] .stTextInput label {

        color: rgba(255,255,255,0.7) !important;

        font-weight: 600;

    }



    /* Global index card */

    .index-card {

        background: rgba(255,255,255,0.03);

        border: 1px solid rgba(255,255,255,0.06);

        border-radius: 10px;

        padding: 0.7rem 1rem;

        margin-bottom: 0.5rem;

    }

    .index-name { color: rgba(255,255,255,0.7); font-size: 0.78rem; font-weight: 600; }

    .index-price { color: #ffffff; font-size: 1rem; font-weight: 700; }

    .index-change-pos { color: #00e676; font-size: 0.78rem; font-weight: 600; }

    .index-change-neg { color: #ff5252; font-size: 0.78rem; font-weight: 600; }



    /* Hide streamlit branding */

    #MainMenu {visibility: hidden;}

    footer {visibility: hidden;}

    header {visibility: hidden;}

    

    /* Tab styling */

    .stTabs [data-baseweb="tab-list"] {

        gap: 0px;

        background: rgba(255,255,255,0.03);

        border-radius: 12px;

        padding: 4px;

    }

    .stTabs [data-baseweb="tab"] {

        border-radius: 10px;

        padding: 8px 20px;

        font-weight: 600;

        font-size: 0.85rem;

    }

    .stTabs [aria-selected="true"] {

        background: linear-gradient(135deg, #667eea, #764ba2) !important;

    }



    /* Download button */

    .stDownloadButton > button {

        background: linear-gradient(135deg, #667eea, #764ba2) !important;

        color: white !important;

        border: none !important;

        border-radius: 10px !important;

        padding: 0.6rem 1.5rem !important;

        font-weight: 600 !important;

        transition: all 0.3s ease !important;

    }

    .stDownloadButton > button:hover {

        transform: translateY(-1px) !important;

        box-shadow: 0 6px 20px rgba(102,126,234,0.4) !important;

    }

</style>

""", unsafe_allow_html=True)

# ─────────────────────────────────────────────
# CONSTANTS
# ─────────────────────────────────────────────
GLOBAL_INDICES = {
    # Indices
    "S&P 500": "^GSPC",
    "NASDAQ": "^IXIC",
    "Dow Jones": "^DJI",
    "FTSE 100": "^FTSE",
    "DAX": "^GDAXI",
    "Nikkei 225": "^N225",
    "Hang Seng": "^HSI",
    "SENSEX": "^BSESN",
    "NIFTY 50": "^NSEI",
    "ASX 200": "^AXJO",
    # Currencies
    "EUR/USD": "EURUSD=X",
    "GBP/USD": "GBPUSD=X",
    "USD/JPY": "JPY=X",
    # Commodities
    "Gold": "GC=F",
    "Brent Oil": "BZ=F",
    "Silver": "SI=F",
    # Crypto
    "Bitcoin": "BTC-USD",
    "Ethereum": "ETH-USD",
}

POPULAR_STOCKS = [
    "AAPL", "MSFT", "GOOGL", "AMZN", "NVDA", "META", "TSLA", "BRK-B",
    "JPM", "V", "JNJ", "WMT", "PG", "MA", "UNH", "HD", "DIS", "BAC",
    "XOM", "NFLX", "ADBE", "CRM", "AMD", "INTC", "CSCO", "PFE", "KO",
    "PEP", "ABT", "MRK", "COST", "NKE", "ORCL", "LLY", "TMO", "AVGO",
    "TXN", "QCOM", "PYPL", "INTU", "AMAT", "ISRG", "BKNG", "GILD",
    "MDLZ", "ADP", "REGN", "VRTX", "LRCX", "MU",
]

PERIOD_MAP = {
    "1 Day": "1d", "5 Days": "5d", "1 Month": "1mo", "3 Months": "3mo",
    "6 Months": "6mo", "1 Year": "1y", "2 Years": "2y", "5 Years": "5y",
    "10 Years": "10y", "Max": "max",
}

INTERVAL_MAP = {
    "1 Day": "5m", "5 Days": "15m", "1 Month": "1d", "3 Months": "1d",
    "6 Months": "1d", "1 Year": "1wk", "2 Years": "1wk", "5 Years": "1mo",
    "10 Years": "1mo", "Max": "1mo",
}

# ─────────────────────────────────────────────
# DATA FETCHING HELPERS
# ─────────────────────────────────────────────
@st.cache_data(ttl=3600)
def search_ticker(query: str):
    """Search for tickers based on a company name query."""
    try:
        search = yf.Search(query, max_results=8)
        results = []
        for res in search.quotes:
            if res.get('quoteType') in ('EQUITY', 'ETF', 'MUTUALFUND', 'INDEX', 'CRYPTOCURRENCY'):
                name = res.get('shortname') or res.get('longname') or res.get('symbol')
                results.append({
                    "symbol": res['symbol'],
                    "name": name,
                    "type": res.get('quoteType'),
                    "exchange": res.get('exchDisp', 'N/A')
                })
        return results
    except Exception:
        return []

def get_industry_peers(industry: str, sector: str):
    """Get a list of top 3 peer tickers for a given industry/sector."""
    # Mapping of common industries to global leaders (tickers)
    peer_map = {
        "Consumer Electronics": ["MSFT", "GOOGL", "NVDA"],
        "Softwareβ€”Infrastructure": ["AMZN", "GOOGL", "ORCL"],
        "Internet Content & Information": ["META", "GOOGL", "SNAP"],
        "Semiconductors": ["NVDA", "TSM", "AMD"],
        "Electric Vehicles": ["TSLA", "BYDDF", "NIO"],
        "Auto Manufacturers": ["TM", "TSLA", "VOW3.DE"],
        "Banksβ€”Diversified": ["JPM", "BAC", "HSBC"],
        "Retailers": ["WMT", "AMZN", "COST"],
        "Energy": ["XOM", "CVX", "SHEL.L"],
        "Beveragesβ€”Non-Alcoholic": ["KO", "PEP", "MNST"],
        "Entertainment": ["DIS", "NFLX", "WBD"]
    }
    
    peers = peer_map.get(industry)
    if not peers:
        # Fallback to search-based discovery for niche industries
        try:
            search = yf.Search(industry, max_results=5)
            peers = [q['symbol'] for q in search.quotes if q.get('quoteType') == 'EQUITY'][:3]
        except Exception:
            peers = ["AAPL", "MSFT", "GOOGL"] # Final fallback
            
    return peers

@st.cache_data(ttl=300)
def fetch_peer_data(tickers: list):
    """Fetch key comparison metrics for a list of tickers."""
    data = []
    for t in tickers:
        try:
            s = yf.Ticker(t)
            i = s.info
            data.append({
                "Ticker": t,
                "Name": i.get("shortName") or i.get("longName") or t,
                "Price": i.get("currentPrice") or i.get("regularMarketPrice"),
                "Market Cap": i.get("marketCap"),
                "P/E Ratio": i.get("trailingPE"),
                "Div Yield": i.get("dividendYield"),
                "Revenue": i.get("totalRevenue"),
                "Currency": i.get("currency", "USD")
            })
        except Exception:
            continue
    return pd.DataFrame(data)

# ─────────────────────────────────────────────
# CACHING (Stock Data)
# ─────────────────────────────────────────────
@st.cache_data(ttl=300)
def fetch_stock_data(ticker: str):
    """Fetch comprehensive stock data from yfinance."""
    stock = yf.Ticker(ticker)
    info = {}
    try:
        info = stock.info
        if not info or 'longName' not in info:
            # Fallback for missing info
            hist = stock.history(period="1d")
            if not hist.empty:
                info['currentPrice'] = hist['Close'].iloc[-1]
                info['longName'] = ticker
    except Exception:
        pass
    return info


@st.cache_data(ttl=120)
def fetch_history(ticker: str, period: str, interval: str):
    """Fetch historical price data."""
    stock = yf.Ticker(ticker)
    return stock.history(period=period, interval=interval)


@st.cache_data(ttl=600)
def fetch_index_data(symbol: str):
    """Fetch current index price."""
    try:
        t = yf.Ticker(symbol)
        hist = t.history(period="2d")
        if len(hist) >= 2:
            current = hist["Close"].iloc[-1]
            prev = hist["Close"].iloc[-2]
            change = current - prev
            pct = (change / prev) * 100
            return current, change, pct
        elif len(hist) == 1:
            return hist["Close"].iloc[-1], 0, 0
    except Exception:
        pass
    return None, None, None


@st.cache_data(ttl=300)
def fetch_recommendations(ticker: str):
    """Fetch analyst recommendations."""
    try:
        stock = yf.Ticker(ticker)
        recs = stock.recommendations
        if recs is not None and not recs.empty:
            return recs
    except Exception:
        pass
    return pd.DataFrame()


@st.cache_data(ttl=300)
def fetch_actions(ticker: str):
    """Fetch stock actions (dividends, splits)."""
    try:
        stock = yf.Ticker(ticker)
        return stock.actions
    except Exception:
        return pd.DataFrame()


@st.cache_data(ttl=300)
def fetch_financials(ticker: str):
    """Fetch financial statements."""
    try:
        stock = yf.Ticker(ticker)
        income = stock.income_stmt
        balance = stock.balance_sheet
        cashflow = stock.cashflow
        return income, balance, cashflow
    except Exception:
        return pd.DataFrame(), pd.DataFrame(), pd.DataFrame()


@st.cache_data(ttl=300)
def fetch_news(ticker: str):
    """Fetch latest stock news."""
    try:
        stock = yf.Ticker(ticker)
        return stock.news
    except Exception:
        return []


# ─────────────────────────────────────────────
# PDF REPORT GENERATOR
# ─────────────────────────────────────────────
class EquityResearchPDF(FPDF):
    """Custom PDF class for equity research reports."""

    def __init__(self):
        super().__init__()
        self.set_auto_page_break(auto=True, margin=20)

    def header(self):
        self.set_fill_color(15, 12, 41)
        self.rect(0, 0, 210, 35, 'F')
        self.set_font("Helvetica", "B", 18)
        self.set_text_color(255, 255, 255)
        self.set_y(8)
        self.cell(0, 10, "FinScope Equity Research Report", align="C", new_x="LMARGIN", new_y="NEXT")
        self.set_font("Helvetica", "", 9)
        self.set_text_color(180, 180, 200)
        self.cell(0, 6, f"Generated on {datetime.now().strftime('%B %d, %Y at %I:%M %p')}", align="C", new_x="LMARGIN", new_y="NEXT")
        self.ln(10)

    def footer(self):
        self.set_y(-15)
        self.set_font("Helvetica", "I", 8)
        self.set_text_color(128, 128, 128)
        self.cell(0, 10, f"FinScope Research  |  Page {self.page_no()}/{{nb}}  |  For informational purposes only", align="C")

    def section_title(self, title):
        self.set_font("Helvetica", "B", 13)
        self.set_text_color(48, 43, 99)
        self.cell(0, 10, title, new_x="LMARGIN", new_y="NEXT")
        self.set_draw_color(102, 126, 234)
        self.set_line_width(0.8)
        self.line(10, self.get_y(), 200, self.get_y())
        self.ln(4)

    def sub_title(self, title):
        self.set_font("Helvetica", "B", 11)
        self.set_text_color(80, 80, 100)
        self.cell(0, 8, title, new_x="LMARGIN", new_y="NEXT")
        self.ln(1)

    def body_text(self, text):
        self.set_font("Helvetica", "", 10)
        self.set_text_color(60, 60, 60)
        self.multi_cell(0, 5.5, text)
        self.ln(2)

    def key_value(self, key, value):
        self.set_font("Helvetica", "B", 10)
        self.set_text_color(80, 80, 100)
        self.cell(65, 6, str(key), new_x="RIGHT")
        self.set_font("Helvetica", "", 10)
        self.set_text_color(40, 40, 40)
        self.cell(0, 6, str(value), new_x="LMARGIN", new_y="NEXT")

    def add_table(self, headers, data, col_widths=None):
        if col_widths is None:
            col_widths = [190 / len(headers)] * len(headers)
        # Header
        self.set_fill_color(48, 43, 99)
        self.set_text_color(255, 255, 255)
        self.set_font("Helvetica", "B", 9)
        for i, h in enumerate(headers):
            self.cell(col_widths[i], 8, str(h), border=1, fill=True, align="C")
        self.ln()
        # Data
        self.set_font("Helvetica", "", 9)
        self.set_text_color(60, 60, 60)
        fill = False
        for row in data:
            if fill:
                self.set_fill_color(240, 240, 250)
            else:
                self.set_fill_color(255, 255, 255)
            for i, val in enumerate(row):
                self.cell(col_widths[i], 7, str(val), border=1, fill=True, align="C")
            self.ln()
            fill = not fill
        self.ln(3)


def generate_research_pdf(ticker: str, info: dict, hist: pd.DataFrame,

                          recs: pd.DataFrame, actions: pd.DataFrame,

                          income: pd.DataFrame, balance: pd.DataFrame,

                          cashflow: pd.DataFrame):
    """Generate a comprehensive equity research PDF report."""
    pdf = EquityResearchPDF()
    pdf.alias_nb_pages()
    pdf.add_page()

    name = info.get("longName", ticker)
    sector = info.get("sector", "N/A")
    industry = info.get("industry", "N/A")
    country = info.get("country", "N/A")
    exchange = info.get("exchange", "N/A")
    currency = info.get("currency", "USD")

    # ── Cover Info
    pdf.set_font("Helvetica", "B", 22)
    pdf.set_text_color(15, 12, 41)
    pdf.cell(0, 12, f"{name} ({ticker})", new_x="LMARGIN", new_y="NEXT")
    pdf.set_font("Helvetica", "", 11)
    pdf.set_text_color(100, 100, 120)
    pdf.cell(0, 7, f"{sector}  |  {industry}  |  {exchange}  |  {country}", new_x="LMARGIN", new_y="NEXT")
    pdf.ln(8)

    # ── Company Overview
    pdf.section_title("1. Company Overview")
    summary = info.get("longBusinessSummary", "No business summary available.")
    pdf.body_text(summary)

    # ── Key Metrics
    pdf.section_title("2. Key Financial Metrics")

    def fmt(val, prefix="", suffix="", div=1):
        if val is None or val == "N/A":
            return "N/A"
        try:
            v = float(val) / div
            if div >= 1e9:
                return f"{prefix}{v:.2f}B{suffix}"
            elif div >= 1e6:
                return f"{prefix}{v:.2f}M{suffix}"
            return f"{prefix}{v:,.2f}{suffix}"
        except (ValueError, TypeError):
            return str(val)

    metrics = [
        ("Market Cap", fmt(info.get("marketCap"), prefix=f"{currency} ", div=1e9)),
        ("Enterprise Value", fmt(info.get("enterpriseValue"), prefix=f"{currency} ", div=1e9)),
        ("Current Price", fmt(info.get("currentPrice"), prefix=f"{currency} ")),
        ("52-Week High", fmt(info.get("fiftyTwoWeekHigh"), prefix=f"{currency} ")),
        ("52-Week Low", fmt(info.get("fiftyTwoWeekLow"), prefix=f"{currency} ")),
        ("P/E Ratio (Trailing)", fmt(info.get("trailingPE"))),
        ("P/E Ratio (Forward)", fmt(info.get("forwardPE"))),
        ("PEG Ratio", fmt(info.get("pegRatio"))),
        ("Price to Book", fmt(info.get("priceToBook"))),
        ("EPS (Trailing)", fmt(info.get("trailingEps"), prefix=f"{currency} ")),
        ("EPS (Forward)", fmt(info.get("forwardEps"), prefix=f"{currency} ")),
        ("Dividend Yield", fmt(info.get("dividendYield", 0) * 100 if info.get("dividendYield") else None, suffix="%")),
        ("Beta", fmt(info.get("beta"))),
        ("Revenue", fmt(info.get("totalRevenue"), prefix=f"{currency} ", div=1e9)),
        ("Gross Margins", fmt(info.get("grossMargins", 0) * 100 if info.get("grossMargins") else None, suffix="%")),
        ("Operating Margins", fmt(info.get("operatingMargins", 0) * 100 if info.get("operatingMargins") else None, suffix="%")),
        ("Profit Margins", fmt(info.get("profitMargins", 0) * 100 if info.get("profitMargins") else None, suffix="%")),
        ("ROE", fmt(info.get("returnOnEquity", 0) * 100 if info.get("returnOnEquity") else None, suffix="%")),
        ("Debt to Equity", fmt(info.get("debtToEquity"))),
        ("Free Cash Flow", fmt(info.get("freeCashflow"), prefix=f"{currency} ", div=1e9)),
    ]

    for key, val in metrics:
        pdf.key_value(key, val)
    pdf.ln(4)

    # ── Price Performance
    pdf.section_title("3. Historical Price Performance")
    if not hist.empty:
        pdf.sub_title("Recent Price Summary")
        recent = hist.tail(20)
        headers = ["Date", "Open", "High", "Low", "Close", "Volume"]
        data = []
        for idx, row in recent.iterrows():
            date_str = idx.strftime("%Y-%m-%d") if hasattr(idx, 'strftime') else str(idx)[:10]
            data.append([
                date_str,
                f"{row.get('Open', 0):.2f}",
                f"{row.get('High', 0):.2f}",
                f"{row.get('Low', 0):.2f}",
                f"{row.get('Close', 0):.2f}",
                f"{int(row.get('Volume', 0)):,}",
            ])
        pdf.add_table(headers, data, col_widths=[30, 28, 28, 28, 28, 48])

        # Performance stats
        if len(hist) > 1:
            pdf.sub_title("Performance Statistics")
            close = hist["Close"]
            returns = close.pct_change().dropna()
            pdf.key_value("Period Return", f"{((close.iloc[-1] / close.iloc[0]) - 1) * 100:.2f}%")
            pdf.key_value("Average Daily Return", f"{returns.mean() * 100:.4f}%")
            pdf.key_value("Volatility (Std Dev)", f"{returns.std() * 100:.4f}%")
            pdf.key_value("Max Drawdown", f"{((close / close.cummax()) - 1).min() * 100:.2f}%")
            pdf.key_value("Highest Close", f"{currency} {close.max():.2f}")
            pdf.key_value("Lowest Close", f"{currency} {close.min():.2f}")
            pdf.ln(3)

    # ── Analyst Recommendations
    pdf.section_title("4. Analyst Recommendations")
    target_high = info.get("targetHighPrice")
    target_low = info.get("targetLowPrice")
    target_mean = info.get("targetMeanPrice")
    target_median = info.get("targetMedianPrice")
    rec = info.get("recommendationKey", "N/A")
    num_analysts = info.get("numberOfAnalystOpinions", "N/A")

    pdf.key_value("Consensus Recommendation", rec.upper() if rec != "N/A" else "N/A")
    pdf.key_value("Number of Analysts", str(num_analysts))
    pdf.key_value("Price Target (Mean)", fmt(target_mean, prefix=f"{currency} "))
    pdf.key_value("Price Target (Median)", fmt(target_median, prefix=f"{currency} "))
    pdf.key_value("Price Target (High)", fmt(target_high, prefix=f"{currency} "))
    pdf.key_value("Price Target (Low)", fmt(target_low, prefix=f"{currency} "))
    pdf.ln(2)

    if not recs.empty:
        pdf.sub_title("Recent Analyst Actions")
        recent_recs = recs.tail(15)
        headers_r = ["Date", "Firm", "To Grade", "From Grade", "Action"]

        data_r = []
        for idx, row in recent_recs.iterrows():
            date_str = idx.strftime("%Y-%m-%d") if hasattr(idx, 'strftime') else str(idx)[:10]
            firm = str(row.get("Firm", "N/A"))[:25]
            to_grade = str(row.get("To Grade", row.get("toGrade", "N/A")))
            from_grade = str(row.get("From Grade", row.get("fromGrade", "-")))
            action = str(row.get("Action", row.get("action", "N/A")))
            data_r.append([date_str, firm, to_grade, from_grade, action])

        pdf.add_table(headers_r, data_r, col_widths=[28, 55, 38, 38, 31])

    # ── Stock Actions
    pdf.section_title("5. Corporate Actions (Dividends & Splits)")
    if not actions.empty:
        recent_actions = actions.tail(20)
        headers_a = ["Date"]
        if "Dividends" in recent_actions.columns:
            headers_a.append("Dividend")
        if "Stock Splits" in recent_actions.columns:
            headers_a.append("Split Ratio")

        data_a = []
        for idx, row in recent_actions.iterrows():
            date_str = idx.strftime("%Y-%m-%d") if hasattr(idx, 'strftime') else str(idx)[:10]
            r = [date_str]
            if "Dividends" in recent_actions.columns:
                d = row.get("Dividends", 0)
                r.append(f"{currency} {d:.4f}" if d > 0 else "-")
            if "Stock Splits" in recent_actions.columns:
                s = row.get("Stock Splits", 0)
                r.append(f"{s:.1f}" if s > 0 else "-")
            data_a.append(r)
        widths = [60] + [65] * (len(headers_a) - 1)
        pdf.add_table(headers_a, data_a, col_widths=widths)
    else:
        pdf.body_text("No corporate actions data available for this security.")

    # ── Financial Statements Summary
    pdf.section_title("6. Financial Statements Summary")

    if not income.empty:
        pdf.sub_title("Income Statement Highlights")
        key_rows = ["Total Revenue", "Gross Profit", "Operating Income", "Net Income", "EBITDA"]
        for row_name in key_rows:
            if row_name in income.index:
                vals = income.loc[row_name]
                latest = vals.iloc[0] if len(vals) > 0 else None
                if latest is not None:
                    pdf.key_value(row_name, fmt(latest, prefix=f"{currency} ", div=1e9))
        pdf.ln(2)

    if not balance.empty:
        pdf.sub_title("Balance Sheet Highlights")
        key_rows_b = ["Total Assets", "Total Liabilities Net Minority Interest",
                       "Stockholders Equity", "Total Debt", "Cash And Cash Equivalents"]
        for row_name in key_rows_b:
            if row_name in balance.index:
                vals = balance.loc[row_name]
                latest = vals.iloc[0] if len(vals) > 0 else None
                if latest is not None:
                    pdf.key_value(row_name, fmt(latest, prefix=f"{currency} ", div=1e9))
        pdf.ln(2)

    if not cashflow.empty:
        pdf.sub_title("Cash Flow Highlights")
        key_rows_c = ["Operating Cash Flow", "Capital Expenditure", "Free Cash Flow",
                       "Investing Cash Flow", "Financing Cash Flow"]
        for row_name in key_rows_c:
            if row_name in cashflow.index:
                vals = cashflow.loc[row_name]
                latest = vals.iloc[0] if len(vals) > 0 else None
                if latest is not None:
                    pdf.key_value(row_name, fmt(latest, prefix=f"{currency} ", div=1e9))

    # ── Disclaimer
    pdf.add_page()
    pdf.section_title("Disclaimer")
    pdf.body_text(
        "This report is generated automatically by FinScope using publicly available data from Yahoo Finance "
        "(yfinance). It is intended for informational and educational purposes only and does not constitute "
        "financial, investment, or trading advice. The information provided may not be accurate, complete, "
        "or current. Past performance does not guarantee future results.\n\n"
        "Users should conduct their own due diligence and consult qualified financial advisors before making "
        "any investment decisions. FinScope assumes no liability for any losses or damages arising from the "
        "use of this report."
    )

    return bytes(pdf.output())


# ─────────────────────────────────────────────
# SIDEBAR
# ─────────────────────────────────────────────
with st.sidebar:
    st.markdown("""

    <div style="text-align:center; padding: 1rem 0;">

        <span style="font-size: 2.5rem;">πŸ“Š</span>

        <h2 style="color: #ffffff; margin: 0.3rem 0 0 0; font-weight: 800; letter-spacing: -0.5px;">FinScope</h2>

        <p style="color: rgba(255,255,255,0.5); font-size: 0.78rem; margin: 0;">Global Financial Intelligence</p>

    </div>

    """, unsafe_allow_html=True)

    st.markdown("---")

    # Advanced Search Integration
    if 'selected_ticker' not in st.session_state:
        st.session_state.selected_ticker = "AAPL"
    if 'search_query' not in st.session_state:
        st.session_state.search_query = ""

    search_query = st.text_input("πŸ” Search Company or Ticker", 
                                value=st.session_state.search_query,
                                placeholder="e.g. Apple, Reliance, Tesla...")
    
    if search_query and search_query != st.session_state.search_query:
        st.session_state.search_query = search_query
        results = search_ticker(search_query)
        if results:
            st.session_state.search_results = results
        else:
            st.sidebar.warning("No matches found. Try a different name.")
    
    if 'search_results' in st.session_state and search_query:
        options = {f"{r['name']} ({r['symbol']}) - {r['exchange']}": r['symbol'] for r in st.session_state.search_results}
        selected_option = st.selectbox("Select Result", options.keys(), index=0)
        if selected_option:
            st.session_state.selected_ticker = options[selected_option]

    st.markdown("")
    quick_pick = st.selectbox("⚑ Quick Pick", [""] + POPULAR_STOCKS, 
                               index=0, key="quick_pick")
    if quick_pick:
        st.session_state.selected_ticker = quick_pick

    selected_ticker = st.session_state.selected_ticker

    st.markdown("---")

    # Global Markets Sidebar
    st.sidebar.markdown("""

    <p style="color: rgba(255,255,255,0.7); font-size: 0.78rem; font-weight: 700; text-transform: uppercase;

    letter-spacing: 1px; margin-bottom: 0.5rem;">🌏 Global Overview</p>

    """, unsafe_allow_html=True)
    
    market_categories = {
        "πŸ“‰ Indices": ["S&P 500", "NASDAQ", "Dow Jones", "FTSE 100", "DAX", "Nikkei 225", "Hang Seng", "SENSEX", "NIFTY 50", "ASX 200"],
        "πŸ’± Currencies": ["EUR/USD", "GBP/USD", "USD/JPY"],
        "πŸ—οΈ Commodities": ["Gold", "Brent Oil", "Silver"],
        "β‚Ώ Crypto": ["Bitcoin", "Ethereum"]
    }

    for cat_name, symbols in market_categories.items():
        with st.sidebar.expander(cat_name, expanded=(cat_name == "πŸ“‰ Indices")):
            for name in symbols:
                symbol = GLOBAL_INDICES.get(name)
                if symbol:
                    data = fetch_index_data(symbol)
                    if data:
                        price, change, pct = data
                        color = "#00e676" if change >= 0 else "#ff5252"
                        arrow = "β–²" if change >= 0 else "β–Ό"
                        st.markdown(f"""

                        <div style="display: flex; justify-content: space-between; font-size: 0.82rem; margin-bottom: 0.1rem;">

                            <span style="color: rgba(255,255,255,0.5);">{name}</span>

                            <span style="color: {color}; font-weight: 600;">{price:,.2f}</span>

                        </div>

                        <div style="text-align: right; font-size: 0.72rem; color: {color}; margin-bottom: 0.6rem; opacity: 0.8;">

                            {arrow} {abs(pct):.2f}%

                        </div>

                        """, unsafe_allow_html=True)

    st.markdown("---")
    st.markdown(f"""

    <p style="color: rgba(255,255,255,0.35); font-size: 0.7rem; text-align: center;">

        Data from Yahoo Finance<br>

        Last refresh: {datetime.now().strftime('%H:%M:%S')}

    </p>

    """, unsafe_allow_html=True)

    if st.button("πŸ”„ Refresh Data", use_container_width=True):
        st.cache_data.clear()
        st.rerun()


# ─────────────────────────────────────────────
# MAIN CONTENT
# ─────────────────────────────────────────────
st.markdown(f"""

<div class="main-header">

    <h1>πŸ“Š FinScope Dashboard</h1>

    <p>Real-time financial intelligence powered by Yahoo Finance β€’ {datetime.now().strftime('%A, %B %d, %Y')}</p>

</div>

""", unsafe_allow_html=True)

# Fetch data
with st.spinner(f"Loading data for **{selected_ticker}**..."):
    info = fetch_stock_data(selected_ticker)
    stock = yf.Ticker(selected_ticker)

if not info:
    st.error(f"⚠️ Could not fetch data for **{selected_ticker}**. Please verify the ticker symbol and try again.")
    st.stop()

# ── Company header
name = info.get("longName", selected_ticker)
sector = info.get("sector", "N/A")
industry = info.get("industry", "N/A")
currency = info.get("currency", "USD")
current_price = info.get("currentPrice") or info.get("regularMarketPrice") or info.get("previousClose", 0)
prev_close = info.get("previousClose", current_price)
price_change = current_price - prev_close if current_price and prev_close else 0
price_pct = (price_change / prev_close * 100) if prev_close else 0

col_title, col_price = st.columns([3, 1])
with col_title:
    st.markdown(f"""

    <h2 style="color:#ffffff; margin:0; font-weight:800;">{name}</h2>

    <p style="color:rgba(255,255,255,0.5); margin:0.2rem 0; font-size:0.88rem;">

        {selected_ticker} β€’ {sector} β€’ {industry} β€’ {info.get('exchange', 'N/A')}

    </p>

    """, unsafe_allow_html=True)

with col_price:
    delta_class = "metric-delta-pos" if price_change >= 0 else "metric-delta-neg"
    arrow = "β–²" if price_change >= 0 else "β–Ό"
    st.markdown(f"""

    <div style="text-align: right;">

        <span style="color:#ffffff; font-size:2rem; font-weight:800;">{currency} {current_price:,.2f}</span><br>

        <span class="{delta_class}">{arrow} {abs(price_change):,.2f} ({abs(price_pct):.2f}%)</span>

    </div>

    """, unsafe_allow_html=True)

st.markdown("")

# ── Key Metrics Strip
metrics_cols = st.columns(6)
metric_items = [
    ("Market Cap", f"{currency} {info.get('marketCap', 0)/1e9:.2f}B" if info.get('marketCap') else "N/A"),
    ("P/E Ratio", f"{info.get('trailingPE', 0):.2f}" if info.get('trailingPE') else "N/A"),
    ("EPS", f"{currency} {info.get('trailingEps', 0):.2f}" if info.get('trailingEps') else "N/A"),
    ("52W High", f"{currency} {info.get('fiftyTwoWeekHigh', 0):,.2f}" if info.get('fiftyTwoWeekHigh') else "N/A"),
    ("52W Low", f"{currency} {info.get('fiftyTwoWeekLow', 0):,.2f}" if info.get('fiftyTwoWeekLow') else "N/A"),
    ("Volume", f"{info.get('volume', 0):,}" if info.get('volume') else "N/A"),
]
for col, (label, value) in zip(metrics_cols, metric_items):
    with col:
        st.markdown(f"""

        <div class="metric-card">

            <div class="metric-label">{label}</div>

            <div class="metric-value">{value}</div>

        </div>

        """, unsafe_allow_html=True)

st.markdown("")


# ─────────────────────────────────────────────
# TABS
# ─────────────────────────────────────────────
tab1, tab2, tab3, tab4, tab5, tab6, tab7 = st.tabs([
    "πŸ“ˆ Price Charts",
    "🎯 Analyst Recommendations",
    "πŸ“° News",
    "βš–οΈ Peer Comparison",
    "πŸ“œ Historical Actions",
    "πŸ“Š Financials",
    "πŸ“„ Research Report",
])


# ── TAB 1: PRICE CHARTS
with tab1:
    st.markdown('<div class="section-header">Interactive Price Charts</div>', unsafe_allow_html=True)

    chart_col1, chart_col2, chart_col3 = st.columns([2, 2, 1])
    with chart_col1:
        period_label = st.selectbox("Time Period", list(PERIOD_MAP.keys()), index=5)
    with chart_col2:
        chart_type = st.selectbox("Chart Type", ["Candlestick", "Line", "Area", "OHLC"])
    with chart_col3:
        show_volume = st.checkbox("Show Volume", value=True)

    period = PERIOD_MAP[period_label]
    interval = INTERVAL_MAP[period_label]
    hist = fetch_history(selected_ticker, period, interval)

    if not hist.empty:
        # Main chart
        if show_volume:
            fig = make_subplots(rows=2, cols=1, shared_xaxes=True,
                                vertical_spacing=0.03, row_heights=[0.75, 0.25])
        else:
            fig = make_subplots(rows=1, cols=1)

        if chart_type == "Candlestick":
            fig.add_trace(go.Candlestick(
                x=hist.index, open=hist["Open"], high=hist["High"],
                low=hist["Low"], close=hist["Close"], name="OHLC",
                increasing_line_color="#00e676", decreasing_line_color="#ff5252",
                increasing_fillcolor="#00e676", decreasing_fillcolor="#ff5252",
            ), row=1, col=1)
        elif chart_type == "OHLC":
            fig.add_trace(go.Ohlc(
                x=hist.index, open=hist["Open"], high=hist["High"],
                low=hist["Low"], close=hist["Close"], name="OHLC",
                increasing_line_color="#00e676", decreasing_line_color="#ff5252",
            ), row=1, col=1)
        elif chart_type == "Area":
            fig.add_trace(go.Scatter(
                x=hist.index, y=hist["Close"], mode="lines", name="Close",
                line=dict(color="#667eea", width=2),
                fill="tozeroy", fillcolor="rgba(102,126,234,0.15)",
            ), row=1, col=1)
        else:  # Line
            fig.add_trace(go.Scatter(
                x=hist.index, y=hist["Close"], mode="lines", name="Close",
                line=dict(color="#667eea", width=2.5),
            ), row=1, col=1)

        # Moving averages
        if len(hist) >= 20:
            ma20 = hist["Close"].rolling(20).mean()
            fig.add_trace(go.Scatter(
                x=hist.index, y=ma20, mode="lines", name="MA 20",
                line=dict(color="#ffc107", width=1, dash="dot"), opacity=0.7,
            ), row=1, col=1)

        if len(hist) >= 50:
            ma50 = hist["Close"].rolling(50).mean()
            fig.add_trace(go.Scatter(
                x=hist.index, y=ma50, mode="lines", name="MA 50",
                line=dict(color="#ff9800", width=1, dash="dash"), opacity=0.7,
            ), row=1, col=1)

        # Volume
        if show_volume and "Volume" in hist.columns:
            colors = ["#00e676" if c >= o else "#ff5252"
                      for c, o in zip(hist["Close"], hist["Open"])]
            fig.add_trace(go.Bar(
                x=hist.index, y=hist["Volume"], name="Volume",
                marker_color=colors, opacity=0.5,
            ), row=2, col=1)

        fig.update_layout(
            template="plotly_dark",
            paper_bgcolor="rgba(0,0,0,0)",
            plot_bgcolor="rgba(0,0,0,0)",
            margin=dict(l=0, r=0, t=30, b=0),
            height=550,
            xaxis_rangeslider_visible=False,
            legend=dict(orientation="h", yanchor="bottom", y=1.02, xanchor="right", x=1,
                        font=dict(size=11)),
            font=dict(family="Inter"),
            hovermode="x unified",
        )
        fig.update_xaxes(gridcolor="rgba(255,255,255,0.04)", zeroline=False)
        fig.update_yaxes(gridcolor="rgba(255,255,255,0.04)", zeroline=False)

        st.plotly_chart(fig, use_container_width=True)

        # Price statistics
        st.markdown('<div class="section-header">Price Statistics</div>', unsafe_allow_html=True)
        stat_cols = st.columns(4)
        close = hist["Close"]
        returns = close.pct_change().dropna()

        stats = [
            ("Period Return", f"{((close.iloc[-1]/close.iloc[0])-1)*100:.2f}%",
             ((close.iloc[-1]/close.iloc[0])-1)*100 >= 0),
            ("Avg Daily Return", f"{returns.mean()*100:.4f}%", returns.mean() >= 0),
            ("Volatility", f"{returns.std()*100:.4f}%", None),
            ("Max Drawdown", f"{((close/close.cummax())-1).min()*100:.2f}%", None),
        ]

        for col, (label, value, positive) in zip(stat_cols, stats):
            with col:
                delta_cls = ""
                if positive is True:
                    delta_cls = "metric-delta-pos"
                elif positive is False:
                    delta_cls = "metric-delta-neg"
                st.markdown(f"""

                <div class="metric-card">

                    <div class="metric-label">{label}</div>

                    <div class="metric-value {'metric-delta-pos' if positive else 'metric-delta-neg' if positive is False else ''}">{value}</div>

                </div>

                """, unsafe_allow_html=True)
    else:
        st.warning("No historical data available for this period.")


# ── TAB 2: ANALYST RECOMMENDATIONS
with tab2:
    st.markdown('<div class="section-header">Analyst Recommendations & Price Targets</div>', unsafe_allow_html=True)

    # Price targets
    target_cols = st.columns(4)
    targets = [
        ("Target High", info.get("targetHighPrice"), "#00e676"),
        ("Target Mean", info.get("targetMeanPrice"), "#667eea"),
        ("Target Median", info.get("targetMedianPrice"), "#ffc107"),
        ("Target Low", info.get("targetLowPrice"), "#ff5252"),
    ]
    for col, (label, val, color) in zip(target_cols, targets):
        with col:
            display = f"{currency} {val:,.2f}" if val else "N/A"
            st.markdown(f"""

            <div class="metric-card">

                <div class="metric-label">{label}</div>

                <div class="metric-value" style="color:{color};">{display}</div>

            </div>

            """, unsafe_allow_html=True)

    st.markdown("")

    # Consensus
    rec_key = info.get("recommendationKey", "none")
    num_analysts = info.get("numberOfAnalystOpinions", 0)
    rec_class = "rec-buy" if rec_key in ("buy", "strong_buy") else "rec-sell" if rec_key in ("sell", "strong_sell") else "rec-hold" if rec_key == "hold" else "rec-neutral"

    col_rec1, col_rec2 = st.columns(2)
    with col_rec1:
        st.markdown(f"""

        <div class="info-box" style="text-align:center;">

            <div class="label">Consensus Rating</div>

            <div style="margin-top:0.5rem;">

                <span class="rec-badge {rec_class}">{rec_key.replace("_", " ").upper()}</span>

            </div>

            <div style="color:rgba(255,255,255,0.4); font-size:0.75rem; margin-top:0.5rem;">

                Based on {num_analysts} analyst(s)

            </div>

        </div>

        """, unsafe_allow_html=True)

    with col_rec2:
        if current_price and info.get("targetMeanPrice"):
            upside = ((info["targetMeanPrice"] - current_price) / current_price) * 100
            u_color = "#00e676" if upside >= 0 else "#ff5252"
            u_arrow = "β–²" if upside >= 0 else "β–Ό"
            st.markdown(f"""

            <div class="info-box" style="text-align:center;">

                <div class="label">Upside / Downside Potential</div>

                <div style="margin-top:0.5rem; font-size:1.8rem; font-weight:800; color:{u_color};">

                    {u_arrow} {abs(upside):.2f}%

                </div>

                <div style="color:rgba(255,255,255,0.4); font-size:0.75rem; margin-top:0.3rem;">

                    vs Mean Target {currency} {info['targetMeanPrice']:,.2f}

                </div>

            </div>

            """, unsafe_allow_html=True)

    st.markdown("")

    # Recommendations table
    recs = fetch_recommendations(selected_ticker)
    if not recs.empty:
        st.markdown('<div class="section-header">Recent Analyst Actions</div>', unsafe_allow_html=True)

        # Visualize recommendation distribution
        grade_counts = {}
        grade_col = "To Grade" if "To Grade" in recs.columns else "toGrade" if "toGrade" in recs.columns else None
        if grade_col and grade_col in recs.columns:
            for g in recs[grade_col].dropna():
                g_str = str(g).strip()
                if g_str:
                    grade_counts[g_str] = grade_counts.get(g_str, 0) + 1

        if grade_counts:
            fig_rec = go.Figure(go.Bar(
                x=list(grade_counts.values()),
                y=list(grade_counts.keys()),
                orientation="h",
                marker=dict(
                    color=list(grade_counts.values()),
                    colorscale=[[0, "#ff5252"], [0.5, "#ffc107"], [1, "#00e676"]],
                ),
                text=list(grade_counts.values()),
                textposition="outside",
            ))
            fig_rec.update_layout(
                template="plotly_dark",
                paper_bgcolor="rgba(0,0,0,0)",
                plot_bgcolor="rgba(0,0,0,0)",
                height=300,
                margin=dict(l=0, r=40, t=10, b=0),
                xaxis=dict(showgrid=False, zeroline=False),
                yaxis=dict(showgrid=False),
                font=dict(family="Inter"),
            )
            st.plotly_chart(fig_rec, use_container_width=True)

        # Table
        display_recs = recs.tail(25).sort_index(ascending=False)
        st.dataframe(display_recs, use_container_width=True, height=400)
    else:
        st.info("No analyst recommendations available for this ticker.")


# ── TAB 3: NEWS
with tab3:
    st.markdown('<div class="section-header">Latest Market News</div>', unsafe_allow_html=True)
    news_items = fetch_news(selected_ticker)
    if news_items:
        for idx, n in enumerate(news_items[:12]):
            with st.container():
                col_n1, col_n2 = st.columns([1, 4])
                thumb = None
                if "thumbnail" in n and "resolutions" in n["thumbnail"]:
                    thumb = n["thumbnail"]["resolutions"][0]["url"]
                
                with col_n1:
                    if thumb:
                        st.image(thumb, use_container_width=True)
                    else:
                        st.markdown("""

                        <div style="background: rgba(255,255,255,0.05); aspect-ratio: 16/9; display: flex; align-items: center; justify-content: center; border-radius: 8px;">

                            <span style="font-size: 2rem; opacity: 0.3;">πŸ“°</span>

                        </div>

                        """, unsafe_allow_html=True)
                
                with col_n2:
                    st.markdown(f"#### [{n.get('title')}]({n.get('link')})")
                    pub_time = datetime.fromtimestamp(n.get('providerPublishTime', 0)).strftime('%Y-%m-%d %H:%M')
                    st.markdown(f"""

                    <p style="color: rgba(255,255,255,0.4); font-size: 0.8rem; margin: 0;">

                        <strong>{n.get('publisher', 'Unknown')}</strong> β€’ {pub_time}

                    </p>

                    """, unsafe_allow_html=True)
                
                st.markdown('<div style="margin: 1.5rem 0; border-bottom: 1px solid rgba(255,255,255,0.05);"></div>', unsafe_allow_html=True)
    else:
        st.info(f"No recent news found for {selected_ticker}.")


# ── TAB 4: PEER COMPARISON
with tab4:
    st.markdown('<div class="section-header">Industry Peer Comparison</div>', unsafe_allow_html=True)
    
    peers = get_industry_peers(industry, sector)
    comparison_tickers = list(dict.fromkeys([selected_ticker] + peers))[:4] # Selected + Top 3 peers
    
    with st.spinner(f"Comparing {selected_ticker} with industry leaders..."):
        peer_df = fetch_peer_data(comparison_tickers)
    
    if not peer_df.empty:
        # Comparison Table
        st.markdown("#### Key Metrics Comparison")
        formatted_df = peer_df.copy()
        for col in ["Market Cap", "Revenue"]:
            formatted_df[col] = formatted_df[col].apply(lambda x: f"{x/1e9:,.2f}B" if pd.notnull(x) else "N/A")
        formatted_df["Div Yield"] = formatted_df["Div Yield"].apply(lambda x: f"{x*100:.2f}%" if pd.notnull(x) else "N/A")
        formatted_df["P/E Ratio"] = formatted_df["P/E Ratio"].apply(lambda x: f"{x:.2f}" if pd.notnull(x) else "N/A")
        
        st.dataframe(formatted_df.drop(columns=["Currency"]), use_container_width=True)
        
        # Relative Performance Chart
        st.markdown("#### 1-Year Relative Performance")
        comp_hist = {}
        for t in comparison_tickers:
            h = fetch_history(t, "1y", "1d")
            if not h.empty:
                # Normalize to starting price (100)
                comp_hist[t] = (h["Close"] / h["Close"].iloc[0]) * 100
        
        if comp_hist:
            fig_perf = go.Figure()
            for t, series in comp_hist.items():
                fig_perf.add_trace(go.Scatter(
                    x=series.index, y=series, mode="lines", name=t,
                    line=dict(width=2 if t == selected_ticker else 1.5),
                    opacity=1 if t == selected_ticker else 0.7
                ))
            
            fig_perf.update_layout(
                template="plotly_dark",
                paper_bgcolor="rgba(0,0,0,0)",
                plot_bgcolor="rgba(0,0,0,0)",
                height=450,
                margin=dict(l=0, r=0, t=20, b=0),
                yaxis_title="Normalized Price (Base 100)",
                legend=dict(orientation="h", yanchor="bottom", y=1.02, xanchor="right", x=1),
                xaxis=dict(gridcolor="rgba(255,255,255,0.04)"),
                yaxis=dict(gridcolor="rgba(255,255,255,0.04)"),
            )
            st.plotly_chart(fig_perf, use_container_width=True)
    else:
        st.info("Peer data comparison not available for this sector.")


# ── TAB 5: HISTORICAL ACTIONS
with tab5:
    st.markdown('<div class="section-header">Stock Actions β€” Dividends & Splits</div>', unsafe_allow_html=True)

    actions = fetch_actions(selected_ticker)

    if not actions.empty:
        col_a1, col_a2 = st.columns(2)

        # Dividends
        if "Dividends" in actions.columns:
            divs = actions[actions["Dividends"] > 0][["Dividends"]].copy()
            with col_a1:
                st.markdown('<div class="section-header">πŸ’° Dividend History</div>', unsafe_allow_html=True)
                if not divs.empty:
                    fig_div = go.Figure()
                    fig_div.add_trace(go.Bar(
                        x=divs.index, y=divs["Dividends"],
                        marker_color="#667eea",
                        marker_line_color="#764ba2",
                        marker_line_width=1,
                        name="Dividend",
                    ))
                    fig_div.update_layout(
                        template="plotly_dark",
                        paper_bgcolor="rgba(0,0,0,0)",
                        plot_bgcolor="rgba(0,0,0,0)",
                        height=350,
                        margin=dict(l=0, r=0, t=10, b=0),
                        xaxis=dict(gridcolor="rgba(255,255,255,0.04)"),
                        yaxis=dict(gridcolor="rgba(255,255,255,0.04)", title="Amount"),
                        font=dict(family="Inter"),
                    )
                    st.plotly_chart(fig_div, use_container_width=True)

                    # Stats
                    total_div = divs["Dividends"].sum()
                    avg_div = divs["Dividends"].mean()
                    div_yield = info.get("dividendYield")
                    sc1, sc2, sc3 = st.columns(3)
                    with sc1:
                        st.markdown(f"""<div class="metric-card"><div class="metric-label">Total Paid</div>

                        <div class="metric-value">{currency} {total_div:.2f}</div></div>""", unsafe_allow_html=True)
                    with sc2:
                        st.markdown(f"""<div class="metric-card"><div class="metric-label">Avg Dividend</div>

                        <div class="metric-value">{currency} {avg_div:.4f}</div></div>""", unsafe_allow_html=True)
                    with sc3:
                        yield_str = f"{div_yield*100:.2f}%" if div_yield else "N/A"
                        st.markdown(f"""<div class="metric-card"><div class="metric-label">Current Yield</div>

                        <div class="metric-value">{yield_str}</div></div>""", unsafe_allow_html=True)

                    st.markdown("")
                    st.dataframe(divs.sort_index(ascending=False).head(30), use_container_width=True)
                else:
                    st.info("No dividend payments recorded.")

        # Splits
        if "Stock Splits" in actions.columns:
            splits = actions[actions["Stock Splits"] > 0][["Stock Splits"]].copy()
            with col_a2:
                st.markdown('<div class="section-header">βœ‚οΈ Stock Splits</div>', unsafe_allow_html=True)
                if not splits.empty:
                    split_data = []
                    for idx, row in splits.iterrows():
                        date_str = idx.strftime("%Y-%m-%d") if hasattr(idx, 'strftime') else str(idx)[:10]
                        ratio = row["Stock Splits"]
                        split_data.append({"Date": date_str, "Split Ratio": f"{ratio}:1"})
                    st.dataframe(pd.DataFrame(split_data), use_container_width=True)
                else:
                    st.info("No stock splits recorded.")
    else:
        st.info("No stock actions (dividends/splits) data available for this ticker.")

    # Full historical data download
    st.markdown('<div class="section-header">πŸ“₯ Download Full Historical Data</div>', unsafe_allow_html=True)
    dl_period = st.selectbox("Select period for download", list(PERIOD_MAP.keys()), index=9, key="dl_period")
    dl_data = fetch_history(selected_ticker, PERIOD_MAP[dl_period], INTERVAL_MAP[dl_period])

    if not dl_data.empty:
        csv_buffer = dl_data.to_csv()
        st.download_button(
            label=f"⬇️ Download {selected_ticker} Historical Data (CSV)",
            data=csv_buffer,
            file_name=f"{selected_ticker}_historical_{dl_period.replace(' ', '_').lower()}.csv",
            mime="text/csv",
            use_container_width=True,
        )


# ── TAB 6: FINANCIALS
with tab6:
    st.markdown('<div class="section-header">Financial Statements</div>', unsafe_allow_html=True)
    income, balance, cashflow = fetch_financials(selected_ticker)

    fin_tab1, fin_tab2, fin_tab3 = st.tabs(["Income Statement", "Balance Sheet", "Cash Flow"])

    with fin_tab1:
        if not income.empty:
            st.dataframe(income, use_container_width=True, height=450)
        else:
            st.info("Income statement data not available.")

    with fin_tab2:
        if not balance.empty:
            st.dataframe(balance, use_container_width=True, height=450)
        else:
            st.info("Balance sheet data not available.")

    with fin_tab3:
        if not cashflow.empty:
            st.dataframe(cashflow, use_container_width=True, height=450)
        else:
            st.info("Cash flow data not available.")

    # Revenue & Net Income chart
    if not income.empty:
        st.markdown('<div class="section-header">Revenue & Net Income Trend</div>', unsafe_allow_html=True)
        rev_data = {}
        ni_data = {}
        for col in income.columns:
            year = col.strftime("%Y") if hasattr(col, 'strftime') else str(col)[:4]
            if "Total Revenue" in income.index:
                rev_data[year] = income.loc["Total Revenue", col]
            if "Net Income" in income.index:
                ni_data[year] = income.loc["Net Income", col]

        if rev_data:
            years = sorted(rev_data.keys())
            fig_fin = go.Figure()
            fig_fin.add_trace(go.Bar(
                x=years, y=[rev_data[y]/1e9 for y in years],
                name="Revenue (B)", marker_color="#667eea",
            ))
            if ni_data:
                fig_fin.add_trace(go.Bar(
                    x=years, y=[ni_data.get(y, 0)/1e9 for y in years],
                    name="Net Income (B)", marker_color="#00e676",
                ))
            fig_fin.update_layout(
                template="plotly_dark",
                paper_bgcolor="rgba(0,0,0,0)",
                plot_bgcolor="rgba(0,0,0,0)",
                height=350,
                margin=dict(l=0, r=0, t=10, b=0),
                barmode="group",
                font=dict(family="Inter"),
                legend=dict(orientation="h", yanchor="bottom", y=1.02, xanchor="right", x=1),
                xaxis=dict(gridcolor="rgba(255,255,255,0.04)"),
                yaxis=dict(gridcolor="rgba(255,255,255,0.04)", title=f"Amount ({currency} Billions)"),
            )
            st.plotly_chart(fig_fin, use_container_width=True)


# ── TAB 7: RESEARCH REPORT
with tab7:
    st.markdown('<div class="section-header">πŸ“„ Equity Research Report Generator</div>', unsafe_allow_html=True)

    st.markdown(f"""

    <div class="info-box">

        <div class="label">Report Details</div>

        <div class="value">

            Generate a comprehensive equity research PDF for <strong>{name} ({selected_ticker})</strong> including

            company overview, key metrics, price performance, analyst recommendations,

            corporate actions, and financial statement summaries.

        </div>

    </div>

    """, unsafe_allow_html=True)

    st.markdown("")

    report_period = st.selectbox("Historical data period for report",
                                  list(PERIOD_MAP.keys()), index=5, key="report_period")

    if st.button("πŸš€ Generate Research Report", use_container_width=True, type="primary"):
        with st.spinner("Generating comprehensive equity research report..."):
            report_info = fetch_stock_data(selected_ticker)
            report_stock = yf.Ticker(selected_ticker)
            report_hist = fetch_history(selected_ticker, PERIOD_MAP[report_period],
                                         INTERVAL_MAP[report_period])
            report_recs = fetch_recommendations(selected_ticker)
            report_actions = fetch_actions(selected_ticker)
            report_income, report_balance, report_cashflow = fetch_financials(selected_ticker)

            pdf_bytes = generate_research_pdf(
                selected_ticker, report_info, report_hist,
                report_recs, report_actions,
                report_income, report_balance, report_cashflow,
            )

        st.success("βœ… Report generated successfully!")

        st.download_button(
            label=f"⬇️ Download {selected_ticker} Research Report (PDF)",
            data=pdf_bytes,
            file_name=f"FinScope_{selected_ticker}_Research_Report_{datetime.now().strftime('%Y%m%d')}.pdf",
            mime="application/pdf",
            use_container_width=True,
        )

        # Preview
        st.markdown('<div class="section-header">Report Preview</div>', unsafe_allow_html=True)
        b64_pdf = base64.b64encode(pdf_bytes).decode("utf-8")
        pdf_display = f'<iframe src="data:application/pdf;base64,{b64_pdf}" width="100%" height="800" type="application/pdf"></iframe>'
        st.markdown(pdf_display, unsafe_allow_html=True)


# ─────────────────────────────────────────────
# FOOTER
# ─────────────────────────────────────────────
st.markdown("---")
st.markdown("""

<div style="text-align:center; padding: 1rem 0;">

    <p style="color:rgba(255,255,255,0.3); font-size:0.75rem;">

        FinScope Β© 2025 β€’ Data sourced from Yahoo Finance via yfinance β€’

        For informational purposes only β€” not financial advice

    </p>

</div>

""", unsafe_allow_html=True)