#!/usr/bin/env python3
import sys; sys.path.insert(0, '/home/hmo/MoFin')
from mo_data import *

pf = read_portfolio()
ta = pf.get('total_assets',0)
ca = pf.get('cash',0)
pp = pf.get('position_pct',0)
print(f'TA:{ta:.0f} CASH:{ca:.0f} POS:{pp:.1f}%')

for h in pf.get('holdings', []):
    c = h.get('currency','CNY')
    p = h.get('price')
    pos = h.get('position_pct')
    if pos is None:
        pos = 0
    if p is None:
        print(f'  {h["code"]} {h["name"]} 价? 仓{pos:.1f}%')
        continue
    tag = ' HKD' if c == 'HKD' else ''
    print(f'  {h["code"]} {h["name"]} 价{p:.2f}{tag} 仓{pos:.1f}%')

wl = read_watchlist()
if wl and 'stocks' in wl:
    for s in wl['stocks']:
        nm = s.get('name','')
        p = s.get('price')
        if p is None:
            print(f'  WATCH {s["code"]} {nm} 价?')
        else:
            print(f'  WATCH {s["code"]} {nm} 价{p:.2f}')

# sector data
import json, sqlite3, os
db = os.path.join('/home/hmo/MoFin', 'data', 'mofin.db')
conn = sqlite3.connect(db)
c = conn.cursor()
try:
    c.execute("SELECT id, market_trend, data FROM sector_snapshots ORDER BY id DESC LIMIT 1")
    row = c.fetchone()
    if row:
        print(f'  TREND:{row[1]}')
        d = json.loads(row[2]) if isinstance(row[2], str) else row[2]
        if d:
            for k,v in list(d.items())[:5]:
                print(f'    {k}: {v}')
except Exception as e:
    print(f'  DB err:{e}')
conn.close()

# decisions
dec = read_decisions()
if isinstance(dec, dict):
    ds = dec.get('decisions',[])
    print(f'  DECISIONS:{len(ds)}')
    for d in ds[:5]:
        print(f'    {d}')
