#!/bin/bash

# Malware Website Crawler - Selective AWS Teardown Script
# This script allows you to selectively destroy specific AWS resources

set -e

echo "🔧 Selective AWS Resource Teardown"
echo "=================================="
echo ""
echo "This script allows you to selectively destroy specific AWS resources."
echo "Useful for partial cleanup or when you want to keep certain resources."
echo ""

# Function to destroy specific resources
destroy_resource() {
    local resource_type=$1
    local resource_name=$2
    local description=$3
    
    echo "🗑️  Destroying $description..."
    terraform destroy -target="$resource_type.$resource_name" -auto-approve
    echo "✅ $description destroyed successfully!"
    echo ""
}

# Function to show current resources
show_resources() {
    echo "📊 Current AWS Resources:"
    echo "========================"
    
    # VPC Resources
    echo "🌐 VPC Resources:"
    terraform state list | grep -E "(aws_vpc|aws_subnet|aws_internet_gateway|aws_nat_gateway|aws_route_table)" || echo "   None found"
    echo ""
    
    # ECS Resources
    echo "🐳 ECS Resources:"
    terraform state list | grep -E "(aws_ecs_cluster|aws_ecs_service|aws_ecs_task_definition)" || echo "   None found"
    echo ""
    
    # Database Resources
    echo "🗄️  Database Resources:"
    terraform state list | grep -E "(aws_db_instance|aws_elasticache_replication_group)" || echo "   None found"
    echo ""
    
    # Storage Resources
    echo "💾 Storage Resources:"
    terraform state list | grep -E "(aws_s3_bucket)" || echo "   None found"
    echo ""
    
    # Load Balancer Resources
    echo "⚖️  Load Balancer Resources:"
    terraform state list | grep -E "(aws_lb|aws_lb_target_group|aws_lb_listener)" || echo "   None found"
    echo ""
    
    # Security Resources
    echo "🔒 Security Resources:"
    terraform state list | grep -E "(aws_security_group|aws_iam_role)" || echo "   None found"
    echo ""
}

# Change to terraform directory
cd "$(dirname "$0")/../terraform"

# Check if terraform is initialized
if [ ! -d ".terraform" ]; then
    echo "📦 Initializing Terraform..."
    terraform init
fi

# Show current resources
show_resources

echo "🎯 Select what you want to destroy:"
echo "1.  Application only (ECS, ALB, ECR)"
echo "2.  Database only (RDS, Redis)"
echo "3.  Storage only (S3 buckets)"
echo "4.  Networking only (VPC, subnets, NAT gateways)"
echo "5.  Security only (Security groups, IAM roles)"
echo "6.  Everything except database"
echo "7.  Everything except storage"
echo "8.  Custom selection"
echo "9.  Show resources again"
echo "0.  Exit"
echo ""

read -p "Enter your choice (0-9): " choice

case $choice in
    1)
        echo "🗑️  Destroying application resources..."
        terraform destroy -target="aws_ecs_service.main" -auto-approve
        terraform destroy -target="aws_ecs_task_definition.app" -auto-approve
        terraform destroy -target="aws_ecs_cluster.main" -auto-approve
        terraform destroy -target="aws_lb_listener.app" -auto-approve
        terraform destroy -target="aws_lb_listener.app_https" -auto-approve
        terraform destroy -target="aws_lb_listener.app_redirect" -auto-approve
        terraform destroy -target="aws_lb_target_group.app" -auto-approve
        terraform destroy -target="aws_lb.main" -auto-approve
        terraform destroy -target="aws_ecr_repository.main" -auto-approve
        terraform destroy -target="aws_cloudwatch_log_group.app" -auto-approve
        echo "✅ Application resources destroyed!"
        ;;
    2)
        echo "🗑️  Destroying database resources..."
        terraform destroy -target="aws_db_instance.main" -auto-approve
        terraform destroy -target="aws_elasticache_replication_group.main" -auto-approve
        terraform destroy -target="aws_db_parameter_group.main" -auto-approve
        terraform destroy -target="aws_db_subnet_group.main" -auto-approve
        terraform destroy -target="aws_elasticache_subnet_group.main" -auto-approve
        echo "✅ Database resources destroyed!"
        ;;
    3)
        echo "🗑️  Destroying storage resources..."
        terraform destroy -target="aws_s3_bucket.files" -auto-approve
        terraform destroy -target="aws_s3_bucket.logs" -auto-approve
        echo "✅ Storage resources destroyed!"
        ;;
    4)
        echo "🗑️  Destroying networking resources..."
        terraform destroy -target="aws_nat_gateway.main" -auto-approve
        terraform destroy -target="aws_eip.nat" -auto-approve
        terraform destroy -target="aws_route_table_association.private" -auto-approve
        terraform destroy -target="aws_route_table_association.public" -auto-approve
        terraform destroy -target="aws_route_table.private" -auto-approve
        terraform destroy -target="aws_route_table.public" -auto-approve
        terraform destroy -target="aws_subnet.private" -auto-approve
        terraform destroy -target="aws_subnet.public" -auto-approve
        terraform destroy -target="aws_internet_gateway.main" -auto-approve
        terraform destroy -target="aws_vpc.main" -auto-approve
        echo "✅ Networking resources destroyed!"
        ;;
    5)
        echo "🗑️  Destroying security resources..."
        terraform destroy -target="aws_security_group.redis" -auto-approve
        terraform destroy -target="aws_security_group.rds" -auto-approve
        terraform destroy -target="aws_security_group.ecs_tasks" -auto-approve
        terraform destroy -target="aws_security_group.alb" -auto-approve
        terraform destroy -target="aws_iam_role_policy_attachment.ecs_task_s3_policy" -auto-approve
        terraform destroy -target="aws_iam_role_policy_attachment.ecs_task_execution_role_policy" -auto-approve
        terraform destroy -target="aws_iam_policy.s3_access" -auto-approve
        terraform destroy -target="aws_iam_role.ecs_task_role" -auto-approve
        terraform destroy -target="aws_iam_role.ecs_task_execution_role" -auto-approve
        echo "✅ Security resources destroyed!"
        ;;
    6)
        echo "🗑️  Destroying everything except database..."
        terraform destroy -auto-approve -var="skip_database=true"
        echo "✅ All resources except database destroyed!"
        ;;
    7)
        echo "🗑️  Destroying everything except storage..."
        terraform destroy -auto-approve -var="skip_storage=true"
        echo "✅ All resources except storage destroyed!"
        ;;
    8)
        echo "🎯 Custom resource selection:"
        echo "Enter resource types to destroy (space-separated):"
        echo "Example: aws_ecs_service.main aws_lb.main"
        read -p "Resources: " resources
        
        for resource in $resources; do
            echo "🗑️  Destroying $resource..."
            terraform destroy -target="$resource" -auto-approve
        done
        echo "✅ Custom resources destroyed!"
        ;;
    9)
        show_resources
        ;;
    0)
        echo "👋 Exiting..."
        exit 0
        ;;
    *)
        echo "❌ Invalid choice. Please run the script again."
        exit 1
        ;;
esac

echo ""
echo "📊 Updated resource list:"
show_resources

echo ""
echo "✅ Selective teardown completed!"
echo "💡 Run 'terraform plan' to see what changes remain."
