# 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.

"""Utility functions for working with Amazon ECR repositories.

This module provides functions to check if an ECR repository exists and create it if needed.

Note: These tools are intended for development and prototyping purposes only and are not meant
for production use cases.
"""

import boto3
from ..consts import STATUS_ERROR, STATUS_SUCCESS
from .common import format_result
from awslabs.finch_mcp_server import __version__
from botocore.config import Config
from botocore.exceptions import ClientError
from loguru import logger
from typing import Dict, Optional


USER_AGENT_EXTRA = f'md/awslabs#mcp#finch-mcp-server#{__version__}'
_config = Config(user_agent_extra=USER_AGENT_EXTRA)


def create_ecr_repository(
    repository_name: str,
    region: Optional[str] = None,
) -> Dict[str, str]:
    """Check if an ECR repository exists and create it if it doesn't.

    This function first checks if the specified ECR repository exists using boto3.
    If the repository doesn't exist, it creates a new one with the given name.

    Args:
        repository_name: The name of the repository to check or create in ECR
        region: AWS region for the ECR repository. If not provided, uses the default region
                from AWS configuration

    Returns:
        Dict[str, Any]: A dictionary containing:
            - status: "success" if the operation succeeded, "error" otherwise
            - message: Details about the result of the operation

    """
    try:
        ecr_client = (
            boto3.client('ecr', region_name=region, config=_config)
            if region
            else boto3.client('ecr', config=_config)
        )

        try:
            response = ecr_client.describe_repositories(repositoryNames=[repository_name])

            if 'repositories' in response and len(response['repositories']) > 0:
                repository = response['repositories'][0]
                repository_uri = repository.get('repositoryUri', '')

                logger.debug(
                    f"ECR repository '{repository_name}' already exists with URI: {repository_uri}"
                )
                return format_result(
                    STATUS_SUCCESS,
                    f"ECR repository '{repository_name}' already exists.",
                )
        except ClientError as e:
            error_code = e.response.get('Error', {}).get('Code')

            if error_code != 'RepositoryNotFoundException':
                return format_result(
                    STATUS_ERROR,
                    f'Error checking ECR repository: {str(e)}',
                )

        response = ecr_client.create_repository(
            repositoryName=repository_name,
            imageTagMutability='IMMUTABLE',
        )

        repository = response.get('repository', {})
        repository_uri = repository.get('repositoryUri', '')

        logger.debug(f"Created ECR repository '{repository_name}' with URI: {repository_uri}")
        return format_result(
            STATUS_SUCCESS,
            f"Successfully created ECR repository '{repository_name}' with URI: {repository_uri}.",
        )

    except ClientError as e:
        return format_result(
            STATUS_ERROR,
            f"Failed to create ECR repository '{repository_name}': {str(e)}",
        )
    except Exception as e:
        return format_result(
            STATUS_ERROR,
            f"Unexpected error creating ECR repository '{repository_name}': {str(e)}",
        )
