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

ROOT = os.path.expanduser("~/kleiner")
DATA = os.path.join(ROOT, "data")
INTRA = os.path.join(DATA, "intraday")
DAYTRADER20 = os.path.join(DATA, "live_scan", "daytrader_20.json")
WATCHLIST = os.path.join(DATA, "watchlist.json")
ENV_JSON = os.path.join(ROOT, ".env.json")
ENV_DOT = os.path.join(ROOT, ".env")


def load_polygon_key():
    if os.path.exists(ENV_JSON):
        with open(ENV_JSON, "r", encoding="utf-8") as f:
            js = json.load(f)
        k = js.get("POLYGON_KEY") or js.get("POLYGON_API_KEY")
        if k:
            return str(k).strip()

    if os.path.exists(ENV_DOT):
        with open(ENV_DOT, "r", encoding="utf-8") as f:
            for line in f:
                line = line.strip()
                if not line or line.startswith("#") or "=" not in line:
                    continue
                k, v = line.split("=", 1)
                k = k.strip()
                if k in ("POLYGON_KEY", "POLYGON_API_KEY"):
                    return v.strip().strip('"').strip("'")

    return os.getenv("POLYGON_KEY") or os.getenv("POLYGON_API_KEY")


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_universe():
    def pick_sym(d):
        for k in ("symbol", "ticker", "t"):
            v = d.get(k)
            if v:
                return str(v).upper().strip()
        return None

    if os.path.exists(DAYTRADER20):
        with open(DAYTRADER20, "r", encoding="utf-8") as f:
            w = json.load(f)

        if isinstance(w, list) and w and isinstance(w[0], dict):
            out = []
            for x in w:
                s = pick_sym(x)
                if s:
                    out.append(s)
            return out

        if isinstance(w, list):
            return [str(x).upper().strip() for x in w if str(x).strip()]

        return []

    if os.path.exists(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):
            out = []
            for x in w:
                s = pick_sym(x)
                if s:
                    out.append(s)
            return out

        if isinstance(w, list):
            return [str(x).upper().strip() for x in w if str(x).strip()]

    return []


def fetch_5m(tk, key, days=2):
    base = "https://api.polygon.io/v2/aggs/ticker"
    end = dt.datetime.utcnow().date()
    start = end - dt.timedelta(days=days)

    url = (
        f"{base}/{tk}/range/5/minute/{start.isoformat()}/{end.isoformat()}"
        f"?adjusted=true&sort=asc&limit=50000&apiKey={key}"
    )

    js = http_get(url)

    if isinstance(js, dict) and js.get("status") in ("ERROR", "NOT_AUTHORIZED"):
        raise RuntimeError(f"Polygon status={js.get('status')} message={js.get('error') or js.get('message')}")

    out = []
    for r in (js.get("results") or []):
        ts = int(r.get("t", 0)) / 1000.0
        iso = dt.datetime.utcfromtimestamp(ts).replace(microsecond=0).isoformat() + "Z"
        out.append(
            {
                "ts": iso,
                "o": float(r.get("o", 0)),
                "h": float(r.get("h", 0)),
                "l": float(r.get("l", 0)),
                "c": float(r.get("c", 0)),
                "v": int(r.get("v", 0)),
            }
        )
    return out


def main():
    key = load_polygon_key()
    if not key:
        raise SystemExit("ERROR: Kein POLYGON_KEY gefunden (~/kleiner/.env.json oder ~/kleiner/.env oder ENV).")

    os.makedirs(INTRA, exist_ok=True)
    tks = read_universe()

    print("Loaded tickers:", len(tks))
    if not tks:
        raise SystemExit("ERROR: 0 Ticker geladen. Prüfe daytrader_20.json (Format/Keys).")

    ok = 0
    for tk in tks:
        try:
            arr = fetch_5m(tk, key, days=2)
            path = os.path.join(INTRA, f"{tk}_5m.json")
            tmp = path + ".tmp"
            with open(tmp, "w", encoding="utf-8") as f:
                json.dump(arr, f, ensure_ascii=False)
            os.replace(tmp, path)
            print("5m ok:", tk, len(arr))
            ok += 1
            time.sleep(0.25)
        except Exception as e:
            print("5m err:", tk, e)
            time.sleep(0.6)

    print("DONE:", ok, "files")


if __name__ == "__main__":
    main()
