# Docker Compose orchestration for the full production stack.
#
# This is an ALTERNATIVE to the bare-metal systemd deployment in
# deployment/systemd/ — choose one approach for the VPS, not both.
# Docker Compose is simpler to operate for a single-VPS deployment
# (one `docker compose up -d` brings up everything); systemd gives
# slightly more direct control and lower overhead. Either is production
# viable at this scale.
#
# Usage:
#   cp .env.example .env   # fill in real values first
#   docker compose -f deployment/docker/docker-compose.yml up -d
#   docker compose -f deployment/docker/docker-compose.yml exec api alembic upgrade head

services:
  postgres:
    image: postgres:16-alpine
    restart: unless-stopped
    environment:
      POSTGRES_USER: crop_user
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:-crop_pass}
      POSTGRES_DB: crop_db
    volumes:
      - postgres_data:/var/lib/postgresql/data
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U crop_user -d crop_db"]
      interval: 10s
      timeout: 5s
      retries: 5
    networks:
      - crop_ai_network

  redis:
    image: redis:7-alpine
    restart: unless-stopped
    command: redis-server --save 60 1 --loglevel warning
    volumes:
      - redis_data:/data
    healthcheck:
      test: ["CMD", "redis-cli", "ping"]
      interval: 10s
      timeout: 5s
      retries: 5
    networks:
      - crop_ai_network

  api:
    build:
      context: ../..
      dockerfile: deployment/docker/Dockerfile
    restart: unless-stopped
    env_file:
      - ../../.env
    environment:
      - DATABASE_URL=postgresql+asyncpg://crop_user:${POSTGRES_PASSWORD:-crop_pass}@postgres:5432/crop_db
      - REDIS_URL=redis://redis:6379/0
      - CELERY_BROKER_URL=redis://redis:6379/1
      - CELERY_RESULT_BACKEND=redis://redis:6379/2
    ports:
      - "127.0.0.1:8000:8000"
    volumes:
      - ../../data/models/v1:/app/data/models/v1:ro
      - ../../data/processed:/app/data/processed:ro
      - ../../logs:/app/logs
    depends_on:
      postgres:
        condition: service_healthy
      redis:
        condition: service_healthy
    networks:
      - crop_ai_network

  worker:
    build:
      context: ../..
      dockerfile: deployment/docker/Dockerfile
    restart: unless-stopped
    command: ["celery", "-A", "app.workers.celery_app", "worker",
              "--loglevel=info", "--concurrency=2", "--max-tasks-per-child=200"]
    env_file:
      - ../../.env
    environment:
      - DATABASE_URL=postgresql+asyncpg://crop_user:${POSTGRES_PASSWORD:-crop_pass}@postgres:5432/crop_db
      - REDIS_URL=redis://redis:6379/0
      - CELERY_BROKER_URL=redis://redis:6379/1
      - CELERY_RESULT_BACKEND=redis://redis:6379/2
    volumes:
      - ../../data/models/v1:/app/data/models/v1:ro
      - ../../data/processed:/app/data/processed:ro
      - ../../logs:/app/logs
    depends_on:
      postgres:
        condition: service_healthy
      redis:
        condition: service_healthy
    networks:
      - crop_ai_network
    # Scale workers independently of the API:
    # docker compose -f deployment/docker/docker-compose.yml up -d --scale worker=3

volumes:
  postgres_data:
  redis_data:

networks:
  crop_ai_network:
    driver: bridge
