services:
  # Aplicación PHP con Nginx y Workers
  app:
    build:
      context: .
      dockerfile: Dockerfile
    # container_name: NO definir — Coolify asigna su propio nombre
    restart: unless-stopped
    # Coolify/Traefik gestiona el enrutamiento - no exponer puertos al host
    expose:
      - "80"
    volumes:
      # NO montar ./:/var/www/html — sobreescribiría el código del contenedor en prod
      # Coolify usa named volumes para logs y uploads (persistencia entre deploys)
      - app-logs:/var/www/html/logs
      - app-uploads:/var/www/html/uploads
    environment:
      - APP_ENV=${APP_ENV:-production}
      - DB_HOST=${DB_HOST}
      - DB_PORT=${DB_PORT:-3306}
      - DB_NAME=${DB_NAME}
      - DB_USER=${DB_USER}
      - DB_PASS=${DB_PASS}
      - REDIS_HOST=${REDIS_HOST:-redis}
      - REDIS_PORT=${REDIS_PORT:-6379}
      - REDIS_PASSWORD=${REDIS_PASSWORD:-}
      # Índice de DB Redis para aislar sesiones entre despliegues (0-15)
      - REDIS_DB=${REDIS_DB:-0}
      - WHATSAPP_TOKEN=${WHATSAPP_TOKEN}
      - WHATSAPP_PHONE_NUMBER_ID=${WHATSAPP_PHONE_NUMBER_ID}
      - WEBHOOK_VERIFY_TOKEN=${WEBHOOK_VERIFY_TOKEN}
      - LOG_LEVEL=${LOG_LEVEL:-INFO}
      - ENABLE_RATE_LIMIT=${ENABLE_RATE_LIMIT:-true}
    healthcheck:
      test: ["CMD-SHELL", "curl -sf http://localhost/health.php || curl -sf http://localhost/ || exit 1"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 60s
    networks:
      - coolify
    logging:
      driver: "json-file"
      options:
        max-size: "10m"
        max-file: "3"

  # MySQL Database (DESHABILITADO - usando BD externa)
  # Descomenta si quieres usar MySQL en Docker
  # db:
  #   image: mysql:8.0
  #   container_name: whatsapp-bot-db
  #   restart: unless-stopped
  #   ports:
  #     - "${DB_PORT:-3306}:3306"
  #   environment:
  #     MYSQL_ROOT_PASSWORD: ${DB_ROOT_PASSWORD}
  #     MYSQL_DATABASE: ${DB_NAME:-whatsapp_bot}
  #     MYSQL_USER: ${DB_USER:-whatsapp_user}
  #     MYSQL_PASSWORD: ${DB_PASS}
  #     MYSQL_CHARACTER_SET_SERVER: utf8mb4
  #     MYSQL_COLLATION_SERVER: utf8mb4_unicode_ci
  #   volumes:
  #     - db-data:/var/lib/mysql
  #     - ./database/init.sql:/docker-entrypoint-initdb.d/init.sql:ro
  #   networks:
  #     - whatsapp-network
  #   healthcheck:
  #     test: ["CMD", "mysqladmin", "ping", "-h", "localhost", "-u", "root", "-p${DB_ROOT_PASSWORD}"]
  #     interval: 10s
  #     timeout: 5s
  #     retries: 5
  #     start_period: 30s
  #   command: --default-authentication-plugin=mysql_native_password --max_connections=500

  # Redis: recurso EXTERNO gestionado por Coolify (no se levanta aquí).
  # El app se conecta vía REDIS_HOST/REDIS_PORT/REDIS_PASSWORD (env vars).

volumes:
  app-logs:
    driver: local
  app-uploads:
    driver: local

networks:
  coolify:
    external: true
