#
# Copyright 2025 The Apache Software Foundation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

# Docker Compose configuration for mail-mcp
#
# Quick Start:
#   mkdir -p data && docker compose up -d
#
# For development (build from source):
#   docker compose build && docker compose up -d

services:
  # Elasticsearch for storing and searching email data
  elasticsearch:
    image: docker.elastic.co/elasticsearch/elasticsearch:8.11.0
    container_name: mail-mcp-elasticsearch
    environment:
      - discovery.type=single-node
      - xpack.security.enabled=false
      - "ES_JAVA_OPTS=-Xms512m -Xmx512m"
      - cluster.name=mail-mcp-cluster
      - node.name=mail-mcp-node
    ports:
      - "59200:9200"
      - "59300:9300"
    volumes:
      - es-data:/usr/share/elasticsearch/data
    healthcheck:
      test: ["CMD-SHELL", "curl -f http://localhost:9200/_cluster/health || exit 1"]
      interval: 10s
      timeout: 5s
      retries: 5
    networks:
      - mail-mcp-network
    restart: unless-stopped

  # Kibana for data visualization and exploration (optional, start manually)
  # Start with: docker compose --profile analysis up -d kibana
  kibana:
    profiles:
      - analysis
    image: docker.elastic.co/kibana/kibana:8.11.0
    container_name: mail-mcp-kibana
    environment:
      - ELASTICSEARCH_HOSTS=http://elasticsearch:9200
      - SERVER_NAME=mail-mcp-kibana
      - SERVER_HOST=0.0.0.0
    ports:
      - "55601:5601"
    depends_on:
      elasticsearch:
        condition: service_healthy
    healthcheck:
      test: ["CMD-SHELL", "curl -f http://localhost:5601/api/status || exit 1"]
      interval: 30s
      timeout: 10s
      retries: 5
      start_period: 60s
    networks:
      - mail-mcp-network
    restart: unless-stopped

  # mail-mcp MCP server
  mail-mcp:
    image: ghcr.io/support-and-care-labs/mail-mcp:latest
    build:
      context: .
      dockerfile: Dockerfile
    container_name: mail-mcp-server
    environment:
      - MAIL_MCP_ELASTICSEARCH_URL=http://elasticsearch:9200
      - MAIL_MCP_ELASTICSEARCH_INDEX_PREFIX=maven
      - MAIL_MCP_DATA_PATH=/app/data
      - MAIL_MCP_LOG_LEVEL=INFO
      - PYTHONUNBUFFERED=1
    volumes:
      # Mount data directory for mbox files
      - ./data:/app/data:ro
      # Mount source code for development (optional)
      - ./src:/app/src:ro
      # Mount maven-jira-projects.toml
      - ./maven-jira-projects.toml:/app/maven-jira-projects.toml:ro
    depends_on:
      elasticsearch:
        condition: service_healthy
    networks:
      - mail-mcp-network
    restart: unless-stopped
    # HTTP transport exposed on port 58080
    ports:
      - "58080:8080"
    healthcheck:
      test: ["CMD", "python", "-c", "import urllib.request; urllib.request.urlopen('http://localhost:8080/health').read()"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 10s

  # Scheduler for periodic mbox updates
  # Runs hourly to fetch the current month's mbox and re-index into Elasticsearch
  scheduler:
    image: ghcr.io/support-and-care-labs/mail-mcp:latest
    build:
      context: .
      dockerfile: Dockerfile
    container_name: mail-mcp-scheduler
    environment:
      - MAIL_MCP_ELASTICSEARCH_URL=http://elasticsearch:9200
      - MAIL_MCP_ELASTICSEARCH_INDEX_PREFIX=maven
      - MAIL_MCP_DATA_PATH=/app/data
      - MAIL_MCP_MAILING_LISTS=dev@maven.apache.org,users@maven.apache.org
      - MAIL_MCP_LOG_LEVEL=INFO
      - PYTHONUNBUFFERED=1
    volumes:
      # Mount data directory as writable for scheduler to update mbox files
      - ./data:/app/data:rw
      # Mount source code for development (optional)
      - ./src:/app/src:ro
    depends_on:
      elasticsearch:
        condition: service_healthy
    networks:
      - mail-mcp-network
    restart: unless-stopped
    # Override default command to run supercronic scheduler
    command: ["/usr/local/bin/supercronic", "-json", "/app/crontab"]

volumes:
  es-data:
    driver: local

networks:
  mail-mcp-network:
    driver: bridge
