#  Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
#
#    Licensed under the Apache License, Version 2.0 (the "License").
#    You may not use this file except in compliance with the License.
#    You may obtain a copy of the License at
#
#        http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS,
#    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#    See the License for the specific language governing permissions and
#    limitations under the License.

import logging
import os
from typing import TYPE_CHECKING, Any, Dict, Optional

import aws_orbit
from aws_orbit.plugins import hooks
from aws_orbit.remote_files import helm

if TYPE_CHECKING:
    from aws_orbit.models.context import Context, TeamContext
_logger: logging.Logger = logging.getLogger("aws_orbit")
CHART_PATH = os.path.join(os.path.dirname(__file__))


@hooks.deploy
def deploy(
    plugin_id: str,
    context: "Context",
    team_context: "TeamContext",
    parameters: Dict[str, Any],
) -> None:
    _logger.debug("Team Env name: %s | Team name: %s", context.name, team_context.name)
    plugin_id = plugin_id.replace("_", "-")
    _logger.debug("plugin_id: %s", plugin_id)
    chart_path = helm.create_team_charts_copy(team_context=team_context, path=CHART_PATH, target_path=plugin_id)

    vars: Dict[str, Optional[str]] = dict(
        team=team_context.name,
        region=context.region,
        account_id=context.account_id,
        env_name=context.name,
        restart_policy=parameters["restartPolicy"] if "restartPolicy" in parameters else "Always",
        path=parameters["path"] if "path" in parameters else "/home/jovyan/shared/voila",
        options=parameters["options"] if "options" in parameters else "",
        plugin_id=plugin_id,
        toolkit_s3_bucket=context.toolkit.s3_bucket,
        image_pull_policy="Always" if aws_orbit.__version__.endswith(".dev0") else "IfNotPresent",
        image=parameters["image"] if "image" in parameters else team_context.final_image_address,
        sts_ep="legacy" if context.networking.data.internet_accessible else "regional",
    )

    repo_location = team_context.team_helm_repository
    if repo_location:
        repo = team_context.name
        helm.add_repo(repo=repo, repo_location=repo_location)
        chart_name, chart_version, chart_package = helm.package_chart(repo=repo, chart_path=chart_path, values=vars)
        helm.install_chart(
            repo=repo,
            namespace=team_context.name,
            name=f"{team_context.name}-{plugin_id}",
            chart_name=chart_name,
            chart_version=chart_version,
        )


@hooks.destroy
def destroy(
    plugin_id: str,
    context: "Context",
    team_context: "TeamContext",
    parameters: Dict[str, Any],
) -> None:
    _logger.debug(
        "Delete Plugin %s of Team Env name: %s | Team name: %s",
        plugin_id,
        context.name,
        team_context.name,
    )
    helm.uninstall_chart(f"{team_context.name}-{plugin_id}", namespace=team_context.name)

[(25, 'logging.getLogger', 'logging.getLogger', False, 'import logging\n'), (26, 'os.path.dirname', 'os.path.dirname', False, 'import os\n'), (39, 'aws_orbit.remote_files.helm.create_team_charts_copy', 'helm.create_team_charts_copy', False, 'from aws_orbit.remote_files import helm\n'), (83, 'aws_orbit.remote_files.helm.uninstall_chart', 'helm.uninstall_chart', False, 'from aws_orbit.remote_files import helm\n'), (59, 'aws_orbit.remote_files.helm.add_repo', 'helm.add_repo', False, 'from aws_orbit.remote_files import helm\n'), (60, 'aws_orbit.remote_files.helm.package_chart', 'helm.package_chart', False, 'from aws_orbit.remote_files import helm\n'), (61, 'aws_orbit.remote_files.helm.install_chart', 'helm.install_chart', False, 'from aws_orbit.remote_files import helm\n'), (51, 'aws_orbit.__version__.endswith', 'aws_orbit.__version__.endswith', False, 'import aws_orbit\n')]