import json, os
import yfinance as yf
from datetime import datetime

out_dir = "data/market"
os.makedirs(out_dir, exist_ok=True)

t = yf.Ticker("TSLA")

payload = {
    "asof": datetime.utcnow().isoformat() + "Z",
    "info": t.info,
    "quarterly_financials": t.quarterly_financials.fillna("").to_dict(),
    "quarterly_balance_sheet": t.quarterly_balance_sheet.fillna("").to_dict(),
    "quarterly_cashflow": t.quarterly_cashflow.fillna("").to_dict(),
    "history_1y": t.history(period="1y").reset_index().to_dict(orient="records"),
    "history_5y": t.history(period="5y").reset_index().to_dict(orient="records"),
}
with open(f"{out_dir}/tsla_raw.json", "w") as f:
    json.dump(payload, f, default=str)
print("wrote", f"{out_dir}/tsla_raw.json")
