import os
import json

from config import HISTORY_DIR, HISTORY_FILE

def ensure_history_dir():
    if not os.path.exists(HISTORY_DIR):
        os.makedirs(HISTORY_DIR)

def load_history():
    ensure_history_dir()
    if not os.path.isfile(HISTORY_FILE):
        return []
    try:
        with open(HISTORY_FILE, "r", encoding="utf-8") as f:
            return json.load(f)
    except Exception:
        return []

def save_history(history):
    ensure_history_dir()
    with open(HISTORY_FILE, "w", encoding="utf-8") as f:
        json.dump(history, f, indent=2)

def reset_history():
    ensure_history_dir()
    if os.path.isfile(HISTORY_FILE):
        os.remove(HISTORY_FILE)
