#!/bin/bash

# Deploy specific environment for Developer Portfolio Tracker
# Usage: ./deploy.sh [environment] [component]
# Environment: dev, staging, prod
# Component: infrastructure, services, ingress, hpa, monitoring, all (default)

set -e

ENVIRONMENT=${1:-prod}
COMPONENT=${2:-all}
NAMESPACE="portfolio-${ENVIRONMENT}"

echo "🚀 Deploying ${COMPONENT} to ${ENVIRONMENT} environment..."

# Validate environment
if [[ ! "${ENVIRONMENT}" =~ ^(dev|staging|prod)$ ]]; then
    echo "❌ Invalid environment. Use: dev, staging, or prod"
    exit 1
fi

# Check if kubectl is available
if ! command -v kubectl &> /dev/null; then
    echo "❌ kubectl is not installed or not in PATH"
    exit 1
fi

# Function to deploy infrastructure
deploy_infrastructure() {
    echo "🏗️  Deploying infrastructure..."
    kubectl apply -f ../deployments/infrastructure/
    kubectl apply -f ../services/infrastructure-services.yaml
    
    # Wait for infrastructure to be ready
    kubectl wait --for=condition=available --timeout=300s deployment/mysql -n ${NAMESPACE}
    kubectl wait --for=condition=available --timeout=300s deployment/mongodb -n ${NAMESPACE}
    kubectl wait --for=condition=available --timeout=300s deployment/redis -n ${NAMESPACE}
    kubectl wait --for=condition=available --timeout=300s deployment/kafka -n ${NAMESPACE}
}

# Function to deploy services
deploy_services() {
    echo "🚀 Deploying application services..."
    kubectl apply -f ../deployments/services/
    kubectl apply -f ../services/application-services.yaml
    
    # Wait for services to be ready
    kubectl wait --for=condition=available --timeout=300s deployment/user-service -n ${NAMESPACE}
    kubectl wait --for=condition=available --timeout=300s deployment/task-service -n ${NAMESPACE}
    kubectl wait --for=condition=available --timeout=300s deployment/analytics-service -n ${NAMESPACE}
    kubectl wait --for=condition=available --timeout=300s deployment/diagram-service -n ${NAMESPACE}
    kubectl wait --for=condition=available --timeout=300s deployment/api-gateway -n ${NAMESPACE}
    kubectl wait --for=condition=available --timeout=300s deployment/frontend -n ${NAMESPACE}
}

# Function to deploy ingress
deploy_ingress() {
    echo "🌐 Deploying ingress..."
    kubectl apply -f ../ingress/ingress.yaml
}

# Function to deploy HPA
deploy_hpa() {
    if [[ "${ENVIRONMENT}" == "dev" ]]; then
        echo "⚠️  Skipping HPA for development environment"
        return
    fi
    echo "📈 Deploying Horizontal Pod Autoscalers..."
    kubectl apply -f ../hpa/hpa.yaml
}

# Function to deploy monitoring
deploy_monitoring() {
    if kubectl get crd servicemonitors.monitoring.coreos.com &> /dev/null; then
        echo "📊 Deploying monitoring..."
        kubectl apply -f ../monitoring/
    else
        echo "⚠️  Prometheus CRDs not found, skipping monitoring"
    fi
}

# Ensure namespace exists
kubectl apply -f ../namespaces/namespaces.yaml

# Apply ConfigMaps
kubectl apply -f ../configmaps/app-config.yaml

# Deploy based on component
case ${COMPONENT} in
    infrastructure)
        deploy_infrastructure
        ;;
    services)
        deploy_services
        ;;
    ingress)
        deploy_ingress
        ;;
    hpa)
        deploy_hpa
        ;;
    monitoring)
        deploy_monitoring
        ;;
    all)
        deploy_infrastructure
        deploy_services
        deploy_ingress
        deploy_hpa
        deploy_monitoring
        ;;
    *)
        echo "❌ Invalid component. Use: infrastructure, services, ingress, hpa, monitoring, or all"
        exit 1
        ;;
esac

echo "✅ Deployment completed successfully!"
echo ""
echo "📋 Current status:"
kubectl get pods -n ${NAMESPACE}
echo ""
kubectl get services -n ${NAMESPACE}