#!/bin/bash
set -euo pipefail

echo "🔧 Installing Monitoring Stack (Staging Profile)"

# Create monitoring namespace
kubectl create namespace monitoring --dry-run=client -o yaml | kubectl apply -f -

# Add Prometheus Helm repository
helm repo add prometheus-community https://prometheus-community.github.io/helm-charts
helm repo update

# Install Prometheus Operator (lighter configuration for staging)
echo "📊 Installing Prometheus Operator (Staging)..."
helm upgrade --install prometheus-operator prometheus-community/kube-prometheus-stack \
  --namespace monitoring \
  --set prometheus.prometheusSpec.retention=7d \
  --set prometheus.prometheusSpec.storageSpec.volumeClaimTemplate.spec.resources.requests.storage=10Gi \
  --set grafana.adminPassword="${GRAFANA_ADMIN_PASSWORD:-staging123}" \
  --set alertmanager.enabled=false \
  --set prometheus.prometheusSpec.replicas=1 \
  --wait

echo "✅ Staging monitoring stack installed successfully"
echo "🌐 Access Grafana at: kubectl port-forward -n monitoring svc/prometheus-operator-grafana 3000:80"
echo "📊 Access Prometheus at: kubectl port-forward -n monitoring svc/prometheus-operator-prometheus 9090:9090"