import os
from dotenv import dotenv_values


def load_env(files_to_load=None):
    config = {}

    # Always load .env first as a base
    config.update(dotenv_values(".env"))

    # Load file specified by ENV_FILE environment variable
    env_file_from_os = os.getenv("ENV_FILE")
    if env_file_from_os and env_file_from_os != ".env": # Avoid double loading if ENV_FILE is .env
        config.update(dotenv_values(env_file_from_os))

    # Load .env.local
    config.update(dotenv_values(".env.local"))

    # Load any additional files passed in the argument
    if files_to_load:
        for f in files_to_load:
            config.update(dotenv_values(f))

    return config
