import json
import os

PERMISSION_PATH = "scripts/index_permissions.json"

def get_user_allowed_indexes(user_id: str):
    if not os.path.exists(PERMISSION_PATH):
        print(f"[Permission Warning] 权限配置文件不存在: {PERMISSION_PATH}")
        return []

    try:
        with open(PERMISSION_PATH, "r", encoding="utf-8") as f:
            permission_map = json.load(f)

        if user_id not in permission_map:
            print(f"[Permission Info] 用户 {user_id} 无共享库配置，使用默认权限。")
        return permission_map.get(user_id, [])

    except json.JSONDecodeError:
        print(f"[Permission Error] 权限配置文件 JSON 格式错误。")
        return []
    except Exception as e:
        print(f"[Permission Error] 未知错误: {e}")
        return []
