# Production stack — VPS deploy.
# Usage:
#   python apps/api/scripts/bootstrap_install.py
#   cp infra/docker/.env.production.example infra/docker/.env.production
#   docker compose -f infra/docker/docker-compose.prod.yml --env-file infra/docker/.env.production up -d --build

services:
  postgres:
    image: postgres:16
    restart: unless-stopped
    env_file:
      - ../../apps/api/data/secrets/install.env
    volumes:
      - prod_postgres_data:/var/lib/postgresql/data
    networks:
      - internal

  redis:
    image: redis:7-alpine
    restart: unless-stopped
    networks:
      - internal

  minio:
    image: minio/minio
    restart: unless-stopped
    command: server /data --console-address ":9001"
    env_file:
      - ../../apps/api/data/secrets/install.env
    volumes:
      - prod_minio_data:/data
    networks:
      - internal

  api:
    build:
      context: ../../apps/api
      dockerfile: Dockerfile
    restart: unless-stopped
    environment:
      APP_ENV: production
      ENABLE_DOCS: "false"
      ENABLE_TEST_ROUTES: "false"
      COOKIE_SECURE: "true"
      ENABLE_RATE_LIMIT: "true"
      EMAIL_DELIVERY_MODE: smtp
      STORAGE_MODE: s3
      S3_ENDPOINT: http://minio:9000
      REDIS_URL: redis://redis:6379/0
      SEED_DEMO_USERS: "false"
    env_file:
      - ../../apps/api/data/secrets/install.env
      - .env.production
    volumes:
      - ../../apps/api/data/secrets:/app/data/secrets
      - prod_api_logs:/app/data/logs
    depends_on:
      - postgres
      - redis
      - minio
    networks:
      - internal

  web:
    build:
      context: ../..
      dockerfile: apps/web/Dockerfile
    restart: unless-stopped
    environment:
      VITE_USE_API_PROXY: "true"
      VITE_API_URL: http://api:8000
    depends_on:
      - api
    networks:
      - internal

  nginx:
    image: nginx:stable-alpine
    restart: unless-stopped
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - ../nginx/default.tls.conf:/etc/nginx/conf.d/default.conf:ro
      - ${TLS_CERT_DIR:-./certs}:/etc/nginx/certs:ro
    depends_on:
      - web
      - api
    networks:
      - internal

  worker:
    build:
      context: ../../apps/api
      dockerfile: Dockerfile
    restart: unless-stopped
    command: ["python", "-m", "app.worker"]
    environment:
      APP_ENV: production
      CELERY_BROKER_URL: redis://redis:6379/0
    env_file:
      - ../../apps/api/data/secrets/install.env
      - .env.production
    depends_on:
      - postgres
      - redis
    networks:
      - internal

  celery-worker:
    build:
      context: ../../apps/api
      dockerfile: Dockerfile
    restart: unless-stopped
    command: ["celery", "-A", "app.celery_app", "worker", "--loglevel=info"]
    environment:
      APP_ENV: production
      CELERY_BROKER_URL: redis://redis:6379/0
    env_file:
      - ../../apps/api/data/secrets/install.env
      - .env.production
    depends_on:
      - postgres
      - redis
    networks:
      - internal

networks:
  internal:
    driver: bridge

volumes:
  prod_postgres_data:
  prod_minio_data:
  prod_api_logs:
