services:
  db:
    image: postgis/postgis:17-3.4
    container_name: postgres-postgis-prod
    restart: always
    environment:
      POSTGRES_DB: ${POSTGRES_DB:-geodb}
      POSTGRES_USER: ${POSTGRES_USER:-geralt}
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:-123456}
    ports:
      - "5432:5432"
    volumes:
      - postgres_data_prod:/var/lib/postgresql/data
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U ${POSTGRES_USER:-geralt} -d ${POSTGRES_DB:-geodb}"]
      interval: 10s
      timeout: 5s
      retries: 5
    networks:
      - app-network

  web:
    build:
      context: ./dbapp
      dockerfile: Dockerfile
    container_name: django-app-prod
    restart: always
    environment:
      - DEBUG=False
      - ENVIRONMENT=production
      - DJANGO_SETTINGS_MODULE=dbapp.settings.production
      - SECRET_KEY=${SECRET_KEY}
      - DB_ENGINE=django.contrib.gis.db.backends.postgis
      - DB_NAME=${DB_NAME:-geodb}
      - DB_USER=${DB_USER:-geralt}
      - DB_PASSWORD=${DB_PASSWORD:-123456}
      - DB_HOST=db
      - DB_PORT=5432
      - ALLOWED_HOSTS=${ALLOWED_HOSTS:-localhost,127.0.0.1}
      - GUNICORN_WORKERS=${GUNICORN_WORKERS:-3}
      - GUNICORN_TIMEOUT=${GUNICORN_TIMEOUT:-120}
    ports:
      - "8000:8000"
    volumes:
      - static_volume_prod:/app/staticfiles
      - media_volume_prod:/app/media
      - logs_volume_prod:/app/logs
    depends_on:
      db:
        condition: service_healthy
    networks:
      - app-network

  tileserver:
    image: maptiler/tileserver-gl:latest
    container_name: tileserver-gl-prod
    restart: always
    ports:
      - "8080:8080"
    volumes:
      - ./tiles:/data
      - tileserver_config_prod:/config
    environment:
      - VERBOSE=false
    networks:
      - app-network

  nginx:
    image: nginx:alpine
    container_name: nginx-prod
    restart: always
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - ./nginx/nginx.conf:/etc/nginx/nginx.conf:ro
      - ./nginx/conf.d:/etc/nginx/conf.d:ro
      - static_volume_prod:/app/staticfiles:ro
      - media_volume_prod:/app/media:ro
      - ./nginx/ssl:/etc/nginx/ssl:ro
    depends_on:
      - web
    networks:
      - app-network

volumes:
  postgres_data_prod:
  static_volume_prod:
  media_volume_prod:
  logs_volume_prod:
  tileserver_config_prod:

networks:
  app-network:
    driver: bridge
