import os
from actions_logging.app_logging import logger
from common.common import run_command
from github.env import exit_on_error_and_write_summary, get_required_env_var, write_github_summary
from core.constants import MANIFESTS_CONFIG


def create_run_command_str(script_name, version_to_deploy, manifest_type, manifest_role):
    """
    get the data that relevant for each one of the manifest types and envs
    the data comes from constants and from env vars
    with the data will create the run command for the update_user_script.py from sx-gw-manifest-generator repo
    :param script_name: the script name which run
    :param version_to_deploy: will use as --git-version for the script
    :param manifest_type: will serve as the value of type=
    :param manifest_role: wil serve as the value of role=
    :return:
    """
    run_command_str = (
        f"python3 {script_name} "
        f"--mongo-uri '{get_required_env_var('MONGO_DB_SAFERX_BE')}' "
        f"--git-version {version_to_deploy} "
        f"--set type={manifest_type} "
        f"role={manifest_role} "
        f"vector_url={get_required_env_var('VECTOR_URL')} "
        f"ca_chain_path={get_required_env_var('CONSUL_TEMP_CA_CHAIN_PATH')}"
    )
    return run_command_str


def prepare_and_validate_manifest_config(manifest_key):
    script_name = MANIFESTS_CONFIG[manifest_key].get('script_name')
    manifest_role = MANIFESTS_CONFIG[manifest_key].get('role')
    manifest_type = MANIFESTS_CONFIG[manifest_key].get('type')
    vars_to_validate = [script_name, manifest_role, manifest_type]
    if not all(vars_to_validate):
        exit_on_error_and_write_summary(f"Failed to update manifest {manifest_key}. at least one of the values {vars_to_validate} is missing")
    return script_name, manifest_role, manifest_type


def main():
    """
    I'll get value of VERSION_TO_DEPLOY env var and then I'll run the update_manifests.py script
    with the args relevant for each manifest type. v3 v4 and edge
    """
    try:
        logger.info_green("will deploy v3 v4 and edge manifests via update user scripts from sx-gw-manifest-generator repo")
        version_to_deploy = get_required_env_var('VERSION_TO_DEPLOY')
        if not version_to_deploy:
            exit_on_error_and_write_summary(f"Failed to get version to deploy - {version_to_deploy}")
        logger.debug(f"will deploy version {version_to_deploy}")
        for manifest_key in MANIFESTS_CONFIG:
            script_name, manifest_role, manifest_type = prepare_and_validate_manifest_config(manifest_key)
            run_command_str = create_run_command_str(script_name,
                                                     version_to_deploy,
                                                     manifest_type,
                                                     manifest_role)
            run_command(run_command_str)
        env_name = os.getenv('ENV_NAME')
        write_github_summary(f":trophy: version {version_to_deploy} of all manifests - {' '.join(MANIFESTS_CONFIG.keys())} was deployed to {env_name} successfully")
    except Exception as e:
        exit_on_error_and_write_summary(f"Failed to update manifests. Error: {e}")


if __name__ == '__main__':
    main()
