#!/bin/bash

# Test script to validate the Mailu Helm installation
# This script verifies that the configuration files are properly set up

set -e  # Exit on any error

echo "🔍 Validating Mailu Helm configuration..."

# Check if required files exist
REQUIRED_FILES=(
    "infrastructure/platform/mail/mailu-helm/values.yaml"
    "infrastructure/platform/mail/mailu-helm/dev/values.yaml"
    "infrastructure/platform/mail/mailu-helm/prod/values.yaml"
    "infrastructure/platform/mail/mailu-helm/README.md"
    "infrastructure/platform/mail/mailu-helm/MIGRATION_GUIDE.md"
    "infrastructure/platform/mail/mailu-helm/mailu-ingress.yaml"
)

echo "✅ Checking required files..."
for file in "${REQUIRED_FILES[@]}"; do
    if [[ -f "$file" ]]; then
        echo "   Found: $file"
    else
        echo "   ❌ Missing: $file"
        exit 1
    fi
done

# Validate YAML syntax
echo "✅ Validating YAML syntax..."
for yaml_file in "${REQUIRED_FILES[@]}"; do
    if [[ "$yaml_file" == *.yaml ]]; then
        echo "   Validating: $yaml_file"
        if python3 -c "import yaml; yaml.safe_load(open('$yaml_file'))" 2>/dev/null; then
            echo "      ✓ Valid YAML"
        else
            echo "      ❌ Invalid YAML"
            exit 1
        fi
    fi
done

# Check if Tiltfile was updated
echo "✅ Checking Tiltfile update..."
if grep -q "mailu-helm" Tiltfile; then
    echo "   ✓ Tiltfile contains mailu-helm manual trigger"
else
    echo "   ❌ Tiltfile does not contain mailu-helm manual trigger"
    exit 1
fi

# Check if configmap was updated
echo "✅ Checking configmap update..."
if grep -q "mailu-postfix.bakery-ia.svc.cluster.local" infrastructure/environments/common/configs/configmap.yaml; then
    echo "   ✓ ConfigMap updated with new SMTP host"
else
    echo "   ❌ ConfigMap not updated with new SMTP host"
    exit 1
fi

# Check if Signoz config was updated
echo "✅ Checking Signoz configuration update..."
if grep -q "mailu-postfix.bakery-ia.svc.cluster.local" infrastructure/monitoring/signoz/signoz-values-prod.yaml; then
    echo "   ✓ Signoz configuration updated with new SMTP host"
else
    echo "   ❌ Signoz configuration not updated with new SMTP host"
    exit 1
fi

echo ""
echo "🎉 Validation completed successfully!"
echo ""
echo "📋 Summary of changes made:"
echo "   • Created Helm values files for base, dev, and prod environments"
echo "   • Updated Tiltfile with manual trigger for Mailu Helm deployment"
echo "   • Updated common ConfigMap with new SMTP service name"
echo "   • Updated Signoz configuration with new SMTP service name"
echo "   • Created documentation and migration guide"
echo ""
echo "🚀 To deploy Mailu via Helm, use the manual trigger in Tilt:"
echo "   tilt trigger mailu-helm"
echo ""
echo "   Or deploy directly with Helm:"
echo "   helm upgrade --install mailu mailu/mailu \\"
echo "     --namespace bakery-ia \\"
echo "     --create-namespace \\"
echo "     -f infrastructure/platform/mail/mailu-helm/values.yaml \\"
echo "     -f infrastructure/platform/mail/mailu-helm/dev/values.yaml"