#!/bin/bash

# A script to create a Redshift cluster, including necessary prerequisites,
# and then clean up all resources using AWS CLI.

# --- Configuration ---
REGION="us-east-1"
CLUSTER_IDENTIFIER="my-cli-redshift-cluster"
MASTER_USERNAME="admin"
MASTER_USER_PASSWORD="MySecurePassword123!" # !!! IMPORTANT: Use a strong password in production !!!
NODE_TYPE="dc2.large"
NUMBER_OF_NODES=1 # Single node for demo
DB_NAME="dev"
CLUSTER_SUBNET_GROUP_NAME="my-cli-redshift-subnet-group"
SECURITY_GROUP_NAME="my-cli-redshift-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 Cluster Subnet Group ---
echo -e "\n--- Creating Cluster Subnet Group: $CLUSTER_SUBNET_GROUP_NAME ---"
aws redshift create-cluster-subnet-group \
  --cluster-subnet-group-name $CLUSTER_SUBNET_GROUP_NAME \
  --description "Subnet group for CLI Redshift cluster" \
  --subnet-ids $SUBNET_IDS \
  --region $REGION

echo "Cluster Subnet Group created."

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

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

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

# --- 4. Create Redshift Cluster ---
echo -e "\n--- Creating Redshift Cluster: $CLUSTER_IDENTIFIER ---"
aws redshift create-cluster \
  --cluster-identifier $CLUSTER_IDENTIFIER \
  --node-type $NODE_TYPE \
  --number-of-nodes $NUMBER_OF_NODES \
  --master-username $MASTER_USERNAME \
  --master-user-password $MASTER_USER_PASSWORD \
  --db-name $DB_NAME \
  --cluster-subnet-group-name $CLUSTER_SUBNET_GROUP_NAME \
  --vpc-security-group-ids $SG_ID \
  --publicly-accessible \
  --tags Key=Name,Value=$CLUSTER_IDENTIFIER \
  --region $REGION

echo "Redshift cluster '$CLUSTER_IDENTIFIER' created. Waiting for it to be available (this can take 10-15 minutes)..."
aws redshift wait cluster-available \
  --cluster-identifier $CLUSTER_IDENTIFIER \
  --region $REGION

echo "Redshift cluster is available."

# --- 5. Output Cluster Endpoint ---
CLUSTER_ENDPOINT=$(aws redshift describe-clusters \
  --cluster-identifier $CLUSTER_IDENTIFIER \
  --query 'Clusters[0].Endpoint.Address' \
  --region $REGION \
  --output text)

echo -e "\n--- Redshift Cluster Created Successfully! ---"
echo "Cluster Identifier: $CLUSTER_IDENTIFIER"
echo "Endpoint: $CLUSTER_ENDPOINT"
echo "Database Name: $DB_NAME"
echo "Username: $MASTER_USERNAME"
echo "Password: $MASTER_USER_PASSWORD" # !!! IMPORTANT: Do not expose in production !!!

read -p "Press Enter to delete the Redshift cluster and clean up resources..."

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

# Delete Redshift Cluster
echo "Deleting Redshift cluster '$CLUSTER_IDENTIFIER' நான"
aws redshift delete-cluster \
  --cluster-identifier $CLUSTER_IDENTIFIER \
  --skip-final-cluster-snapshot \
  --region $REGION

echo "Waiting for Redshift cluster to be deleted..."
aws redshift wait cluster-deleted \
  --cluster-identifier $CLUSTER_IDENTIFIER \
  --region $REGION

echo "Redshift cluster deleted."

# Delete Cluster Subnet Group
echo "Deleting Cluster Subnet Group '$CLUSTER_SUBNET_GROUP_NAME' நான"
aws redshift delete-cluster-subnet-group \
  --cluster-subnet-group-name $CLUSTER_SUBNET_GROUP_NAME \
  --region $REGION

echo "Cluster 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--- Redshift cluster demonstration and cleanup complete ---"
