version: "3.8"

services:
  redis:
    image: redis:7-alpine
    container_name: redis_container
    ports:
      - "6999:6379"
    command: redis-server --appendonly yes
    volumes:
      - redis_data:/data
    networks:
      - memorymag-network
    healthcheck:
      test: ["CMD", "redis-cli", "ping"]
      interval: 5s
      timeout: 3s
      retries: 5
      start_period: 10s

  # Main Application
  app:
    build: .
    container_name: memorymag_app
    ports:
      - "3040:3040"
    environment:
      - NODE_ENV=production
      - DB_URI=mongodb+srv://devapp:hNdA94M9ZhqqCiM3@cluster0.tsctq.mongodb.net/memory-mag
      - REDIS_HOST=redis
      - REDIS_PORT=6379
      - BASE_URL_MEDIA=${BASE_URL_MEDIA}
      - SSL_KEY_PATH=${SSL_KEY_PATH}
      - SSL_CERT_PATH=${SSL_CERT_PATH}
      - SSL_CA_PATH=${SSL_CA_PATH}
      - MODE=${MODE}
      - REDIS_DB=0
      - PORT=3040
      - JWT_SECRET=your_jwt_secret_here
      - NODE_OPTIONS=--max-old-space-size=4096
      - MAX_CONCURRENT_VIDEO_JOBS=10
      - AWS_ACCESS_KEY_ID=${AWS_ACCESS_KEY_ID}
      - AWS_SECRET_ACCESS_KEY=${AWS_SECRET_ACCESS_KEY}
      - AWS_REGION=${AWS_REGION}
      - S3_BUCKET_NAME=${S3_BUCKET_NAME}
      - FFMPEG_PATH=/usr/bin/ffmpeg
      - FFPROBE_PATH=/usr/bin/ffprobe
      - TEMP_DIR=/app/temp/video-processing
      - CDN_DOMAIN=${CDN_DOMAIN}
      - MAIL_HOST=${MAIL_HOST}
      - MAIL_USERNAME=${MAIL_USERNAME}
      - MAIL_PASSWORD=${MAIL_PASSWORD}
      - STRIPE_SECRET_KEY=${STRIPE_SECRET_KEY}
      - PAYPAL_CLIENT_ID=${PAYPAL_CLIENT_ID}
      - PAYPAL_CLIENT_SECRET=${PAYPAL_CLIENT_SECRET}
    env_file:
      - .env
    depends_on:
      redis:
        condition: service_healthy
    volumes:
      - uploads_data:/app/uploads
      - temp_data:/app/temp
    networks:
      - memorymag-network
    restart: unless-stopped

  # Video Processing Worker
  worker:
    build: .
    container_name: memorymag_worker
    command: npm run worker
    deploy:
      resources:
        limits:
          memory: 4G
        reservations:
          memory: 2G
    environment:
      - NODE_ENV=production
      - MODE=${MODE}
      - SSL_KEY_PATH=${SSL_KEY_PATH}
      - SSL_CERT_PATH=${SSL_CERT_PATH}
      - SSL_CA_PATH=${SSL_CA_PATH}
      - DB_URI=mongodb+srv://devapp:hNdA94M9ZhqqCiM3@cluster0.tsctq.mongodb.net/memory-mag
      - REDIS_HOST=redis
      - REDIS_PORT=6379
      - REDIS_DB=0
      - NODE_OPTIONS=--max-old-space-size=4096
      - MAX_CONCURRENT_VIDEO_JOBS=10
      - AWS_ACCESS_KEY_ID=${AWS_ACCESS_KEY_ID}
      - AWS_SECRET_ACCESS_KEY=${AWS_SECRET_ACCESS_KEY}
      - AWS_REGION=${AWS_REGION}
      - S3_BUCKET_NAME=${S3_BUCKET_NAME}
      - FFMPEG_PATH=/usr/bin/ffmpeg
      - FFPROBE_PATH=/usr/bin/ffprobe
      - TEMP_DIR=/app/temp/video-processing
    depends_on:
      redis:
        condition: service_healthy
    volumes:
      - temp_data:/app/temp
    networks:
      - memorymag-network
    restart: unless-stopped

networks:
  memorymag-network:
    driver: bridge

volumes:
  redis_data:
    driver: local
  uploads_data:
    driver: local
  temp_data:
    driver: local
