
import os

CONFIG_FILE = "features.conf"
FEATURE_FLAGS_FILE = "radioactive/feature_flags.py"

def generate_flags():
    print(f"Generating {FEATURE_FLAGS_FILE} from {CONFIG_FILE}...")
    
    flags = {}
    if os.path.exists(CONFIG_FILE):
        with open(CONFIG_FILE, "r") as f:
            for line in f:
                line = line.strip()
                if not line or line.startswith("#"):
                    continue
                if "=" in line:
                    key, value = line.split("=", 1)
                    flags[key.strip()] = value.strip().lower() == "true"
    else:
        print(f"Warning: {CONFIG_FILE} not found. Using defaults.")
        # Default flags if config is missing
        flags["RECORDING_FEATURE"] = True
        flags["TRACK_FEATURE"] = True
        flags["SEARCH_FEATURE"] = True
        flags["CYCLE_FEATURE"] = True
        flags["INFO_FEATURE"] = True
        flags["TIMER_FEATURE"] = True
        flags["MINIMAL_FEATURE"] = False

    # Apply limits if MINIMAL_FEATURE is True
    if flags.get("MINIMAL_FEATURE", False):
        print("MINIMAL_FEATURE is enabled. Disabling all optional features.")
        flags["RECORDING_FEATURE"] = False
        flags["TRACK_FEATURE"] = False
        flags["SEARCH_FEATURE"] = False
        flags["CYCLE_FEATURE"] = False
        flags["INFO_FEATURE"] = False
        flags["TIMER_FEATURE"] = False

    with open(FEATURE_FLAGS_FILE, "w") as f:
        f.write("# This file is auto-generated by the configure step. Do not edit manually.\n\n")
        for key, value in flags.items():
            f.write(f"{key} = {value}\n")
    
    print(f"done. {FEATURE_FLAGS_FILE} is ready.")

if __name__ == "__main__":
    generate_flags()
