##############################################################
# GMLM Platform — Monitoring Stack
#
# Services:
#   prometheus      — Metrics collection and alerting rules
#   grafana         — Dashboards and visualisation
#   alertmanager    — Alert routing to Slack/email/PagerDuty
#   node_exporter   — Host system metrics
#   mysql_exporter  — MySQL metrics
#   redis_exporter  — Redis metrics
#   nginx_exporter  — Nginx metrics
#   loki            — Log aggregation
#   promtail        — Log shipping agent
#
# Deploy alongside the main application:
#   docker compose -f docker-compose.yml \
#                  -f docker/monitoring/docker-compose.monitoring.yml \
#                  up -d
##############################################################

name: gmlm-monitoring-${DEPLOYMENT_ID:-production}

services:

  # ── Prometheus ──────────────────────────────────────────────
  prometheus:
    image: prom/prometheus:latest
    container_name: gmlm-prometheus
    restart: unless-stopped
    command:
      - '--config.file=/etc/prometheus/prometheus.yml'
      - '--storage.tsdb.path=/prometheus'
      - '--storage.tsdb.retention.time=30d'
      - '--storage.tsdb.retention.size=10GB'
      - '--web.enable-lifecycle'
      - '--web.enable-admin-api'
    volumes:
      - ./prometheus.yml:/etc/prometheus/prometheus.yml:ro
      - ./alertmanager/gmlm.rules.yml:/etc/prometheus/rules/gmlm.rules.yml:ro
      - prometheus_data:/prometheus
    ports:
      - "127.0.0.1:9090:9090"  # Only accessible locally — proxy via Nginx if needed
    networks:
      - monitoring
      - gmlm-network  # Access to app services
    healthcheck:
      test: ["CMD", "wget", "-qO-", "http://localhost:9090/-/healthy"]
      interval: 30s
      timeout:  10s

  # ── Alertmanager ────────────────────────────────────────────
  alertmanager:
    image: prom/alertmanager:latest
    container_name: gmlm-alertmanager
    restart: unless-stopped
    command:
      - '--config.file=/etc/alertmanager/alertmanager.yml'
      - '--storage.path=/alertmanager'
      - '--cluster.advertise-address=0.0.0.0:9093'
    volumes:
      - ./alertmanager.yml:/etc/alertmanager/alertmanager.yml:ro
      - alertmanager_data:/alertmanager
    ports:
      - "127.0.0.1:9093:9093"
    environment:
      SMTP_HOST:             ${SMTP_HOST:-localhost}
      SMTP_USERNAME:         ${SMTP_USERNAME:-}
      SMTP_PASSWORD:         ${SMTP_PASSWORD:-}
      ADMIN_EMAIL:           ${ADMIN_EMAIL:-admin@example.com}
      DOMAIN:                ${DOMAIN:-example.com}
      SLACK_WEBHOOK_URL:     ${SLACK_WEBHOOK_URL:-}
      PAGERDUTY_SERVICE_KEY: ${PAGERDUTY_SERVICE_KEY:-}
    networks:
      - monitoring

  # ── Grafana ─────────────────────────────────────────────────
  grafana:
    image: grafana/grafana:latest
    container_name: gmlm-grafana
    restart: unless-stopped
    environment:
      GF_SECURITY_ADMIN_USER:     ${GRAFANA_USER:-admin}
      GF_SECURITY_ADMIN_PASSWORD: ${GRAFANA_PASSWORD}
      GF_SERVER_DOMAIN:           ${DOMAIN}
      GF_SERVER_ROOT_URL:         https://${DOMAIN}/grafana/
      GF_SERVER_SERVE_FROM_SUB_PATH: "true"
      GF_FEATURE_TOGGLES_ENABLE:  ngalert
      GF_ALERTING_ENABLED:        "false"  # Use Alertmanager, not Grafana alerting
      GF_USERS_ALLOW_SIGN_UP:     "false"
      GF_ANALYTICS_REPORTING_ENABLED: "false"
      GF_ANALYTICS_CHECK_FOR_UPDATES: "false"
    volumes:
      - grafana_data:/var/lib/grafana
      - ./grafana/dashboards:/etc/grafana/provisioning/dashboards:ro
      - ./grafana/datasources.yml:/etc/grafana/provisioning/datasources/datasources.yml:ro
    ports:
      - "127.0.0.1:3000:3000"
    depends_on:
      - prometheus
      - loki
    networks:
      - monitoring

  # ── Loki (Log Aggregation) ───────────────────────────────────
  loki:
    image: grafana/loki:latest
    container_name: gmlm-loki
    restart: unless-stopped
    command: -config.file=/etc/loki/loki-config.yml
    volumes:
      - ./loki-config.yml:/etc/loki/loki-config.yml:ro
      - loki_data:/loki
    ports:
      - "127.0.0.1:3100:3100"
    networks:
      - monitoring

  # ── Promtail (Log Shipper → Loki) ────────────────────────────
  promtail:
    image: grafana/promtail:latest
    container_name: gmlm-promtail
    restart: unless-stopped
    command: -config.file=/etc/promtail/promtail-config.yml
    volumes:
      - ./promtail-config.yml:/etc/promtail/promtail-config.yml:ro
      - /var/lib/docker/containers:/var/lib/docker/containers:ro
      - /var/log:/var/log:ro
      # GMLM application logs
      - /var/www/gmlm/storage/logs:/gmlm/logs:ro
    networks:
      - monitoring
      - gmlm-network

  # ── Exporters ─────────────────────────────────────────────────
  node_exporter:
    image: prom/node-exporter:latest
    container_name: gmlm-node-exporter
    restart: unless-stopped
    command:
      - '--path.rootfs=/host'
      - '--collector.filesystem.mount-points-exclude=^/(proc|sys|dev|run)($|/)'
    volumes:
      - /:/host:ro,rslave
    pid: host
    networks:
      - monitoring

  mysql_exporter:
    image: prom/mysqld-exporter:latest
    container_name: gmlm-mysql-exporter
    restart: unless-stopped
    environment:
      DATA_SOURCE_NAME: "${DB_USERNAME}:${DB_PASSWORD}@tcp(mysql:3306)/${DB_DATABASE}"
    networks:
      - monitoring
      - gmlm-network
    depends_on:
      - mysql  # Requires the main gmlm-network to have a 'mysql' service

  redis_exporter:
    image: oliver006/redis_exporter:latest
    container_name: gmlm-redis-exporter
    restart: unless-stopped
    environment:
      REDIS_ADDR:     redis://redis:6379
      REDIS_PASSWORD: ${REDIS_PASSWORD:-}
    networks:
      - monitoring
      - gmlm-network

  nginx_exporter:
    image: nginx/nginx-prometheus-exporter:latest
    container_name: gmlm-nginx-exporter
    restart: unless-stopped
    command:
      - '-nginx.scrape-uri=http://app/nginx_status'
    networks:
      - monitoring
      - gmlm-network

volumes:
  prometheus_data:
    name: gmlm-prometheus-${DEPLOYMENT_ID:-prod}
  alertmanager_data:
    name: gmlm-alertmanager-${DEPLOYMENT_ID:-prod}
  grafana_data:
    name: gmlm-grafana-${DEPLOYMENT_ID:-prod}
  loki_data:
    name: gmlm-loki-${DEPLOYMENT_ID:-prod}

networks:
  monitoring:
    name: gmlm-monitoring
    driver: bridge
  gmlm-network:
    external: true
    name: gmlm-net-${DEPLOYMENT_ID:-default}
