#!/bin/bash

# Bakery-IA Infrastructure Setup Script
# This script applies infrastructure resources in the correct dependency order

set -e  # Exit on error

echo "🚀 Starting Bakery-IA infrastructure setup..."

# Step 1: Apply namespaces first (they must exist before other resources)
echo "📦 Creating namespaces..."
kubectl apply -f infrastructure/namespaces/

# Step 2: Apply common configurations (depends on bakery-ia namespace)
echo "🔧 Applying common configurations..."
kubectl apply -f infrastructure/environments/common/configs/

# Step 3: Apply platform components
echo "🖥️  Applying platform components..."
kubectl apply -f infrastructure/platform/

# Step 4: Apply CI/CD components (depends on tekton-pipelines and flux-system namespaces)
echo "🔄 Applying CI/CD components..."
kubectl apply -f infrastructure/cicd/

# Step 5: Apply monitoring components
echo "📊 Applying monitoring components..."
kubectl apply -f infrastructure/monitoring/

echo "✅ Infrastructure setup completed successfully!"

# Verify namespaces
echo "🔍 Verifying namespaces..."
kubectl get namespaces | grep -E "(bakery-ia|tekton-pipelines|flux-system)"

echo "🎉 All infrastructure components have been deployed."