# scripts/memory_store.py
from pathlib import Path
import json
from datetime import datetime

MEMORY_DIR = Path.home() / ".config" / "aigpt" / "memory"
MEMORY_DIR.mkdir(parents=True, exist_ok=True)

def get_today_path():
    today = datetime.utcnow().strftime("%Y-%m-%d")
    return MEMORY_DIR / f"{today}.json"

def save_message(sender: str, message: str):
    entry = {
        "timestamp": datetime.utcnow().isoformat(),
        "sender": sender,
        "message": message
    }

    path = get_today_path()
    data = []

    if path.exists():
        with open(path, "r") as f:
            data = json.load(f)

    data.append(entry)

    with open(path, "w") as f:
        json.dump(data, f, indent=2, ensure_ascii=False)

def load_messages():
    path = get_today_path()
    if not path.exists():
        return []
    with open(path, "r") as f:
        return json.load(f)