# MIESC v4.0.0 - Docker Compose Configuration
# Multi-layer Intelligent Evaluation for Smart Contracts
# Now with ML Pipeline: FP filtering, severity prediction, clustering
#
# Usage:
#   Build and run:     docker-compose up --build
#   Run tests:         docker-compose run miesc-test
#   Run API server:    docker-compose up miesc-api
#   Run ML CLI:        docker-compose run miesc-ml
#   Interactive shell: docker-compose run miesc-shell

version: '3.8'

services:
  # Main MIESC service (runs tests by default)
  miesc:
    build:
      context: .
      dockerfile: Dockerfile
      args:
        - BUILDKIT_INLINE_CACHE=1
    image: miesc:4.0.0
    container_name: miesc-main
    environment:
      - MIESC_VERSION=4.0.0
      - MIESC_ENV=docker
      - PYTHONUNBUFFERED=1
    volumes:
      # Mount source code for development (optional, comment out for production)
      # - ./src:/app/src:ro
      # - ./tests:/app/tests:ro
      # Mount data directory for analysis results
      - miesc-data:/data
      # Mount contracts directory (if analyzing local contracts)
      - ./contracts:/app/contracts:ro
    networks:
      - miesc-network
    healthcheck:
      test: ["CMD", "python", "-c", "from src.agents.base_agent import BaseAgent; print('OK')"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 10s

  # MIESC ML CLI (ML-enhanced analysis)
  miesc-ml:
    build:
      context: .
      dockerfile: Dockerfile
    image: miesc:4.0.0
    container_name: miesc-ml
    environment:
      - MIESC_VERSION=4.0.0
      - MIESC_ENV=docker-ml
      - PYTHONUNBUFFERED=1
    command: python -m src.miesc_ml_cli --help
    volumes:
      - miesc-data:/data
      - ./contracts:/app/contracts:ro
    networks:
      - miesc-network
    profiles:
      - ml

  # MIESC Test Runner (explicitly runs test suite)
  miesc-test:
    build:
      context: .
      dockerfile: Dockerfile
    image: miesc:4.0.0
    container_name: miesc-test
    environment:
      - MIESC_VERSION=4.0.0
      - MIESC_ENV=docker-test
      - PYTHONUNBUFFERED=1
    command: >
      sh -c "echo '=== MIESC v4.0.0 - Test Suite ===' &&
             echo 'Running comprehensive tests including ML pipeline...' &&
             python -m pytest tests/ -v --tb=short --maxfail=5 --cov=src --cov-report=term-missing"
    volumes:
      - miesc-data:/data
    networks:
      - miesc-network
    profiles:
      - test

  # MIESC API Server (FastAPI)
  miesc-api:
    build:
      context: .
      dockerfile: Dockerfile
    image: miesc:4.0.0
    container_name: miesc-api
    environment:
      - MIESC_VERSION=4.0.0
      - MIESC_ENV=docker-api
      - PYTHONUNBUFFERED=1
    command: uvicorn src.api.main:app --host 0.0.0.0 --port 8000 --reload
    ports:
      - "8000:8000"
    volumes:
      - miesc-data:/data
      - ./contracts:/app/contracts:ro
    networks:
      - miesc-network
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:8000/health"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 15s
    profiles:
      - api

  # MIESC Interactive Shell (for development)
  miesc-shell:
    build:
      context: .
      dockerfile: Dockerfile
    image: miesc:4.0.0
    container_name: miesc-shell
    environment:
      - MIESC_VERSION=4.0.0
      - MIESC_ENV=docker-shell
      - PYTHONUNBUFFERED=1
    command: /bin/bash
    stdin_open: true
    tty: true
    volumes:
      - miesc-data:/data
      - ./contracts:/app/contracts
    networks:
      - miesc-network
    profiles:
      - dev

  # MIESC Analyzer (analyze specific contract)
  miesc-analyzer:
    build:
      context: .
      dockerfile: Dockerfile
    image: miesc:4.0.0
    container_name: miesc-analyzer
    environment:
      - MIESC_VERSION=4.0.0
      - MIESC_ENV=docker-analyzer
      - PYTHONUNBUFFERED=1
      - CONTRACT_PATH=${CONTRACT_PATH:-/app/contracts/example.sol}
    command: >
      sh -c "echo '=== MIESC v4.0.0 - Contract Analyzer ===' &&
             echo 'Analyzing: ${CONTRACT_PATH}' &&
             python -m src.cli.miesc_cli analyze ${CONTRACT_PATH} --output /data/report.json"
    volumes:
      - miesc-data:/data
      - ./contracts:/app/contracts:ro
    networks:
      - miesc-network
    profiles:
      - analyze

networks:
  miesc-network:
    driver: bridge
    name: miesc-network

volumes:
  miesc-data:
    driver: local
    name: miesc-data

# Usage Examples:
# ================
#
# 1. Build and run default (tests):
#    docker-compose up --build
#
# 2. Run test suite explicitly:
#    docker-compose run miesc-test
#
# 3. Run API server:
#    docker-compose --profile api up miesc-api
#
# 4. Interactive shell for development:
#    docker-compose --profile dev run miesc-shell
#
# 5. Analyze a specific contract:
#    CONTRACT_PATH=/app/contracts/MyToken.sol docker-compose --profile analyze run miesc-analyzer
#
# 6. Clean up:
#    docker-compose down -v
#    docker rmi miesc:4.0.0
