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

"""
Deployment Status module for ECS MCP Server.
This module provides tools to check the status of ECS deployments.
"""

from typing import Any, Dict, Optional

from fastmcp import FastMCP
from pydantic import Field

from awslabs.ecs_mcp_server.api.status import get_deployment_status


def register_module(mcp: FastMCP) -> None:
    """Register deployment status module tools and prompts with the MCP server."""

    @mcp.tool(name="get_deployment_status", annotations=None)
    async def mcp_get_deployment_status(
        app_name: str = Field(
            ...,
            description="Name of the application",
        ),
        cluster_name: Optional[str] = Field(
            default=None,
            description="Name of the ECS Cluster",
        ),
        stack_name: Optional[str] = Field(
            default=None,
            description=(
                "Name of the CloudFormation Stack "
                "(optional, defaults to {app_name}-ecs-infrastructure)"
            ),
        ),
        service_name: Optional[str] = Field(
            default=None,
            description="Name of the ECS Service (optional, defaults to {app_name}-service)",
        ),
    ) -> Dict[str, Any]:
        """
        Gets the status of an ECS deployment and returns the ALB URL.

        This tool checks the status of your ECS deployment and provides information
        about the Service, Tasks, and the Application Load Balancer URL for accessing
        your application.

        USAGE INSTRUCTIONS:
        1. Provide the name of your application
        2. Optionally specify the ECS Cluster name if different from the application name
        3. Optionally specify the CloudFormation Stack name if different from the default naming
           convention
        4. Optionally specify the ECS Service name if different from the default naming pattern
        5. The tool will return the deployment status and access URL once the deployment
           is complete.

        Poll this tool every 30 seconds till the status is active.

        The status information includes:
        - ECS Service status (active, draining, etc.)
        - Running Task count
        - Desired Task count
        - Application Load Balancer URL
        - Recent deployment events
        - Health check status
        - Custom domain and HTTPS setup guidance (when deployment is complete)

        Parameters:
            app_name: Name of the application
            cluster_name: Name of the ECS Cluster (optional, defaults to app_name)
            stack_name: Name of the CloudFormation Stack
                       (optional, defaults to {app_name}-ecs-infrastructure)
            service_name: Name of the ECS Service (optional, defaults to {app_name}-service)

        Returns:
            Dictionary containing deployment status and ALB URL
        """
        return await get_deployment_status(app_name, cluster_name, stack_name, service_name)
