import os
import json


def json_to_env(json_file):
    try:
        with open(json_file, 'r') as f:
            data = json.load(f)
            for key, value in data.items():
                print(key)
                if key == "REACT_APP_GOOGLE_RECAPTCHA_SECRET":
                    reacaptcha = os.getenv("GLOBAL_KEYS_GOOGLE_RECAPTCHA_SECRET")
                    os.system(f"echo '{key}={reacaptcha}' >> $GITHUB_ENV")
                else:
                    os.system(f"echo '{key}={value}' >> $GITHUB_ENV")
            print("loaded parameters to envs")
    except Exception as e:
        print(f"error in loading local env file in path: {json_file} to enirnment veriables")
        print(e)
        exit(1)



if __name__ == "__main__":
    env_name = os.getenv('ENV_NAME')
    if env_name not in ['staging', 'production']:
        dir_name = 'dev'
    else:
        dir_name = env_name

    json_file = f".github/env_files/{dir_name}/{env_name}.json"
    json_to_env(json_file)