import json
from devmemory.config import DEDUP_THRESHOLD
from devmemory.db.memory_repo import MemoryRepo
from devmemory.errors import success_response

CATEGORY_TAG_MAP = {
    "decisions": "decision",
    "modifications": "modification",
    "pitfalls": "pitfall",
    "todos": "todo",
}


def handle_auto_save(args, *, cm, engine, session_id, **_):
    scope = args.get("scope", "project")
    repo = MemoryRepo(cm.conn, cm.project_dir)
    saved = []

    for category, tag in CATEGORY_TAG_MAP.items():
        items = args.get(category, [])
        if not isinstance(items, list):
            continue
        for item in items:
            if not item or not isinstance(item, str):
                continue
            embedding = engine.encode(item)
            tags = [tag] + args.get("extra_tags", [])
            result = repo.insert(item, tags, scope, session_id, embedding, DEDUP_THRESHOLD)
            saved.append({"id": result["id"], "action": result["action"], "category": category})

    return json.dumps(success_response(saved=saved, count=len(saved)))
