"""
Command-line interface for k8s-helper
"""

import typer
from typing import Optional, List
from rich.console import Console
from rich.table import Table
from rich.panel import Panel
from rich.text import Text
import time
import time
from kubernetes.client.rest import ApiException

from .core import K8sClient
from .config import get_config
from .utils import (
    format_pod_list,
    format_deployment_list,
    format_service_list,
    format_events,
    format_yaml_output,
    format_json_output,
    validate_name,
    validate_image,
    parse_env_vars,
    parse_labels,
    format_age
)
from . import __version__

def version_callback(value: bool):
    """Version callback for the CLI"""
    if value:
        typer.echo(f"k8s-helper-cli version {__version__}")
        raise typer.Exit()

app = typer.Typer(help="k8s-helper: Simplified Kubernetes operations")
console = Console()

@app.callback()
def main(
    version: Optional[bool] = typer.Option(None, "--version", callback=version_callback, is_eager=True, help="Show version and exit")
):
    """Main callback to handle global options"""
    return

# Global options
namespace_option = typer.Option(None, "--namespace", "-n", help="Kubernetes namespace")
output_option = typer.Option("table", "--output", "-o", help="Output format: table, yaml, json")


@app.command()
def config(
    namespace: Optional[str] = typer.Option(None, help="Set default namespace"),
    output_format: Optional[str] = typer.Option(None, help="Set output format"),
    timeout: Optional[int] = typer.Option(None, help="Set default timeout"),
    verbose: Optional[bool] = typer.Option(None, help="Enable verbose output"),
    show: bool = typer.Option(False, "--show", help="Show current configuration")
):
    """Configure k8s-helper settings"""
    config_obj = get_config()
    
    if show:
        console.print(Panel(format_yaml_output(config_obj.to_dict()), title="Current Configuration"))
        return
    
    if namespace:
        config_obj.set_namespace(namespace)
        console.print(f"✅ Default namespace set to: {namespace}")
    
    if output_format:
        try:
            config_obj.set_output_format(output_format)
            console.print(f"✅ Output format set to: {output_format}")
        except ValueError as e:
            console.print(f"❌ {e}")
            return
    
    if timeout:
        config_obj.set_timeout(timeout)
        console.print(f"✅ Timeout set to: {timeout} seconds")
    
    if verbose is not None:
        config_obj.set_verbose(verbose)
        console.print(f"✅ Verbose mode: {'enabled' if verbose else 'disabled'}")
    
    if any([namespace, output_format, timeout, verbose is not None]):
        config_obj.save_config()
        console.print("✅ Configuration saved")


@app.command()
def create_deployment(
    name: str = typer.Argument(..., help="Deployment name"),
    image: str = typer.Argument(..., help="Container image"),
    replicas: int = typer.Option(1, "--replicas", "-r", help="Number of replicas"),
    port: int = typer.Option(80, "--port", "-p", help="Container port"),
    env: Optional[str] = typer.Option(None, "--env", "-e", help="Environment variables (KEY1=value1,KEY2=value2)"),
    labels: Optional[str] = typer.Option(None, "--labels", "-l", help="Labels (key1=value1,key2=value2)"),
    namespace: Optional[str] = namespace_option,
    wait: bool = typer.Option(False, "--wait", help="Wait for deployment to be ready")
):
    """Create a new deployment"""
    if not validate_name(name):
        console.print(f"❌ Invalid deployment name: {name}")
        return
    
    if not validate_image(image):
        console.print(f"❌ Invalid image name: {image}")
        return
    
    # Parse environment variables and labels
    env_vars = parse_env_vars(env) if env else None
    label_dict = parse_labels(labels) if labels else None
    
    # Get client
    ns = namespace or get_config().get_namespace()
    client = K8sClient(namespace=ns)
    
    # Create deployment
    with console.status(f"Creating deployment {name}..."):
        result = client.create_deployment(
            name=name,
            image=image,
            replicas=replicas,
            container_port=port,
            env_vars=env_vars,
            labels=label_dict
        )
    
    if result:
        console.print(f"✅ Deployment {name} created successfully")
        
        if wait:
            with console.status(f"Waiting for deployment {name} to be ready..."):
                if client.wait_for_deployment_ready(name):
                    console.print(f"✅ Deployment {name} is ready")
                else:
                    console.print(f"❌ Deployment {name} failed to become ready")
    else:
        console.print(f"❌ Failed to create deployment {name}")


@app.command()
def delete_deployment(
    name: str = typer.Argument(..., help="Deployment name"),
    namespace: Optional[str] = namespace_option
):
    """Delete a deployment"""
    ns = namespace or get_config().get_namespace()
    client = K8sClient(namespace=ns)
    
    if typer.confirm(f"Are you sure you want to delete deployment {name}?"):
        with console.status(f"Deleting deployment {name}..."):
            if client.delete_deployment(name):
                console.print(f"✅ Deployment {name} deleted successfully")
            else:
                console.print(f"❌ Failed to delete deployment {name}")


@app.command()
def scale_deployment(
    name: str = typer.Argument(..., help="Deployment name"),
    replicas: int = typer.Argument(..., help="Number of replicas"),
    namespace: Optional[str] = namespace_option
):
    """Scale a deployment"""
    ns = namespace or get_config().get_namespace()
    client = K8sClient(namespace=ns)
    
    with console.status(f"Scaling deployment {name} to {replicas} replicas..."):
        if client.scale_deployment(name, replicas):
            console.print(f"✅ Deployment {name} scaled to {replicas} replicas")
        else:
            console.print(f"❌ Failed to scale deployment {name}")


@app.command()
def rolling_update(
    name: str = typer.Argument(..., help="Deployment name"),
    image: Optional[str] = typer.Option(None, "--image", "-i", help="New container image"),
    replicas: Optional[int] = typer.Option(None, "--replicas", "-r", help="New number of replicas"),
    port: Optional[int] = typer.Option(None, "--port", "-p", help="New container port"),
    env: Optional[str] = typer.Option(None, "--env", "-e", help="New environment variables (KEY1=value1,KEY2=value2)"),
    labels: Optional[str] = typer.Option(None, "--labels", "-l", help="New labels (key1=value1,key2=value2)"),
    namespace: Optional[str] = namespace_option,
    wait: bool = typer.Option(True, "--wait/--no-wait", help="Wait for rollout to complete"),
    timeout: int = typer.Option(300, "--timeout", "-t", help="Timeout in seconds for rollout completion"),
    show_status: bool = typer.Option(False, "--show-status", "-s", help="Show rollout status during update")
):
    """Perform a rolling update on a deployment"""
    if not validate_name(name):
        console.print(f"❌ Invalid deployment name: {name}")
        return
    
    # Validate that at least one parameter is provided
    if not any([image, replicas is not None, port, env, labels]):
        console.print("❌ At least one update parameter must be provided")
        console.print("   Use --image, --replicas, --port, --env, or --labels")
        return
    
    # Validate image if provided
    if image and not validate_image(image):
        console.print(f"❌ Invalid image name: {image}")
        return
    
    # Parse environment variables and labels
    env_vars = parse_env_vars(env) if env else None
    label_dict = parse_labels(labels) if labels else None
    
    ns = namespace or get_config().get_namespace()
    client = K8sClient(namespace=ns)
    
    # Show what will be updated
    console.print(f"🔄 Performing rolling update on deployment: {name}")
    console.print(f"📍 Namespace: {ns}")
    
    updates = []
    if image:
        updates.append(f"🖼️  Image: {image}")
    if replicas is not None:
        updates.append(f"📊 Replicas: {replicas}")
    if port:
        updates.append(f"🚪 Port: {port}")
    if env_vars:
        updates.append(f"🔧 Environment variables: {list(env_vars.keys())}")
    if label_dict:
        updates.append(f"🏷️  Labels: {list(label_dict.keys())}")
    
    console.print("📋 Updates to apply:")
    for update in updates:
        console.print(f"   • {update}")
    
    # Perform the rolling update
    with console.status(f"Applying rolling update to {name}..."):
        success = client.rolling_update_deployment(
            name=name,
            image=image,
            replicas=replicas,
            env_vars=env_vars,
            labels=label_dict,
            container_port=port
        )
    
    if not success:
        console.print(f"❌ Failed to start rolling update for deployment {name}")
        return
    
    console.print(f"✅ Rolling update initiated for deployment {name}")
    
    if wait:
        console.print(f"⏳ Waiting for rollout to complete (timeout: {timeout}s)...")
        
        if show_status:
            # Show rolling status updates
            start_time = time.time()
            while time.time() - start_time < timeout:
                status = client.get_rollout_status(name)
                if status:
                    console.print(f"📊 Status: {status['ready_replicas']}/{status['desired_replicas']} ready, "
                                f"{status['updated_replicas']} updated, {status['available_replicas']} available")
                    
                    # Check if rollout is complete
                    if (status['ready_replicas'] == status['desired_replicas'] and
                        status['updated_replicas'] == status['desired_replicas'] and
                        status['available_replicas'] == status['desired_replicas']):
                        break
                
                time.sleep(3)
        
        # Wait for rollout completion
        if client.wait_for_rollout_complete(name, timeout):
            console.print("✅ Rolling update completed successfully!")
            
            # Show final status
            final_status = client.get_rollout_status(name)
            if final_status:
                console.print(f"📊 Final status: {final_status['ready_replicas']}/{final_status['desired_replicas']} replicas ready")
            
            console.print(f"🎉 Deployment {name} is now running with the updated configuration")
        else:
            console.print(f"❌ Rolling update timed out after {timeout} seconds")
            console.print("💡 Check rollout status with: kubectl rollout status deployment/{name}")
    else:
        console.print(f"💡 Monitor rollout progress with: kubectl rollout status deployment/{name}")
        console.print(f"💡 Or use: k8s-helper rolling-update {name} --show-status --wait")


@app.command()
def list_deployments(
    namespace: Optional[str] = namespace_option,
    output: str = output_option
):
    """List deployments"""
    ns = namespace or get_config().get_namespace()
    client = K8sClient(namespace=ns)
    
    deployments = client.list_deployments()
    
    if output == "table":
        console.print(format_deployment_list(deployments))
    elif output == "yaml":
        console.print(format_yaml_output(deployments))
    elif output == "json":
        console.print(format_json_output(deployments))


@app.command()
def create_pod(
    name: str = typer.Argument(..., help="Pod name"),
    image: str = typer.Argument(..., help="Container image"),
    port: int = typer.Option(80, "--port", "-p", help="Container port"),
    env: Optional[str] = typer.Option(None, "--env", "-e", help="Environment variables"),
    labels: Optional[str] = typer.Option(None, "--labels", "-l", help="Labels"),
    namespace: Optional[str] = namespace_option
):
    """Create a new pod"""
    if not validate_name(name):
        console.print(f"❌ Invalid pod name: {name}")
        return
    
    if not validate_image(image):
        console.print(f"❌ Invalid image name: {image}")
        return
    
    env_vars = parse_env_vars(env) if env else None
    label_dict = parse_labels(labels) if labels else None
    
    ns = namespace or get_config().get_namespace()
    client = K8sClient(namespace=ns)
    
    with console.status(f"Creating pod {name}..."):
        result = client.create_pod(
            name=name,
            image=image,
            container_port=port,
            env_vars=env_vars,
            labels=label_dict
        )
    
    if result:
        console.print(f"✅ Pod {name} created successfully")
    else:
        console.print(f"❌ Failed to create pod {name}")


@app.command()
def delete_pod(
    name: str = typer.Argument(..., help="Pod name"),
    namespace: Optional[str] = namespace_option
):
    """Delete a pod"""
    ns = namespace or get_config().get_namespace()
    client = K8sClient(namespace=ns)
    
    if typer.confirm(f"Are you sure you want to delete pod {name}?"):
        with console.status(f"Deleting pod {name}..."):
            if client.delete_pod(name):
                console.print(f"✅ Pod {name} deleted successfully")
            else:
                console.print(f"❌ Failed to delete pod {name}")


@app.command()
def list_pods(
    namespace: Optional[str] = namespace_option,
    output: str = output_option
):
    """List pods"""
    ns = namespace or get_config().get_namespace()
    client = K8sClient(namespace=ns)
    
    pods = client.list_pods()
    
    if output == "table":
        console.print(format_pod_list(pods))
    elif output == "yaml":
        console.print(format_yaml_output(pods))
    elif output == "json":
        console.print(format_json_output(pods))


@app.command()
def create_service(
    name: str = typer.Argument(..., help="Service name"),
    port: int = typer.Argument(..., help="Service port"),
    target_port: Optional[int] = typer.Option(None, help="Target port (defaults to port)"),
    service_type: str = typer.Option("ClusterIP", help="Service type"),
    selector: Optional[str] = typer.Option(None, help="Selector labels"),
    namespace: Optional[str] = namespace_option
):
    """Create a new service"""
    if not validate_name(name):
        console.print(f"❌ Invalid service name: {name}")
        return
    
    if target_port is None:
        target_port = port
    
    selector_dict = parse_labels(selector) if selector else None
    
    ns = namespace or get_config().get_namespace()
    client = K8sClient(namespace=ns)
    
    with console.status(f"Creating service {name}..."):
        result = client.create_service(
            name=name,
            port=port,
            target_port=target_port,
            service_type=service_type,
            selector=selector_dict
        )
    
    if result:
        console.print(f"✅ Service {name} created successfully")
    else:
        console.print(f"❌ Failed to create service {name}")


@app.command()
def delete_service(
    name: str = typer.Argument(..., help="Service name"),
    namespace: Optional[str] = namespace_option
):
    """Delete a service"""
    ns = namespace or get_config().get_namespace()
    client = K8sClient(namespace=ns)
    
    if typer.confirm(f"Are you sure you want to delete service {name}?"):
        with console.status(f"Deleting service {name}..."):
            if client.delete_service(name):
                console.print(f"✅ Service {name} deleted successfully")
            else:
                console.print(f"❌ Failed to delete service {name}")


@app.command()
def list_services(
    namespace: Optional[str] = namespace_option,
    output: str = output_option
):
    """List services"""
    ns = namespace or get_config().get_namespace()
    client = K8sClient(namespace=ns)
    
    services = client.list_services()
    
    if output == "table":
        console.print(format_service_list(services))
    elif output == "yaml":
        console.print(format_yaml_output(services))
    elif output == "json":
        console.print(format_json_output(services))


@app.command()
def logs(
    pod_name: str = typer.Argument(..., help="Pod name"),
    container: Optional[str] = typer.Option(None, help="Container name"),
    tail: Optional[int] = typer.Option(None, help="Number of lines to tail"),
    namespace: Optional[str] = namespace_option
):
    """Get pod logs"""
    ns = namespace or get_config().get_namespace()
    client = K8sClient(namespace=ns)
    
    logs = client.get_logs(pod_name, container_name=container, tail_lines=tail)
    if logs:
        console.print(logs)
    else:
        console.print(f"❌ Failed to get logs for pod {pod_name}")


@app.command()
def events(
    resource: Optional[str] = typer.Option(None, help="Resource name to filter events"),
    namespace: Optional[str] = namespace_option,
    output: str = output_option
):
    """Get events"""
    ns = namespace or get_config().get_namespace()
    client = K8sClient(namespace=ns)
    
    events = client.get_events(resource_name=resource)
    
    if output == "table":
        console.print(format_events(events))
    elif output == "yaml":
        console.print(format_yaml_output(events))
    elif output == "json":
        console.print(format_json_output(events))


@app.command()
def describe(
    resource_type: str = typer.Argument(..., help="Resource type: pod, deployment, service"),
    name: str = typer.Argument(..., help="Resource name"),
    namespace: Optional[str] = namespace_option,
    output: str = output_option
):
    """Describe a resource"""
    ns = namespace or get_config().get_namespace()
    client = K8sClient(namespace=ns)
    
    if resource_type.lower() == "pod":
        info = client.describe_pod(name)
    elif resource_type.lower() == "deployment":
        info = client.describe_deployment(name)
    elif resource_type.lower() == "service":
        info = client.describe_service(name)
    else:
        console.print(f"❌ Unsupported resource type: {resource_type}")
        return
    
    if info:
        if output == "yaml":
            console.print(format_yaml_output(info))
        elif output == "json":
            console.print(format_json_output(info))
        else:
            console.print(format_yaml_output(info))  # Default to YAML for describe
    else:
        console.print(f"❌ Failed to describe {resource_type} {name}")


@app.command()
def status(
    namespace: Optional[str] = namespace_option
):
    """Show namespace status"""
    ns = namespace or get_config().get_namespace()
    client = K8sClient(namespace=ns)
    
    console.print(f"\n[bold]Namespace: {ns}[/bold]")
    
    # Get resource counts
    resources = client.get_namespace_resources()
    
    table = Table(title="Resource Summary")
    table.add_column("Resource", style="cyan")
    table.add_column("Count", style="magenta")
    
    for resource, count in resources.items():
        table.add_row(resource.capitalize(), str(count))
    
    console.print(table)
    
    # Show recent events
    events = client.get_events()
    if events:
        console.print(f"\n[bold]Recent Events (last 5):[/bold]")
        recent_events = events[:5]
        for event in recent_events:
            event_type = event['type']
            color = "green" if event_type == "Normal" else "red"
            console.print(f"[{color}]{event['type']}[/{color}] {event['reason']}: {event['message']}")


@app.command()
def apply(
    name: str = typer.Argument(..., help="Application name"),
    image: str = typer.Argument(..., help="Container image"),
    replicas: int = typer.Option(1, "--replicas", "-r", help="Number of replicas"),
    port: int = typer.Option(80, "--port", "-p", help="Container port"),
    service_type: str = typer.Option("ClusterIP", help="Service type"),
    env: Optional[str] = typer.Option(None, "--env", "-e", help="Environment variables"),
    labels: Optional[str] = typer.Option(None, "--labels", "-l", help="Labels"),
    init_container: Optional[str] = typer.Option(None, "--init-container", help="Init container (name:image:command)"),
    init_env: Optional[str] = typer.Option(None, "--init-env", help="Init container environment variables"),
    pvc: Optional[str] = typer.Option(None, "--pvc", help="PVC to mount (name:mount_path)"),
    secret: Optional[str] = typer.Option(None, "--secret", help="Secret to mount (name:mount_path)"),
    namespace: Optional[str] = namespace_option,
    wait: bool = typer.Option(True, "--wait/--no-wait", help="Wait for deployment to be ready"),
    show_url: bool = typer.Option(True, "--show-url/--no-show-url", help="Show service URL after deployment")
):
    """Deploy an application (deployment + service) with advanced features"""
    if not validate_name(name):
        console.print(f"❌ Invalid application name: {name}")
        return
    
    if not validate_image(image):
        console.print(f"❌ Invalid image name: {image}")
        return
    
    env_vars = parse_env_vars(env) if env else None
    label_dict = parse_labels(labels) if labels else None
    
    ns = namespace or get_config().get_namespace()
    client = K8sClient(namespace=ns)
    
    console.print(f"🚀 Deploying application: {name}")
    
    # Prepare init containers
    init_containers = []
    if init_container:
        try:
            parts = init_container.split(":")
            if len(parts) >= 2:
                init_name, init_image = parts[0], parts[1]
                init_command = parts[2].split(" ") if len(parts) > 2 else None
                
                init_env_vars = parse_env_vars(init_env) if init_env else None
                
                init_containers.append({
                    'name': init_name,
                    'image': init_image,
                    'command': init_command,
                    'env_vars': init_env_vars
                })
                
                console.print(f"🔧 Init container: {init_name} ({init_image})")
            else:
                console.print(f"❌ Invalid init container format: {init_container}")
                return
        except Exception as e:
            console.print(f"❌ Error parsing init container: {e}")
            return
    
    # Prepare volumes and volume mounts
    volumes = []
    volume_mounts = []
    
    if pvc:
        try:
            pvc_parts = pvc.split(":")
            if len(pvc_parts) == 2:
                pvc_name, mount_path = pvc_parts
                volumes.append({
                    'name': f"{pvc_name}-volume",
                    'type': 'pvc',
                    'claim_name': pvc_name
                })
                volume_mounts.append({
                    'name': f"{pvc_name}-volume",
                    'mount_path': mount_path
                })
                console.print(f"💾 PVC mount: {pvc_name} → {mount_path}")
            else:
                console.print(f"❌ Invalid PVC format: {pvc}")
                return
        except Exception as e:
            console.print(f"❌ Error parsing PVC: {e}")
            return
    
    if secret:
        try:
            secret_parts = secret.split(":")
            if len(secret_parts) == 2:
                secret_name, mount_path = secret_parts
                volumes.append({
                    'name': f"{secret_name}-volume",
                    'type': 'secret',
                    'secret_name': secret_name
                })
                volume_mounts.append({
                    'name': f"{secret_name}-volume",
                    'mount_path': mount_path
                })
                console.print(f"🔐 Secret mount: {secret_name} → {mount_path}")
            else:
                console.print(f"❌ Invalid secret format: {secret}")
                return
        except Exception as e:
            console.print(f"❌ Error parsing secret: {e}")
            return
    
    # Create deployment
    with console.status(f"Creating deployment {name}..."):
        deployment_result = client.create_deployment(
            name=name,
            image=image,
            replicas=replicas,
            container_port=port,
            env_vars=env_vars,
            labels=label_dict,
            init_containers=init_containers if init_containers else None,
            volume_mounts=volume_mounts if volume_mounts else None,
            volumes=volumes if volumes else None
        )
    
    if not deployment_result:
        console.print(f"❌ Failed to create deployment {name}")
        return
    
    # Create service
    with console.status(f"Creating service {name}-service..."):
        service_result = client.create_service(
            name=f"{name}-service",
            port=port,
            target_port=port,
            service_type=service_type,
            selector=label_dict or {"app": name}
        )
    
    if not service_result:
        console.print(f"❌ Failed to create service {name}-service")
        return
    
    console.print(f"✅ Application {name} deployed successfully")
    
    if wait:
        with console.status(f"Waiting for deployment {name} to be ready..."):
            if client.wait_for_deployment_ready(name):
                console.print(f"✅ Application {name} is ready")
            else:
                console.print(f"❌ Application {name} failed to become ready")
    
    # Show service URL if requested
    if show_url:
        console.print(f"\n🔗 Service URL Information:")
        
        # Wait a moment for service to be ready
        time.sleep(2)
        
        url_info = client.get_service_url(f"{name}-service", ns)
        if url_info:
            console.print(f"🔧 Service Type: {url_info['type']}")
            console.print(f"🖥️  Cluster IP: {url_info['cluster_ip']}")
            
            if url_info['type'] == 'LoadBalancer':
                if url_info.get('aws_elb'):
                    console.print(f"🌐 AWS ELB DNS: [green]{url_info['elb_dns_name']}[/green]")
                    console.print(f"🔗 External URL: [blue]{url_info['external_url']}[/blue]")
                elif url_info.get('external_url'):
                    console.print(f"🔗 External URL: [blue]{url_info['external_url']}[/blue]")
                else:
                    console.print(f"⏳ LoadBalancer provisioning... Use 'k8s-helper service-url {name}-service' to check status")
            
            elif url_info['type'] == 'NodePort':
                if url_info.get('external_url'):
                    console.print(f"🔗 NodePort URL: [blue]{url_info['external_url']}[/blue]")
            
            elif url_info['type'] == 'ClusterIP':
                console.print(f"💡 ClusterIP service - accessible within cluster at {url_info['cluster_ip']}:{port}")
        else:
            console.print("❌ Could not retrieve service URL information")


@app.command()
def cleanup(
    name: str = typer.Argument(..., help="Application name"),
    namespace: Optional[str] = namespace_option
):
    """Clean up an application (delete deployment + service)"""
    ns = namespace or get_config().get_namespace()
    client = K8sClient(namespace=ns)
    
    if typer.confirm(f"Are you sure you want to delete application {name} and its service?"):
        console.print(f"🧹 Cleaning up application: {name}")
        
        # Delete deployment
        with console.status(f"Deleting deployment {name}..."):
            deployment_deleted = client.delete_deployment(name)
        
        # Delete service
        with console.status(f"Deleting service {name}-service..."):
            service_deleted = client.delete_service(f"{name}-service")
        
        if deployment_deleted and service_deleted:
            console.print(f"✅ Application {name} cleaned up successfully")
        else:
            console.print(f"⚠️  Partial cleanup completed for application {name}")


# ======================
# EKS COMMANDS
# ======================
@app.command()
def create_eks_cluster(
    name: str = typer.Argument(..., help="Cluster name"),
    region: str = typer.Option("us-west-2", "--region", "-r", help="AWS region"),
    version: str = typer.Option("1.29", "--version", "-v", help="Kubernetes version"),
    node_group: str = typer.Option(None, "--node-group", help="Node group name"),
    instance_types: str = typer.Option("t3.medium", "--instance-types", help="EC2 instance types (comma-separated)"),
    min_size: int = typer.Option(1, "--min-size", help="Minimum number of nodes"),
    max_size: int = typer.Option(3, "--max-size", help="Maximum number of nodes"),
    desired_size: int = typer.Option(2, "--desired-size", help="Desired number of nodes"),
    wait: bool = typer.Option(True, "--wait/--no-wait", help="Wait for cluster to be ready"),
    create_nodegroup: bool = typer.Option(True, "--create-nodegroup/--no-nodegroup", help="Create node group automatically"),
    ami_type: str = typer.Option("AL2_x86_64", "--ami-type", help="AMI type for nodes"),
    capacity_type: str = typer.Option("ON_DEMAND", "--capacity-type", help="Capacity type: ON_DEMAND or SPOT")
):
    """Create an AWS EKS cluster"""
    if not validate_name(name):
        console.print(f"❌ Invalid cluster name: {name}")
        return
    
    try:
        from .core import EKSClient
        
        eks_client = EKSClient(region=region)
        
        # Parse instance types
        instance_type_list = [t.strip() for t in instance_types.split(",")]
        
        scaling_config = {
            "minSize": min_size,
            "maxSize": max_size,
            "desiredSize": desired_size
        }
        
        console.print(f"🚀 Creating EKS cluster: {name}")
        console.print(f"📍 Region: {region}")
        console.print(f"🎯 Version: {version}")
        console.print(f"💻 Instance types: {instance_type_list}")
        console.print(f"📊 Scaling: {min_size}-{max_size} nodes (desired: {desired_size})")
        console.print(f"🛠️  AMI Type: {ami_type}")
        console.print(f"⚡ Capacity Type: {capacity_type}")
        
        # Show what will be created
        console.print("\n🔧 EKS Resources to create:")
        console.print("   • IAM roles for cluster and node groups")
        console.print("   • VPC subnets in at least 2 availability zones")
        console.print("   • Security groups for cluster communication")
        console.print("   • EKS cluster control plane")
        if create_nodegroup:
            if node_group:
                console.print(f"   • Managed node group: {node_group}")
            else:
                console.print(f"   • Managed node group: {name}-nodegroup")
        else:
            console.print("   • ⚠️  No node group (cluster will have no worker nodes)")
        
        with console.status("Creating EKS cluster and required resources..."):
            cluster_info = eks_client.create_cluster(
                cluster_name=name,
                version=version,
                node_group_name=node_group,
                instance_types=instance_type_list,
                scaling_config=scaling_config,
                ami_type=ami_type,
                capacity_type=capacity_type,
                create_nodegroup=create_nodegroup,
                wait_for_cluster=wait
            )
        
        console.print(f"✅ EKS cluster creation initiated")
        console.print(f"📋 Cluster ARN: {cluster_info['cluster_arn']}")
        console.print(f"🕐 Created at: {cluster_info['created_at']}")
        
        if 'subnets' in cluster_info:
            console.print(f"🌐 Subnets: {cluster_info['subnets']}")
        
        # Show node group information
        if create_nodegroup:
            if 'nodegroup_info' in cluster_info:
                nodegroup_info = cluster_info['nodegroup_info']
                console.print(f"\n✅ Node group creation initiated")
                console.print(f"📋 Node group name: {nodegroup_info['nodegroup_name']}")
                console.print(f"📋 Node group ARN: {nodegroup_info['nodegroup_arn']}")
                console.print(f"💻 Instance types: {nodegroup_info['instance_types']}")
                console.print(f"📊 Scaling config: {nodegroup_info['scaling_config']}")
            elif 'nodegroup_error' in cluster_info:
                console.print(f"\n⚠️  Node group creation failed: {cluster_info['nodegroup_error']}")
                console.print(f"💡 You can create it manually later using: k8s-helper create-nodegroup {name}")
            elif 'node_group_name' in cluster_info:
                console.print(f"\n📋 Node group will be created: {cluster_info['node_group_name']}")
                console.print(f"💡 Create it after cluster is active: k8s-helper create-nodegroup {name}")
        
        if wait:
            console.print("⏳ Waiting for cluster to become active...")
            if eks_client.wait_for_cluster_active(name):
                console.print("✅ EKS cluster is now active!")
                
                # Show cluster status
                status = eks_client.get_cluster_status(name)
                console.print(f"🔗 Endpoint: {status['endpoint']}")
                
                # If node group was created, wait for it too
                if create_nodegroup and 'nodegroup_info' in cluster_info:
                    nodegroup_name = cluster_info['nodegroup_info']['nodegroup_name']
                    console.print(f"⏳ Waiting for node group {nodegroup_name} to become active...")
                    if eks_client.wait_for_nodegroup_active(name, nodegroup_name):
                        console.print("✅ Node group is now active!")
                        console.print("🎉 Cluster is ready with worker nodes!")
                    else:
                        console.print("❌ Timeout waiting for node group to become active")
                elif create_nodegroup and not wait:
                    # Create node group now that cluster is active
                    nodegroup_name = cluster_info.get('node_group_name') or f"{name}-nodegroup"
                    console.print(f"🔧 Creating node group: {nodegroup_name}")
                    try:
                        nodegroup_info = eks_client.create_nodegroup(
                            cluster_name=name,
                            nodegroup_name=nodegroup_name,
                            instance_types=instance_type_list,
                            ami_type=ami_type,
                            capacity_type=capacity_type,
                            scaling_config=scaling_config
                            # No SSH key for automatic creation
                        )
                        console.print(f"✅ Node group creation initiated: {nodegroup_name}")
                        console.print(f"📋 Node group ARN: {nodegroup_info['nodegroup_arn']}")
                        
                        console.print(f"⏳ Waiting for node group to become active...")
                        if eks_client.wait_for_nodegroup_active(name, nodegroup_name):
                            console.print("✅ Node group is now active!")
                            console.print("🎉 Cluster is ready with worker nodes!")
                        else:
                            console.print("❌ Timeout waiting for node group to become active")
                    except Exception as e:
                        console.print(f"❌ Failed to create node group: {e}")
                
                # Show next steps
                console.print(f"\n🚀 Next steps:")
                console.print(f"   1. Configure kubectl: aws eks update-kubeconfig --name {name} --region {region}")
                if create_nodegroup:
                    console.print(f"   2. Verify nodes: kubectl get nodes")
                    console.print(f"   3. Verify connection: kubectl get svc")
                    console.print(f"   4. Deploy applications: k8s-helper apply <app-name> <image>")
                else:
                    console.print(f"   2. Create node group: k8s-helper create-nodegroup {name}")
                    console.print(f"   3. Verify connection: kubectl get svc")
                    console.print(f"   4. Deploy applications: k8s-helper apply <app-name> <image>")
            else:
                console.print("❌ Timeout waiting for cluster to become active")
        else:
            console.print(f"💡 Use 'aws eks update-kubeconfig --name {name} --region {region}' to configure kubectl")
    
    except Exception as e:
        error_message = str(e)
        console.print(f"❌ Failed to create EKS cluster: {error_message}")
        
        # Provide helpful guidance based on error type
        if "Need at least 2 subnets" in error_message:
            console.print("\n🛠️  Troubleshooting:")
            console.print("   • EKS requires subnets in at least 2 availability zones")
            console.print("   • Check your VPC configuration in the AWS Console")
            console.print("   • Ensure you have subnets in different AZs")
            console.print("   • The tool will attempt to create subnets if none exist")
        elif "credentials not found" in error_message:
            console.print("\n🛠️  Troubleshooting:")
            console.print("   • Configure AWS credentials: aws configure")
            console.print("   • Or set environment variables: AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY")
            console.print("   • Ensure you have EKS permissions")
        elif "VPC" in error_message:
            console.print("\n🛠️  Troubleshooting:")
            console.print("   • Check your VPC configuration")
            console.print("   • Ensure you have a default VPC or create one")
            console.print("   • Verify subnet CIDR ranges don't overlap")

@app.command()
def create_nodegroup(
    cluster_name: str = typer.Argument(..., help="EKS cluster name"),
    nodegroup_name: str = typer.Argument(..., help="Node group name"),
    region: str = typer.Option("us-west-2", "--region", "-r", help="AWS region"),
    instance_types: str = typer.Option("t3.medium", "--instance-types", help="EC2 instance types (comma-separated)"),
    min_size: int = typer.Option(1, "--min-size", help="Minimum number of nodes"),
    max_size: int = typer.Option(3, "--max-size", help="Maximum number of nodes"),
    desired_size: int = typer.Option(2, "--desired-size", help="Desired number of nodes"),
    ami_type: str = typer.Option("AL2_x86_64", "--ami-type", help="AMI type for nodes"),
    capacity_type: str = typer.Option("ON_DEMAND", "--capacity-type", help="Capacity type: ON_DEMAND or SPOT"),
    ssh_key: Optional[str] = typer.Option(None, "--ssh-key", help="EC2 SSH key name for remote access"),
    wait: bool = typer.Option(True, "--wait/--no-wait", help="Wait for node group to be ready")
):
    """Create an EKS managed node group"""
    if not validate_name(cluster_name):
        console.print(f"❌ Invalid cluster name: {cluster_name}")
        return
    
    if not validate_name(nodegroup_name):
        console.print(f"❌ Invalid node group name: {nodegroup_name}")
        return
    
    try:
        from .core import EKSClient
        
        eks_client = EKSClient(region=region)
        
        # Parse instance types
        instance_type_list = [t.strip() for t in instance_types.split(",")]
        
        scaling_config = {
            "minSize": min_size,
            "maxSize": max_size,
            "desiredSize": desired_size
        }
        
        console.print(f"🚀 Creating node group: {nodegroup_name}")
        console.print(f"📋 For cluster: {cluster_name}")
        console.print(f"📍 Region: {region}")
        console.print(f"💻 Instance types: {instance_type_list}")
        console.print(f"📊 Scaling: {min_size}-{max_size} nodes (desired: {desired_size})")
        console.print(f"🛠️  AMI Type: {ami_type}")
        console.print(f"⚡ Capacity Type: {capacity_type}")
        
        # Check if cluster exists and is active
        try:
            cluster_status = eks_client.get_cluster_status(cluster_name)
            if cluster_status['status'] != 'ACTIVE':
                console.print(f"❌ Cluster {cluster_name} is not active (status: {cluster_status['status']})")
                console.print("💡 Wait for cluster to become active before creating node group")
                return
        except Exception as e:
            console.print(f"❌ Cluster {cluster_name} not found: {e}")
            return
        
        with console.status("Creating node group..."):
            nodegroup_info = eks_client.create_nodegroup(
                cluster_name=cluster_name,
                nodegroup_name=nodegroup_name,
                instance_types=instance_type_list,
                ami_type=ami_type,
                capacity_type=capacity_type,
                scaling_config=scaling_config,
                ssh_key=ssh_key
            )
        
        console.print(f"✅ Node group creation initiated")
        console.print(f"📋 Node group ARN: {nodegroup_info['nodegroup_arn']}")
        console.print(f"🕐 Created at: {nodegroup_info['created_at']}")
        console.print(f"💻 Instance types: {nodegroup_info['instance_types']}")
        console.print(f"📊 Scaling config: {nodegroup_info['scaling_config']}")
        
        if wait:
            console.print("⏳ Waiting for node group to become active...")
            if eks_client.wait_for_nodegroup_active(cluster_name, nodegroup_name):
                console.print("✅ Node group is now active!")
                console.print("🎉 You can now deploy workloads!")
                
                # Show next steps
                console.print(f"\n🚀 Next steps:")
                console.print(f"   1. Verify nodes: kubectl get nodes")
                console.print(f"   2. Deploy applications: k8s-helper apply <app-name> <image>")
            else:
                console.print("❌ Timeout waiting for node group to become active")
        else:
            console.print(f"💡 Use 'kubectl get nodes' to check when nodes are ready")
    
    except Exception as e:
        error_message = str(e)
        console.print(f"❌ Failed to create node group: {error_message}")
        
        # Provide helpful guidance based on error type
        if "Node group role" in error_message:
            console.print("\n🛠️  Troubleshooting:")
            console.print("   • The tool will automatically create required IAM roles")
            console.print("   • Ensure you have IAM permissions to create roles")
            console.print("   • Check AWS IAM console for existing roles")
        elif "subnets" in error_message:
            console.print("\n🛠️  Troubleshooting:")
            console.print("   • Node groups require valid subnets")
            console.print("   • Ensure cluster subnets are properly configured")
            console.print("   • Check VPC and subnet configuration")

@app.command()
def list_nodegroups(
    cluster_name: str = typer.Argument(..., help="EKS cluster name"),
    region: str = typer.Option("us-west-2", "--region", "-r", help="AWS region"),
    output: str = output_option
):
    """List node groups for an EKS cluster"""
    if not validate_name(cluster_name):
        console.print(f"❌ Invalid cluster name: {cluster_name}")
        return
    
    try:
        from .core import EKSClient
        
        eks_client = EKSClient(region=region)
        
        with console.status("Fetching node groups..."):
            nodegroups = eks_client.list_nodegroups(cluster_name)
        
        if not nodegroups:
            console.print(f"📋 No node groups found for cluster: {cluster_name}")
            console.print(f"💡 Create one with: k8s-helper create-nodegroup {cluster_name} <nodegroup-name>")
            return
        
        if output == "table":
            table = Table(title=f"Node Groups for {cluster_name}")
            table.add_column("Name", style="cyan")
            table.add_column("Status", style="green")
            table.add_column("Instance Types", style="blue")
            table.add_column("Capacity Type", style="yellow")
            table.add_column("Scaling Config", style="magenta")
            table.add_column("Created", style="white")
            
            for ng in nodegroups:
                scaling = f"{ng['scaling_config']['minSize']}-{ng['scaling_config']['maxSize']} (desired: {ng['scaling_config']['desiredSize']})"
                table.add_row(
                    ng['name'],
                    ng['status'],
                    ', '.join(ng['instance_types']),
                    ng['capacity_type'],
                    scaling,
                    format_age(ng['created_at'])
                )
            
            console.print(table)
        elif output == "json":
            console.print(format_json_output(nodegroups))
        elif output == "yaml":
            console.print(format_yaml_output(nodegroups))
    
    except Exception as e:
        console.print(f"❌ Failed to list node groups: {e}")
# ======================
# SECRET COMMANDS
# ======================
@app.command()
def create_secret(
    name: str = typer.Argument(..., help="Secret name"),
    data: str = typer.Option(..., "--data", "-d", help="Secret data (key1=value1,key2=value2)"),
    secret_type: str = typer.Option("Opaque", "--type", "-t", help="Secret type"),
    namespace: Optional[str] = namespace_option
):
    """Create a Kubernetes secret"""
    if not validate_name(name):
        console.print(f"❌ Invalid secret name: {name}")
        return
    
    # Parse data
    try:
        data_dict = {}
        for pair in data.split(","):
            if "=" in pair:
                key, value = pair.split("=", 1)
                data_dict[key.strip()] = value.strip()
            else:
                console.print(f"❌ Invalid data format: {pair}")
                return
        
        if not data_dict:
            console.print("❌ No valid data provided")
            return
        
        ns = namespace or get_config().get_namespace()
        client = K8sClient(namespace=ns)
        
        with console.status(f"Creating secret {name}..."):
            result = client.create_secret(name, data_dict, secret_type, ns)
        
        if result:
            console.print(f"✅ Secret {name} created successfully")
            console.print(f"📋 Type: {secret_type}")
            console.print(f"🔑 Keys: {list(data_dict.keys())}")
        else:
            console.print(f"❌ Failed to create secret {name}")
    
    except Exception as e:
        console.print(f"❌ Error creating secret: {e}")


@app.command()
def list_secrets(
    namespace: Optional[str] = namespace_option,
    output: str = output_option
):
    """List secrets"""
    ns = namespace or get_config().get_namespace()
    client = K8sClient(namespace=ns)
    
    secrets = client.list_secrets(ns)
    
    if output == "table":
        table = Table(title=f"Secrets in {ns}")
        table.add_column("Name", style="cyan")
        table.add_column("Type", style="magenta")
        table.add_column("Keys", style="green")
        table.add_column("Age", style="blue")
        
        for secret in secrets:
            age = format_age(secret['created_at'])
            keys = ", ".join(secret['data_keys'])
            table.add_row(secret['name'], secret['type'], keys, age)
        
        console.print(table)
    elif output == "yaml":
        console.print(format_yaml_output(secrets))
    elif output == "json":
        console.print(format_json_output(secrets))


@app.command()
def delete_secret(
    name: str = typer.Argument(..., help="Secret name"),
    namespace: Optional[str] = namespace_option
):
    """Delete a secret"""
    ns = namespace or get_config().get_namespace()
    client = K8sClient(namespace=ns)
    
    if typer.confirm(f"Are you sure you want to delete secret {name}?"):
        with console.status(f"Deleting secret {name}..."):
            if client.delete_secret(name, ns):
                console.print(f"✅ Secret {name} deleted successfully")
            else:
                console.print(f"❌ Failed to delete secret {name}")


# ======================
# PVC COMMANDS
# ======================
@app.command()
def create_pvc(
    name: str = typer.Argument(..., help="PVC name"),
    size: str = typer.Argument(..., help="Storage size (e.g., 10Gi, 100Mi)"),
    access_modes: str = typer.Option("ReadWriteOnce", "--access-modes", "-a", help="Access modes (comma-separated)"),
    storage_class: Optional[str] = typer.Option(None, "--storage-class", "-s", help="Storage class"),
    namespace: Optional[str] = namespace_option
):
    """Create a Persistent Volume Claim"""
    if not validate_name(name):
        console.print(f"❌ Invalid PVC name: {name}")
        return
    
    # Parse access modes
    access_modes_list = [mode.strip() for mode in access_modes.split(",")]
    
    ns = namespace or get_config().get_namespace()
    client = K8sClient(namespace=ns)
    
    with console.status(f"Creating PVC {name}..."):
        result = client.create_pvc(
            name=name,
            size=size,
            access_modes=access_modes_list,
            storage_class=storage_class,
            namespace=ns
        )
    
    if result:
        console.print(f"✅ PVC {name} created successfully")
        console.print(f"💾 Size: {size}")
        console.print(f"🔐 Access modes: {access_modes_list}")
        if storage_class:
            console.print(f"📦 Storage class: {storage_class}")
    else:
        console.print(f"❌ Failed to create PVC {name}")


@app.command()
def list_pvcs(
    namespace: Optional[str] = namespace_option,
    output: str = output_option
):
    """List Persistent Volume Claims"""
    ns = namespace or get_config().get_namespace()
    client = K8sClient(namespace=ns)
    
    pvcs = client.list_pvcs(ns)
    
    if output == "table":
        table = Table(title=f"PVCs in {ns}")
        table.add_column("Name", style="cyan")
        table.add_column("Status", style="magenta")
        table.add_column("Volume", style="green")
        table.add_column("Size", style="blue")
        table.add_column("Access Modes", style="yellow")
        table.add_column("Storage Class", style="red")
        table.add_column("Age", style="blue")
        
        for pvc in pvcs:
            age = format_age(pvc['created_at'])
            status_color = "green" if pvc['status'] == 'Bound' else "yellow"
            table.add_row(
                pvc['name'],
                f"[{status_color}]{pvc['status']}[/{status_color}]",
                pvc['volume_name'] or "N/A",
                pvc['size'],
                ", ".join(pvc['access_modes']),
                pvc['storage_class'] or "N/A",
                age
            )
        
        console.print(table)
    elif output == "yaml":
        console.print(format_yaml_output(pvcs))
    elif output == "json":
        console.print(format_json_output(pvcs))


@app.command()
def delete_pvc(
    name: str = typer.Argument(..., help="PVC name"),
    namespace: Optional[str] = namespace_option
):
    """Delete a Persistent Volume Claim"""
    ns = namespace or get_config().get_namespace()
    client = K8sClient(namespace=ns)
    
    if typer.confirm(f"Are you sure you want to delete PVC {name}?"):
        with console.status(f"Deleting PVC {name}..."):
            if client.delete_pvc(name, ns):
                console.print(f"✅ PVC {name} deleted successfully")
            else:
                console.print(f"❌ Failed to delete PVC {name}")


# ======================
# SERVICE URL COMMAND
# ======================
@app.command()
def service_url(
    name: str = typer.Argument(..., help="Service name"),
    namespace: Optional[str] = namespace_option,
    watch: bool = typer.Option(False, "--watch", "-w", help="Watch for URL changes")
):
    """Get service URL including AWS ELB URLs"""
    ns = namespace or get_config().get_namespace()
    client = K8sClient(namespace=ns)
    
    def show_service_url():
        url_info = client.get_service_url(name, ns)
        if not url_info:
            console.print(f"❌ Service {name} not found")
            return False
        
        console.print(f"\n🔗 Service URL Information for [cyan]{name}[/cyan]")
        console.print(f"📍 Namespace: {url_info['namespace']}")
        console.print(f"🔧 Type: {url_info['type']}")
        console.print(f"🖥️  Cluster IP: {url_info['cluster_ip']}")
        
        # Show ports
        console.print("\n📋 Ports:")
        for port in url_info['ports']:
            console.print(f"  • {port['port']}/{port['protocol']} → {port['target_port']}")
        
        # Show external access
        if url_info['type'] == 'LoadBalancer':
            if url_info.get('aws_elb'):
                console.print(f"\n🌐 AWS ELB DNS: [green]{url_info['elb_dns_name']}[/green]")
                console.print(f"🔗 External URL: [blue]{url_info['external_url']}[/blue]")
            elif url_info.get('external_url'):
                console.print(f"\n🔗 External URL: [blue]{url_info['external_url']}[/blue]")
            elif url_info.get('status'):
                console.print(f"\n⏳ Status: {url_info['status']}")
        
        elif url_info['type'] == 'NodePort':
            if url_info.get('external_url'):
                console.print(f"\n🔗 NodePort URL: [blue]{url_info['external_url']}[/blue]")
                console.print(f"🖥️  Node IP: {url_info['node_ip']}")
                console.print(f"🚪 Node Port: {url_info['node_port']}")
        
        elif url_info['type'] == 'ClusterIP':
            console.print(f"\n💡 ClusterIP service - only accessible within cluster")
        
        return True
    
    if watch:
        console.print("👁️  Watching for service URL changes (Ctrl+C to stop)")
        try:
            while True:
                show_service_url()
                time.sleep(5)
        except KeyboardInterrupt:
            console.print("\n👋 Stopped watching")
    else:
        show_service_url()


# ======================
# MONITORING COMMANDS
# ======================
@app.command()
def setup_monitoring(
    namespace: str = typer.Option("monitoring", "--namespace", "-n", help="Namespace for monitoring stack"),
    grafana_service_type: str = typer.Option("NodePort", "--service-type", "-t", help="Grafana service type: NodePort, LoadBalancer, ClusterIP"),
    import_dashboard: bool = typer.Option(True, "--import-dashboard/--no-dashboard", help="Import default Kubernetes dashboard"),
    install_kube_state_metrics: bool = typer.Option(True, "--install-kube-state-metrics/--no-kube-state-metrics", help="Install kube-state-metrics for cluster metrics"),
    wait: bool = typer.Option(True, "--wait/--no-wait", help="Wait for deployments to be ready"),
    show_info: bool = typer.Option(True, "--show-info/--no-show-info", help="Show monitoring stack information after setup")
):
    """Deploy complete monitoring stack with Prometheus and Grafana"""
    
    # Validate service type
    valid_service_types = ["NodePort", "LoadBalancer", "ClusterIP"]
    if grafana_service_type not in valid_service_types:
        console.print(f"❌ Invalid service type: {grafana_service_type}")
        console.print(f"💡 Valid options: {', '.join(valid_service_types)}")
        return
    
    console.print(f"🚀 Setting up monitoring stack in namespace: {namespace}")
    console.print(f"🔧 Grafana service type: {grafana_service_type}")
    
    if import_dashboard:
        console.print("📊 Will import default Kubernetes dashboard")
    
    if install_kube_state_metrics:
        console.print("📈 Will install kube-state-metrics for cluster metrics")
    
    # Show what will be deployed
    console.print("\n📋 Components to deploy:")
    console.print("   • Prometheus server with cluster monitoring configuration")
    console.print("   • Grafana with admin credentials (admin/admin123)")
    console.print("   • ServiceAccount and RBAC for Prometheus")
    console.print("   • ConfigMaps for Prometheus configuration")
    if install_kube_state_metrics:
        console.print("   • kube-state-metrics for cluster metrics (via Helm or manual)")
    if import_dashboard:
        console.print("   • Default Kubernetes metrics dashboard")
    
    try:
        client = K8sClient()
        
        with console.status("Deploying monitoring stack..."):
            result = client.setup_monitoring(
                namespace=namespace,
                grafana_service_type=grafana_service_type,
                import_dashboard=import_dashboard,
                wait_for_ready=wait,
                install_kube_state_metrics=install_kube_state_metrics
            )
        
        if result['success']:
            console.print("✅ Monitoring stack deployed successfully!")
            
            # Show warning if Grafana configuration failed
            if result.get('warning'):
                console.print(f"⚠️  {result['warning']}")
                console.print("💡 You can manually configure Grafana by:")
                console.print("   1. Accessing Grafana with admin/admin123")
                console.print("   2. Adding Prometheus as a data source")
                console.print(f"   3. Use URL: http://prometheus-service.{result['namespace']}.svc.cluster.local:9090")
            
            # Show deployment summary
            console.print(f"\n📋 Deployment Summary:")
            console.print(f"📍 Namespace: {result['namespace']}")
            
            if result['prometheus']['deployed']:
                console.print("✅ Prometheus: Deployed")
            else:
                console.print("❌ Prometheus: Failed to deploy")
            
            if result['grafana']['deployed']:
                console.print("✅ Grafana: Deployed")
                console.print(f"🔑 Admin credentials: {result['grafana']['admin_user']}/{result['grafana']['admin_password']}")
                
                # Show Grafana configuration status
                if result.get('grafana_configured', True):
                    console.print("✅ Grafana: Automatically configured with Prometheus")
                else:
                    console.print("⚠️  Grafana: Manual configuration required")
            else:
                console.print("❌ Grafana: Failed to deploy")
            
            # Show kube-state-metrics status
            if install_kube_state_metrics and 'kube_state_metrics' in result:
                ksm = result['kube_state_metrics']
                if ksm.get('installed'):
                    method = ksm.get('method', 'unknown')
                    if method == 'helm':
                        console.print("✅ kube-state-metrics: Deployed via Helm")
                    elif method == 'manual':
                        console.print("✅ kube-state-metrics: Deployed via manual YAML")
                    elif method == 'existing':
                        console.print("✅ kube-state-metrics: Already deployed")
                else:
                    console.print(f"❌ kube-state-metrics: Failed to deploy")
                    if ksm.get('error'):
                        console.print(f"   Error: {ksm['error']}")
            
            if show_info:
                # Get and display monitoring information
                with console.status("Retrieving monitoring stack information..."):
                    time.sleep(2)  # Give services time to be ready
                    info = client.get_monitoring_info(namespace)
                
                console.print(f"\n🔗 Monitoring Stack Information:")
                
                # Prometheus info
                if info['prometheus']['running']:
                    console.print(f"✅ Prometheus: Running")
                    if info['prometheus']['url']:
                        console.print(f"🔗 Prometheus URL: [blue]{info['prometheus']['url']}[/blue]")
                    else:
                        console.print(f"💡 Prometheus: Access via port-forward: kubectl port-forward -n {namespace} svc/prometheus-service 9090:9090")
                else:
                    console.print(f"⏳ Prometheus: Starting up...")
                
                # Grafana info
                if info['grafana']['running']:
                    console.print(f"✅ Grafana: Running")
                    if info['grafana']['url']:
                        console.print(f"🔗 Grafana URL: [blue]{info['grafana']['url']}[/blue]")
                    else:
                        console.print(f"💡 Grafana: Access via port-forward: kubectl port-forward -n {namespace} svc/grafana-service 3000:3000")
                    
                    if info['grafana']['credentials']:
                        creds = info['grafana']['credentials']
                        console.print(f"🔑 Login: [green]{creds['username']}[/green] / [green]{creds['password']}[/green]")
                else:
                    console.print(f"⏳ Grafana: Starting up...")
                
                # Show services
                console.print(f"\n📋 Services:")
                for service in info['services']:
                    console.print(f"   • {service['name']} ({service['type']}) - IP: {service['cluster_ip']}")
                    for port in service['ports']:
                        port_info = f"     Port: {port['port']} → {port['target_port']}"
                        if 'node_port' in port:
                            port_info += f" (NodePort: {port['node_port']})"
                        console.print(port_info)
            
            # Show next steps
            console.print(f"\n🚀 Next Steps:")
            console.print(f"   1. Access Grafana with the provided URL and credentials")
            console.print(f"   2. Verify Prometheus data source is configured")
            console.print(f"   3. Explore the imported Kubernetes dashboard")
            console.print(f"   4. Create custom dashboards and alerts as needed")
            console.print(f"\n💡 Useful commands:")
            console.print(f"   • Check status: k8s-helper monitoring-status -n {namespace}")
            console.print(f"   • Port-forward Grafana: kubectl port-forward -n {namespace} svc/grafana-service 3000:3000")
            console.print(f"   • Port-forward Prometheus: kubectl port-forward -n {namespace} svc/prometheus-service 9090:9090")
            
        else:
            console.print(f"❌ Failed to deploy monitoring stack: {result.get('error', 'Unknown error')}")
            
            # Show partial deployment info
            if result['prometheus']['deployed']:
                console.print("✅ Prometheus was deployed successfully")
            if result['grafana']['deployed']:
                console.print("✅ Grafana was deployed successfully")
            
            console.print("\n🛠️  Troubleshooting:")
            console.print("   • Check cluster connectivity: kubectl get nodes")
            console.print("   • Verify namespace permissions")
            console.print("   • Check resource availability in the cluster")
            console.print(f"   • View monitoring namespace: kubectl get all -n {namespace}")
    
    except Exception as e:
        console.print(f"❌ Error setting up monitoring: {e}")
        console.print("\n🛠️  Troubleshooting:")
        console.print("   • Ensure kubectl is configured and cluster is accessible")
        console.print("   • Check if you have sufficient permissions to create resources")
        console.print("   • Verify the cluster has enough resources available")


@app.command()
def monitoring_status(
    namespace: str = typer.Option("monitoring", "--namespace", "-n", help="Monitoring namespace"),
    output: str = output_option
):
    """Show status of monitoring stack"""
    try:
        client = K8sClient()
        
        with console.status("Checking monitoring stack status..."):
            info = client.get_monitoring_info(namespace)
        
        if 'error' in info:
            console.print(f"❌ Error getting monitoring status: {info['error']}")
            return
        
        if output == "table":
            # Overview table
            table = Table(title=f"Monitoring Stack Status - {namespace}")
            table.add_column("Component", style="cyan")
            table.add_column("Status", style="green")
            table.add_column("URL", style="blue")
            
            # Prometheus row
            prometheus_status = "🟢 Running" if info['prometheus']['running'] else "🔴 Not Running"
            prometheus_url = info['prometheus']['url'] or "Port-forward required"
            table.add_row("Prometheus", prometheus_status, prometheus_url)
            
            # Grafana row
            grafana_status = "🟢 Running" if info['grafana']['running'] else "🔴 Not Running"
            grafana_url = info['grafana']['url'] or "Port-forward required"
            table.add_row("Grafana", grafana_status, grafana_url)
            
            console.print(table)
            
            # Credentials info
            if info['grafana']['credentials']:
                creds = info['grafana']['credentials']
                console.print(f"\n🔑 Grafana Credentials:")
                console.print(f"   Username: [green]{creds['username']}[/green]")
                console.print(f"   Password: [green]{creds['password']}[/green]")
            
            # Services table
            if info['services']:
                services_table = Table(title="Services")
                services_table.add_column("Name", style="cyan")
                services_table.add_column("Type", style="magenta")
                services_table.add_column("Cluster IP", style="yellow")
                services_table.add_column("Ports", style="green")
                
                for service in info['services']:
                    ports_str = ", ".join([
                        f"{port['port']}:{port['target_port']}" + 
                        (f" (NodePort: {port['node_port']})" if 'node_port' in port else "")
                        for port in service['ports']
                    ])
                    services_table.add_row(
                        service['name'],
                        service['type'],
                        service['cluster_ip'],
                        ports_str
                    )
                
                console.print(services_table)
        
        elif output == "json":
            console.print(format_json_output(info))
        elif output == "yaml":
            console.print(format_yaml_output(info))
    
    except Exception as e:
        console.print(f"❌ Error checking monitoring status: {e}")


@app.command()
def delete_monitoring(
    namespace: str = typer.Option("monitoring", "--namespace", "-n", help="Monitoring namespace"),
    force: bool = typer.Option(False, "--force", help="Skip confirmation prompt")
):
    """Delete monitoring stack"""
    if not force:
        if not typer.confirm(f"Are you sure you want to delete the monitoring stack in namespace '{namespace}'?"):
            console.print("❌ Operation cancelled")
            return
    
    try:
        client = K8sClient()
        
        console.print(f"🗑️  Deleting monitoring stack from namespace: {namespace}")
        
        # Delete deployments
        deployments_to_delete = ["prometheus", "grafana"]
        for deployment in deployments_to_delete:
            try:
                with console.status(f"Deleting deployment {deployment}..."):
                    client.apps_v1.delete_namespaced_deployment(name=deployment, namespace=namespace)
                console.print(f"✅ Deleted deployment: {deployment}")
            except ApiException as e:
                if e.status == 404:
                    console.print(f"⚠️  Deployment {deployment} not found")
                else:
                    console.print(f"❌ Error deleting deployment {deployment}: {e}")
        
        # Delete services
        services_to_delete = ["prometheus-service", "grafana-service"]
        for service in services_to_delete:
            try:
                with console.status(f"Deleting service {service}..."):
                    client.core_v1.delete_namespaced_service(name=service, namespace=namespace)
                console.print(f"✅ Deleted service: {service}")
            except ApiException as e:
                if e.status == 404:
                    console.print(f"⚠️  Service {service} not found")
                else:
                    console.print(f"❌ Error deleting service {service}: {e}")
        
        # Delete ConfigMaps
        configmaps_to_delete = ["prometheus-config"]
        for configmap in configmaps_to_delete:
            try:
                with console.status(f"Deleting configmap {configmap}..."):
                    client.core_v1.delete_namespaced_config_map(name=configmap, namespace=namespace)
                console.print(f"✅ Deleted configmap: {configmap}")
            except ApiException as e:
                if e.status == 404:
                    console.print(f"⚠️  ConfigMap {configmap} not found")
                else:
                    console.print(f"❌ Error deleting configmap {configmap}: {e}")
        
        # Delete ServiceAccount
        try:
            with console.status("Deleting ServiceAccount prometheus..."):
                client.core_v1.delete_namespaced_service_account(name="prometheus", namespace=namespace)
            console.print("✅ Deleted ServiceAccount: prometheus")
        except ApiException as e:
            if e.status == 404:
                console.print("⚠️  ServiceAccount prometheus not found")
            else:
                console.print(f"❌ Error deleting ServiceAccount: {e}")
        
        # Delete RBAC resources
        try:
            from kubernetes import client as k8s_client
            rbac_v1 = k8s_client.RbacAuthorizationV1Api()
            with console.status("Deleting RBAC resources..."):
                rbac_v1.delete_cluster_role(name="prometheus")
                rbac_v1.delete_cluster_role_binding(name="prometheus")
            console.print("✅ Deleted RBAC resources")
        except ApiException as e:
            if e.status == 404:
                console.print("⚠️  RBAC resources not found")
            else:
                console.print(f"❌ Error deleting RBAC resources: {e}")
        
        console.print(f"✅ Monitoring stack cleanup completed for namespace: {namespace}")
        console.print(f"💡 To delete the namespace entirely: kubectl delete namespace {namespace}")
    
    except Exception as e:
        console.print(f"❌ Error deleting monitoring stack: {e}")
        console.print("💡 You may need to clean up resources manually with kubectl")


@app.command()
def add_prometheus_target(
    job_name: str = typer.Argument(..., help="Name for the Prometheus job"),
    targets: str = typer.Argument(..., help="Comma-separated list of targets (e.g., 'service:port,1.2.3.4:9090')"),
    namespace: str = typer.Option("monitoring", "--namespace", "-n", help="Monitoring namespace"),
    metrics_path: str = typer.Option("/metrics", "--metrics-path", "-p", help="Path to metrics endpoint"),
    scrape_interval: str = typer.Option("15s", "--scrape-interval", "-i", help="Scrape interval (e.g., 15s, 30s, 1m)")
):
    """Add a new target to Prometheus monitoring"""
    if not validate_name(job_name):
        console.print(f"❌ Invalid job name: {job_name}")
        return
    
    # Parse targets
    target_list = [target.strip() for target in targets.split(",") if target.strip()]
    
    if not target_list:
        console.print("❌ No valid targets provided")
        return
    
    console.print(f"🎯 Adding Prometheus target job: {job_name}")
    console.print(f"📋 Targets: {target_list}")
    console.print(f"📍 Namespace: {namespace}")
    console.print(f"🛣️  Metrics path: {metrics_path}")
    console.print(f"⏱️  Scrape interval: {scrape_interval}")
    
    try:
        client = K8sClient()
        
        with console.status("Adding Prometheus target..."):
            success = client.add_prometheus_target(
                job_name=job_name,
                targets=target_list,
                namespace=namespace,
                metrics_path=metrics_path,
                scrape_interval=scrape_interval
            )
        
        if success:
            console.print(f"✅ Successfully added Prometheus target job '{job_name}'")
            console.print(f"🔄 Prometheus configuration updated and deployment restarted")
            console.print(f"\n💡 Next steps:")
            console.print(f"   • Check targets in Prometheus UI: Status > Targets")
            console.print(f"   • Verify metrics are being scraped")
            console.print(f"   • Create Grafana dashboards for the new metrics")
        else:
            console.print(f"❌ Failed to add Prometheus target")
    
    except Exception as e:
        console.print(f"❌ Error adding Prometheus target: {e}")


@app.command()
def remove_prometheus_target(
    job_name: str = typer.Argument(..., help="Name of the Prometheus job to remove"),
    namespace: str = typer.Option("monitoring", "--namespace", "-n", help="Monitoring namespace"),
    force: bool = typer.Option(False, "--force", help="Skip confirmation prompt")
):
    """Remove a target from Prometheus monitoring"""
    if not force:
        if not typer.confirm(f"Are you sure you want to remove Prometheus job '{job_name}'?"):
            console.print("❌ Operation cancelled")
            return
    
    console.print(f"🗑️  Removing Prometheus target job: {job_name}")
    console.print(f"📍 Namespace: {namespace}")
    
    try:
        client = K8sClient()
        
        with console.status("Removing Prometheus target..."):
            success = client.remove_prometheus_target(
                job_name=job_name,
                namespace=namespace
            )
        
        if success:
            console.print(f"✅ Successfully removed Prometheus target job '{job_name}'")
            console.print(f"🔄 Prometheus configuration updated and deployment restarted")
        else:
            console.print(f"❌ Failed to remove Prometheus target")
    
    except Exception as e:
        console.print(f"❌ Error removing Prometheus target: {e}")


@app.command()
def list_prometheus_targets(
    namespace: str = typer.Option("monitoring", "--namespace", "-n", help="Monitoring namespace"),
    output: str = output_option
):
    """List all Prometheus monitoring targets"""
    try:
        client = K8sClient()
        
        with console.status("Fetching Prometheus targets..."):
            result = client.list_prometheus_targets(namespace)
        
        if 'error' in result:
            console.print(f"❌ Error getting Prometheus targets: {result['error']}")
            return
        
        targets = result.get('targets', [])
        
        if not targets:
            console.print(f"📋 No Prometheus targets found in namespace: {namespace}")
            console.print(f"💡 Add targets with: k8s-helper add-prometheus-target <job-name> <targets>")
            return
        
        if output == "table":
            table = Table(title=f"Prometheus Targets - {namespace}")
            table.add_column("Job Name", style="cyan")
            table.add_column("Type", style="magenta")
            table.add_column("Targets", style="green")
            table.add_column("Metrics Path", style="blue")
            table.add_column("Scrape Interval", style="yellow")
            
            for target in targets:
                job_type = target.get('type', 'static')
                job_type_display = "🔄 Kubernetes" if job_type == 'kubernetes_discovery' else "📍 Static"
                
                targets_display = ", ".join(target.get('targets', []))
                if len(targets_display) > 50:
                    targets_display = targets_display[:47] + "..."
                
                table.add_row(
                    target.get('job_name', 'unknown'),
                    job_type_display,
                    targets_display,
                    target.get('metrics_path', '/metrics'),
                    target.get('scrape_interval', 'default')
                )
            
            console.print(table)
            console.print(f"\n📊 Total jobs: {result.get('total_jobs', 0)}")
        
        elif output == "json":
            console.print(format_json_output(result))
        elif output == "yaml":
            console.print(format_yaml_output(result))
    
    except Exception as e:
        console.print(f"❌ Error listing Prometheus targets: {e}")


@app.command()
def update_prometheus_target(
    job_name: str = typer.Argument(..., help="Name of the Prometheus job to update"),
    targets: str = typer.Argument(..., help="New comma-separated list of targets"),
    namespace: str = typer.Option("monitoring", "--namespace", "-n", help="Monitoring namespace"),
    metrics_path: Optional[str] = typer.Option(None, "--metrics-path", "-p", help="New metrics path"),
    scrape_interval: Optional[str] = typer.Option(None, "--scrape-interval", "-i", help="New scrape interval")
):
    """Update an existing Prometheus target"""
    if not validate_name(job_name):
        console.print(f"❌ Invalid job name: {job_name}")
        return
    
    # Parse targets
    target_list = [target.strip() for target in targets.split(",") if target.strip()]
    
    if not target_list:
        console.print("❌ No valid targets provided")
        return
    
    console.print(f"🔄 Updating Prometheus target job: {job_name}")
    console.print(f"📋 New targets: {target_list}")
    console.print(f"📍 Namespace: {namespace}")
    
    if metrics_path:
        console.print(f"🛣️  New metrics path: {metrics_path}")
    if scrape_interval:
        console.print(f"⏱️  New scrape interval: {scrape_interval}")
    
    try:
        client = K8sClient()
        
        with console.status("Updating Prometheus target..."):
            success = client.update_prometheus_target(
                job_name=job_name,
                targets=target_list,
                namespace=namespace,
                metrics_path=metrics_path,
                scrape_interval=scrape_interval
            )
        
        if success:
            console.print(f"✅ Successfully updated Prometheus target job '{job_name}'")
            console.print(f"🔄 Prometheus configuration updated and deployment restarted")
        else:
            console.print(f"❌ Failed to update Prometheus target")
    
    except Exception as e:
        console.print(f"❌ Error updating Prometheus target: {e}")


# ======================
# HELM-BASED MONITORING COMMANDS
# ======================
@app.command()
def setup_monitoring_stack(
    namespace: str = typer.Option("monitoring", "--namespace", "-n", help="Namespace for monitoring stack"),
    grafana_service_type: str = typer.Option("NodePort", "--service-type", "-t", help="Grafana service type: NodePort, LoadBalancer, ClusterIP"),
    prometheus_storage_size: str = typer.Option("10Gi", "--prometheus-storage", help="Prometheus storage size"),
    grafana_storage_size: str = typer.Option("5Gi", "--grafana-storage", help="Grafana storage size"),
    wait: bool = typer.Option(True, "--wait/--no-wait", help="Wait for deployments to be ready"),
    install_ingress: bool = typer.Option(False, "--install-ingress", help="Install ingress for external access")
):
    """Deploy monitoring stack using official Helm charts (auto-installs Helm if needed)"""
    
    # Validate service type
    valid_service_types = ["NodePort", "LoadBalancer", "ClusterIP"]
    if grafana_service_type not in valid_service_types:
        console.print(f"❌ Invalid service type: {grafana_service_type}")
        console.print(f"💡 Valid options: {', '.join(valid_service_types)}")
        return
    
    console.print(f"🚀 Setting up Helm-based monitoring stack in namespace: {namespace}")
    console.print(f"🔧 Grafana service type: {grafana_service_type}")
    console.print(f"💾 Prometheus storage: {prometheus_storage_size}")
    console.print(f"💾 Grafana storage: {grafana_storage_size}")
    console.print(f"⚙️  Note: Helm will be automatically installed if not present")
    
    # Show what will be deployed
    console.print("\n📋 Components to deploy via Helm:")
    console.print("   • Prometheus Operator (kube-prometheus-stack)")
    console.print("   • Grafana with persistent storage")
    console.print("   • AlertManager for alerts")
    console.print("   • Node Exporter for node metrics")
    console.print("   • kube-state-metrics for cluster state")
    console.print("   • Prometheus rules and dashboards")
    if install_ingress:
        console.print("   • Ingress for external access")
    
    try:
        client = K8sClient()
        
        with console.status("Deploying Helm monitoring stack..."):
            result = client.setup_helm_monitoring(
                namespace=namespace,
                grafana_service_type=grafana_service_type,
                prometheus_storage_size=prometheus_storage_size,
                grafana_storage_size=grafana_storage_size,
                wait_for_ready=wait,
                install_ingress=install_ingress
            )
        
        if result['success']:
            console.print("✅ Helm monitoring stack deployed successfully!")
            
            # Show deployment summary
            console.print(f"\n📋 Deployment Summary:")
            console.print(f"📍 Namespace: {result['namespace']}")
            console.print(f"🎯 Helm Release: {result['release_name']}")
            
            if result.get('prometheus', {}).get('deployed'):
                console.print("✅ Prometheus Operator: Deployed")
            else:
                console.print("❌ Prometheus Operator: Failed to deploy")
            
            if result.get('grafana', {}).get('deployed'):
                console.print("✅ Grafana: Deployed")
                if result['grafana'].get('admin_password'):
                    console.print(f"🔑 Grafana admin password: {result['grafana']['admin_password']}")
                else:
                    console.print("🔑 Grafana admin password: admin")
            else:
                console.print("❌ Grafana: Failed to deploy")
            
            # Show access information
            console.print(f"\n🔗 Access Information:")
            
            if result.get('grafana_url'):
                console.print(f"🔗 Grafana URL: [blue]{result['grafana_url']}[/blue]")
            else:
                console.print(f"💡 Grafana: kubectl port-forward -n {namespace} svc/kube-prometheus-stack-grafana 3000:80")
            
            if result.get('prometheus_url'):
                console.print(f"🔗 Prometheus URL: [blue]{result['prometheus_url']}[/blue]")
            else:
                console.print(f"💡 Prometheus: kubectl port-forward -n {namespace} svc/kube-prometheus-stack-prometheus 9090:9090")
            
            if result.get('alertmanager_url'):
                console.print(f"🔗 AlertManager URL: [blue]{result['alertmanager_url']}[/blue]")
            else:
                console.print(f"💡 AlertManager: kubectl port-forward -n {namespace} svc/kube-prometheus-stack-alertmanager 9093:9093")
            
            # Show next steps
            console.print(f"\n🚀 Next Steps:")
            console.print(f"   1. Access Grafana with admin/admin (or password shown above)")
            console.print(f"   2. Explore pre-configured dashboards")
            console.print(f"   3. Set up custom alerts in AlertManager")
            console.print(f"   4. Add custom Prometheus targets if needed")
            console.print(f"\n💡 Useful commands:")
            console.print(f"   • Check status: k8s-helper monitoring-stack-status -n {namespace}")
            console.print(f"   • List dashboards: kubectl get configmaps -n {namespace} | grep dashboard")
            console.print(f"   • View Helm release: helm list -n {namespace}")
            
        else:
            console.print(f"❌ Failed to deploy Helm monitoring stack: {result.get('error', 'Unknown error')}")
            
            # Show suggestion if provided
            if result.get('suggestion'):
                console.print(f"💡 {result['suggestion']}")
            
            console.print("\n🛠️  Troubleshooting:")
            if 'Helm' in str(result.get('error', '')):
                console.print("   • Helm installation failed - you may need to install manually")
                console.print("   • Visit: https://helm.sh/docs/intro/install/")
                console.print("   • Restart your terminal after installation")
            else:
                console.print("   • Check cluster connectivity: kubectl get nodes")
                console.print("   • Verify namespace permissions")
                console.print(f"   • View Helm status: helm status -n {namespace} kube-prometheus-stack")
    
    except Exception as e:
        console.print(f"❌ Error setting up Helm monitoring: {e}")
        console.print("\n🛠️  Troubleshooting:")
        console.print("   • Ensure Helm is installed and configured")
        console.print("   • Check if kubectl is configured correctly")
        console.print("   • Verify you have cluster admin permissions")


@app.command()
def monitoring_stack_status(
    namespace: str = typer.Option("monitoring", "--namespace", "-n", help="Monitoring namespace"),
    output: str = output_option
):
    """Show status of Helm-based monitoring stack"""
    try:
        client = K8sClient()
        
        with console.status("Checking Helm monitoring stack status..."):
            info = client.get_helm_monitoring_info(namespace)
        
        if 'error' in info:
            console.print(f"❌ Error getting monitoring status: {info['error']}")
            return
        
        if output == "table":
            # Helm release info
            console.print(f"🎯 Helm Release: {info.get('release_name', 'kube-prometheus-stack')}")
            console.print(f"📊 Release Status: {info.get('release_status', 'Unknown')}")
            console.print(f"📅 Last Deployed: {info.get('last_deployed', 'Unknown')}")
            
            # Overview table
            table = Table(title=f"Monitoring Stack Status - {namespace}")
            table.add_column("Component", style="cyan")
            table.add_column("Status", style="green")
            table.add_column("URL", style="blue")
            
            # Components status
            components = ['prometheus', 'grafana', 'alertmanager']
            for component in components:
                if component in info:
                    comp_info = info[component]
                    status = "🟢 Running" if comp_info.get('running') else "🔴 Not Running"
                    url = comp_info.get('url', 'Port-forward required')
                    table.add_row(component.capitalize(), status, url)
            
            console.print(table)
            
            # Show pod status
            if info.get('pods'):
                pod_table = Table(title="Pod Status")
                pod_table.add_column("Pod", style="cyan")
                pod_table.add_column("Status", style="green")
                pod_table.add_column("Ready", style="blue")
                
                for pod in info['pods']:
                    pod_table.add_row(
                        pod['name'],
                        pod['status'],
                        f"{pod['ready']}/{pod['total']}"
                    )
                
                console.print(pod_table)
        
        elif output == "json":
            console.print(format_json_output(info))
        elif output == "yaml":
            console.print(format_yaml_output(info))
    
    except Exception as e:
        console.print(f"❌ Error checking Helm monitoring status: {e}")


@app.command()
def delete_monitoring_stack(
    namespace: str = typer.Option("monitoring", "--namespace", "-n", help="Monitoring namespace"),
    release_name: str = typer.Option("kube-prometheus-stack", "--release-name", help="Helm release name"),
    force: bool = typer.Option(False, "--force", help="Skip confirmation prompt")
):
    """Delete Helm-based monitoring stack"""
    if not force:
        if not typer.confirm(f"Are you sure you want to delete the Helm monitoring stack '{release_name}' in namespace '{namespace}'?"):
            console.print("❌ Operation cancelled")
            return
    
    try:
        client = K8sClient()
        
        console.print(f"🗑️  Deleting Helm monitoring stack: {release_name}")
        
        with console.status("Uninstalling Helm release..."):
            result = client.delete_helm_monitoring(namespace, release_name)
        
        if result['success']:
            console.print(f"✅ Helm monitoring stack '{release_name}' deleted successfully")
            console.print(f"📋 Cleaned up {result.get('resources_deleted', 0)} resources")
        else:
            console.print(f"❌ Failed to delete Helm monitoring stack: {result.get('error', 'Unknown error')}")
    
    except Exception as e:
        console.print(f"❌ Error deleting Helm monitoring: {e}")


@app.command()
def install_helm(
    force: bool = typer.Option(False, "--force", help="Force reinstall even if Helm is already installed")
):
    """Install Helm automatically on your system"""
    try:
        client = K8sClient()
        
        # Check if Helm is already installed
        if not force and client._check_helm_available():
            console.print("✅ Helm is already installed on your system")
            
            import subprocess
            try:
                result = subprocess.run(['helm', 'version'], capture_output=True, text=True)
                console.print(f"📋 Version: {result.stdout.strip()}")
            except:
                pass
            
            console.print("💡 Use --force to reinstall")
            return
        
        console.print("🔧 Installing Helm automatically...")
        
        with console.status("Installing Helm..."):
            result = client._install_helm_automatically()
        
        if result['success']:
            console.print(f"✅ {result['message']}")
            console.print(f"📦 Installation method: {result['method']}")
            
            # Test the installation
            if client._check_helm_available():
                console.print("✅ Helm installation verified successfully")
                console.print("\n🚀 You can now use Helm-based monitoring commands:")
                console.print("   • k8s-helper setup-monitoring-stack")
                console.print("   • k8s-helper monitoring-stack-status")
                console.print("   • k8s-helper delete-monitoring-stack")
            else:
                console.print("⚠️  Helm was installed but may not be in your PATH")
                console.print("💡 You may need to restart your terminal")
        else:
            console.print(f"❌ Failed to install Helm: {result.get('error', 'Unknown error')}")
            console.print("\n🛠️  Manual installation:")
            console.print("   • Windows: choco install kubernetes-helm")
            console.print("   • macOS: brew install helm")
            console.print("   • Linux: curl https://raw.githubusercontent.com/helm/helm/main/scripts/get-helm-3 | bash")
            console.print("   • Visit: https://helm.sh/docs/intro/install/")
    
    except Exception as e:
        console.print(f"❌ Error installing Helm: {e}")


if __name__ == "__main__":
    app()
