#!/bin/bash

# Production Deployment Script
# This script handles the complete production deployment process

set -e

echo "🚀 Starting Production Deployment..."

# Configuration
NAMESPACE=${NAMESPACE:-sankofa}
ENVIRONMENT=${ENVIRONMENT:-production}
KUBECONFIG=${KUBECONFIG:-~/.kube/config}

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

# Function to print colored output
print_status() {
    echo -e "${GREEN}✓${NC} $1"
}

print_error() {
    echo -e "${RED}✗${NC} $1"
}

print_warning() {
    echo -e "${YELLOW}⚠${NC} $1"
}

# Pre-deployment checks
echo "📋 Running pre-deployment checks..."

# Check kubectl
if ! command -v kubectl &> /dev/null; then
    print_error "kubectl is not installed"
    exit 1
fi
print_status "kubectl found"

# Check Kubernetes connection
if ! kubectl cluster-info &> /dev/null; then
    print_error "Cannot connect to Kubernetes cluster"
    exit 1
fi
print_status "Kubernetes cluster accessible"

# Check namespace
if ! kubectl get namespace $NAMESPACE &> /dev/null; then
    print_warning "Namespace $NAMESPACE does not exist, creating..."
    kubectl create namespace $NAMESPACE
fi
print_status "Namespace $NAMESPACE ready"

# Deploy database migrations
echo "📦 Deploying database migrations..."
kubectl apply -f gitops/apps/api/migrations.yaml -n $NAMESPACE
print_status "Database migrations applied"

# Deploy API
echo "📦 Deploying API..."
kubectl apply -f gitops/apps/api/ -n $NAMESPACE
kubectl rollout status deployment/api -n $NAMESPACE --timeout=5m
print_status "API deployed"

# Deploy Frontend
echo "📦 Deploying Frontend..."
kubectl apply -f gitops/apps/frontend/ -n $NAMESPACE
kubectl rollout status deployment/frontend -n $NAMESPACE --timeout=5m
print_status "Frontend deployed"

# Deploy Portal
echo "📦 Deploying Portal..."
kubectl apply -f gitops/apps/portal/ -n $NAMESPACE
kubectl rollout status deployment/portal -n $NAMESPACE --timeout=5m
print_status "Portal deployed"

# Run smoke tests
echo "🧪 Running smoke tests..."
SMOKE_TEST_URL=${SMOKE_TEST_URL:-http://api.sankofa.nexus/health}

if curl -f $SMOKE_TEST_URL > /dev/null 2>&1; then
    print_status "Smoke tests passed"
else
    print_error "Smoke tests failed"
    exit 1
fi

# Verify deployments
echo "🔍 Verifying deployments..."
kubectl get deployments -n $NAMESPACE
kubectl get services -n $NAMESPACE
kubectl get pods -n $NAMESPACE

print_status "✅ Production deployment completed successfully!"

echo ""
echo "📊 Deployment Summary:"
echo "  - Namespace: $NAMESPACE"
echo "  - Environment: $ENVIRONMENT"
echo "  - API: $(kubectl get deployment api -n $NAMESPACE -o jsonpath='{.status.readyReplicas}')/$(kubectl get deployment api -n $NAMESPACE -o jsonpath='{.spec.replicas}') replicas"
echo "  - Frontend: $(kubectl get deployment frontend -n $NAMESPACE -o jsonpath='{.status.readyReplicas}')/$(kubectl get deployment frontend -n $NAMESPACE -o jsonpath='{.spec.replicas}') replicas"
echo "  - Portal: $(kubectl get deployment portal -n $NAMESPACE -o jsonpath='{.status.readyReplicas}')/$(kubectl get deployment portal -n $NAMESPACE -o jsonpath='{.spec.replicas}') replicas"

