services:
  ai-service:
    build:
      context: ./ai-service
      dockerfile: Dockerfile
    container_name: ai-service
    restart: unless-stopped
    ports:
      - "8182:8080"
    environment:
      - AI_SERVICE_DEBUG=false
      - AI_SERVICE_LOG_LEVEL=INFO
      - AI_SERVICE_DATABASE_URL=postgresql+asyncpg://postgres:postgres@postgres:5432/ai_service
      - AI_SERVICE_QDRANT_URL=http://qdrant:6333
      - AI_SERVICE_LLM_PROVIDER=${AI_SERVICE_LLM_PROVIDER:-openai}
      - AI_SERVICE_LLM_API_KEY=${AI_SERVICE_LLM_API_KEY:-}
      - AI_SERVICE_LLM_BASE_URL=${AI_SERVICE_LLM_BASE_URL:-https://api.openai.com/v1}
      - AI_SERVICE_LLM_MODEL=${AI_SERVICE_LLM_MODEL:-gpt-4o-mini}
      - AI_SERVICE_OLLAMA_BASE_URL=${AI_SERVICE_OLLAMA_BASE_URL:-http://ollama:11434}
    volumes:
      - ai_service_config:/app/config
    depends_on:
      postgres:
        condition: service_healthy
      qdrant:
        condition: service_started
    networks:
      - ai-network
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:8080/ai/health"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 40s

  ai-service-admin:
    build:
      context: ./ai-service-admin
      dockerfile: Dockerfile
      args:
        VITE_APP_API_KEY: ${VITE_APP_API_KEY:-}
        VITE_APP_BASE_API: /api
    container_name: ai-service-admin
    restart: unless-stopped
    ports:
      - "8183:80"
    depends_on:
      - ai-service
    networks:
      - ai-network

  postgres:
    image: postgres:15-alpine
    container_name: ai-postgres
    restart: unless-stopped
    environment:
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=postgres
      - POSTGRES_DB=ai_service
    volumes:
      - postgres_data:/var/lib/postgresql/data
      - ./ai-service/scripts/init_db.sql:/docker-entrypoint-initdb.d/init_db.sql:ro
    ports:
      - "5432:5432"
    networks:
      - ai-network
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U postgres -d ai_service"]
      interval: 10s
      timeout: 5s
      retries: 5

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

  ollama:
    image: ollama/ollama:latest
    container_name: ai-ollama
    restart: unless-stopped
    ports:
      - "11434:11434"
    volumes:
      - ollama_data:/root/.ollama
    networks:
      - ai-network
    deploy:
      resources:
        reservations:
          memory: 1G

networks:
  ai-network:
    driver: bridge

volumes:
  postgres_data:
  qdrant_data:
  ollama_data:
  ai_service_config:
