services:
  # ============================================
  # APLICAÇÃO BACKEND - SPRING BOOT
  # ============================================
  backend:
    build:
      context: ./social-media-manager
      dockerfile: Dockerfile
    container_name: social_media_backend_prod
    restart: unless-stopped
    environment:
      - SPRING_PROFILES_ACTIVE=production
      - DATABASE_URL=jdbc:postgresql://postgres:5432/${DB_NAME}
      - DATABASE_USERNAME=${DB_USER}
      - DATABASE_PASSWORD=${DB_PASSWORD}
      - REDIS_HOST=redis
      - REDIS_PASSWORD=${REDIS_PASSWORD}
      - RABBITMQ_HOST=rabbitmq
      - RABBITMQ_USERNAME=${RABBITMQ_USER}
      - RABBITMQ_PASSWORD=${RABBITMQ_PASSWORD}
      - MONGODB_URI=${MONGODB_URI}
      - JWT_SECRET=${JWT_SECRET}
      - CORS_ALLOWED_ORIGINS=${CORS_ALLOWED_ORIGINS}
      - LOG_LEVEL=${LOG_LEVEL}
      - SHOW_SQL=${SHOW_SQL}
      - DDL_AUTO=${DDL_AUTO}
    ports:
      - "8000:8000"
    volumes:
      - backend_logs:/app/logs
      - backend_uploads:/app/uploads
    networks:
      - social_media_network
    depends_on:
      postgres:
        condition: service_healthy
      redis:
        condition: service_healthy
      rabbitmq:
        condition: service_healthy
      mongodb:
        condition: service_healthy
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:8000/actuator/health"]
      interval: 30s
      timeout: 10s
      retries: 5
      start_period: 90s
    # Configura o DNS para o contêiner em execução
    dns:
      - 8.8.8.8

  # ============================================
  # APLICAÇÃO FRONTEND - ANGULAR
  # ============================================
  frontend:
    build:
      context: ./hubber-dashboard
      dockerfile: Dockerfile
    container_name: social_media_frontend_prod
    restart: unless-stopped
    environment:
      - NODE_ENV=production
      - PORT=4000
      - API_URL=http://162.240.175.216:8000/api/v1
    ports:
      - "4000:4000"
    networks:
      - social_media_network
    depends_on:
      backend:
        condition: service_healthy
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:4000/health"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 60s
    # Configura o DNS para o contêiner em execução
    dns:
      - 8.8.8.8

  # ============================================
  # POSTGRESQL DATABASE
  # ============================================
  postgres:
    image: postgres:17-alpine
    container_name: social_media_postgres_prod
    restart: unless-stopped
    environment:
      POSTGRES_DB: ${DB_NAME}
      POSTGRES_USER: ${DB_USER}
      POSTGRES_PASSWORD: ${DB_PASSWORD}
      POSTGRES_INITDB_ARGS: "--encoding=UTF-8 --lc-collate=C --lc-ctype=C"
      POSTGRES_SHARED_PRELOAD_LIBRARIES: "pg_stat_statements"
      POSTGRES_MAX_CONNECTIONS: "300"
      POSTGRES_SHARED_BUFFERS: "512MB"
      POSTGRES_EFFECTIVE_CACHE_SIZE: "2GB"
      POSTGRES_WORK_MEM: "8MB"
    ports:
      - "5432:5432"
    volumes:
      - postgres_data_prod:/var/lib/postgresql/data
      - postgres_logs_prod:/var/lib/postgresql/data/logs
    networks:
      - social_media_network
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U ${DB_USER} -d ${DB_NAME}"]
      interval: 10s
      timeout: 5s
      retries: 5
    command: >
      postgres 
      -c max_connections=300
      -c shared_buffers=512MB
      -c effective_cache_size=2GB
      -c maintenance_work_mem=128MB
      -c checkpoint_completion_target=0.9
      -c wal_buffers=32MB
      -c default_statistics_target=100
      -c random_page_cost=1.1
      -c effective_io_concurrency=200
      -c work_mem=8MB
      -c min_wal_size=2GB
      -c max_wal_size=8GB
      -c log_statement=mod
      -c log_duration=on
      -c log_line_prefix='%t [%p]: [%l-1] user=%u,db=%d,app=%a,client=%h '
      -c logging_collector=on
      -c log_directory=/var/lib/postgresql/data/logs
      -c log_filename='postgresql-%Y-%m-%d_%H%M%S.log'

  # ============================================
  # REDIS CACHE
  # ============================================
  redis:
    image: redis:7-alpine
    container_name: social_media_redis_prod
    restart: unless-stopped
    command: redis-server --appendonly yes --requirepass ${REDIS_PASSWORD}
    ports:
      - "6379:6379"
    volumes:
      - redis_data_prod:/data
      - redis_config_prod:/usr/local/etc/redis/
    networks:
      - social_media_network
    healthcheck:
      test: ["CMD", "redis-cli", "-a", "${REDIS_PASSWORD}", "ping"]
      interval: 10s
      timeout: 3s
      retries: 5

  # ============================================
  # MONGODB
  # ============================================
  mongodb:
    image: mongo:7-jammy
    container_name: social_media_mongodb_prod
    restart: unless-stopped
    environment:
      MONGO_INITDB_DATABASE: ${MONGO_DB}
      MONGO_INITDB_ROOT_USERNAME: ${MONGO_USER}
      MONGO_INITDB_ROOT_PASSWORD: ${MONGO_PASSWORD}
    ports:
      - "27017:27017"
    volumes:
      - mongodb_data_prod:/data/db
      - mongodb_config_prod:/data/configdb
      - mongodb_logs_prod:/var/log/mongodb
    networks:
      - social_media_network
    healthcheck:
      test: ["CMD", "mongosh", "--quiet", "--eval", "db.admin.ping()"]
      interval: 10s
      timeout: 5s
      retries: 5

  # ============================================
  # RABBITMQ
  # ============================================
  rabbitmq:
    image: rabbitmq:3-management-alpine
    container_name: social_media_rabbitmq_prod
    restart: unless-stopped
    environment:
      RABBITMQ_DEFAULT_USER: ${RABBITMQ_USER}
      RABBITMQ_DEFAULT_PASS: ${RABBITMQ_PASSWORD}
      RABBITMQ_DEFAULT_VHOST: /
      RABBITMQ_VM_MEMORY_HIGH_WATERMARK: 0.6
    ports:
      - "5672:5672"
      - "15672:15672"
    volumes:
      - rabbitmq_data_prod:/var/lib/rabbitmq
      - rabbitmq_logs_prod:/var/log/rabbitmq
    networks:
      - social_media_network
    healthcheck:
      test: ["CMD", "rabbitmq-diagnostics", "ping"]
      interval: 30s
      timeout: 10s
      retries: 5

  # ============================================
  # AXON SERVER
  # ============================================
  axonserver:
    image: axoniq/axonserver:latest
    container_name: social_media_axonserver_prod
    restart: unless-stopped
    environment:
      AXONIQ_AXONSERVER_NAME: social-media-axonserver-prod
      AXONIQ_AXONSERVER_HOSTNAME: axonserver
      AXONIQ_AXONSERVER_DOMAIN: 162.240.175.216
      AXONIQ_AXONSERVER_DEVMODE_ENABLED: false
    ports:
      - "8024:8024"
      - "8124:8124"
    volumes:
      - axonserver_data_prod:/data
      - axonserver_events_prod:/eventdata
      - axonserver_config_prod:/config
    networks:
      - social_media_network
    healthcheck:
      test: ["CMD", "wget", "--no-verbose", "--tries=1", "--spider", "http://localhost:8024/actuator/health"]
      interval: 30s
      timeout: 10s
      retries: 5

  # ============================================
  # PROMETHEUS
  # ============================================
  prometheus:
    image: prom/prometheus:latest
    container_name: social_media_prometheus_prod
    restart: unless-stopped
    command:
      - '--config.file=/etc/prometheus/prometheus.yml'
      - '--storage.tsdb.path=/prometheus'
      - '--web.console.libraries=/etc/prometheus/console_libraries'
      - '--web.console.templates=/etc/prometheus/consoles'
      - '--storage.tsdb.retention.time=30d'
      - '--web.enable-lifecycle'
      - '--web.enable-admin-api'
    ports:
      - "9090:9090"
    volumes:
      - prometheus_data_prod:/prometheus
      - prometheus_config_prod:/etc/prometheus
    networks:
      - social_media_network
    healthcheck:
      test: ["CMD", "wget", "--no-verbose", "--tries=1", "--spider", "http://localhost:9090/-/healthy"]
      interval: 30s
      timeout: 10s
      retries: 3

  # ============================================
  # GRAFANA
  # ============================================
  grafana:
    image: grafana/grafana:latest
    container_name: social_media_grafana_prod
    restart: unless-stopped
    environment:
      GF_SECURITY_ADMIN_USER: ${PRODUCTION_USER}
      GF_SECURITY_ADMIN_PASSWORD: ${GRAFANA_PASSWORD}
      GF_USERS_ALLOW_SIGN_UP: false
      GF_SECURITY_ALLOW_EMBEDDING: true
      GF_AUTH_ANONYMOUS_ENABLED: false
      GF_INSTALL_PLUGINS: grafana-piechart-panel,grafana-clock-panel
    ports:
      - "3000:3000"
    volumes:
      - grafana_data_prod:/var/lib/grafana
      - grafana_config_prod:/etc/grafana
    networks:
      - social_media_network
    depends_on:
      - prometheus
    healthcheck:
      test: ["CMD-SHELL", "wget --no-verbose --tries=1 --spider http://localhost:3000/api/health || exit 1"]
      interval: 30s
      timeout: 10s
      retries: 3

  # ============================================
  # NGINX REVERSE PROXY (OPCIONAL)
  # ============================================
  nginx:
    image: nginx:alpine
    container_name: social_media_nginx_prod
    restart: unless-stopped
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - ./nginx/nginx.conf:/etc/nginx/nginx.conf:ro
      - ./nginx/ssl:/etc/nginx/ssl:ro
      - nginx_logs_prod:/var/log/nginx
    networks:
      - social_media_network
    depends_on:
      - frontend
      - backend
    healthcheck:
      test: ["CMD", "nginx", "-t"]
      interval: 30s
      timeout: 10s
      retries: 3
    profiles:
      - nginx

# ============================================
# VOLUMES PARA PRODUÇÃO
# ============================================
volumes:
  # Dados dos bancos
  postgres_data_prod:
    driver: local
  postgres_logs_prod:
    driver: local
  redis_data_prod:
    driver: local
  redis_config_prod:
    driver: local
  mongodb_data_prod:
    driver: local
  mongodb_config_prod:
    driver: local
  mongodb_logs_prod:
    driver: local
  rabbitmq_data_prod:
    driver: local
  rabbitmq_logs_prod:
    driver: local
  axonserver_data_prod:
    driver: local
  axonserver_events_prod:
    driver: local
  axonserver_config_prod:
    driver: local
  
  # Dados de monitoramento
  prometheus_data_prod:
    driver: local
  prometheus_config_prod:
    driver: local
  grafana_data_prod:
    driver: local
  grafana_config_prod:
    driver: local
  
  # Dados das aplicações
  backend_logs:
    driver: local
  backend_uploads:
    driver: local
  nginx_logs_prod:
    driver: local

# ============================================
# REDE PARA PRODUÇÃO
# ============================================
networks:
  social_media_network:
    driver: bridge
    ipam:
      config:
        - subnet: 172.20.0.0/16
