#!/bin/bash

# Adopt-a-Street Kubernetes Deployment Script
# This script deploys the Adopt-a-Street application to a Kubernetes cluster

set -e  # Exit on error

# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color

# Configuration
NAMESPACE="adopt-a-street"
DEPLOY_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
K8S_DIR="${DEPLOY_DIR}/k8s"

echo -e "${GREEN}🚀 Adopt-a-Street Kubernetes Deployment${NC}"
echo "================================================"
echo ""

# Check if kubectl is installed
if ! command -v kubectl &> /dev/null; then
    echo -e "${RED}❌ kubectl not found. Please install kubectl first.${NC}"
    exit 1
fi

# Check if secrets.yaml exists
if [ ! -f "${K8S_DIR}/secrets.yaml" ]; then
    echo -e "${RED}❌ secrets.yaml not found!${NC}"
    echo -e "${YELLOW}Please copy secrets.yaml.example to secrets.yaml and fill in your secrets:${NC}"
    echo "  cp ${K8S_DIR}/secrets.yaml.example ${K8S_DIR}/secrets.yaml"
    echo "  nano ${K8S_DIR}/secrets.yaml"
    exit 1
fi

echo -e "${GREEN}✓${NC} Prerequisites check passed"
echo ""

# Function to wait for pods
wait_for_pods() {
    local label=$1
    local timeout=${2:-120}
    echo -e "${YELLOW}⏳ Waiting for pods with label ${label} to be ready...${NC}"
    kubectl wait --for=condition=ready pod -l "${label}" -n "${NAMESPACE}" --timeout="${timeout}s" || {
        echo -e "${RED}❌ Timeout waiting for pods${NC}"
        kubectl get pods -n "${NAMESPACE}" -l "${label}"
        return 1
    }
    echo -e "${GREEN}✓${NC} Pods ready"
}

# Create namespace
echo "📦 Creating namespace..."
kubectl apply -f "${K8S_DIR}/namespace.yaml"
echo -e "${GREEN}✓${NC} Namespace created"
echo ""

# Apply secrets
echo "🔐 Applying secrets..."
kubectl apply -f "${K8S_DIR}/secrets.yaml"
echo -e "${GREEN}✓${NC} Secrets applied"
echo ""

# Apply configmap
echo "⚙️  Applying ConfigMap..."
kubectl apply -f "${K8S_DIR}/configmap.yaml"
echo -e "${GREEN}✓${NC} ConfigMap applied"
echo ""

# Deploy MongoDB
echo "🗄️  Deploying MongoDB..."
kubectl apply -f "${K8S_DIR}/mongodb-statefulset.yaml"
wait_for_pods "app=mongodb" 180
echo -e "${GREEN}✓${NC} MongoDB deployed"
echo ""

# Deploy backend
echo "🔧 Deploying backend..."
kubectl apply -f "${K8S_DIR}/backend-deployment.yaml"
wait_for_pods "app=backend" 120
echo -e "${GREEN}✓${NC} Backend deployed"
echo ""

# Deploy frontend
echo "🎨 Deploying frontend..."
kubectl apply -f "${K8S_DIR}/frontend-deployment.yaml"
wait_for_pods "app=frontend" 120
echo -e "${GREEN}✓${NC} Frontend deployed"
echo ""

# Deploy ingress
echo "🌐 Deploying ingress..."
kubectl apply -f "${K8S_DIR}/ingress.yaml"
echo -e "${GREEN}✓${NC} Ingress deployed"
echo ""

# Show deployment status
echo "================================================"
echo -e "${GREEN}✅ Deployment Complete!${NC}"
echo "================================================"
echo ""

echo "📊 Current Status:"
kubectl get all -n "${NAMESPACE}"
echo ""

echo "🌐 Ingress:"
kubectl get ingress -n "${NAMESPACE}"
echo ""

echo -e "${YELLOW}📝 Next Steps:${NC}"
echo "1. Check pod logs:"
echo "   kubectl logs -f deployment/adopt-a-street-backend -n ${NAMESPACE}"
echo ""
echo "2. Access the application through your ingress URL"
echo ""
echo "3. Or port-forward for testing:"
echo "   kubectl port-forward svc/adopt-a-street-frontend 3000:80 -n ${NAMESPACE}"
echo "   Then open http://localhost:3000"
echo ""
echo "4. Monitor resources:"
echo "   kubectl top pods -n ${NAMESPACE}"
echo ""

echo -e "${GREEN}🎉 Happy deploying!${NC}"
