services:
  redis:
    image: redis:7-alpine
    container_name: redis
    restart: unless-stopped
    ports:
      - "6379:6379"
    volumes:
      - redis_data:/data
    command: redis-server --appendonly yes
    networks:
      - app-network
    healthcheck:
      test: ["CMD", "redis-cli", "ping"]
      interval: 10s
      timeout: 5s
      retries: 5

  qdrant:
    image: qdrant/qdrant:latest
    container_name: qdrant
    restart: unless-stopped
    ports:
      - "6333:6333"
      - "6334:6334"
    volumes:
      - qdrant_data:/qdrant/storage
    networks:
      - app-network

  qdrant-init:
    image: python:3.11-alpine
    container_name: qdrant-init
    restart: "no"
    working_dir: /app
    volumes:
      - ./qdrant_init/entrypoint.py:/app/entrypoint.py
    command: ["python", "-u", "/app/entrypoint.py"]
    depends_on:
      - qdrant
    environment:
      - QDRANT_URL=http://qdrant:6333
      - QDRANT_COLLECTION_CHAPTER_ANALYSES=chapter_analyses
      - QDRANT_VECTOR_SIZE=1024
      - QDRANT_INIT_DELAY=15
    networks:
      - app-network

  ollama:
    image: ollama/ollama:latest
    container_name: ollama
    restart: unless-stopped
    ports:
      - "11434:11434"
    volumes:
      - ollama_data:/root/.ollama
    networks:
      - app-network
    environment:
      - OLLAMA_HOST=0.0.0.0:11434
      # Контекст для qwen3:14b (анализ глав). Рекомендация ARCHITECTURE: 12000–13000.
      - OLLAMA_NUM_CTX=12000
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:11434/api/tags"]
      interval: 30s
      timeout: 10s
      retries: 3

  epub-parser:
    build:
      context: ./epub-parser
      dockerfile: Dockerfile
    container_name: epub-parser
    restart: unless-stopped
    ports:
      - "5001:5000"
    networks:
      - app-network
    healthcheck:
      test: ["CMD", "python", "-c", "import urllib.request; urllib.request.urlopen('http://localhost:5000/health')"]
      interval: 10s
      timeout: 5s
      retries: 5
  postgres:
    image: postgres:15-alpine
    container_name: postgres
    restart: unless-stopped
    ports:
      - "5432:5432"
    environment:
      - POSTGRES_USER=n8n
      - POSTGRES_PASSWORD=n8n_password
      - POSTGRES_DB=n8n
    volumes:
      - postgres_data:/var/lib/postgresql/data
      - ./8_сохранение_postgres/schema.sql:/docker-entrypoint-initdb.d/01-schema.sql:ro
    networks:
      - app-network
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U n8n"]
      interval: 10s
      timeout: 5s
      retries: 5

volumes:
  redis_data:
  qdrant_data:
  ollama_data:
  n8n_data:
  postgres_data:

networks:
  app-network:
    driver: bridge
