# engine/state.py
import json
from pathlib import Path

STATE_FILE = Path("storage/state.json")

DEFAULT_STATE = {
    "total_invested": 0.0,
    "sp_units": 0.0,
    "gd_units": 0.0,
    "last_sip_ts": None
}

def load_state():
    if not STATE_FILE.exists():
        return DEFAULT_STATE.copy()
    return json.loads(STATE_FILE.read_text())

def save_state(state):
    STATE_FILE.parent.mkdir(exist_ok=True)
    STATE_FILE.write_text(json.dumps(state, indent=2))
