import yaml
import importlib.resources
from pathlib import Path
from typing import Any, Dict
{%- if use_env %}
from dotenv import load_dotenv

# Load Environment Variables from the isolated .env file in this package
load_dotenv(Path(__file__).parent / ".env")
{%- endif %}

# Load configuration safely whether installed or local
try:
    # Modern Way (Python 3.9+) - works when installed as a package
    _config_path = importlib.resources.files("{{ package_name }}").joinpath("config.yaml")
    with _config_path.open("r") as f:
        SETTINGS: Dict[str, Any] = yaml.safe_load(f)
except Exception:
    # Fallback for local dev without install or older python
    _local_path = Path(__file__).parent / "config.yaml"
    if _local_path.exists():
        with open(_local_path, "r") as f:
            SETTINGS = yaml.safe_load(f)
    else:
        SETTINGS = {}

def get_config(key: str, default: Any = None) -> Any:
    """Retrieve a value from the globally loaded settings."""
    return SETTINGS.get(key, default)

{%- if project_type != 'classic' %}
def get_dataset_path(notebook_name: str, key: str = "datasets", extension: str = ".csv") -> str | None:
    """
    Helper for notebook data loading.
    Looks up 'notebook_name' in the 'key' section of config.yaml.
    """
    datasets = SETTINGS.get(key, {})
    dataset_name = datasets.get(notebook_name)
    if not dataset_name:
        return None
    return f"{dataset_name}{extension}"
{%- endif %}
