# Docker Compose para DESARROLLO
# Uso: docker-compose -f docker-compose.dev.yml up -d

services:
  # MariaDB Database (compatible con producción)
  mysql:
    image: mariadb:10.11
    container_name: whatsapp-dev-mysql
    restart: unless-stopped
    ports:
      - "3306:3306"
    environment:
      MARIADB_ROOT_PASSWORD: ${DB_ROOT_PASSWORD:-root_password_2026}
      MARIADB_DATABASE: ${DB_NAME:-usite_whatsapp_bot}
      MARIADB_USER: ${DB_USER:-usite_whatsapp_user}
      MARIADB_PASSWORD: ${DB_PASS}
      TZ: America/Bogota
    volumes:
      - mysql-dev-data:/var/lib/mysql
      - ./database:/docker-entrypoint-initdb.d:ro
    command: --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci --default-time-zone=America/Bogota
    networks:
      - whatsapp-dev-network
    healthcheck:
      test: ["CMD", "mariadb-admin", "ping", "-h", "localhost", "-u", "root", "-p${DB_ROOT_PASSWORD:-root_password_2026}"]
      interval: 10s
      timeout: 5s
      retries: 5
      start_period: 30s

  # Aplicación PHP con Nginx - MODO DEV
  app:
    build:
      context: .
      dockerfile: Dockerfile  # Usar Dockerfile principal
      args:
        - APP_ENV=development
    container_name: whatsapp-dev-app
    restart: unless-stopped
    working_dir: /var/www/html
    ports:
      - "8080:80"      # HTTP
      - "8443:443"     # HTTPS
      - "9003:9003"    # XDebug (para IDEs)
    volumes:
      # Hot-reload: cambios en código se reflejan inmediatamente
      - ./:/var/www/html
      - ./logs:/var/www/html/logs
      - ./uploads:/var/www/html/uploads
      - ./docker/nginx/ssl.conf:/etc/nginx/http.d/ssl.conf:ro
      - ssl-certs:/etc/nginx/ssl
      - php-socket:/run/php
    environment:
      # Zona horaria Colombia
      - TZ=America/Bogota
      
      # Entorno de desarrollo
      - APP_ENV=development
      - APP_DEBUG=true
      
      # URL pública de la aplicación (para generar URLs absolutas en archivos multimedia)
      - APP_URL=${APP_URL:-https://bot.laboratorioximenacaicedo.com/}
      
      # Base de datos - apuntar a MySQL local
      - DB_HOST=mysql
      - DB_PORT=${DB_PORT:-3306}
      - DB_NAME=${DB_NAME:-usite_whatsapp_bot}
      - DB_USER=${DB_USER:-usite_whatsapp_user}
      - DB_PASS=${DB_PASS}
      
      # Redis
      - REDIS_HOST=redis
      - REDIS_PORT=6379
      - REDIS_PASSWORD=${REDIS_PASSWORD:-}
      
      # WhatsApp API
      - WHATSAPP_TOKEN=${WHATSAPP_TOKEN}
      - WHATSAPP_PHONE_NUMBER_ID=${WHATSAPP_PHONE_NUMBER_ID}
      - WEBHOOK_VERIFY_TOKEN=${WEBHOOK_VERIFY_TOKEN}
      
      # Configuración de desarrollo
      - LOG_LEVEL=DEBUG
      - ENABLE_RATE_LIMIT=false
      - DISPLAY_ERRORS=1
      - ERROR_REPORTING=E_ALL
      
      # XDebug (para debugging con IDE)
      - XDEBUG_MODE=debug,develop,coverage
      - XDEBUG_CONFIG=client_host=host.docker.internal client_port=9003
      - PHP_IDE_CONFIG=serverName=whatsapp-docker
      
      # PHP optimizado para desarrollo
      - PHP_MEMORY_LIMIT=512M
      - PHP_MAX_EXECUTION_TIME=300
      - PHP_OPCACHE_ENABLE=0  # Desactivar OPcache para hot-reload
      
    depends_on:
      redis:
        condition: service_healthy
      mysql:
        condition: service_healthy
    networks:
      - whatsapp-dev-network
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost/health.php"]
      interval: 30s
      timeout: 5s
      retries: 3
      start_period: 40s

  # Redis para colas y caché
  redis:
    image: redis:7-alpine
    container_name: whatsapp-dev-redis
    restart: unless-stopped
    ports:
      - "6379:6379"
    command: >
      redis-server 
      --appendonly yes 
      --maxmemory 256mb 
      --maxmemory-policy allkeys-lru
      --loglevel verbose
    volumes:
      - redis-dev-data:/data
    networks:
      - whatsapp-dev-network
    healthcheck:
      test: ["CMD", "redis-cli", "ping"]
      interval: 10s
      timeout: 3s
      retries: 5
      start_period: 10s

  # Workers (solo 1 en desarrollo para facilitar debugging)
  worker:
    build:
      context: .
      dockerfile: Dockerfile.worker  # Usar Dockerfile de worker existente
    container_name: whatsapp-dev-worker
    restart: unless-stopped
    working_dir: /var/www/html
    volumes:
      - ./:/var/www/html
      - ./logs:/var/www/html/logs
    environment:
      - APP_ENV=development
      - APP_DEBUG=true
      - DB_HOST=mysql
      - DB_PORT=${DB_PORT:-3306}
      - DB_NAME=${DB_NAME:-usite_whatsapp_bot}
      - DB_USER=${DB_USER:-usite_whatsapp_user}
      - DB_PASS=${DB_PASS}
      - REDIS_HOST=redis
      - REDIS_PORT=6379
      - REDIS_PASSWORD=${REDIS_PASSWORD:-}
      - LOG_LEVEL=${LOG_LEVEL:-DEBUG}
      - XDEBUG_MODE=debug
      - XDEBUG_CONFIG=client_host=host.docker.internal client_port=9003
    depends_on:
      redis:
        condition: service_healthy
      mysql:
        condition: service_healthy
    networks:
      - whatsapp-dev-network
    command: php worker.php --daemon

  # Redis Commander - Interface web para Redis
  redis-commander:
    image: rediscommander/redis-commander:latest
    container_name: whatsapp-dev-redis-ui
    restart: unless-stopped
    ports:
      - "8082:8081"
    environment:
      # Usa host.docker.internal para alcanzar Redis aunque esté en otra red Docker
      - REDIS_HOSTS=local:host.docker.internal:6379
      - REDIS_PASSWORD=${REDIS_PASSWORD:-}
    extra_hosts:
      - "host.docker.internal:host-gateway"
    networks:
      - whatsapp-dev-network

  # MailHog - Captura emails para testing (opcional)
  mailhog:
    image: mailhog/mailhog:latest
    container_name: whatsapp-dev-mailhog
    restart: unless-stopped
    ports:
      - "1025:1025"   # SMTP
      - "8025:8025"   # Web UI
    networks:
      - whatsapp-dev-network

  # Adminer - Gestor de base de datos ligero (alternativa a phpMyAdmin)
  adminer:
    image: adminer:latest
    container_name: whatsapp-dev-adminer
    restart: unless-stopped
    ports:
      - "8083:8080"
    environment:
      - ADMINER_DEFAULT_SERVER=mysql
      - ADMINER_DESIGN=nette
    depends_on:
      mysql:
        condition: service_healthy
    networks:
      - whatsapp-dev-network

  # phpMyAdmin - Gestor de base de datos alternativo
  phpmyadmin:
    image: phpmyadmin:latest
    container_name: whatsapp-dev-phpmyadmin
    restart: unless-stopped
    ports:
      - "8084:80"
    environment:
      PMA_HOST: mysql
      PMA_PORT: 3306
      PMA_USER: ${DB_USER:-usite_whatsapp_user}
      PMA_PASSWORD: ${DB_PASS:-6q!Vio0fn@PFmsLyHjH5}
      UPLOAD_LIMIT: 100M
    depends_on:
      mysql:
        condition: service_healthy
    networks:
      - whatsapp-dev-network

  # Contenedor de mantenimiento automatizado
  maintenance:
    build:
      context: .
      dockerfile: Dockerfile.maintenance
    container_name: whatsapp-dev-maintenance
    restart: unless-stopped
    cap_add:
      - SYS_NICE
      - SETGID
      - SETUID
    volumes:
      - .:/var/www/html
      - ./logs:/var/www/html/logs
      - ./backups:/backups
      - maintenance-cron-logs:/var/log/cron
    environment:
      - TZ=America/Bogota
      - DB_HOST=mysql
      - DB_NAME=${DB_NAME:-usite_whatsapp_bot}
      - DB_USER=${DB_USER:-usite_whatsapp_user}
      - DB_PASS=${DB_PASS}
      - REDIS_HOST=redis
      - REDIS_PORT=6379
      - APP_URL=http://app/health.php
    depends_on:
      mysql:
        condition: service_healthy
      redis:
        condition: service_healthy
    networks:
      - whatsapp-dev-network

volumes:
  mysql-dev-data:
    driver: local
  redis-dev-data:
    driver: local
  ssl-certs:
    driver: local
  php-socket:
    driver: local
  maintenance-cron-logs:
    driver: local

networks:
  whatsapp-dev-network:
    driver: bridge
