File size: 35,720 Bytes
96ba538
 
 
 
 
d7c2702
 
96ba538
 
 
 
 
 
 
 
1652663
43af6e8
22e3078
96ba538
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1652663
 
22e3078
 
 
 
 
96ba538
 
d7c2702
 
96ba538
 
1652663
96ba538
28bf275
1652663
96ba538
 
 
 
 
 
 
 
28bf275
96ba538
 
d7c2702
 
 
 
 
 
 
 
 
 
 
 
43af6e8
 
 
d7c2702
43af6e8
d7c2702
 
 
 
 
 
43af6e8
d7c2702
 
 
 
43af6e8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d7c2702
 
 
 
43af6e8
d7c2702
 
 
43af6e8
 
 
 
 
d7c2702
 
 
 
 
 
 
 
 
 
 
 
43af6e8
 
 
d7c2702
 
43af6e8
d7c2702
 
 
 
 
43af6e8
 
 
 
 
d7c2702
 
43af6e8
 
 
 
 
 
 
 
d7c2702
 
43af6e8
 
 
d7c2702
 
 
 
96ba538
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1652663
 
 
96ba538
1652663
96ba538
 
 
1652663
 
 
96ba538
1652663
 
 
 
 
96ba538
 
 
 
 
 
 
 
1652663
 
96ba538
 
 
1652663
96ba538
 
 
 
 
 
 
 
 
1652663
 
96ba538
 
 
 
1652663
 
96ba538
 
 
1652663
 
 
 
96ba538
 
 
 
1652663
 
96ba538
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1652663
 
 
 
 
 
 
96ba538
 
 
 
 
1652663
 
 
 
 
 
 
96ba538
 
 
e7e8c49
1652663
 
e7e8c49
96ba538
 
 
 
 
 
1652663
 
96ba538
1652663
 
 
 
 
 
 
 
 
d7c2702
1652663
54a59fc
 
1652663
 
 
 
 
96ba538
 
 
 
 
 
 
 
1652663
 
 
 
96ba538
 
 
 
 
d7c2702
 
 
 
 
 
 
 
 
 
96ba538
 
 
 
 
 
 
 
 
 
 
 
 
 
1652663
96ba538
 
 
1652663
96ba538
 
 
 
 
 
 
 
 
 
 
 
 
 
1652663
 
 
 
 
 
 
 
 
 
 
 
 
 
96ba538
 
 
 
 
 
 
075cd2c
 
 
 
 
 
 
 
 
96ba538
1652663
96ba538
 
 
 
 
 
 
 
 
28bf275
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
96ba538
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
075cd2c
 
 
 
96ba538
 
 
 
 
e585c02
 
 
 
 
 
 
 
 
 
 
 
 
 
96ba538
 
115b7a9
 
 
 
 
 
 
 
 
edd3486
 
 
96ba538
 
 
 
 
1652663
96ba538
 
 
 
 
 
 
 
 
 
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
import streamlit as st
import json
import plotly.graph_objects as go
import plotly.express as px
from datetime import datetime, timezone, timedelta
import threading
import time
import pandas as pd
import sys
import os

sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
from collector import DataCollector
from analyzer import SentimentAnalyzer
from config import COMMODITY_CONFIGS, REFRESH_INTERVAL_SECONDS
from groq_client import GroqAnalyzer
import cache_manager
import build_landing

st.set_page_config(
    page_title="Commodity Sentiment Dashboard",
    page_icon="πŸ“Š",
    layout="wide",
    initial_sidebar_state="collapsed",
)

BIAS_COLORS = {
    "Strong Buy": "#30D158",
    "Buy": "#34C759",
    "Neutral": "#FFD60A",
    "Sell": "#FF453A",
    "Strong Sell": "#FF3B30",
}

MOOD_ICONS = {"Risk-Off": "πŸ›‘οΈ", "Risk-On": "πŸ“ˆ"}

APPLE_FONT = "-apple-system, BlinkMacSystemFont, 'Segoe UI', 'Inter', Roboto, Helvetica, Arial, sans-serif"

MYT = timezone(timedelta(hours=8))

ENV_FILE = os.path.join(os.path.dirname(os.path.abspath(__file__)), ".env")
if os.path.exists(ENV_FILE):
    with open(ENV_FILE, "r") as f:
        for line in f:
            line = line.strip()
            if line and not line.startswith("#") and "=" in line:
                key, _, val = line.partition("=")
                os.environ.setdefault(key.strip(), val.strip())

groq_client = GroqAnalyzer()

# Start background landing.html generator (runs once on server start)
if "_landing_started" not in st.session_state:
    st.session_state["_landing_started"] = True
    build_landing.run_background(interval_sec=REFRESH_INTERVAL_SECONDS)


@st.cache_data(ttl=REFRESH_INTERVAL_SECONDS, show_spinner=False)
def _fetch_single(commodity="gold"):
    """Low-level fetch for one commodity. Cached + parallelized."""
    cfg = COMMODITY_CONFIGS.get(commodity, COMMODITY_CONFIGS["gold"])
    collector = DataCollector(commodity=commodity)
    analyzer = SentimentAnalyzer(commodity=commodity, groq_client=groq_client)
    price_data = collector.fetch_price()
    market_data = collector.fcpo_market_data() if commodity == "fcpo" else None
    data = collector.collect_all(groq_client=groq_client)
    result = analyzer.run_full_analysis(data["articles"])
    result["collection_meta"] = {
        "total_articles": data["total_articles"],
        "rss_count": data["rss_count"],
        "web_count": data["web_count"],
        "timestamp": data["timestamp"],
        "price_label": cfg["price_label"],
    }
    return result, data, price_data, market_data


def _prefetch_commodity(comm):
    """Background thread: fetch one commodity into session cache."""
    try:
        r, d, p, m = _fetch_single(comm)
        st.session_state["_commodity_data"][comm] = {
            "result": r, "data": d, "price_data": p, "market_data": m,
            "loaded_at": datetime.now(timezone.utc).isoformat(),
        }
    except Exception:
        pass


_DISK_CACHE_LOCK = threading.Lock()


def get_commodity_data(commodity="gold"):
    """Return cached data for commodity. Priority: session > disk > network."""
    cache_key = "_commodity_data"
    if cache_key not in st.session_state:
        st.session_state[cache_key] = {}

    data_dict = st.session_state[cache_key]

    # 1. Session cache (fastest)
    if commodity in data_dict and "result" in data_dict[commodity]:
        d = data_dict[commodity]
        return d["result"], d["data"], d["price_data"], d["market_data"]

    # 2. Disk cache (survives refresh/restart)
    disk_result, disk_data, disk_price, disk_market = cache_manager.load(
        commodity, ttl_seconds=REFRESH_INTERVAL_SECONDS
    )
    if disk_result is not None:
        data_dict[commodity] = {
            "result": disk_result, "data": disk_data,
            "price_data": disk_price, "market_data": disk_market,
            "loaded_at": datetime.now(timezone.utc).isoformat(),
            "source": "disk",
        }
        st.session_state[cache_key] = data_dict
        for comm in ["gold", "wti", "fcpo"]:
            if comm != commodity and (comm not in data_dict or "error" in data_dict.get(comm, {})):
                threading.Thread(target=_prefetch_commodity, args=(comm,), daemon=True).start()
        return disk_result, disk_data, disk_price, disk_market

    # 3. Network fetch (slowest)
    r, d, p, m = _fetch_single(commodity)
    data_dict[commodity] = {
        "result": r, "data": d, "price_data": p, "market_data": m,
        "loaded_at": datetime.now(timezone.utc).isoformat(),
        "source": "network",
    }
    st.session_state[cache_key] = data_dict

    # Persist to disk (thread-safe)
    with _DISK_CACHE_LOCK:
        cache_manager.save(commodity, r, d, p, m)

    # Background prefetch other commodities
    for comm in ["gold", "wti", "fcpo"]:
        if comm != commodity and (comm not in data_dict or "error" in data_dict.get(comm, {})):
            threading.Thread(target=_prefetch_commodity, args=(comm,), daemon=True).start()

    return r, d, p, m


def render_skeleton():
    """CSS shimmer loading animation while data loads."""
    st.markdown(f"""
    <style>
    @keyframes pulse {{
        0% {{ opacity: 0.25; }}
        50% {{ opacity: 0.6; }}
        100% {{ opacity: 0.25; }}
    }}
    .skel {{
        background: rgba(255,255,255,0.05);
        border-radius: 12px;
        animation: pulse 1.2s ease-in-out infinite;
        height: 24px;
        margin-bottom: 8px;
    }}
    .skel-lg {{ height: 56px; border-radius: 16px; }}
    .skel-md {{ height: 36px; border-radius: 12px; }}
    .skel-sm {{ height: 16px; width: 50%; border-radius: 8px; }}
    .skel-card {{ height: 100px; border-radius: 16px; }}
    .skel-chart {{ height: 180px; border-radius: 12px; }}
    </style>
    <div style="background:#1C1C1E;border:1px solid #2C2C2E;border-radius:20px;padding:32px 40px;margin-bottom:24px;">
        <div class="skel skel-sm" style="margin-bottom:16px;width:30%;"></div>
        <div class="skel skel-lg" style="width:45%;"></div>
    </div>
    <div style="display:flex;gap:16px;margin-bottom:24px;">
        <div style="flex:1;"><div class="skel skel-card"></div></div>
        <div style="flex:1;"><div class="skel skel-card"></div></div>
        <div style="flex:1;"><div class="skel skel-card"></div></div>
        <div style="flex:1;"><div class="skel skel-card"></div></div>
    </div>
    <div style="display:flex;gap:16px;margin-bottom:24px;">
        <div style="flex:1;"><div class="skel skel-chart"></div></div>
        <div style="flex:1;"><div class="skel skel-chart"></div></div>
        <div style="flex:1.5;"><div class="skel skel-chart"></div></div>
    </div>
    """, unsafe_allow_html=True)


def render_gauge(score, title, min_val=-100, max_val=100):
    bar_color = "#FFD60A" if -25 <= score <= 25 else ("#30D158" if score > 25 else "#FF453A")
    fig = go.Figure(go.Indicator(
        mode="gauge+number+delta",
        value=score,
        domain={"x": [0, 1], "y": [0, 1]},
        title={"text": title, "font": {"size": 15, "color": "#86868B", "family": APPLE_FONT}},
        delta={"reference": 0, "increasing": {"color": "#30D158", "symbol": "β–²"}, "decreasing": {"color": "#FF453A", "symbol": "β–Ό"}},
        number={"font": {"size": 36, "color": "#F5F5F7", "family": APPLE_FONT, "weight": 600}},
        gauge={
            "axis": {
                "range": [min_val, max_val],
                "tickwidth": 0.5,
                "tickcolor": "rgba(142,142,147,0.3)",
                "tickfont": {"size": 9, "color": "#86868B", "family": APPLE_FONT},
            },
            "bar": {"color": bar_color, "thickness": 0.6, "line": {"width": 0}},
            "bgcolor": "rgba(0,0,0,0)",
            "steps": [
                {"range": [-100, -75], "color": "rgba(255,59,48,0.1)"},
                {"range": [-75, -25], "color": "rgba(255,69,58,0.07)"},
                {"range": [-25, 25], "color": "rgba(255,214,10,0.06)"},
                {"range": [25, 75], "color": "rgba(48,209,88,0.07)"},
                {"range": [75, 100], "color": "rgba(52,199,90,0.1)"},
            ],
            "threshold": {
                "line": {"color": "#F5F5F7", "width": 2},
                "thickness": 0.9,
                "value": score,
            },
        },
    ))
    fig.update_layout(
        height=260,
        margin=dict(l=10, r=10, t=50, b=10),
        paper_bgcolor="rgba(0,0,0,0)",
        font={"color": "#F5F5F7", "family": APPLE_FONT},
    )
    return fig


def render_category_bar(category_counts):
    if not category_counts:
        return None
    cats = list(category_counts.keys())
    vals = list(category_counts.values())
    colors = ["#FF9F0A" if v > 0 else "rgba(142,142,147,0.15)" for v in vals]
    fig = go.Figure(go.Bar(
        x=cats, y=vals, marker_color=colors, text=vals, textposition="auto",
        marker_line_width=0, marker_line_color="rgba(0,0,0,0)",
        textfont={"size": 12, "color": "#F5F5F7", "family": APPLE_FONT},
    ))
    fig.update_layout(
        height=260,
        margin=dict(l=10, r=10, t=40, b=30),
        paper_bgcolor="rgba(0,0,0,0)",
        plot_bgcolor="rgba(0,0,0,0)",
        font={"color": "#86868B", "size": 11, "family": APPLE_FONT},
        title={"text": "KEYWORD CATEGORY HITS", "font": {"size": 11, "color": "#86868B", "family": APPLE_FONT, "weight": 600}},
        xaxis=dict(tickangle=0, gridcolor="rgba(0,0,0,0)", tickfont={"size": 10, "color": "#86868B", "family": APPLE_FONT}),
        yaxis=dict(gridcolor="rgba(142,142,147,0.08)", tickfont={"size": 10, "color": "#86868B", "family": APPLE_FONT}),
    )
    return fig


def render_sentiment_timeline(articles):
    if not articles:
        return None
    df_data = []
    for a in articles:
        vader_s = a.get("vader_score", 0)
        df_data.append({"title": a["title"][:50], "score": vader_s, "category": ", ".join(a.get("categories", []))[:30]})
    df = pd.DataFrame(df_data)
    fig = px.bar(df, x=df.index, y="score", color="score",
                 color_continuous_scale=["#FF453A", "#FFD60A", "#30D158"],
                 hover_data=["title"])
    fig.update_traces(marker_line_width=0, marker_line_color="rgba(0,0,0,0)")
    fig.update_layout(
        height=260,
        margin=dict(l=10, r=10, t=40, b=10),
        paper_bgcolor="rgba(0,0,0,0)",
        plot_bgcolor="rgba(0,0,0,0)",
        font={"color": "#86868B", "size": 11, "family": APPLE_FONT},
        title={"text": "ARTICLE SENTIMENT DISTRIBUTION", "font": {"size": 11, "color": "#86868B", "family": APPLE_FONT, "weight": 600}},
        xaxis=dict(title="", gridcolor="rgba(0,0,0,0)", tickfont={"size": 9, "color": "#86868B"}),
        yaxis=dict(title="", gridcolor="rgba(142,142,147,0.08)", tickfont={"size": 9, "color": "#86868B"}),
        coloraxis_colorbar=dict(
            title="",
            tickfont={"size": 9, "color": "#86868B"},
            thickness=12,
            len=0.6,
        ),
    )
    return fig


def metric_card(label, value, delta=None, delta_color=None):
    delta_html = ""
    if delta is not None:
        dc = delta_color or ("#30D158" if "+" in str(delta) else "#FF453A" if "-" in str(delta) else "#86868B")
        delta_html = f'<div style="color:{dc}; font-size:15px; font-weight:600; margin-top:2px; font-family:{APPLE_FONT};">{delta}</div>'
    return f"""
    <div style="
        background: rgba(28,28,30,0.75);
        backdrop-filter: blur(40px);
        -webkit-backdrop-filter: blur(40px);
        border: 1px solid rgba(255,255,255,0.06);
        border-radius: 20px;
        padding: 28px 32px;
    ">
        <div style="
            color: #86868B;
            font-size: 13px;
            font-weight: 600;
            letter-spacing: 0.08em;
            text-transform: uppercase;
            font-family: {APPLE_FONT};
            margin-bottom: 8px;
        ">{label}</div>
        <div style="
            color: #F5F5F7;
            font-size: 36px;
            font-weight: 600;
            letter-spacing: -0.02em;
            font-variant-numeric: tabular-nums;
            font-family: {APPLE_FONT};
            line-height: 1.1;
        ">{value}</div>
        {delta_html}
    </div>
    """


def main():
    st.markdown(f"""
    <style>
        @import url('https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap');

        [data-testid="stAppViewContainer"] {{
            background: #0A0A0A;
        }}
        .stApp {{
            background: #0A0A0A;
            color: #F5F5F7;
            font-family: {APPLE_FONT};
        }}
        [data-testid="stHeader"] {{
            background: #0A0A0A;
        }}
        .stSidebar {{
            background: #111111 !important;
            border-right: 1px solid #1C1C1E !important;
        }}
        section[data-testid="stSidebar"] {{
            background: #111111 !important;
        }}

        h1, h2, h3, h4, h5, h6 {{
            color: #F5F5F7 !important;
            font-family: {APPLE_FONT} !important;
            font-weight: 600 !important;
            letter-spacing: -0.02em !important;
        }}
        .stMarkdown, .stText, p {{
            color: #8E8E93 !important;
            font-family: {APPLE_FONT} !important;
        }}
        .stCaption {{
            color: #6E6E73 !important;
            font-family: {APPLE_FONT} !important;
        }}
        .stDivider {{
            border-top-color: rgba(255,255,255,0.06) !important;
        }}

        .stButton > button {{
            background: rgba(255,255,255,0.08) !important;
            color: #F5F5F7 !important;
            border: 1px solid rgba(255,255,255,0.12) !important;
            border-radius: 999px !important;
            padding: 10px 28px !important;
            font-weight: 600 !important;
            font-family: {APPLE_FONT} !important;
            font-size: 14px !important;
            transition: all 0.15s ease !important;
            min-height: 44px !important;
        }}
        .stButton > button:hover {{
            background: rgba(255,255,255,0.14) !important;
            border-color: rgba(255,255,255,0.22) !important;
        }}
        .stButton > button:active {{
            background: rgba(255,255,255,0.2) !important;
        }}

        .stExpander {{
            border: 1px solid rgba(255,255,255,0.06) !important;
            border-radius: 14px !important;
            background: rgba(30,30,32,0.6) !important;
            margin-bottom: 6px !important;
        }}
        .stExpander:hover {{
            border-color: rgba(255,255,255,0.1) !important;
        }}
        .stExpander summary {{
            font-family: {APPLE_FONT} !important;
            color: #F5F5F7 !important;
            font-size: 14px !important;
            font-weight: 500 !important;
            padding: 12px 16px !important;
        }}

        a {{
            color: #0A84FF !important;
            text-decoration: none !important;
        }}
        a:hover {{
            text-decoration: underline !important;
        }}
        code {{
            font-family: 'SF Mono', 'Fira Code', 'Cascadia Code', monospace !important;
            background: rgba(255,255,255,0.06) !important;
            border-radius: 8px !important;
            padding: 2px 8px !important;
            color: #FFD60A !important;
        }}

        [data-testid="stMetricValue"] {{
            font-family: {APPLE_FONT} !important;
            font-size: 32px !important;
            font-weight: 700 !important;
        }}
        [data-testid="stMetricLabel"] {{
            font-family: {APPLE_FONT} !important;
        }}

        .element-container {{
            margin-bottom: 4px !important;
        }}

        @media (max-width: 768px) {{
            .stButton > button {{
                font-size: 13px !important;
                padding: 10px 18px !important;
            }}
        }}
    </style>
    """, unsafe_allow_html=True)

    with st.sidebar:
        st.markdown(f"<div style='color:#8E8E93;font-size:13px;font-weight:600;letter-spacing:0.1em;margin-bottom:8px;font-family:{APPLE_FONT};'>ABOUT</div>", unsafe_allow_html=True)
        st.markdown(f"<div style='color:#8E8E93;font-size:12px;line-height:1.6;font-family:{APPLE_FONT};'>Real-time sentiment for Gold, WTI Crude Oil & FCPO Palm Oil.<br><br>Groq AI + VADER NLP engine scanning RSS feeds and news articles.<br><br>Daily Telegram report at <b>6:01 AM MYT</b>.<br><br><a href='https://t.me/PedotTTRG' style='color:#0A84FF;'>Prepared by @PedotTTRG</a></div>", unsafe_allow_html=True)

    if "commodity" not in st.session_state:
        st.session_state["commodity"] = "gold"

    commodity = st.session_state["commodity"]
    cfg = COMMODITY_CONFIGS.get(commodity, COMMODITY_CONFIGS["gold"])

    TELEGRAM_BOT = os.environ.get("TELEGRAM_BOT_USERNAME", "SentimentIntelligence26Bot")
    SENANGPAY_URL = os.environ.get("SENANGPAY_URL", "https://app.senangpay.my/payment/177739832230")

    st.markdown(f'''<div style="display:flex;align-items:flex-start;justify-content:space-between;margin-bottom:12px;flex-wrap:wrap;gap:12px;">
<div style="flex:1;min-width:200px;">
<div style="color:#8E8E93;font-size:13px;font-weight:600;letter-spacing:0.1em;text-transform:uppercase;font-family:{APPLE_FONT};">Commodity Sentiment Intelligence</div>
<div style="color:#F5F5F7;font-size:44px;font-weight:700;letter-spacing:-0.03em;line-height:1.1;margin-top:2px;font-family:{APPLE_FONT};">{cfg["display_name"]}</div>
</div>
<div style="text-align:right;">
<div style="display:flex;align-items:center;gap:10px;justify-content:flex-end;flex-wrap:wrap;">
<div>
<div style="color:#8E8E93;font-size:14px;font-family:{APPLE_FONT};">{datetime.now(MYT).strftime("%I:%M %p")} MYT</div>
<div style="color:#6E6E73;font-size:11px;font-family:{APPLE_FONT};margin-top:2px;">Auto-refresh 5 min</div>
</div>
<a href="https://t.me/{TELEGRAM_BOT}" target="_blank" style="display:inline-flex;align-items:center;gap:5px;background:#FFFFFF;border:1px solid #0A84FF;border-radius:999px;padding:8px 18px;color:#0A84FF;font-family:{APPLE_FONT};font-size:13px;font-weight:600;text-decoration:none;white-space:nowrap;margin-left:8px;"><span style="font-size:14px;">✈️</span> Get Via Telegram</a>
<a href="{SENANGPAY_URL}" target="_blank" style="display:inline-flex;align-items:center;gap:5px;background:#FFFFFF;border:1px solid #000000;border-radius:999px;padding:8px 18px;color:#000000;font-family:{APPLE_FONT};font-size:13px;font-weight:600;text-decoration:none;white-space:nowrap;"><span style="font-size:14px;">β˜•</span> Support Us</a>
</div>
</div>
</div>''', unsafe_allow_html=True)

    col_gold, col_wti, col_fcpo = st.columns([1, 1, 1])
    with col_gold:
        if st.button("πŸ₯‡ GOLD", key="btn_gold", use_container_width=True):
            st.session_state["commodity"] = "gold"
            st.rerun()
    with col_wti:
        if st.button("πŸ›’οΈ WTI CRUDE OIL", key="btn_wti", use_container_width=True):
            st.session_state["commodity"] = "wti"
            st.rerun()
    with col_fcpo:
        if st.button("🌴 FCPO PALM OIL", key="btn_fcpo", use_container_width=True):
            st.session_state["commodity"] = "fcpo"
            st.rerun()

    if st.button("Refresh Analysis", use_container_width=False):
        st.cache_data.clear()
        st.rerun()

    # Show skeleton while data loads for first time
    cache_key = "_commodity_data"
    if cache_key not in st.session_state:
        st.session_state[cache_key] = {}

    has_cached = commodity in st.session_state[cache_key] and "result" in st.session_state[cache_key][commodity]
    if not has_cached:
        render_skeleton()

    result, data, price_data, market_data = get_commodity_data(commodity)

    a1 = result["analysis_1_macro"]
    a2 = result["analysis_2_sentiment"]
    a3 = result["analysis_3_dxy"]
    fs = result["final_synthesis"]
    meta = result["meta"]
    cm = result["collection_meta"]

    sk = cfg["score_key"]
    bias_key = f"final_{sk}_bias"
    score_key = f"{sk}_sentiment_score"

    bias_color = BIAS_COLORS.get(fs[bias_key], "#FFD60A")

    currency = cfg.get("currency", "$")
    price_str = ""
    if price_data and price_data.get("price"):
        price_val = price_data["price"]
        price_str = f'<span style="color:#FFD60A;font-size:2.4em;font-weight:700;letter-spacing:-0.03em;font-variant-numeric:tabular-nums;font-family:{APPLE_FONT};">{currency}{price_val:,.2f}</span>'
        if price_data.get("change") is not None:
            chg = price_data["change"]
            pct = price_data["change_pct"]
            chg_color = "#30D158" if chg >= 0 else "#FF453A"
            sign = "+" if chg >= 0 else ""
            price_str += f'<div style="color:{chg_color};font-size:15px;font-weight:600;margin-top:4px;font-family:{APPLE_FONT};">{sign}{chg:.2f} ({sign}{pct:.2f}%)</div>'

    pull_time_str = ""
    try:
        pull_dt = datetime.fromisoformat(cm["timestamp"].replace("Z", "+00:00")).astimezone(MYT)
        pull_time_str = pull_dt.strftime("%I:%M %p")
    except Exception:
        pull_time_str = "β€”"

    ai_badge = groq_client.get_badge_html() if groq_client and groq_client.available else ""

    st.markdown(f'''<div style="background:#1C1C1E;border:1px solid #2C2C2E;border-radius:20px;padding:32px 40px;margin-bottom:24px;display:flex;align-items:center;justify-content:space-between;flex-wrap:wrap;gap:24px;">
<div style="flex:1;min-width:280px;">
<div style="color:#8E8E93;font-size:11px;font-weight:600;letter-spacing:0.1em;text-transform:uppercase;font-family:{APPLE_FONT};margin-bottom:6px;">Signal{ai_badge}</div>
<div style="color:{bias_color};font-size:2.6em;font-weight:700;letter-spacing:-0.03em;line-height:1;font-family:{APPLE_FONT};">{fs[bias_key]}</div>
<div style="color:#8E8E93;font-size:14px;margin-top:10px;max-width:560px;line-height:1.5;font-family:{APPLE_FONT};">{fs["justification"]}</div>
</div>
<div style="text-align:right;flex-shrink:0;min-width:200px;">
<div style="color:#8E8E93;font-size:11px;font-weight:600;letter-spacing:0.1em;text-transform:uppercase;font-family:{APPLE_FONT};margin-bottom:6px;">{cfg["price_label"]}</div>
{price_str}
<div style="color:#6E6E73;font-size:11px;margin-top:8px;font-family:{APPLE_FONT};">Data as of {pull_time_str} MYT</div>
</div>
</div>''', unsafe_allow_html=True)

    col1, col2, col3, col4 = st.columns(4)
    with col1:
        st.markdown(metric_card("Sentiment Score", f"{a2[score_key]}", a2["sentiment_label"]), unsafe_allow_html=True)
    with col2:
        st.markdown(metric_card("Market Mood", a1["overall_market_mood"]), unsafe_allow_html=True)
    with col3:
        if commodity == "fcpo":
            myr_bias = meta.get("myr_bias", "Neutral")
            myr_delta = "↓ Ringgit" if myr_bias == "Bearish" else "↑ Ringgit" if myr_bias == "Bullish" else "β€”"
            myr_dc = "#30D158" if myr_bias == "Bearish" else "#FF453A" if myr_bias == "Bullish" else "#8E8E93"
            st.markdown(metric_card("MYR (Ringgit)", myr_bias, myr_delta, myr_dc), unsafe_allow_html=True)
        else:
            dxy_delta = "↑ Dollar" if a3["dxy_directional_bias"] == "Bullish" else "↓ Dollar" if a3["dxy_directional_bias"] == "Bearish" else "β€”"
            dxy_dc = "#FF453A" if a3["dxy_directional_bias"] == "Bullish" else "#30D158" if a3["dxy_directional_bias"] == "Bearish" else "#86868B"
            st.markdown(metric_card("DXY Bias", a3["dxy_directional_bias"], dxy_delta, dxy_dc), unsafe_allow_html=True)
    with col4:
        if commodity in ("wti", "fcpo"):
            supply_score = meta.get("supply_score", 0)
            supply_label = "Tight" if supply_score > 0.15 else "Oversupply" if supply_score < -0.15 else "Balanced"
            supply_dc = "#30D158" if supply_score > 0.15 else "#FF453A" if supply_score < -0.15 else "#FFD60A"
            st.markdown(metric_card("Supply", supply_label, f"{supply_score:+d}", supply_dc), unsafe_allow_html=True)
        else:
            ca_delta = "Over-leveraged" if a2["contrarian_signal"] == "YES" else "Normal range"
            ca_dc = "#FF9F0A" if a2["contrarian_signal"] == "YES" else "#30D158"
            st.markdown(metric_card("Contrarian Alert", a2["contrarian_signal"], ca_delta, ca_dc), unsafe_allow_html=True)

    if commodity == "fcpo" and market_data:
        md = market_data
        currency = cfg.get("currency", "RM")

        vol_display = md.get("volume_display") or "β€”"
        vol_raw = md.get("volume")
        vol_delta = f"{int(vol_raw):,} lots" if isinstance(vol_raw, (int, float)) and vol_raw else ""

        open_str = f"{currency}{md['open']:,.2f}" if md.get("open") else "β€”"
        high_str = f"{currency}{md['high']:,.2f}" if md.get("high") else "β€”"
        low_str = f"{currency}{md['low']:,.2f}" if md.get("low") else "β€”"
        prev_str = f"{currency}{md['prev_close']:,.2f}" if md.get("prev_close") else "β€”"
        day_range_str = md.get("day_range") or "β€”"
        week_range_str = md.get("week_range_52") or "β€”"
        settlement = md.get("settlement_type") or "Physical"
        contract = md.get("contract_size") or "25 MT"
        point_val = md.get("point_value") or ""

        st.markdown(f'''<div style="color:#86868B;font-size:11px;font-weight:600;letter-spacing:0.1em;text-transform:uppercase;font-family:{APPLE_FONT};margin-bottom:12px;margin-top:4px;">Market Data β€” Last Session</div>''', unsafe_allow_html=True)

        mc1, mc2, mc3, mc4, mc5 = st.columns(5)
        with mc1:
            st.markdown(metric_card("Volume", vol_display, vol_delta, "#0A84FF"), unsafe_allow_html=True)
        with mc2:
            st.markdown(metric_card("Open", open_str, None), unsafe_allow_html=True)
        with mc3:
            st.markdown(metric_card("High", high_str, None, "#30D158"), unsafe_allow_html=True)
        with mc4:
            st.markdown(metric_card("Low", low_str, None, "#FF453A"), unsafe_allow_html=True)
        with mc5:
            st.markdown(metric_card("Prev Close", prev_str, None), unsafe_allow_html=True)

        contract_info = f"Settlement: {settlement} | Contract: {contract}"
        if point_val:
            contract_info += f" | Point: {point_val}"
        st.markdown(f'''<div style="display:flex;justify-content:space-between;color:#6E6E73;font-size:11px;font-family:{APPLE_FONT};margin-top:2px;margin-bottom:20px;flex-wrap:wrap;gap:8px;">
<span>Day Range: {day_range_str}</span>
<span>52W Range: {week_range_str}</span>
<span>{contract_info}</span>
</div>''', unsafe_allow_html=True)

    st.markdown("<div style='border-top: 1px solid rgba(255,255,255,0.06); margin: 28px 0;'></div>", unsafe_allow_html=True)

    col_gauge, col_bar, col_timeline = st.columns([1, 1, 1.5])
    with col_gauge:
        fig_gauge = render_gauge(a2[score_key], "SENTIMENT GAUGE")
        st.plotly_chart(fig_gauge, width="stretch")

    with col_bar:
        fig_cat = render_category_bar(meta["category_counts"])
        if fig_cat:
            st.plotly_chart(fig_cat, width="stretch")

    with col_timeline:
        fig_tl = render_sentiment_timeline(data["articles"])
        if fig_tl:
            st.plotly_chart(fig_tl, width="stretch")

    st.markdown("<div style='border-top: 1px solid rgba(255,255,255,0.06); margin: 28px 0;'></div>", unsafe_allow_html=True)

    col_macro, col_geo = st.columns(2)
    with col_macro:
        if commodity == "fcpo":
            st.markdown(f'<div><div style="color:#86868B;font-size:11px;font-weight:600;letter-spacing:0.1em;text-transform:uppercase;font-family:{APPLE_FONT};margin-bottom:16px;">Supply & Policy</div><div style="color:#F5F5F7;font-size:14px;line-height:1.6;font-family:{APPLE_FONT};">{a1["macro_event_impact"]}</div><div style="margin-top:12px;color:#86868B;font-size:12px;font-family:{APPLE_FONT};">Supply: <span style="color:#30D158;font-weight:600;">{meta.get("supply_score",0):+d}</span></div></div>', unsafe_allow_html=True)
        else:
            st.markdown(f'<div><div style="color:#86868B;font-size:11px;font-weight:600;letter-spacing:0.1em;text-transform:uppercase;font-family:{APPLE_FONT};margin-bottom:16px;">Macro & Fed</div><div style="color:#F5F5F7;font-size:14px;line-height:1.6;font-family:{APPLE_FONT};">{a1["macro_event_impact"]}</div><div style="margin-top:12px;color:#86868B;font-size:12px;font-family:{APPLE_FONT};">Bias: <span style="color:#0A84FF;font-weight:600;">{meta["macro_bias"]}</span></div></div>', unsafe_allow_html=True)
    with col_geo:
        st.markdown(f'<div><div style="color:#86868B;font-size:11px;font-weight:600;letter-spacing:0.1em;text-transform:uppercase;font-family:{APPLE_FONT};margin-bottom:16px;">Geopolitical</div><div style="color:#F5F5F7;font-size:14px;line-height:1.6;font-family:{APPLE_FONT};">{a1["geopolitical_summary"]}</div><div style="margin-top:12px;color:#86868B;font-size:12px;font-family:{APPLE_FONT};">Intensity: <span style="color:#FF9F0A;font-weight:600;">{meta["geo_intensity"]}</span></div></div>', unsafe_allow_html=True)

    st.markdown("<div style='border-top: 1px solid rgba(255,255,255,0.06); margin: 28px 0;'></div>", unsafe_allow_html=True)

    st.markdown(f"<div style='color:#86868B;font-size:11px;font-weight:600;letter-spacing:0.1em;text-transform:uppercase;font-family:{APPLE_FONT};margin-bottom:12px;'>How to Read</div>", unsafe_allow_html=True)
    g1, g2, g3 = st.columns(3)
    with g1:
        st.markdown(f"<div style='background:rgba(28,28,30,0.5);border:1px solid rgba(255,255,255,0.06);border-radius:16px;padding:16px 20px;margin-bottom:6px;'><div style='color:#F5F5F7;font-size:13px;font-weight:600;font-family:{APPLE_FONT};margin-bottom:6px;'>Sentiment Score</div><div style='color:#86868B;font-size:11px;line-height:1.5;font-family:{APPLE_FONT};'>Range &minus;100 to +100.<br><span style='color:#30D158;'>+50/+100</span> Bullish &middot; <span style='color:#FF453A;'>&minus;50/&minus;100</span> Bearish<br><span style='color:#FFD60A;'>-20/+20</span> Neutral<br><i>VADER x keyword weight</i></div></div>", unsafe_allow_html=True)
        st.markdown(f"<div style='background:rgba(28,28,30,0.5);border:1px solid rgba(255,255,255,0.06);border-radius:16px;padding:16px 20px;margin-bottom:6px;'><div style='color:#F5F5F7;font-size:13px;font-weight:600;font-family:{APPLE_FONT};margin-bottom:6px;'>DXY Bias</div><div style='color:#86868B;font-size:11px;line-height:1.5;font-family:{APPLE_FONT};'>US Dollar direction.<br>Weak dollar = commodities go up<br>Strong dollar = commodities go down</div></div>", unsafe_allow_html=True)
    with g2:
        st.markdown(f"<div style='background:rgba(28,28,30,0.5);border:1px solid rgba(255,255,255,0.06);border-radius:16px;padding:16px 20px;margin-bottom:6px;'><div style='color:#F5F5F7;font-size:13px;font-weight:600;font-family:{APPLE_FONT};margin-bottom:6px;'>Market Mood</div><div style='color:#86868B;font-size:11px;line-height:1.5;font-family:{APPLE_FONT};'>πŸ“ˆ Risk-On = bullish for commodities<br>πŸ›‘οΈ Risk-Off = safe haven (gold up, oil demand down)</div></div>", unsafe_allow_html=True)
        st.markdown(f"<div style='background:rgba(28,28,30,0.5);border:1px solid rgba(255,255,255,0.06);border-radius:16px;padding:16px 20px;margin-bottom:6px;'><div style='color:#F5F5F7;font-size:13px;font-weight:600;font-family:{APPLE_FONT};margin-bottom:6px;'>Contrarian Alert</div><div style='color:#86868B;font-size:11px;line-height:1.5;font-family:{APPLE_FONT};'>⚠️ YES = sentiment over-leveraged, reversal possible<br>βœ… Normal = within safe range</div></div>", unsafe_allow_html=True)
    with g3:
        st.markdown(f"<div style='background:rgba(28,28,30,0.5);border:1px solid rgba(255,255,255,0.06);border-radius:16px;padding:16px 20px;margin-bottom:6px;'><div style='color:#F5F5F7;font-size:13px;font-weight:600;font-family:{APPLE_FONT};margin-bottom:6px;'>VADER (per article)</div><div style='color:#86868B;font-size:11px;line-height:1.5;font-family:{APPLE_FONT};'>🟒 > +0.05 positive article<br>🟑 &minus;0.05 to +0.05 neutral<br>πŸ”΄ < &minus;0.05 negative article</div></div>", unsafe_allow_html=True)
        st.markdown(f"<div style='background:rgba(28,28,30,0.5);border:1px solid rgba(255,255,255,0.06);border-radius:16px;padding:16px 20px;margin-bottom:6px;'><div style='color:#F5F5F7;font-size:13px;font-weight:600;font-family:{APPLE_FONT};margin-bottom:6px;'>Supply (WTI Only)</div><div style='color:#86868B;font-size:11px;line-height:1.5;font-family:{APPLE_FONT};'>Tight = bullish (OPEC cuts, war)<br>Balanced = neutral<br>Oversupply = bearish</div></div>", unsafe_allow_html=True)

    st.markdown("<div style='border-top: 1px solid rgba(255,255,255,0.06); margin: 28px 0;'></div>", unsafe_allow_html=True)

    st.markdown(f'<div style="color:#86868B;font-size:11px;font-weight:600;letter-spacing:0.1em;text-transform:uppercase;font-family:{APPLE_FONT};margin-bottom:16px;">Intelligence Feed</div><div style="color:#636366;font-size:12px;font-family:{APPLE_FONT};margin-bottom:12px;">{len(data["articles"])} articles from {cm["total_articles"]} collected &middot; {cm["rss_count"]} RSS &middot; {cm["web_count"]} web</div>', unsafe_allow_html=True)

    articles_to_show = data["articles"][:20]
    if articles_to_show:
        for i, article in enumerate(articles_to_show):
            cat_str = " &middot; ".join(article.get("categories", []))
            vader_s = article.get("vader_score", 0)
            ic = "#30D158" if vader_s > 0.05 else "#FF453A" if vader_s < -0.05 else "#FFD60A"
            ks = article.get("keyword_score", 0)
            badge = f'<span style="background:rgba(255,255,255,0.06);color:#86868B;font-size:11px;padding:2px 8px;border-radius:980px;font-family:{APPLE_FONT};font-weight:600;">{ks}</span>'
            src = f'<div style="margin-top:6px;"><a href="{article["link"]}" style="font-size:12px;font-family:{APPLE_FONT};">Source</a></div>' if article.get("link") else ""
            age = article.get("age")
            age_html = f'<span style="color:#0A84FF;font-size:11px;font-family:{APPLE_FONT};margin-left:12px;">&#128336; {age} ago</span>' if age else ""
            st.markdown(f'<div style="background:rgba(28,28,30,0.5);border:1px solid rgba(255,255,255,0.06);border-radius:16px;padding:16px 20px;margin-bottom:6px;"><div style="display:flex;align-items:center;gap:10px;margin-bottom:8px;"><div style="width:8px;height:8px;border-radius:50%;background:{ic};flex-shrink:0;"></div><div style="color:#F5F5F7;font-size:14px;font-weight:500;font-family:{APPLE_FONT};flex:1;">{article["title"][:90]}</div>{badge}</div><div style="color:#86868B;font-size:11px;font-family:{APPLE_FONT};margin-bottom:4px;">{cat_str}</div><div style="color:#86868B;font-size:11px;font-family:{APPLE_FONT};">VADER <span style="color:{ic};font-weight:600;">{vader_s:.3f}</span>{age_html}</div><div style="color:#AEAEB2;font-size:13px;line-height:1.5;margin-top:6px;font-family:{APPLE_FONT};">{article["summary"][:200]}</div>{src}</div>', unsafe_allow_html=True)



    st.markdown("<div style='border-top: 1px solid rgba(255,255,255,0.06); margin: 28px 0;'></div>", unsafe_allow_html=True)

    st.markdown(f'<div style="text-align:center;color:#636366;font-size:11px;font-family:{APPLE_FONT};margin-bottom:20px;">Daily reports at 6:01 AM MYT &middot; /report /report\\_wti /report\\_fcpo on-demand &middot; VADER + Groq AI Engine<br><br><span style="color:#86868B;">Prepared by</span> <a href="https://t.me/PedotTTRG" target="_blank" style="color:#0A84FF;text-decoration:none;">@PedotTTRG</a></div>', unsafe_allow_html=True)

    try:
        from streamlit_autorefresh import st_autorefresh
        st_autorefresh(interval=REFRESH_INTERVAL_SECONDS * 1000, key="datarefresh")
    except ImportError:
        pass


if __name__ == "__main__":
    main()