"""Load and format SRF, GCM, and cross-cutting priority data for LLM consumption"""

# AUTOGENERATED! DO NOT EDIT! File to edit: ../nbs/04_themes.ipynb.

# %% auto 0
__all__ = ['load_enablers', 'load_ccp', 'load_srf_outs', 'load_gcms', 'load_gcm_lut', 'fmt_enablers_ccp', 'get_srf_out',
           'fmt_srf_out', 'fmt_srf_outs', 'get_srf_outs', 'load_all_themes']

# %% ../nbs/04_themes.ipynb 2
from fastcore.all import *
from pathlib import Path
import json

# %% ../nbs/04_themes.ipynb 3
def _load(fname:str, # Filename to load
          path:str='files/themes', # Directory containing theme files
          md:bool=False # Return raw text instead of JSON?
         ) -> dict|str:
    "Load theme file (JSON or markdown)"
    p = Path(path)/fname
    return p.read_text() if md else json.loads(p.read_text())

# %% ../nbs/04_themes.ipynb 4
def load_enablers(path:str='files/themes' # Directory containing theme files
                 ) -> list:
    "Load SRF enablers"
    return _load('srf_enablers.json', path)

# %% ../nbs/04_themes.ipynb 5
def load_ccp(path:str='files/themes' # Directory containing theme files
            ) -> list:
    "Load cross-cutting priorities"
    return _load('crosscutting_priorities.json', path)

# %% ../nbs/04_themes.ipynb 6
def load_srf_outs(path:str='files/themes' # Directory containing theme files
                 ) -> list:
    "Load SRF objectives with outputs"
    return _load('srf_objectives.json', path)

# %% ../nbs/04_themes.ipynb 7
def load_gcms(path:str='files/themes' # Directory containing theme files
             ) -> str:
    "Load GCM objectives markdown"
    return _load('gcms_long.md', path, md=True)

# %% ../nbs/04_themes.ipynb 8
def load_gcm_lut(path:str='files/themes' # Directory containing theme files
                ) -> dict:
    "Load GCM to SRF output lookup table"
    return _load('gcm_to_srf_outputs.json', path)

# %% ../nbs/04_themes.ipynb 11
def fmt_enablers_ccp(items:list # List of enabler/CCP dicts with id, title, description
                    ) -> str:
    "Format enablers or cross-cutting priorities for LLM"
    return '\n\n'.join([f'## Cross-cutting {o["id"]}: {o["title"]}\n### Description\n{o["description"]}' for o in items])

# %% ../nbs/04_themes.ipynb 13
def get_srf_out(objectives:list, # SRF objectives structure
                output_id:str # Output ID to find
               ) -> dict|None:
    "Retrieve single SRF output with hierarchy"
    for obj in objectives:
        for lt_out in obj.get('long_term_outcomes', []):
            for st_out in lt_out.get('short_term_outcomes', []):
                if o := next((o for o in st_out.get('outputs', []) if o['id'] == output_id), None):
                    return dict(obj_id=obj['id'], obj=obj['title'], lt_out_id=lt_out['id'], lt_out=lt_out['title'],
                                st_out_id=st_out['id'], st_out=st_out['title'], output=o)

# %% ../nbs/04_themes.ipynb 15
def fmt_srf_out(output_ctx:dict # Dict with obj, lt_out, st_out, and output fields
               ) -> str:
    "Format SRF output with hierarchical context for LLM"
    o = output_ctx
    return '\n'.join([
        f'## SRF Output {o["output"]["id"]}: {o["output"]["title"]}',
        '### Hierarchical Context',
        f'**Objective {o["obj_id"]}**: {o["obj"]}',
        f'**Long-term Outcome {o["lt_out_id"]}:** {o["lt_out"]}',
        f'**Short-term Outcome {o["st_out_id"]}:** {o["st_out"]}'
    ])

# %% ../nbs/04_themes.ipynb 17
def fmt_srf_outs(objectives:list, # SRF objectives structure
                 output_ids:list # List of output IDs to format
                ) -> str:
    "Format multiple SRF outputs"
    return '\n\n'.join(fmt_srf_out(get_srf_out(objectives, i)) for i in output_ids)


# %% ../nbs/04_themes.ipynb 20
def get_srf_outs(lut:dict, # GCM to SRF lookup dict
                 gcm_ids:list # List of GCM IDs to filter by
                ) -> L:
    "Get SRF output IDs filtered by GCM IDs"
    return L(v for k,v in lut.items() if k in gcm_ids).concat()

# %% ../nbs/04_themes.ipynb 23
def load_all_themes(path:str='files/themes'  # Directory containing theme JSON files
                   ) -> AttrDict:             # Dict with enablers, ccp, gcms, srf_outs, gcm_lut
    "Load all theme data from path"
    return AttrDict(enablers=load_enablers(path), ccp=load_ccp(path), gcms=load_gcms(path), 
                    srf_outs=load_srf_outs(path), gcm_lut=load_gcm_lut(path))
