#!/bin/bash

ALL_REGIONS=$(aws ec2 describe-regions --query 'Regions[].RegionName' --output text --region us-east-1)

echo "Checking for region where StackSets are deployed..."

for REGION in $ALL_REGIONS; do
  echo "Checking region: $REGION"
  STDERR_OUTPUT=$(aws cloudformation list-stack-instances \
  --stack-set-name "AIStrikeInfraAccessRoleOrgStackSet" \
  --region "$REGION" 2>&1 1>/dev/null)

  if [[ $? -eq 0 ]]; then
    echo "Found StackSets in region: $REGION"

    echo "Updating AIStrikeInfraAccessRoleOrgStackSet..."
    aws cloudformation update-stack-set \
      --stack-set-name "AIStrikeInfraAccessRoleOrgStackSet" \
      --auto-deployment Enabled=true,RetainStacksOnAccountRemoval=false \
      --use-previous-template \
      --capabilities CAPABILITY_NAMED_IAM \
      --operation-preferences RegionConcurrencyType=PARALLEL,ConcurrencyMode=SOFT_FAILURE_TOLERANCE,MaxConcurrentPercentage=100 \
      --region "$REGION"
    echo "Done."

    echo "Updating AIStrikeInfraSecurityAuditOrgStackSet..."
    aws cloudformation update-stack-set \
      --stack-set-name "AIStrikeInfraSecurityAuditOrgStackSet" \
      --auto-deployment Enabled=true,RetainStacksOnAccountRemoval=false \
      --use-previous-template \
      --capabilities CAPABILITY_NAMED_IAM \
      --operation-preferences RegionConcurrencyType=PARALLEL,ConcurrencyMode=SOFT_FAILURE_TOLERANCE,MaxConcurrentPercentage=100 \
      --region "$REGION"

    echo "Done."


    echo "Creating Stacks instances in new accounts..." 
    STACKSET_JSON=$(aws cloudformation describe-stack-set --stack-set-name "AIStrikeInfraAccessRoleOrgStackSet" --region "$REGION")

    OU_ID=$(echo "$STACKSET_JSON" | jq -r '.StackSet.OrganizationalUnitIds[0]')
    REGIONS=$(echo "$STACKSET_JSON" | jq -r '.StackSet.Regions[]' | paste -sd " " -)

    echo "OU ID: $OU_ID"
    echo "Region for AIStrikeInfraAccessRoleOrgStackSet: $REGIONS"

    aws cloudformation create-stack-instances \
      --stack-set-name "AIStrikeInfraAccessRoleOrgStackSet" \
      --deployment-targets OrganizationalUnitIds=$OU_ID \
      --regions $REGIONS \
      --operation-preferences RegionConcurrencyType=PARALLEL,ConcurrencyMode=SOFT_FAILURE_TOLERANCE,MaxConcurrentPercentage=100 \
      --region "$REGION"

    STACKSET_JSON=$(aws cloudformation describe-stack-set --stack-set-name "AIStrikeInfraSecurityAuditOrgStackSet" --region "$REGION")

    OU_ID=$(echo "$STACKSET_JSON" | jq -r '.StackSet.OrganizationalUnitIds[0]')
    REGIONS=$(echo "$STACKSET_JSON" | jq -r '.StackSet.Regions[]' | paste -sd " " -)

    echo "OU ID: $OU_ID"
    echo "Regions for AIStrikeInfraSecurityAuditOrgStackSet: $REGIONS"

    aws cloudformation create-stack-instances \
      --stack-set-name "AIStrikeInfraSecurityAuditOrgStackSet" \
      --deployment-targets OrganizationalUnitIds=$OU_ID \
      --regions $REGIONS \
      --operation-preferences RegionConcurrencyType=PARALLEL,ConcurrencyMode=SOFT_FAILURE_TOLERANCE,MaxConcurrentPercentage=100 \
      --region "$REGION"

    exit 0

  elif echo "$STDERR_OUTPUT" | grep -q "StackSetNotFoundException"; then
    echo "StackSets not found in $REGION skipping..."

  else
    echo "Unexpected error:"
    echo "$STDERR_OUTPUT"
    exit 1
  fi
done
