#!/usr/bin/env python3
import json, os, sys, time, datetime as dt, urllib.request

ROOT = os.path.expanduser("~/kleiner")
DATA = os.path.join(ROOT, "data")
HIST = os.path.join(DATA, "history")
ENVF = os.path.join(ROOT, ".env.json")
WATCHLIST = os.path.join(DATA, "watchlist.json")

def load_env():
    with open(ENVF,"r",encoding="utf-8") as f: return json.load(f)

def http_get(url):
    req = urllib.request.Request(url, headers={"Accept":"application/json"})
    with urllib.request.urlopen(req, timeout=30) as r:
        return json.loads(r.read().decode("utf-8"))

def read_watchlist():
    with open(WATCHLIST,"r",encoding="utf-8") as f:
        w=json.load(f)
    if isinstance(w, list) and w and isinstance(w[0], dict):
        return [x.get("symbol","").upper() for x in w if x.get("symbol")]
    return [str(x).upper() for x in w]

def load_existing(path):
    if os.path.exists(path):
        with open(path,"r",encoding="utf-8") as f: return json.load(f)
    return []

def dedup_sort(rows):
    # rows: [{t: "YYYY-MM-DD", o,h,l,c,v}, ...]
    d={}
    for r in rows:
        d[r["t"]]=r
    out=sorted(d.values(), key=lambda x: x["t"])
    return out[-365:]  # Sicherheitslimit

def fetch_range(tk, key, start, end):
    base="https://api.polygon.io/v2/aggs/ticker"
    url=f"{base}/{tk}/range/1/day/{start}/{end}?adjusted=true&sort=asc&limit=50000&apiKey={key}"
    js=http_get(url)
    out=[]
    for r in js.get("results",[]) or []:
        ts=int(r.get("t",0))/1000.0
        d=dt.datetime.utcfromtimestamp(ts).date().isoformat()
        out.append({
            "t": d,
            "o": round(float(r.get("o",0)), 4),
            "h": round(float(r.get("h",0)), 4),
            "l": round(float(r.get("l",0)), 4),
            "c": round(float(r.get("c",0)), 4),
            "v": int(r.get("v",0))
        })
    return out

def main():
    key=load_env()["POLYGON_KEY"]
    os.makedirs(HIST, exist_ok=True)
    tickers=read_watchlist()

    today=dt.date.today()
    start=(today - dt.timedelta(days=400)).isoformat()  # genug für EMA220
    end=today.isoformat()

    for tk in tickers:
        path=os.path.join(HIST, f"{tk}.json")
        existing=load_existing(path)
        # Lücke finden: letzter Tag + 1
        from_date=start
        if existing:
            from_date=(dt.date.fromisoformat(existing[-1]["t"]) + dt.timedelta(days=1)).isoformat()
        # Nur fetchen, wenn nötig
        if from_date <= end:
            try:
                rows=existing + fetch_range(tk, key, from_date, end)
                rows=dedup_sort(rows)
                tmp=path+".tmp"
                with open(tmp,"w",encoding="utf-8") as f: json.dump(rows,f,ensure_ascii=False)
                os.replace(tmp,path)
                print("history ok:", tk, len(rows))
                time.sleep(0.25)
            except Exception as e:
                print("history err:", tk, e)
                time.sleep(0.6)

if __name__=="__main__":
    main()
