# Postgres with pgvector + RAG Agent app (from repo git@git.lesha.spb.ru:alex/RagAgent.git).
# Clone the repo, then: docker compose up -d
# App and DB share network "rag_net"; app uses RAG_DB_DSN with host=postgres.
# DB init: scripts in docker/postgres-init/ run on first start (empty volume); to disable, comment out the init volume.

services:
  postgres:
    image: pgvector/pgvector:pg16
    container_name: rag-postgres
    environment:
      POSTGRES_USER: ${POSTGRES_USER:-rag}
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:-rag_secret}
      POSTGRES_DB: ${POSTGRES_DB:-rag}
    ports:
      - "${POSTGRES_PORT:-5432}:5432"
    volumes:
      # PG 18+: mount at /var/lib/postgresql (data goes in versioned subdir). For pg16 use /var/lib/postgresql/data.
      - rag_pgdata:/var/lib/postgresql
      # Init scripts run once on first start (create extension, tables). Optional: comment out to skip.
      - ./docker/postgres-init:/docker-entrypoint-initdb.d:ro
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U ${POSTGRES_USER:-rag} -d ${POSTGRES_DB:-rag}"]
      interval: 5s
      timeout: 5s
      retries: 5
    networks:
      - rag_net

  app:
    build:
      context: .
      dockerfile: Dockerfile
    image: rag-agent:latest
    container_name: rag-agent
    restart: unless-stopped
    depends_on:
      postgres:
        condition: service_healthy
    ports:
      - "${WEBHOOK_PORT:-8000}:8000"
    environment:
      RAG_DB_DSN: "postgresql://${POSTGRES_USER:-rag}:${POSTGRES_PASSWORD:-rag_secret}@postgres:5432/${POSTGRES_DB:-rag}"
      # In container repo is always at /data (mounted below). Use RAG_REPO_HOST in .env for host path.
      RAG_REPO_PATH: "/data"
      # Accept host key on first connect; git fetch uses SSH from /root/.ssh (mounted below).
      GIT_SSH_COMMAND: "ssh -o StrictHostKeyChecking=accept-new"
      RAG_EMBEDDINGS_DIM: ${RAG_EMBEDDINGS_DIM:-1024}
      GIGACHAT_CREDENTIALS: ${GIGACHAT_CREDENTIALS:-}
      GIGACHAT_EMBEDDINGS_MODEL: ${GIGACHAT_EMBEDDINGS_MODEL:-Embeddings}
      WEBHOOK_SECRET: ${WEBHOOK_SECRET:-}
    volumes:
      # Host path: set RAG_REPO_HOST in .env (e.g. /Users/you/repo). Falls back to RAG_REPO_PATH then ./data.
      - ${RAG_REPO_HOST:-${RAG_REPO_PATH:-./data}}:/data
      # SSH for git fetch (webhook): put deploy key and known_hosts in RAG_SSH_DIR. See docker/ssh/README.md.
      - ${RAG_SSH_DIR:-./docker/ssh}:/root/.ssh:ro
    entrypoint: ["rag-agent"]
    command: ["serve", "--host", "0.0.0.0", "--port", "8000"]
    networks:
      - rag_net

  bot:
    build:
      context: .
      dockerfile: Dockerfile
    image: rag-agent:latest
    container_name: rag-bot
    restart: unless-stopped
    depends_on:
      postgres:
        condition: service_healthy
    environment:
      RAG_DB_DSN: "postgresql://${POSTGRES_USER:-rag}:${POSTGRES_PASSWORD:-rag_secret}@postgres:5432/${POSTGRES_DB:-rag}"
      RAG_REPO_PATH: "/data"
      RAG_EMBEDDINGS_DIM: ${RAG_EMBEDDINGS_DIM:-1024}
      GIGACHAT_CREDENTIALS: ${GIGACHAT_CREDENTIALS:-}
      GIGACHAT_EMBEDDINGS_MODEL: ${GIGACHAT_EMBEDDINGS_MODEL:-Embeddings}
      TELEGRAM_BOT_TOKEN: ${TELEGRAM_BOT_TOKEN:-}
      RAG_BOT_VERBOSE_LOGGING: ${RAG_BOT_VERBOSE_LOGGING:-true}
    volumes:
      - ${RAG_REPO_HOST:-${RAG_REPO_PATH:-./data}}:/data
    entrypoint: ["rag-agent"]
    command: ["bot"]
    networks:
      - rag_net

networks:
  rag_net:
    driver: bridge

volumes:
  rag_pgdata:
