#!/bin/bash

# A script to demonstrate basic CRUD (Create, Read, Update, Delete) operations
# on an Amazon DynamoDB table using AWS CLI.

# --- Configuration ---
REGION="us-east-1"
TABLE_NAME="MyCLICRUDTable"
PARTITION_KEY_NAME="Id"
PARTITION_KEY_TYPE="S" # S for String, N for Number

# --- 1. Create DynamoDB Table ---
echo "--- Creating DynamoDB Table: $TABLE_NAME ---"
aws dynamodb create-table \
  --table-name $TABLE_NAME \
  --attribute-definitions AttributeName=$PARTITION_KEY_NAME,AttributeType=$PARTITION_KEY_TYPE \
  --key-schema AttributeName=$PARTITION_KEY_NAME,KeyType=HASH \
  --billing-mode PAY_PER_REQUEST \
  --region $REGION

echo "Waiting for table to become active..."
aws dynamodb wait table-exists --table-name $TABLE_NAME --region $REGION
echo "Table $TABLE_NAME is active."

# --- 2. Put Item (Create) ---
echo -e "\n--- Putting an item into the table ---"
ITEM_ID="item001"
ITEM_NAME="Example Item"
ITEM_DESCRIPTION="This is a test item for CRUD operations."

aws dynamodb put-item \
  --table-name $TABLE_NAME \
  --item "{\"$PARTITION_KEY_NAME\": {\"$PARTITION_KEY_TYPE\": \"$ITEM_ID\"}, \"Name\": {\"S\": \"$ITEM_NAME\"}, \"Description\": {\"S\": \"$ITEM_DESCRIPTION\"}}" \
  --region $REGION

echo "Item '$ITEM_ID' added."

# --- 3. Get Item (Read) ---
echo -e "\n--- Getting the item from the table ---"
aws dynamodb get-item \
  --table-name $TABLE_NAME \
  --key "{\"$PARTITION_KEY_NAME\": {\"$PARTITION_KEY_TYPE\": \"$ITEM_ID\"}}" \
  --region $REGION \
  --output json

# --- 4. Update Item ---
echo -e "\n--- Updating the item's description ---"
NEW_DESCRIPTION="This item has been updated."

aws dynamodb update-item \
  --table-name $TABLE_NAME \
  --key "{\"$PARTITION_KEY_NAME\": {\"$PARTITION_KEY_TYPE\": \"$ITEM_ID\"}}" \
  --update-expression "SET Description = :new_desc" \
  --expression-attribute-values "{ \":new_desc\": {\"S\": \"$NEW_DESCRIPTION\"} }" \
  --return-values ALL_NEW \
  --region $REGION \
  --output json

echo "Item '$ITEM_ID' updated."

# Verify update
echo -e "\n--- Getting the updated item ---"
aws dynamodb get-item \
  --table-name $TABLE_NAME \
  --key "{\"$PARTITION_KEY_NAME\": {\"$PARTITION_KEY_TYPE\": \"$ITEM_ID\"}}" \
  --region $REGION \
  --output json

# --- 5. Delete Item ---
echo -e "\n--- Deleting the item from the table ---"
aws dynamodb delete-item \
  --table-name $TABLE_NAME \
  --key "{\"$PARTITION_KEY_NAME\": {\"$PARTITION_KEY_TYPE\": \"$ITEM_ID\"}}" \
  --region $REGION

echo "Item '$ITEM_ID' deleted."

# Verify deletion
echo -e "\n--- Attempting to get the deleted item (should return empty) ---"
aws dynamodb get-item \
  --table-name $TABLE_NAME \
  --key "{\"$PARTITION_KEY_NAME\": {\"$PARTITION_KEY_TYPE\": \"$ITEM_ID\"}}" \
  --region $REGION \
  --output json

# --- 6. Delete Table ---
echo -e "\n--- Deleting DynamoDB Table: $TABLE_NAME ---"
aws dynamodb delete-table \
  --table-name $TABLE_NAME \
  --region $REGION

echo "Waiting for table to be deleted..."
aws dynamodb wait table-not-exists --table-name $TABLE_NAME --region $REGION
echo "Table $TABLE_NAME deleted successfully."

echo -e "\n--- DynamoDB CRUD operations demonstration complete ---"
