#!/bin/bash
# Kubernetes deployment script for MerchantsOfHope application

set -e  # Exit on any error

echo "Starting deployment of MerchantsOfHope application..."

# Create namespace
echo "Creating namespace..."
kubectl apply -f k8s/namespace.yaml

# Apply secrets (these should be properly base64 encoded in production)
echo "Applying secrets..."
# Note: In production, manage secrets more securely (HashiCorp Vault, AWS Secrets Manager, etc.)
kubectl apply -f k8s/secrets.yaml

# Apply configmap
echo "Applying configmap..."
kubectl apply -f k8s/configmap.yaml

# Apply database components (optional - in production, consider using a managed database)
echo "Applying database components..."
kubectl apply -f k8s/database.yaml

# Wait for database to be ready
echo "Waiting for database to be ready..."
kubectl wait --for=condition=ready pod -l app=postgres --timeout=120s

# Apply application deployment
echo "Applying application deployment..."
kubectl apply -f k8s/deployment.yaml

# Apply service
echo "Applying service..."
kubectl apply -f k8s/service.yaml

# Apply ingress
echo "Applying ingress..."
kubectl apply -f k8s/ingress.yaml

echo "Deployment completed successfully!"
echo "To check the status of your deployment, run:"
echo "  kubectl get pods -n merchants-of-hope"
echo "  kubectl get services -n merchants-of-hope"
echo "  kubectl get ingress -n merchants-of-hope"