version: '3.8'

services:
  # PostgreSQL Database
  postgres:
    image: postgres:15-alpine
    container_name: roottrace-postgres
    environment:
      POSTGRES_USER: ${POSTGRES_USER:-roottrace}
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:-quantum2025}
      POSTGRES_DB: ${POSTGRES_DB:-roottrace_quantum}
    ports:
      - "5432:5432"
    volumes:
      - postgres_data:/var/lib/postgresql/data
      - ./database/init.sql:/docker-entrypoint-initdb.d/init.sql
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U roottrace"]
      interval: 10s
      timeout: 5s
      retries: 5

  # Redis Cache & Queue
  redis:
    image: redis:7-alpine
    container_name: roottrace-redis
    ports:
      - "6379:6379"
    volumes:
      - redis_data:/data
    command: redis-server --appendonly yes
    healthcheck:
      test: ["CMD", "redis-cli", "ping"]
      interval: 10s
      timeout: 5s
      retries: 5

  # Neo4j Graph Database
  neo4j:
    image: neo4j:5.15
    container_name: roottrace-neo4j
    environment:
      NEO4J_AUTH: neo4j/${NEO4J_PASSWORD:-quantum2025}
      NEO4J_PLUGINS: '["apoc", "graph-data-science"]'
      NEO4J_dbms_security_procedures_unrestricted: apoc.*,gds.*
    ports:
      - "7474:7474"  # HTTP
      - "7687:7687"  # Bolt
    volumes:
      - neo4j_data:/data
      - neo4j_logs:/logs
    healthcheck:
      test: ["CMD-SHELL", "wget --quiet --tries=1 --spider http://localhost:7474 || exit 1"]
      interval: 30s
      timeout: 10s
      retries: 5

  # FastAPI Application
  api:
    build:
      context: .
      dockerfile: Dockerfile
    container_name: roottrace-api
    environment:
      DATABASE_URL: postgresql://roottrace:quantum2025@postgres:5432/roottrace_quantum
      REDIS_URL: redis://redis:6379/0
      NEO4J_URI: bolt://neo4j:7687
      NEO4J_USER: neo4j
      NEO4J_PASSWORD: ${NEO4J_PASSWORD:-quantum2025}
      IBMQ_TOKEN: ${IBMQ_TOKEN:-}
      JWT_SECRET_KEY: ${JWT_SECRET_KEY:-dev_secret_key_change_in_production}
    ports:
      - "8000:8000"
    depends_on:
      postgres:
        condition: service_healthy
      redis:
        condition: service_healthy
    volumes:
      - ./quantum_ancestry_engine.py:/app/quantum_ancestry_engine.py
      - ./api_server.py:/app/api_server.py
    command: uvicorn api_server:app --host 0.0.0.0 --port 8000 --reload

  # Celery Worker for Background Jobs
  worker:
    build:
      context: .
      dockerfile: Dockerfile
    container_name: roottrace-worker
    environment:
      DATABASE_URL: postgresql://roottrace:quantum2025@postgres:5432/roottrace_quantum
      REDIS_URL: redis://redis:6379/0
      IBMQ_TOKEN: ${IBMQ_TOKEN:-}
    depends_on:
      - redis
      - postgres
    command: celery -A tasks worker --loglevel=info

  # Nginx Reverse Proxy (optional for production)
  nginx:
    image: nginx:alpine
    container_name: roottrace-nginx
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - ./nginx.conf:/etc/nginx/nginx.conf:ro
      - ./landing_page.html:/usr/share/nginx/html/index.html:ro
      - ./ssl:/etc/nginx/ssl:ro
    depends_on:
      - api

volumes:
  postgres_data:
  redis_data:
  neo4j_data:
  neo4j_logs:

networks:
  default:
    name: roottrace-network
