# Copyright (c) 2020 CloudZero - ALL RIGHTS RESERVED - PROPRIETARY AND CONFIDENTIAL
# Unauthorized copying of this file and/or project, via any medium is strictly prohibited.
# Direct all questions to legal@cloudzero.com
#

from pprint import pprint

from src.api.v4_connections import disable_connection, enable_connection
from src.constants import AWS_ACCOUNT_ID_REGEX  # , SNOWFLAKE_ACCOUNT_ID_REGEX
from src.utils.console import eprint


def functions(arguments, _organization_data=None):
    debug = arguments.get("--debug", False)
    connection_id = arguments["--connection-id"]

    if arguments["--type"] == "AWS" and not AWS_ACCOUNT_ID_REGEX.match(connection_id):
        eprint(f"ERROR: Cloud Account ID {connection_id} is not a valid AWS Account ID")
        return
    # elif arguments["--type"] == "SNOWFLAKE" and not SNOWFLAKE_ACCOUNT_ID_REGEX.match(connection_id):
    #     eprint(f"ERROR: Cloud Account ID {connection_id} is not a valid SNOWFLAKE Account ID")
    #     return

    try:
        if arguments["enable"]:
            result = enable_connection(connection_id, debug=debug)
        elif arguments["disable"]:
            result = disable_connection(connection_id, debug=debug)
        else:
            result = None

        pprint(result)
    except Exception as e:
        eprint(f"ERROR: Failed to {'enable' if arguments['enable'] else 'disable'} connection: {e}")
        if debug:
            raise
