from pathlib import Path
import yaml


def is_app_based_tracing_intervention(intervention=None, intervention_conf=None):
    """
    Determines if the intervention requires an app.

    Args:
        intervention (str): name of the intervention that matches a configuration file in `configs/simulation/intervention`. Default is None.
        intervention_conf (dict): an experimental configuration. Default is None.

    Returns:
        (bool): True if an app is required.
    """
    assert intervention is not None or intervention_conf is not None, "Expects non-None intervention_conf when internvention is None. "
    if intervention_conf is not None:
        return intervention_conf['RISK_MODEL'] != ""
 
    if isinstance(intervention, dict):
        # This can happen if intervention is transformer (with weights and rec levels specified)
        intervention = next(iter(intervention.keys()))

    intervention_yaml_file = Path(__file__).resolve().parent.parent / "configs/simulation/intervention" / f"{intervention}.yaml"
    if "transformer" in intervention_yaml_file.name:
        intervention_yaml_file = Path(__file__).resolve().parent.parent / "configs/simulation/intervention" / f"transformer.yaml"
    with open(intervention_yaml_file, "r") as f:
        conf = yaml.safe_load(f)
        app_required = conf['RISK_MODEL'] != ""

    return app_required