import os
import boto3

from actions_logging.app_logging import logger
from env_files.constants import VERSION_HISTORY_FOLDER

from env_files.version_history import (
    commit_history_changes,
    upload_version_history_to_s3
)
from git.utils import tag_and_push
from github.env import exit_on_error_and_write_summary, get_required_env_var
from aws.constants import STAGING, PROD_ENVS
from env_files.build_and_upload_dotenv import delete_backup_dot_env
from env_files.utils import get_multi_service_repo, route_core_ecs


@route_core_ecs
def upload_version_history_to_s3_and_delete_backup(env_name: str,
                                                   svc_name: str,
                                                   is_multi_service_repo: bool,
                                                   version_history_folder: str = VERSION_HISTORY_FOLDER):
    history_folder_path = version_history_folder
    if is_multi_service_repo:
        history_folder_path = os.path.join(version_history_folder, svc_name)
    upload_version_history_to_s3([env_name], svc_name, history_folder_path)
    delete_backup_dot_env(env_name, f"{svc_name}.env")


@route_core_ecs
def upload_prod_version_history_to_s3_on_hotfix(svc_name: str,
                                                is_multi_service_repo: bool,
                                                version_history_folder: str = VERSION_HISTORY_FOLDER):
    history_folder_path = version_history_folder
    if is_multi_service_repo:
        history_folder_path = os.path.join(version_history_folder, svc_name)
    upload_version_history_to_s3(PROD_ENVS, svc_name, history_folder_path)


def main():
    try:
        svc_name = get_required_env_var("SVC_NAME")
        svc_type = get_required_env_var("SVC_TYPE")
        env_name = get_required_env_var("ENV_NAME")
        build_artifact = os.getenv("BUILD_ARTIFACT", "false") == "true"
        version_history_folder = os.getenv("VERSION_HISTORY_FOLDER", VERSION_HISTORY_FOLDER)

        is_multi_service_repo = get_multi_service_repo(svc_type)

        logger.info_green_bg("recording env vars version changes in repository and env files s3 bucket")
        env_file_dev_version = os.getenv(f"ENV_FILE_DEV_VERSION")
        if env_file_dev_version:
            tag_and_push(env_file_dev_version)

        upload_version_history_to_s3_and_delete_backup(env_name, svc_name, is_multi_service_repo, version_history_folder)

        if env_name == STAGING and build_artifact:
            upload_prod_version_history_to_s3_on_hotfix(svc_name, is_multi_service_repo, version_history_folder)

        # # This has to happen in the end - else the commit will happen and get_dev_version() will return the new version and the backup .env cannot be deleted
        # # this can be solved but a refactor needed
        commit_history_changes(version_history_folder)
    except Exception as e:
        logger.trace_back(e.__traceback__)
        exit_on_error_and_write_summary(f"couldn't commit and tag env vars version history due to error: {e}")


if __name__ == '__main__':
    main()