#!/bin/bash

# A script to create a basic MySQL RDS instance, including necessary prerequisites,
# and then clean up all resources using AWS CLI.

# --- Configuration ---
REGION="us-east-1"
DB_INSTANCE_IDENTIFIER="my-cli-mysql-instance"
MASTER_USERNAME="admin"
MASTER_USER_PASSWORD="MySecurePassword123!" # !!! IMPORTANT: Use a strong password in production !!!
ALLOCATED_STORAGE=20 # GB
DB_INSTANCE_CLASS="db.t3.micro"
ENGINE="mysql"
ENGINE_VERSION="8.0.28"
DB_SUBNET_GROUP_NAME="my-cli-db-subnet-group"
SECURITY_GROUP_NAME="my-cli-rds-sg"

# --- 1. Get Default VPC and Subnets ---
echo "--- Getting Default VPC and Subnet IDs ---"
VPC_ID=$(aws ec2 describe-vpcs \
  --filters "Name=is-default,Values=true" \
  --query "Vpcs[0].VpcId" \
  --region $REGION \
  --output text)

if [ -z "$VPC_ID" ]; then
  echo "Error: Could not find a default VPC. Exiting."
  exit 1
fi
echo "Default VPC ID: $VPC_ID"

# Get at least two subnet IDs in different AZs within the default VPC
SUBNET_IDS=$(aws ec2 describe-subnets \
  --filters "Name=vpc-id,Values=$VPC_ID" "Name=default-for-az,Values=true" \
  --query "Subnets[0:2].SubnetId" \
  --region $REGION \
  --output text)

if [ -z "$SUBNET_IDS" ]; then
  echo "Error: Could not find enough default subnets. Exiting."
  exit 1
fi
echo "Default Subnet IDs: $SUBNET_IDS"

# --- 2. Create DB Subnet Group ---
echo -e "\n--- Creating DB Subnet Group: $DB_SUBNET_GROUP_NAME ---"
aws rds create-db-subnet-group \
  --db-subnet-group-name $DB_SUBNET_GROUP_NAME \
  --db-subnet-group-description "Subnet group for CLI RDS instance" \
  --subnet-ids $SUBNET_IDS \
  --region $REGION

echo "DB Subnet Group created."

# --- 3. Create Security Group for RDS ---
echo -e "\n--- Creating Security Group: $SECURITY_GROUP_NAME ---"
SG_ID=$(aws ec2 create-security-group \
  --group-name $SECURITY_GROUP_NAME \
  --description "Allow MySQL traffic for RDS" \
  --vpc-id $VPC_ID \
  --region $REGION \
  --query 'GroupId' --output text)

# Authorize MySQL (port 3306) from anywhere (for simplicity, restrict in production)
aws ec2 authorize-security-group-ingress \
  --group-id $SG_ID \
  --protocol tcp \
  --port 3306 \
  --cidr 0.0.0.0/0 \
  --region $REGION

echo "Security Group '$SECURITY_GROUP_NAME' created with ID: $SG_ID"

# --- 4. Create RDS Instance ---
echo -e "\n--- Creating RDS Instance: $DB_INSTANCE_IDENTIFIER ---"
aws rds create-db-instance \
  --db-instance-identifier $DB_INSTANCE_IDENTIFIER \
  --db-instance-class $DB_INSTANCE_CLASS \
  --engine $ENGINE \
  --master-username $MASTER_USERNAME \
  --master-user-password $MASTER_USER_PASSWORD \
  --allocated-storage $ALLOCATED_STORAGE \
  --db-subnet-group-name $DB_SUBNET_GROUP_NAME \
  --vpc-security-group-ids $SG_ID \
  --engine-version $ENGINE_VERSION \
  --publicly-accessible \
  --tags Key=Name,Value=$DB_INSTANCE_IDENTIFIER \
  --region $REGION

echo "RDS instance '$DB_INSTANCE_IDENTIFIER' created. Waiting for it to be available..."
aws rds wait db-instance-available \
  --db-instance-identifier $DB_INSTANCE_IDENTIFIER \
  --region $REGION

echo "RDS instance is available."

# --- 5. Output Endpoint ---
DB_ENDPOINT=$(aws rds describe-db-instances \
  --db-instance-identifier $DB_INSTANCE_IDENTIFIER \
  --query 'DBInstances[0].Endpoint.Address' \
  --region $REGION \
  --output text)

echo -e "\n--- RDS Instance Created Successfully! ---"
echo "DB Instance Identifier: $DB_INSTANCE_IDENTIFIER"
echo "Endpoint: $DB_ENDPOINT"
echo "Username: $MASTER_USERNAME"
echo "Password: $MASTER_USER_PASSWORD" # !!! IMPORTANT: Do not expose in production !!!

read -p "Press Enter to delete the RDS instance and clean up resources..."

# --- Clean Up ---
echo -e "\n--- Cleaning up resources ---"

# Delete RDS Instance
echo "Deleting RDS instance '$DB_INSTANCE_IDENTIFIER' நான"
aws rds delete-db-instance \
  --db-instance-identifier $DB_INSTANCE_IDENTIFIER \
  --skip-final-snapshot \
  --delete-automated-backups \
  --region $REGION

echo "Waiting for RDS instance to be deleted..."
aws rds wait db-instance-deleted \
  --db-instance-identifier $DB_INSTANCE_IDENTIFIER \
  --region $REGION

echo "RDS instance deleted."

# Delete DB Subnet Group
echo "Deleting DB Subnet Group '$DB_SUBNET_GROUP_NAME' நான"
aws rds delete-db-subnet-group \
  --db-subnet-group-name $DB_SUBNET_GROUP_NAME \
  --region $REGION

echo "DB Subnet Group deleted."

# Delete Security Group
echo "Deleting Security Group '$SECURITY_GROUP_NAME' நான"
aws ec2 delete-security-group \
  --group-id $SG_ID \
  --region $REGION

echo "Security Group deleted."

echo -e "\n--- RDS instance demonstration and cleanup complete ---"
