import os

from actions_logging.app_logging import logger
from env_files.create_and_load_dotenv import load_dotenv_with_placeholders
from github.env import exit_on_error_and_write_summary, get_required_env_var, write_github_env

"""
This script loads a env vars with placeholders for the given service name, environment name,
and git version for env keys that have REACT_APP_ and VITE_ prefixes in them.
"""

if __name__ == "__main__":
    version = get_required_env_var("VERSION_TO_DEPLOY")
    svc_name = get_required_env_var("SVC_NAME")
    env_name = get_required_env_var("ENV_NAME")
    repo_name = get_required_env_var("GITHUB_REPOSITORY")
    git_env_version = os.getenv("ENV_FILE_VERSION", "")
    try:
        load_dotenv_with_placeholders(repo_name, svc_name, env_name, git_env_version)
        write_github_env(version, "REACT_APP_GIT_TAG")
        logger.info_yellow(f"wrote REACT_APP_GIT_TAG={version} to env context")
    except Exception as e:
        exit_on_error_and_write_summary(e)