import json
from pathlib import Path
import sys

# The configuration is expected to be in a 'config' subdirectory relative to this script's location.
CONFIG_DIR = Path(__file__).parent.parent / 'config'
CONFIG_FILE = CONFIG_DIR / 'llm_config.json'

DEFAULT_CONFIG = {
    "_comment": "Controls the output of the data extractors. 'shrink_json' (true/false) tokenizes all JSON keys to reduce file size. 'indentation_level' (e.g., 2, 4) controls pretty-printing; use 0 or null for compact, single-line output. `shrink_json` takes precedence on formatting.",
    "shrink_json": False,
    "indentation_level": 4,
    "ignored_folders": []
}

def get_config_path():
    """Returns the absolute path to the configuration file."""
    return CONFIG_FILE

def load_config():
    """
    Loads the configuration from llm_config.json.

    If the file doesn't exist, it creates a default one.
    Returns the configuration dictionary.
    """
    if not CONFIG_FILE.is_file():
        print(f"Configuration file not found at {CONFIG_FILE}. Creating a default one.")
        try:
            CONFIG_DIR.mkdir(parents=True, exist_ok=True)
            with open(CONFIG_FILE, 'w', encoding='utf-8') as f:
                json.dump(DEFAULT_CONFIG, f, indent=4)
            return DEFAULT_CONFIG
        except IOError as e:
            print(f"Error: Could not create default configuration file at {CONFIG_FILE}. {e}", file=sys.stderr)
            # Return default config to allow the script to proceed if possible
            return DEFAULT_CONFIG

    try:
        with open(CONFIG_FILE, 'r', encoding='utf-8') as f:
            # Use a more robust way to load to handle comments if any
            config_text = ''.join(line for line in f if not line.strip().startswith('//'))
            config = json.loads(config_text)
            
            # Ensure all default keys are present
            for key, value in DEFAULT_CONFIG.items():
                config.setdefault(key, value)
            return config
    except (json.JSONDecodeError, IOError) as e:
        print(f"Warning: Could not read or parse {CONFIG_FILE}. Using default settings. Error: {e}", file=sys.stderr)
        return DEFAULT_CONFIG

if __name__ == '__main__':
    # For testing purposes, you can run this script directly
    print(f"Config file path: {get_config_path()}")
    config = load_config()
    print("Loaded configuration:")
    print(json.dumps(config, indent=4))
