version: '3.8'

services:
  redis:
    image: redis:7-alpine
    networks:
      - appnet

  web:
    build: .
    ports:
      - "5000:5000"
    env_file:
      - .env
    environment:
      - FLASK_APP=app:create_app
      - FLASK_ENV=${FLASK_ENV}
      - SECRET_KEY=${SECRET_KEY}
      - USE_REMOTE_MYSQL=${USE_REMOTE_MYSQL}
      - ENABLE_DB_SEEDING=${ENABLE_DB_SEEDING}
      - MYSQL_HOST=${MYSQL_HOST}
      - MYSQL_PORT=${MYSQL_PORT}
      - MYSQL_DATABASE=${MYSQL_DATABASE}
      - MYSQL_USER=${MYSQL_USER}
      - MYSQL_PASSWORD=${MYSQL_PASSWORD}
      - CELERY_BROKER_URL=redis://redis:6379/0
      - CELERY_RESULT_BACKEND=redis://redis:6379/0
    depends_on:
      - redis
      - db
    healthcheck:
      test: ["CMD-SHELL", "curl -fs http://127.0.0.1:5000/health || exit 1"]
      interval: 10s
      timeout: 3s
      retries: 3
      start_period: 30s
    volumes:
      - .:/app:delegated
      - ./${UPLOAD_FOLDER}:/app/${UPLOAD_FOLDER}
    networks:
      - appnet

  db:
    image: mysql:8
    restart: unless-stopped
    env_file:
      - .env
    environment:
      - MYSQL_ROOT_PASSWORD=${MYSQL_ROOT_PASSWORD}
      - MYSQL_DATABASE=${MYSQL_DATABASE}
      - MYSQL_USER=${MYSQL_USER}
      - MYSQL_PASSWORD=${MYSQL_PASSWORD}
    ports:
      - "42000:3306"
    volumes:
      - ./${MYSQL_DATA_FOLDER}:/var/lib/mysql
    entrypoint: >
      sh -c "mkdir -p /var/lib/mysql &&
             chown -R 1000:998 /var/lib/mysql &&
             chmod -R 770 /var/lib/mysql &&
             exec docker-entrypoint.sh mysqld"
    healthcheck:
      test: ["CMD", "mysqladmin", "ping", "-h", "localhost"]
      interval: 10s
      timeout: 5s
      retries: 5
    networks:
      - appnet

  adminer:
    image: adminer
    restart: always
    ports:
      - "8080:8080"
    env_file:
      - .env
    environment:
      - ADMINER_DEFAULT_SERVER=db
    depends_on:
      - db
    networks:
      - appnet

  neo4j:
    image: neo4j:5.18
    container_name: nip_neo4j
    ports:
      - "7474:7474"
      - "7687:7687"
    environment:
      - NEO4J_AUTH=neo4j/${NEO4J_PASSWORD}
    volumes:
      - neo4j_data:/data
    networks:
      - appnet

  worker:
    build: .
    env_file:
      - .env
    environment:
      - SECRET_KEY=${SECRET_KEY}
      - MYSQL_HOST=${MYSQL_HOST}
      - MYSQL_PORT=${MYSQL_PORT}
      - CELERY_BROKER_URL=redis://redis:6379/0
      - CELERY_RESULT_BACKEND=redis://redis:6379/0
    user: "appuser"
    command: celery -A app.celery_app:celery worker --beat --loglevel=info
    depends_on:
      - redis
      - db
    networks:
      - appnet

  flower:
    build: .
    env_file:
      - .env
    environment:
      - FLASK_ENV=production
      - CELERY_BROKER_URL=redis://redis:6379/0
      - CELERY_RESULT_BACKEND=redis://redis:6379/0
    command: >
      celery
      --app app.celery_app.celery
      --broker redis://redis:6379/0
      flower
      --port=5555
    depends_on:
      - redis
      - db
    ports:
      - "5555:5555"
    networks:
      - appnet

volumes:
  neo4j_data:

networks:
  appnet:
    driver: bridge
