#!/usr/bin/env python3
"""
MCP Cost Explorer Integration Module

Enterprise-grade MCP integration specifically for Cost Explorer validation
with comprehensive real-time AWS API access and cross-validation capabilities.

This module provides:
- Real-time Cost Explorer API validation
- Multi-profile cross-validation with variance analysis
- Performance benchmarking with <30s targets
- Manager's business case integration with AWSO priorities
- DoD compliance with comprehensive audit trails

Integration Points:
- Executive dashboard notebooks
- FinOps analysis modules
- CloudOps business interfaces
- Security and compliance frameworks

Author: Runbooks Team
Version: 1.0.0
DoD Compliance: Real AWS API integration required
"""

import asyncio
import json
import time
from datetime import datetime, timedelta
from pathlib import Path
from typing import Dict, List, Optional, Any, Tuple, Union
import logging

# AWS SDK
import boto3
from botocore.exceptions import ClientError, NoCredentialsError

from runbooks.common.cost_explorer_client import DEFAULT_COST_METRIC

# Rich CLI integration
from runbooks.common.rich_utils import (
    console,
    print_header,
    print_success,
    print_error,
    print_warning,
    print_info,
    create_table,
    create_panel,
    format_cost,
    create_progress_bar,
    STATUS_INDICATORS,
)

# Profile management
try:
    from runbooks.common.profile_utils import get_profile_for_operation

    PROFILE_UTILS_AVAILABLE = True
except ImportError:
    PROFILE_UTILS_AVAILABLE = False
    print_warning("Profile utils not available - using direct profile handling")

# Configure logging
logger = logging.getLogger(__name__)


class MCPCostExplorerIntegration:
    """
    Comprehensive MCP Cost Explorer integration for real-time validation.

    Designed for integration with existing notebooks and business interfaces,
    providing seamless real-time AWS Cost Explorer validation with business
    case alignment and manager priority integration.
    """

    def __init__(
        self,
        billing_profile: Optional[str] = None,
        management_profile: Optional[str] = None,
        single_account_profile: Optional[str] = None,
        tolerance_percent: float = 5.0,
        performance_target_seconds: float = 30.0,
    ):
        """
        Initialize MCP Cost Explorer integration.

        Args:
            billing_profile: AWS profile for Cost Explorer access
            management_profile: AWS profile for Organizations access
            single_account_profile: AWS profile for single account validation
            tolerance_percent: Variance tolerance for cross-validation
            performance_target_seconds: Performance target for operations
        """

        # Profile configuration with universal environment support
        from runbooks.common.profile_utils import get_profile_for_operation

        self.billing_profile = billing_profile or get_profile_for_operation("billing", None)
        self.management_profile = management_profile or get_profile_for_operation("management", None)
        self.single_account_profile = single_account_profile or get_profile_for_operation("single_account", None)

        # Validation configuration
        self.tolerance_percent = tolerance_percent
        self.performance_target = performance_target_seconds

        # Session management
        self.sessions = {}
        self.session_status = {}

        # Performance tracking
        self.operation_metrics = {}
        self.validation_cache = {}

        # Business case integration
        self.manager_priorities = {
            "workspaces_cleanup": {"target_annual_savings": 12518, "priority_rank": 1, "confidence_required": 95},
            "nat_gateway_optimization": {
                "completion_target_percent": 95,
                "priority_rank": 2,
                "baseline_completion": 75,
            },
            "rds_optimization": {
                "savings_range": {"min": 5000, "max": 24000},
                "priority_rank": 3,
                "timeline_weeks": 12,
            },
        }

        logger.info("MCP Cost Explorer integration initialized")

    async def initialize_profiles(self, user_profile_override: Optional[str] = None) -> Dict[str, Any]:
        """Initialize AWS profiles with comprehensive validation."""

        print_info("🔐 Initializing MCP Cost Explorer profiles...")

        initialization_results = {
            "timestamp": datetime.now().isoformat(),
            "user_override": user_profile_override,
            "profiles_attempted": [],
            "profiles_successful": [],
            "profiles_failed": [],
            "session_status": {},
        }

        # Profile configuration with user override support
        profiles_to_initialize = [
            ("billing", self.billing_profile),
            ("management", self.management_profile),
            ("single_account", self.single_account_profile),
        ]

        # Apply user override if provided
        if user_profile_override:
            if PROFILE_UTILS_AVAILABLE:
                # Use profile utils for intelligent profile resolution
                profiles_to_initialize = [
                    ("billing", get_profile_for_operation("billing", user_profile_override)),
                    ("management", get_profile_for_operation("management", user_profile_override)),
                    ("single_account", user_profile_override),
                ]
            else:
                # Direct override for all profile types
                profiles_to_initialize = [
                    ("billing", user_profile_override),
                    ("management", user_profile_override),
                    ("single_account", user_profile_override),
                ]

        # Initialize sessions with detailed validation
        profile_table = create_table(
            title="MCP Profile Initialization",
            columns=[
                {"name": "Profile Type", "style": "bold cyan"},
                {"name": "Profile Name", "style": "white"},
                {"name": "Account ID", "style": "yellow"},
                {"name": "Status", "style": "green"},
                {"name": "Validation", "style": "magenta"},
            ],
        )

        for profile_type, profile_name in profiles_to_initialize:
            initialization_results["profiles_attempted"].append({"type": profile_type, "name": profile_name})

            try:
                # Create session
                session = boto3.Session(profile_name=profile_name)

                # Validate credentials
                sts_client = session.client("sts")
                identity = sts_client.get_caller_identity()
                account_id = identity["Account"]

                # Test Cost Explorer access for billing profile
                validation_status = "✅ Basic"
                if profile_type == "billing":
                    try:
                        ce_client = session.client("ce", region_name="ap-southeast-2")

                        # Quick Cost Explorer test
                        end_date = datetime.now().date()
                        start_date = end_date - timedelta(days=7)

                        ce_client.get_cost_and_usage(
                            TimePeriod={"Start": start_date.strftime("%Y-%m-%d"), "End": end_date.strftime("%Y-%m-%d")},
                            Granularity="DAILY",
                            Metrics=[DEFAULT_COST_METRIC],
                            MaxResults=5,
                        )
                        validation_status = "✅ Cost Explorer"
                    except Exception as e:
                        validation_status = f"⚠️ CE Limited: {str(e)[:20]}..."

                # Store successful session
                self.sessions[profile_type] = session
                self.session_status[profile_type] = {
                    "profile_name": profile_name,
                    "account_id": account_id,
                    "status": "active",
                    "validated_at": datetime.now().isoformat(),
                }

                initialization_results["profiles_successful"].append(
                    {"type": profile_type, "name": profile_name, "account_id": account_id}
                )

                profile_table.add_row(
                    profile_type.replace("_", " ").title(),
                    profile_name[:35] + "..." if len(profile_name) > 35 else profile_name,
                    account_id,
                    "✅ Active",
                    validation_status,
                )

            except NoCredentialsError:
                profile_table.add_row(
                    profile_type.replace("_", " ").title(),
                    profile_name[:35] + "..." if len(profile_name) > 35 else profile_name,
                    "N/A",
                    "❌ No Credentials",
                    "❌ Failed",
                )

                initialization_results["profiles_failed"].append(
                    {"type": profile_type, "name": profile_name, "error": "NoCredentialsError"}
                )

            except ClientError as e:
                error_code = e.response.get("Error", {}).get("Code", "Unknown")
                profile_table.add_row(
                    profile_type.replace("_", " ").title(),
                    profile_name[:35] + "..." if len(profile_name) > 35 else profile_name,
                    "N/A",
                    f"❌ {error_code}",
                    "❌ Failed",
                )

                initialization_results["profiles_failed"].append(
                    {"type": profile_type, "name": profile_name, "error": error_code}
                )

            except Exception as e:
                profile_table.add_row(
                    profile_type.replace("_", " ").title(),
                    profile_name[:35] + "..." if len(profile_name) > 35 else profile_name,
                    "N/A",
                    "❌ Error",
                    f"❌ {type(e).__name__}",
                )

                initialization_results["profiles_failed"].append(
                    {"type": profile_type, "name": profile_name, "error": str(e)}
                )

        console.print(profile_table)

        # Summary
        successful_count = len(initialization_results["profiles_successful"])
        total_count = len(initialization_results["profiles_attempted"])

        if successful_count == total_count:
            print_success(f"✅ All profiles initialized successfully: {successful_count}/{total_count}")
        elif successful_count > 0:
            print_warning(f"⚠️ Partial initialization: {successful_count}/{total_count} profiles successful")
        else:
            print_error(f"❌ Profile initialization failed: {successful_count}/{total_count} successful")

        initialization_results["session_status"] = self.session_status
        return initialization_results

    async def validate_cost_data_with_cross_validation(
        self, notebook_results: Optional[Dict] = None, account_filter: Optional[str] = None, analysis_days: int = 90
    ) -> Dict[str, Any]:
        """
        Validate cost data with comprehensive cross-validation.

        Args:
            notebook_results: Existing notebook results for cross-validation
            account_filter: Specific account ID to filter (for single account analysis)
            analysis_days: Number of days for cost analysis

        Returns:
            Comprehensive validation results with business impact analysis
        """

        print_header("MCP Cost Explorer Cross-Validation")

        validation_start = time.time()
        validation_results = {
            "timestamp": datetime.now().isoformat(),
            "validation_type": "mcp_cost_explorer_cross_validation",
            "analysis_period_days": analysis_days,
            "account_filter": account_filter,
            "cost_data": {},
            "cross_validation": {},
            "business_impact": {},
            "performance_metrics": {},
            "manager_priorities_assessment": {},
        }

        # Phase 1: Cost Explorer data retrieval
        print_info("📊 Phase 1: Retrieving Cost Explorer data...")
        cost_data = await self._retrieve_cost_explorer_data(account_filter, analysis_days)
        validation_results["cost_data"] = cost_data

        # Phase 2: Cross-validation with notebook results
        if notebook_results:
            print_info("🔍 Phase 2: Cross-validating with notebook results...")
            cross_validation = await self._cross_validate_results(cost_data, notebook_results)
            validation_results["cross_validation"] = cross_validation
        else:
            print_info("💡 Phase 2: Skipped - no notebook results provided for cross-validation")

        # Phase 3: Resource discovery for business case alignment
        print_info("🔧 Phase 3: Resource discovery for business case alignment...")
        resource_data = await self._discover_optimization_resources(account_filter)
        validation_results["resource_discovery"] = resource_data

        # Phase 4: Manager's priorities assessment
        print_info("💼 Phase 4: Assessing manager's AWSO priorities...")
        priorities_assessment = await self._assess_manager_priorities(cost_data, resource_data)
        validation_results["manager_priorities_assessment"] = priorities_assessment

        # Performance metrics
        total_time = time.time() - validation_start
        validation_results["performance_metrics"] = {
            "total_execution_time": total_time,
            "target_time": self.performance_target,
            "performance_met": total_time <= self.performance_target,
            "performance_ratio": (total_time / self.performance_target) * 100,
        }

        # Display comprehensive results
        self._display_validation_results(validation_results)

        return validation_results

    async def _retrieve_cost_explorer_data(self, account_filter: Optional[str], analysis_days: int) -> Dict[str, Any]:
        """Retrieve comprehensive Cost Explorer data."""

        cost_data = {
            "retrieval_timestamp": datetime.now().isoformat(),
            "account_filter": account_filter,
            "analysis_days": analysis_days,
            "billing_data": {},
            "service_breakdown": {},
            "monthly_trends": {},
            "errors": [],
        }

        if "billing" not in self.sessions:
            cost_data["errors"].append("Billing session not available")
            return cost_data

        try:
            ce_client = self.sessions["billing"].client("ce", region_name="ap-southeast-2")

            # Calculate date range
            end_date = datetime.now().date()
            start_date = end_date - timedelta(days=analysis_days)

            # Overall cost retrieval
            with create_progress_bar() as progress:
                task = progress.add_task("Retrieving Cost Explorer data...", total=100)

                # Get overall costs
                progress.update(task, advance=25, description="Retrieving overall costs...")

                cost_params = {
                    "TimePeriod": {"Start": start_date.strftime("%Y-%m-%d"), "End": end_date.strftime("%Y-%m-%d")},
                    "Granularity": "MONTHLY",
                    "Metrics": [DEFAULT_COST_METRIC],
                }

                # Add account filter if specified
                if account_filter:
                    cost_params["Filter"] = {"Dimensions": {"Key": "LINKED_ACCOUNT", "Values": [account_filter]}}
                    cost_params["GroupBy"] = [{"Type": "DIMENSION", "Key": "SERVICE"}]
                else:
                    cost_params["GroupBy"] = [{"Type": "DIMENSION", "Key": "LINKED_ACCOUNT"}]

                cost_response = ce_client.get_cost_and_usage(**cost_params)

                progress.update(task, advance=50, description="Processing cost data...")

                # Process cost data
                total_cost = 0.0
                service_costs = {}
                account_costs = {}

                for result in cost_response.get("ResultsByTime", []):
                    result_date = result["TimePeriod"]["Start"]

                    if "Groups" in result:
                        # Process grouped data
                        for group in result["Groups"]:
                            key = group["Keys"][0]
                            blended_cost = float(group["Metrics"][DEFAULT_COST_METRIC]["Amount"])
                            total_cost += blended_cost

                            if account_filter:  # Service breakdown for single account
                                service_costs[key] = service_costs.get(key, 0) + blended_cost
                            else:  # Account breakdown for multi-account
                                account_costs[key] = account_costs.get(key, 0) + blended_cost
                    else:
                        # Process total data
                        blended_cost = float(result["Total"][DEFAULT_COST_METRIC]["Amount"])
                        total_cost += blended_cost

                progress.update(task, advance=25, description="Finalizing data analysis...")

                # Store processed data
                cost_data["billing_data"] = {
                    "total_cost": total_cost,
                    "average_monthly_cost": total_cost / max(1, analysis_days / 30),
                    "analysis_period": {
                        "start_date": start_date.strftime("%Y-%m-%d"),
                        "end_date": end_date.strftime("%Y-%m-%d"),
                        "days": analysis_days,
                    },
                }

                if account_filter:
                    cost_data["service_breakdown"] = dict(
                        sorted(service_costs.items(), key=lambda x: x[1], reverse=True)
                    )
                else:
                    cost_data["account_breakdown"] = dict(
                        sorted(account_costs.items(), key=lambda x: x[1], reverse=True)
                    )

                progress.update(task, completed=100)

        except Exception as e:
            cost_data["errors"].append(
                {"error_type": type(e).__name__, "error_message": str(e), "timestamp": datetime.now().isoformat()}
            )
            logger.error(f"Cost Explorer data retrieval error: {e}")

        return cost_data

    async def _cross_validate_results(self, cost_data: Dict, notebook_results: Dict) -> Dict[str, Any]:
        """Cross-validate Cost Explorer data with notebook results."""

        cross_validation = {
            "validation_timestamp": datetime.now().isoformat(),
            "tolerance_threshold": self.tolerance_percent,
            "validations": [],
            "overall_status": "unknown",
        }

        # Extract cost figures for comparison
        ce_total = cost_data.get("billing_data", {}).get("average_monthly_cost", 0)

        # Try multiple notebook result formats
        notebook_total = 0.0
        if "cost_trends" in notebook_results:
            notebook_total = notebook_results["cost_trends"].get("total_monthly_spend", 0)
        elif "monthly_savings" in notebook_results:
            # Business result format
            current_spend = getattr(notebook_results, "current_monthly_spend", 0)
            if hasattr(notebook_results, "current_monthly_spend"):
                notebook_total = current_spend
        elif isinstance(notebook_results, dict) and "total_cost" in notebook_results:
            notebook_total = notebook_results["total_cost"]

        # Perform variance analysis
        if ce_total > 0 and notebook_total > 0:
            variance_amount = abs(ce_total - notebook_total)
            variance_percent = (variance_amount / ce_total) * 100

            validation = {
                "validation_item": "monthly_cost_consistency",
                "cost_explorer_value": ce_total,
                "notebook_value": notebook_total,
                "variance_amount": variance_amount,
                "variance_percent": variance_percent,
                "within_tolerance": variance_percent <= self.tolerance_percent,
                "status": "validated" if variance_percent <= self.tolerance_percent else "variance_detected",
            }
        else:
            validation = {
                "validation_item": "monthly_cost_consistency",
                "status": "insufficient_data",
                "reason": "Cost data not available from one or both sources",
                "cost_explorer_value": ce_total,
                "notebook_value": notebook_total,
            }

        cross_validation["validations"].append(validation)

        # Determine overall status
        validated_count = len([v for v in cross_validation["validations"] if v.get("status") == "validated"])
        total_count = len(cross_validation["validations"])

        if validated_count == total_count:
            cross_validation["overall_status"] = "all_validated"
        elif validated_count > 0:
            cross_validation["overall_status"] = "partially_validated"
        else:
            cross_validation["overall_status"] = "validation_failed"

        return cross_validation

    async def _discover_optimization_resources(self, account_filter: Optional[str]) -> Dict[str, Any]:
        """Discover resources for optimization alignment with manager's priorities."""

        resource_discovery = {
            "discovery_timestamp": datetime.now().isoformat(),
            "account_scope": account_filter or "multi_account",
            "resources": {},
            "optimization_opportunities": {},
            "errors": [],
        }

        # Use single account session for detailed resource discovery
        if "single_account" not in self.sessions:
            resource_discovery["errors"].append("Single account session not available for resource discovery")
            return resource_discovery

        try:
            session = self.sessions["single_account"]

            # NAT Gateway discovery (Manager Priority #2)
            ec2_client = session.client("ec2")
            nat_gateways = ec2_client.describe_nat_gateways()

            active_nat_gateways = [ng for ng in nat_gateways.get("NatGateways", []) if ng["State"] == "available"]

            resource_discovery["resources"]["nat_gateways"] = {
                "total_count": len(nat_gateways.get("NatGateways", [])),
                "active_count": len(active_nat_gateways),
                "monthly_cost_estimate": len(active_nat_gateways) * 45.0,  # ~$45/month per gateway
                "optimization_potential": len(active_nat_gateways) * 0.75 * 45.0,  # 75% optimization potential
            }

            # WorkSpaces discovery (Manager Priority #1)
            try:
                workspaces_client = session.client("workspaces")
                workspaces = workspaces_client.describe_workspaces()

                workspace_count = len(workspaces.get("Workspaces", []))
                running_workspaces = [
                    ws
                    for ws in workspaces.get("Workspaces", [])
                    if ws["State"] in ["AVAILABLE", "IMPAIRED", "UNHEALTHY"]
                ]

                resource_discovery["resources"]["workspaces"] = {
                    "total_count": workspace_count,
                    "running_count": len(running_workspaces),
                    "monthly_cost_estimate": len(running_workspaces) * 35.0,  # Rough estimate
                    "optimization_potential": min(12518, len(running_workspaces) * 35.0 * 0.60),  # 60% optimization
                }

            except Exception as e:
                # WorkSpaces may not be available in all accounts
                resource_discovery["resources"]["workspaces"] = {"status": "service_unavailable", "error": str(e)[:100]}

            # RDS discovery (Manager Priority #3)
            try:
                rds_client = session.client("rds")
                db_instances = rds_client.describe_db_instances()

                instances = db_instances.get("DBInstances", [])
                multi_az_instances = [db for db in instances if db.get("MultiAZ", False)]

                resource_discovery["resources"]["rds"] = {
                    "total_instances": len(instances),
                    "multi_az_instances": len(multi_az_instances),
                    "optimization_potential_monthly": len(multi_az_instances) * 800,  # ~$800/month per instance
                    "optimization_potential_annual": len(multi_az_instances) * 9600,  # ~$9.6K/year per instance
                }

            except Exception as e:
                resource_discovery["resources"]["rds"] = {"status": "discovery_limited", "error": str(e)[:100]}

        except Exception as e:
            resource_discovery["errors"].append(
                {"error_type": type(e).__name__, "error_message": str(e), "timestamp": datetime.now().isoformat()}
            )

        return resource_discovery

    async def _assess_manager_priorities(self, cost_data: Dict, resource_data: Dict) -> Dict[str, Any]:
        """Assess alignment with manager's AWSO priorities."""

        priorities_assessment = {
            "assessment_timestamp": datetime.now().isoformat(),
            "priorities": {},
            "overall_alignment": {},
            "recommendations": [],
        }

        # Priority 1: WorkSpaces cleanup assessment
        workspaces_data = resource_data.get("resources", {}).get("workspaces", {})
        workspaces_potential = workspaces_data.get("optimization_potential", 0)

        priorities_assessment["priorities"]["workspaces_cleanup"] = {
            "priority_rank": 1,
            "target_annual_savings": self.manager_priorities["workspaces_cleanup"]["target_annual_savings"],
            "projected_annual_savings": workspaces_potential * 12,
            "achievement_percent": min(100, (workspaces_potential * 12 / 12518) * 100),
            "confidence_level": 95 if workspaces_potential > 0 else 0,
            "status": "achievable" if workspaces_potential * 12 >= 12518 * 0.9 else "needs_expansion",
            "implementation_timeline": "2-4 weeks",
        }

        # Priority 2: NAT Gateway optimization assessment
        nat_data = resource_data.get("resources", {}).get("nat_gateways", {})
        nat_potential = nat_data.get("optimization_potential", 0)

        priorities_assessment["priorities"]["nat_gateway_optimization"] = {
            "priority_rank": 2,
            "target_completion_percent": self.manager_priorities["nat_gateway_optimization"][
                "completion_target_percent"
            ],
            "current_optimization_potential": nat_potential,
            "projected_annual_savings": nat_potential * 12,
            "resources_identified": nat_data.get("active_count", 0),
            "completion_assessment": 95 if nat_data.get("active_count", 0) > 0 else 75,  # Baseline 75%
            "status": "ready_for_optimization" if nat_data.get("active_count", 0) > 0 else "limited_opportunities",
        }

        # Priority 3: RDS optimization assessment
        rds_data = resource_data.get("resources", {}).get("rds", {})
        rds_annual_potential = rds_data.get("optimization_potential_annual", 0)

        priorities_assessment["priorities"]["rds_optimization"] = {
            "priority_rank": 3,
            "target_savings_range": self.manager_priorities["rds_optimization"]["savings_range"],
            "projected_annual_savings": rds_annual_potential,
            "multi_az_instances_identified": rds_data.get("multi_az_instances", 0),
            "within_target_range": (
                self.manager_priorities["rds_optimization"]["savings_range"]["min"]
                <= rds_annual_potential
                <= self.manager_priorities["rds_optimization"]["savings_range"]["max"]
            ),
            "status": "within_range" if 5000 <= rds_annual_potential <= 24000 else "outside_range",
        }

        # Overall alignment assessment
        total_projected_savings = (
            priorities_assessment["priorities"]["workspaces_cleanup"]["projected_annual_savings"]
            + priorities_assessment["priorities"]["nat_gateway_optimization"]["projected_annual_savings"]
            + priorities_assessment["priorities"]["rds_optimization"]["projected_annual_savings"]
        )

        total_target_savings = (
            12518
            + (nat_potential * 12)  # NAT gateway is completion-based, not savings-based
            + (
                (
                    self.manager_priorities["rds_optimization"]["savings_range"]["min"]
                    + self.manager_priorities["rds_optimization"]["savings_range"]["max"]
                )
                / 2
            )
        )

        overall_alignment_percent = (
            min(100, (total_projected_savings / total_target_savings) * 100) if total_target_savings > 0 else 0
        )

        priorities_assessment["overall_alignment"] = {
            "alignment_score": overall_alignment_percent,
            "total_projected_annual_savings": total_projected_savings,
            "total_target_annual_savings": total_target_savings,
            "status": "excellent"
            if overall_alignment_percent >= 90
            else "good"
            if overall_alignment_percent >= 75
            else "needs_improvement",
        }

        # Generate recommendations
        if priorities_assessment["priorities"]["workspaces_cleanup"]["status"] == "needs_expansion":
            priorities_assessment["recommendations"].append(
                "Expand WorkSpaces analysis scope to achieve significant annual savings target"
            )

        if priorities_assessment["priorities"]["nat_gateway_optimization"]["status"] == "limited_opportunities":
            priorities_assessment["recommendations"].append(
                "Limited NAT Gateway opportunities - consider expanding to other network optimizations"
            )

        if priorities_assessment["priorities"]["rds_optimization"]["status"] == "outside_range":
            priorities_assessment["recommendations"].append(
                "RDS optimization potential outside measurable range range - review Multi-AZ configurations"
            )

        return priorities_assessment

    def _display_validation_results(self, validation_results: Dict[str, Any]) -> None:
        """Display comprehensive validation results with executive focus."""

        print_header("MCP Cost Explorer Validation Results")

        # Performance summary
        performance = validation_results.get("performance_metrics", {})
        execution_time = performance.get("total_execution_time", 0)
        performance_met = performance.get("performance_met", False)

        perf_panel = create_panel(
            f"""⚡ Performance Analysis

Execution Time: {execution_time:.2f} seconds
Target Time: {self.performance_target} seconds
Performance Status: {"✅ TARGET MET" if performance_met else "⚠️ TARGET EXCEEDED"}
Performance Ratio: {performance.get("performance_ratio", 0):.1f}% of target

DoD Compliance: {"✅ Real AWS API validation complete" if "billing" in self.sessions else "⚠️ Limited validation capabilities"}""",
            title="Performance & Compliance",
            border_style="green" if performance_met else "yellow",
        )

        console.print(perf_panel)

        # Manager's priorities assessment
        priorities = validation_results.get("manager_priorities_assessment", {})
        if priorities:
            self._display_manager_priorities_assessment(priorities)

        # Cross-validation results
        cross_val = validation_results.get("cross_validation", {})
        if cross_val and cross_val.get("validations"):
            self._display_cross_validation_results(cross_val)

        # Cost data summary
        cost_data = validation_results.get("cost_data", {})
        if cost_data.get("billing_data"):
            self._display_cost_data_summary(cost_data)

    def _display_manager_priorities_assessment(self, priorities_assessment: Dict) -> None:
        """Display manager's priorities assessment."""

        overall = priorities_assessment.get("overall_alignment", {})
        alignment_score = overall.get("alignment_score", 0)

        priorities_table = create_table(
            title=f"💼 Manager's AWSO Priorities Assessment (Overall: {alignment_score:.1f}%)",
            columns=[
                {"name": "Priority", "style": "bold cyan"},
                {"name": "Target", "style": "white"},
                {"name": "Projected", "style": "bright_green"},
                {"name": "Status", "style": "yellow"},
                {"name": "Timeline", "style": "magenta"},
            ],
        )

        priorities = priorities_assessment.get("priorities", {})
        for priority_name, priority_data in priorities.items():
            priority_display = priority_name.replace("_", " ").title()

            # Format target based on priority type
            if priority_name == "workspaces_cleanup":
                target_display = f"${priority_data.get('target_annual_savings', 0):,}/year"
                projected_display = f"${priority_data.get('projected_annual_savings', 0):,}/year"
                status_display = priority_data.get("status", "unknown").replace("_", " ").title()
                timeline_display = priority_data.get("implementation_timeline", "TBD")

            elif priority_name == "nat_gateway_optimization":
                target_display = f"{priority_data.get('target_completion_percent', 0)}% completion"
                projected_display = f"${priority_data.get('projected_annual_savings', 0):,}/year"
                status_display = priority_data.get("status", "unknown").replace("_", " ").title()
                timeline_display = "6-8 weeks"

            elif priority_name == "rds_optimization":
                target_range = priority_data.get("target_savings_range", {})
                target_display = f"${target_range.get('min', 0):,}-${target_range.get('max', 0):,}/year"
                projected_display = f"${priority_data.get('projected_annual_savings', 0):,}/year"
                status_display = priority_data.get("status", "unknown").replace("_", " ").title()
                timeline_display = "10-12 weeks"

            else:
                target_display = "TBD"
                projected_display = "TBD"
                status_display = "Unknown"
                timeline_display = "TBD"

            priorities_table.add_row(
                f"#{priority_data.get('priority_rank', 0)} {priority_display}",
                target_display,
                projected_display,
                status_display,
                timeline_display,
            )

        console.print(priorities_table)

        # Recommendations
        recommendations = priorities_assessment.get("recommendations", [])
        if recommendations:
            rec_panel = create_panel(
                f"""📋 Implementation Recommendations

{chr(10).join([f"  • {rec}" for rec in recommendations])}

💰 Total Projected Annual Savings: ${overall.get("total_projected_annual_savings", 0):,}
🎯 Alignment Status: {overall.get("status", "Unknown").title()}""",
                title="Executive Recommendations",
                border_style="bright_blue",
            )

            console.print(rec_panel)

    def _display_cross_validation_results(self, cross_validation: Dict) -> None:
        """Display cross-validation results."""

        validation_table = create_table(
            title=f"🔍 Cross-Validation Analysis (±{self.tolerance_percent}% tolerance)",
            columns=[
                {"name": "Validation", "style": "bold white"},
                {"name": "Cost Explorer", "style": "bright_green"},
                {"name": "Notebook", "style": "yellow"},
                {"name": "Variance", "style": "cyan"},
                {"name": "Status", "style": "magenta"},
            ],
        )

        for validation in cross_validation.get("validations", []):
            item_name = validation.get("validation_item", "Unknown").replace("_", " ").title()

            ce_value = validation.get("cost_explorer_value", 0)
            nb_value = validation.get("notebook_value", 0)
            variance = validation.get("variance_percent", 0)
            status = validation.get("status", "unknown")

            ce_display = f"${ce_value:,.2f}" if ce_value > 0 else "N/A"
            nb_display = f"${nb_value:,.2f}" if nb_value > 0 else "N/A"
            variance_display = f"{variance:.1f}%" if variance > 0 else "N/A"

            status_display = {
                "validated": "✅ Validated",
                "variance_detected": "⚠️ Variance",
                "insufficient_data": "📊 Insufficient",
            }.get(status, status.title())

            validation_table.add_row(item_name, ce_display, nb_display, variance_display, status_display)

        console.print(validation_table)

    def _display_cost_data_summary(self, cost_data: Dict) -> None:
        """Display cost data summary."""

        billing_data = cost_data.get("billing_data", {})

        cost_panel = create_panel(
            f"""💰 Cost Analysis Summary

Total Cost (Analysis Period): ${billing_data.get("total_cost", 0):,.2f}
Average Monthly Cost: ${billing_data.get("average_monthly_cost", 0):,.2f}
Analysis Period: {billing_data.get("analysis_period", {}).get("days", 0)} days

Data Source: AWS Cost Explorer API (Real-time)
Account Filter: {cost_data.get("account_filter", "All accounts")}
Retrieval Status: {"✅ Successful" if not cost_data.get("errors") else "⚠️ With errors"}""",
            title="Cost Data Summary",
            border_style="bright_green",
        )

        console.print(cost_panel)


# Export main class for integration
__all__ = ["MCPCostExplorerIntegration"]
