# Staged environment: same as prod but tuned for 8GB VPS (lower heap and container limits).
# Use with: docker compose -f docker-compose.staged.yml up -d
# Rolling update: scripts/rolling-update.staged.sh

version: "3.9"

services:
  db:
    image: mysql:8.0
    container_name: honey-mysql
    restart: always
    environment:
      MYSQL_DATABASE: honey_db
      MYSQL_ROOT_PASSWORD: ${DB_ROOT_PASSWORD}
    volumes:
      - mysql_data:/var/lib/mysql
    deploy:
      resources:
        limits:
          cpus: '1.0'
          memory: 2G
    healthcheck:
      test: ["CMD-SHELL", "mysqladmin ping -h localhost -u root -p$$MYSQL_ROOT_PASSWORD || exit 1"]
      interval: 10s
      timeout: 5s
      retries: 5
    networks:
      - honey-network

  backend:
    build:
      context: .
      dockerfile: Dockerfile
    container_name: honey-backend
    depends_on:
      db:
        condition: service_healthy
    ports:
      - "127.0.0.1:8080:8080"
    labels:
      - "deployment.role=primary"
      - "deployment.version=current"
    volumes:
      - /opt/app/data/avatars:/app/data/avatars
      - /run/secrets/honey-config.properties:/run/secrets/honey-config.properties:ro
      - /opt/app/backend/config:/app/config:rw
      - /opt/app/logs:/app/logs
    environment:
      # Staged: 2GB heap (prod uses 10GB)
      JAVA_OPTS: -Xms2g -Xmx2g -XX:+UseG1GC -XX:MaxGCPauseMillis=200
      LOGGING_CONFIG: /app/config/logback-spring.xml
      LOG_DIR: /app/logs
    deploy:
      resources:
        limits:
          cpus: '2.0'
          memory: 3G
    networks:
      - honey-network
    restart: always
    healthcheck:
      test: ["CMD", "wget", "--quiet", "--tries=1", "--spider", "http://localhost:8080/actuator/health/liveness"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 60s

  backend-new:
    build:
      context: .
      dockerfile: Dockerfile
    container_name: honey-backend-new
    depends_on:
      db:
        condition: service_healthy
    ports:
      - "127.0.0.1:8082:8080"
    profiles:
      - rolling-update
    labels:
      - "deployment.role=standby"
      - "deployment.version=new"
    volumes:
      - /opt/app/data/avatars:/app/data/avatars
      - /run/secrets/honey-config.properties:/run/secrets/honey-config.properties:ro
      - /opt/app/backend/config:/app/config:rw
      - /opt/app/logs:/app/logs
    environment:
      JAVA_OPTS: -Xms2g -Xmx2g -XX:+UseG1GC -XX:MaxGCPauseMillis=200
      LOGGING_CONFIG: /app/config/logback-spring.xml
      LOG_DIR: /app/logs
    deploy:
      resources:
        limits:
          cpus: '2.0'
          memory: 3G
    networks:
      - honey-network
    restart: always
    healthcheck:
      test: ["CMD", "wget", "--quiet", "--tries=1", "--spider", "http://localhost:8080/actuator/health/liveness"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 60s

  phpmyadmin:
    image: phpmyadmin:latest
    container_name: honey-phpmyadmin
    restart: always
    depends_on:
      db:
        condition: service_healthy
    ports:
      - "127.0.0.1:8081:80"
    environment:
      PMA_HOST: db
      PMA_PORT: 3306
      MYSQL_ROOT_PASSWORD: ${DB_ROOT_PASSWORD}
      UPLOAD_LIMIT: 64M
      PMA_ABSOLUTE_URI: ${PMA_ABSOLUTE_URI:-}
      PMA_SSL: "true"
      PMA_TRUSTED_PROXIES: "127.0.0.1"
    networks:
      - honey-network
    deploy:
      resources:
        limits:
          cpus: '0.5'
          memory: 256M

volumes:
  mysql_data:
    driver: local

networks:
  honey-network:
    driver: bridge
