#!/bin/bash
# Deploy environment variables to test111.nxtgauge.com
# This script updates the Kubernetes ConfigMap or Docker environment

set -e

echo "=== Nxtgauge Test111 Environment Setup ==="
echo ""

# Configuration
NAMESPACE="nxtgauge"
CONFIGMAP_NAME="nxtgauge-config"
SECRET_NAME="nxtgauge-secrets"
DEPLOYMENT_NAME="nxtgauge-rust-gateway"

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

echo -e "${YELLOW}Step 1: Checking kubectl access...${NC}"
if ! kubectl cluster-info > /dev/null 2>&1; then
    echo -e "${RED}Error: Cannot connect to Kubernetes cluster${NC}"
    echo "Please ensure:"
    echo "  1. kubectl is installed"
    echo "  2. You have access to the test111 cluster"
    echo "  3. Your kubeconfig is properly set up"
    exit 1
fi
echo -e "${GREEN}✓ Kubernetes cluster accessible${NC}"

echo ""
echo -e "${YELLOW}Step 2: Checking namespace...${NC}"
if ! kubectl get namespace "$NAMESPACE" > /dev/null 2>&1; then
    echo -e "${RED}Error: Namespace '$NAMESPACE' not found${NC}"
    exit 1
fi
echo -e "${GREEN}✓ Namespace '$NAMESPACE' exists${NC}"

echo ""
echo -e "${YELLOW}Step 3: Creating/updating ConfigMap with SMTP settings...${NC}"

# Create ConfigMap with SMTP configuration
cat <<EOF | kubectl apply -f -
apiVersion: v1
kind: ConfigMap
metadata:
  name: $CONFIGMAP_NAME
  namespace: $NAMESPACE
data:
  EMAIL_PROVIDER: "SMTP"
  SMTP_HOST: "smtp.zeptomail.in"
  SMTP_PORT: "587"
  SMTP_USER: "emailapikey"
  SMTP_FROM_EMAIL: "support@nxtgauge.com"
  SMTP_FROM_NAME: "NXTGAUGE"
  SMTP_SECURE: "true"
  FRONTEND_URL: "https://test111.nxtgauge.com"
  ADMIN_URL: "https://admin.test111.nxtgauge.com"
EOF

echo -e "${GREEN}✓ ConfigMap updated${NC}"

echo ""
echo -e "${YELLOW}Step 4: Creating/updating Secret with SMTP credentials...${NC}"

# ZeptoMail credentials
SMTP_PASS="PHtE6r1ZR+zi3jV88RNW4/O4F8CkPdksqO9iJAhA4YcTD6dQFk1S+dl/wDC3/h97AKYWFfSczo1rt72etOuDLTnrMjlEDWqyqK3sx/VYSPOZsbq6x00esVgYdEfYVYDpcNFj3SPQut7dNA=="

# Prompt for Demo account emails
read -p "Enter Demo Account Emails (comma-separated, press Enter for default): " DEMO_EMAILS
DEMO_EMAILS=${DEMO_EMAILS:-"test@example.com,demo@nxtgauge.com"}

# Create Secret
kubectl create secret generic "$SECRET_NAME" \
  --namespace="$NAMESPACE" \
  --from-literal=SMTP_PASS="$SMTP_PASS" \
  --from-literal=DEMO_ACCOUNT_EMAILS="$DEMO_EMAILS" \
  --dry-run=client -o yaml | kubectl apply -f -

echo "✓ SMTP credentials configured (ZeptoMail)"

echo -e "${GREEN}✓ Secret updated${NC}"

echo ""
echo -e "${YELLOW}Step 5: Restarting deployment to apply changes...${NC}"
kubectl rollout restart deployment/$DEPLOYMENT_NAME -n $NAMESPACE
echo -e "${GREEN}✓ Deployment restarted${NC}"

echo ""
echo -e "${YELLOW}Step 6: Waiting for rollout to complete...${NC}"
if kubectl rollout status deployment/$DEPLOYMENT_NAME -n $NAMESPACE --timeout=120s; then
    echo -e "${GREEN}✓ Rollout completed successfully${NC}"
else
    echo -e "${RED}Warning: Rollout may not have completed within timeout${NC}"
fi

echo ""
echo "=== Setup Complete ==="
echo ""
echo "Email verification is now: ENABLED"
echo "SMTP Provider: ZeptoMail (smtp.zeptomail.in)"
echo "Demo Accounts (bypass verification): $DEMO_EMAILS"
echo ""
echo "To verify email is working, check logs:"
echo "  kubectl logs -f deployment/$DEPLOYMENT_NAME -n $NAMESPACE | grep -i email"
