#!/bin/bash
set -euo pipefail

echo "🔧 Installing Monitoring Stack (Production 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
echo "📊 Installing Prometheus Operator..."
helm upgrade --install prometheus-operator prometheus-community/kube-prometheus-stack \
  --namespace monitoring \
  --set prometheus.prometheusSpec.retention=30d \
  --set prometheus.prometheusSpec.storageSpec.volumeClaimTemplate.spec.resources.requests.storage=50Gi \
  --set grafana.adminPassword="${GRAFANA_ADMIN_PASSWORD:-admin123}" \
  --set alertmanager.config.global.slack_api_url="${SLACK_WEBHOOK_URL:-}" \
  --wait

# Install additional monitoring tools
echo "📈 Installing additional monitoring components..."

# Node Exporter DaemonSet
kubectl apply -f - <<EOF
apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: node-exporter
  namespace: monitoring
spec:
  selector:
    matchLabels:
      app: node-exporter
  template:
    metadata:
      labels:
        app: node-exporter
    spec:
      containers:
      - name: node-exporter
        image: prom/node-exporter:v1.7.0
        args:
        - --path.procfs=/host/proc
        - --path.sysfs=/host/sys
        - --collector.filesystem.mount-points-exclude=^/(sys|proc|dev|host|etc)($$|/)
        ports:
        - containerPort: 9100
          name: metrics
        volumeMounts:
        - name: proc
          mountPath: /host/proc
          readOnly: true
        - name: sys
          mountPath: /host/sys
          readOnly: true
      volumes:
      - name: proc
        hostPath:
          path: /proc
      - name: sys
        hostPath:
          path: /sys
      hostNetwork: true
      hostPID: true
EOF

# Install Blackbox Exporter for endpoint monitoring
helm upgrade --install blackbox-exporter prometheus-community/prometheus-blackbox-exporter \
  --namespace monitoring \
  --wait

# Create ServiceMonitor for custom applications
kubectl apply -f - <<EOF
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
  name: custom-app-metrics
  namespace: monitoring
spec:
  selector:
    matchLabels:
      app: custom-app
  endpoints:
  - port: metrics
    interval: 30s
    path: /metrics
EOF

echo "✅ 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"
echo "🚨 Access AlertManager at: kubectl port-forward -n monitoring svc/prometheus-operator-alertmanager 9093:9093"