import os
import json
from functools import lru_cache
from typing import Any


@lru_cache(maxsize=2)
def get_policy_summaries_list(llm_summaries_dir: str) -> list[dict[str, Any]]:
    llm_summaries_available = []

    if os.path.exists(llm_summaries_dir):
        for filename in os.listdir(llm_summaries_dir):
            if filename.endswith(".json"):
                llm_summaries_available.append(filename[:-5])

    return llm_summaries_available


@lru_cache(maxsize=30)
def get_policy_summary(llm_summary_path: str) -> dict[str, Any] | None:
    if os.path.exists(llm_summary_path):
        with open(llm_summary_path, "r") as file:
            llm_summary = json.load(file)
            return llm_summary
    return None
