version: '3.8'

services:
  db:
    image: timescale/timescaledb:latest-pg14
    environment:
      POSTGRES_DB: ${DB_NAME}
      POSTGRES_USER: ${DB_USER}
      POSTGRES_PASSWORD: ${DB_PASSWORD}
    volumes:
      - postgres_data:/var/lib/postgresql/data
    restart: unless-stopped

  redis:
    image: redis:7-alpine
    volumes:
      - redis_data:/data
    restart: unless-stopped

  elasticsearch:
    image: elasticsearch:8.11.0
    environment:
      - discovery.type=single-node
      - xpack.security.enabled=false
      - "ES_JAVA_OPTS=-Xms1g -Xmx1g"
    volumes:
      - elasticsearch_data:/usr/share/elasticsearch/data
    restart: unless-stopped

  web:
    build:
      context: .
      dockerfile: docker/production/Dockerfile
    command: gunicorn config.wsgi:application --bind 0.0.0.0:8000 --workers 4
    volumes:
      - static_volume:/app/staticfiles
      - media_volume:/app/media
    expose:
      - 8000
    depends_on:
      - db
      - redis
      - elasticsearch
    environment:
      - DJANGO_ENVIRONMENT=production
    restart: unless-stopped

  celery:
    build:
      context: .
      dockerfile: docker/production/Dockerfile
    command: celery -A config worker -l info
    depends_on:
      - db
      - redis
    environment:
      - DJANGO_ENVIRONMENT=production
    restart: unless-stopped

  celery-beat:
    build:
      context: .
      dockerfile: docker/production/Dockerfile
    command: celery -A config beat -l info
    depends_on:
      - db
      - redis
    environment:
      - DJANGO_ENVIRONMENT=production
    restart: unless-stopped

  nginx:
    build:
      context: ./docker/nginx
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - static_volume:/app/staticfiles
      - media_volume:/app/media
      - ./docker/nginx/certs:/etc/nginx/certs
    depends_on:
      - web
    restart: unless-stopped

volumes:
  postgres_data:
  redis_data:
  elasticsearch_data:
  static_volume:
  media_volume:
