#!/bin/bash
# Load Azure environment variables from .env file
# Usage: source infra/scripts/azure-load-env.sh

set -e

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(cd "$SCRIPT_DIR/../.." && pwd)"
ENV_FILE="$PROJECT_ROOT/.env"
TERRAFORM_ENV_FILE="$PROJECT_ROOT/infra/terraform/.env"

echo "🔧 Loading Azure environment variables..."

# Check for .env file in project root
if [ -f "$ENV_FILE" ]; then
  echo "Loading from: $ENV_FILE"
  set -a
  source "$ENV_FILE"
  set +a
elif [ -f "$TERRAFORM_ENV_FILE" ]; then
  echo "Loading from: $TERRAFORM_ENV_FILE"
  set -a
  source "$TERRAFORM_ENV_FILE"
  set +a
else
  echo "⚠️  No .env file found. Looking for:"
  echo "   - $ENV_FILE"
  echo "   - $TERRAFORM_ENV_FILE"
  echo ""
  echo "Creating example file..."
  cp "$PROJECT_ROOT/infra/terraform/.env.example" "$TERRAFORM_ENV_FILE"
  echo "✅ Created $TERRAFORM_ENV_FILE"
  echo "Please edit it with your Azure credentials and run this script again."
  return 1
fi

# Map AZURE_* to ARM_* if needed (for Terraform compatibility)
if [ -n "$AZURE_SUBSCRIPTION_ID" ] && [ -z "$ARM_SUBSCRIPTION_ID" ]; then
  export ARM_SUBSCRIPTION_ID="$AZURE_SUBSCRIPTION_ID"
fi

if [ -n "$AZURE_TENANT_ID" ] && [ -z "$ARM_TENANT_ID" ]; then
  export ARM_TENANT_ID="$AZURE_TENANT_ID"
fi

if [ -n "$AZURE_LOCATION" ] && [ -z "$ARM_LOCATION" ]; then
  export ARM_LOCATION="$AZURE_LOCATION"
fi

# Validate required variables (check both ARM_* and AZURE_*)
SUBSCRIPTION_ID="${ARM_SUBSCRIPTION_ID:-$AZURE_SUBSCRIPTION_ID}"
TENANT_ID="${ARM_TENANT_ID:-$AZURE_TENANT_ID}"

MISSING_VARS=()

if [ -z "$SUBSCRIPTION_ID" ]; then
  MISSING_VARS+=("ARM_SUBSCRIPTION_ID or AZURE_SUBSCRIPTION_ID")
fi

if [ -z "$TENANT_ID" ]; then
  MISSING_VARS+=("ARM_TENANT_ID or AZURE_TENANT_ID")
fi

if [ ${#MISSING_VARS[@]} -gt 0 ]; then
  echo "❌ Missing required environment variables:"
  for var in "${MISSING_VARS[@]}"; do
    echo "   - $var"
  done
  echo ""
  echo "Please set these in your .env file."
  return 1
fi

# Set Terraform variables from environment (use mapped values)
export TF_VAR_subscription_id="${SUBSCRIPTION_ID}"
export TF_VAR_tenant_id="${TENANT_ID}"
export TF_VAR_client_id="${ARM_CLIENT_ID:-$AZURE_CLIENT_ID:-}"
export TF_VAR_client_secret="${ARM_CLIENT_SECRET:-$AZURE_CLIENT_SECRET:-}"

# Set Azure CLI defaults if using CLI auth
if [ -z "$ARM_CLIENT_ID" ] && [ -z "$AZURE_CLIENT_ID" ]; then
  echo "ℹ️  Using Azure CLI authentication (no service principal set)"
  az account set --subscription "$SUBSCRIPTION_ID" 2>/dev/null || true
fi

echo "✅ Environment variables loaded"
echo ""
echo "Azure Configuration:"
echo "  Subscription ID: ${SUBSCRIPTION_ID:0:8}...${SUBSCRIPTION_ID: -4}"
echo "  Tenant ID: ${TENANT_ID:0:8}...${TENANT_ID: -4}"
echo "  Location: ${ARM_LOCATION:-${AZURE_LOCATION:-westeurope}}"
echo "  Environment: ${TF_VAR_environment:-dev}"
if [ -n "$AZURE_MANAGEMENT_GROUP_ID" ]; then
  echo "  Management Group: $AZURE_MANAGEMENT_GROUP_ID"
fi
echo ""
echo "You can now run Terraform commands."

