services:
  db:
    image: mirror-docker.runflare.com/library/mysql:8
    container_name: ai-db
    restart: always
    environment:
      MYSQL_DATABASE: ${DB_NAME}
      MYSQL_USER: ${DB_USER}
      MYSQL_PASSWORD: ${DB_PASSWORD}
      MYSQL_ROOT_PASSWORD: ${DB_ROOT_PASSWORD}
    volumes:
      - ai_mysql_data:/var/lib/mysql
    healthcheck:
      test: ["CMD", "mysqladmin", "ping", "-h", "localhost", "-u", "root", "-p$${MYSQL_ROOT_PASSWORD}"]
      interval: 10s
      timeout: 5s
      retries: 5
    networks:
      - crop_network

  redis:
    image: mirror-docker.runflare.com/library/redis:latest
    container_name: ai-redis
    restart: always
    networks:
      - crop_network

  qdrant:
    image: mirror-docker.runflare.com/qdrant/qdrant:latest
    container_name: ai-qdrant
    restart: always
    volumes:
      - qdrant_data:/qdrant/storage
    networks:
      - crop_network

  web:
    build:
      context: .
      dockerfile: Dockerfile.Dev
    container_name: ai-web
    restart: always
    command: gunicorn config.wsgi:application --bind 0.0.0.0:8000 --workers 4 --threads 2
    volumes:
      - ./logs:/app/logs
      - ./static:/app/static
      - ./media:/app/media
    ports:
      - "8020:8000"
    env_file:
      - .env
    environment:
      DB_HOST: ai-db
      CELERY_BROKER_URL: redis://ai-redis:6379/0
      CELERY_RESULT_BACKEND: redis://ai-redis:6379/0
      QDRANT_HOST: ai-qdrant
      QDRANT_PORT: 6333
      DEBUG: "False"
      ENABLE_PROXYCHAINS: ${ENABLE_PROXYCHAINS:-1}
      PROXYCHAINS_PROXY_TYPE: ${PROXYCHAINS_PROXY_TYPE:-socks4}
      PROXYCHAINS_PROXY_HOST: ${PROXYCHAINS_PROXY_HOST:-host.docker.internal}
      PROXYCHAINS_PROXY_PORT: ${PROXYCHAINS_PROXY_PORT:-10808}
      PROXYCHAINS_CHAIN_MODE: ${PROXYCHAINS_CHAIN_MODE:-strict_chain}
    depends_on:
      db:
        condition: service_healthy
      redis:
        condition: service_started

    networks:
      - crop_network
    extra_hosts:
      - "host.docker.internal:host-gateway"

  celery:
    build:
      context: .
      dockerfile: Dockerfile.Dev
    container_name: ai-celery
    restart: always
    command: celery -A config worker -l info --concurrency=4
    healthcheck:
      test: ["CMD-SHELL", "celery -A config inspect ping --timeout 10 || exit 1"]
      interval: 30s
      timeout: 15s
      retries: 5
      start_period: 30s
    volumes:
      - ./logs:/app/logs
      - ./media:/app/media
    env_file:
      - .env
    environment:
      DB_HOST: ai-db
      CELERY_BROKER_URL: redis://ai-redis:6379/0
      CELERY_RESULT_BACKEND: redis://ai-redis:6379/0
      SKIP_MIGRATE: "1"
      DEBUG: "False"
      ENABLE_PROXYCHAINS: ${ENABLE_PROXYCHAINS:-1}
      PROXYCHAINS_PROXY_TYPE: ${PROXYCHAINS_PROXY_TYPE:-socks4}
      PROXYCHAINS_PROXY_HOST: ${PROXYCHAINS_PROXY_HOST:-host.docker.internal}
      PROXYCHAINS_PROXY_PORT: ${PROXYCHAINS_PROXY_PORT:-10808}
      PROXYCHAINS_CHAIN_MODE: ${PROXYCHAINS_CHAIN_MODE:-strict_chain}
    depends_on:
      db:
        condition: service_healthy
      redis:
        condition: service_started
    networks:
      - crop_network
    extra_hosts:
      - "host.docker.internal:host-gateway"


volumes:
  ai_mysql_data:
  qdrant_data:

networks:
  crop_network:
    external: true
