# docker-compose.yml
version: '3.8'

services:
  # MongoDB Database
  mongodb:
    image: mongo:6.0
    container_name: tender-mongodb
    restart: always
    environment:
      MONGO_INITDB_ROOT_USERNAME: ${MONGO_INITDB_ROOT_USERNAME:-admin}
      MONGO_INITDB_ROOT_PASSWORD: ${MONGO_INITDB_ROOT_PASSWORD:-password}
      MONGO_INITDB_DATABASE: ${MONGO_INITDB_DATABASE:-tenderdb}
    ports:
      - "27017:27017"
    volumes:
      - mongodb_data:/data/db
      - ./docker/mongo-init.js:/docker-entrypoint-initdb.d/mongo-init.js:ro
    networks:
      - tender_network

  # Node.js Backend
  backend:
    build:
      context: ./server
      dockerfile: Dockerfile
    container_name: tender-backend
    restart: always
    environment:
      - NODE_ENV=production
      - PORT=5000
      - MONGODB_URI=mongodb://${MONGO_INITDB_ROOT_USERNAME:-admin}:${MONGO_INITDB_ROOT_PASSWORD:-password}@mongodb:27017/${MONGO_INITDB_DATABASE:-tenderdb}?authSource=admin
      - JWT_SECRET=${JWT_SECRET}
      - AWS_ACCESS_KEY_ID=${AWS_ACCESS_KEY_ID}
      - AWS_SECRET_ACCESS_KEY=${AWS_SECRET_ACCESS_KEY}
      - AWS_REGION=${AWS_REGION:-ap-south-1}
      - AWS_S3_BUCKET=${AWS_S3_BUCKET}
      - GOOGLE_CLIENT_ID=${GOOGLE_CLIENT_ID}
      - GOOGLE_CLIENT_SECRET=${GOOGLE_CLIENT_SECRET}
      - GOOGLE_REDIRECT_URI=${GOOGLE_REDIRECT_URI}
      - AI_SERVICE_URL=http://ai-engine:8000
      - AI_SERVICE_API_KEY=${AI_SERVICE_API_KEY}
      - PAYGLOCAL_API_KEY=${PAYGLOCAL_API_KEY}
      - PAYGLOCAL_SECRET_KEY=${PAYGLOCAL_SECRET_KEY}
      - PAYGLOCAL_MERCHANT_ID=${PAYGLOCAL_MERCHANT_ID}
      - PAYGLOCAL_BASE_URL=${PAYGLOCAL_BASE_URL}
      - CLIENT_URL=${CLIENT_URL:-http://localhost:3000}
      - API_URL=${API_URL:-http://localhost:5000}
      - DOCUMENT_ENCRYPTION_KEY=${DOCUMENT_ENCRYPTION_KEY}
    ports:
      - "5000:5000"
    volumes:
      - ./server:/app
      - /app/node_modules
      - temp_data:/app/temp
    depends_on:
      - mongodb
    networks:
      - tender_network

  # React Frontend
  frontend:
    build:
      context: ./client
      dockerfile: Dockerfile
      args:
        - REACT_APP_API_URL=${API_URL:-http://localhost:5000/api}
    container_name: tender-frontend
    restart: always
    environment:
      - NODE_ENV=production
    ports:
      - "3000:80"
    depends_on:
      - backend
    networks:
      - tender_network

  # Python AI Engine
  ai-engine:
    build:
      context: ./ai-engine
      dockerfile: Dockerfile
    container_name: tender-ai-engine
    restart: always
    environment:
      - ANTHROPIC_API_KEY=${ANTHROPIC_API_KEY}
      - MONGODB_URI=mongodb://${MONGO_INITDB_ROOT_USERNAME:-admin}:${MONGO_INITDB_ROOT_PASSWORD:-password}@mongodb:27017/${MONGO_INITDB_DATABASE:-tenderdb}?authSource=admin
      - AWS_ACCESS_KEY_ID=${AWS_ACCESS_KEY_ID}
      - AWS_SECRET_ACCESS_KEY=${AWS_SECRET_ACCESS_KEY}
      - AWS_REGION=${AWS_REGION:-ap-south-1}
      - AWS_S3_BUCKET=${AWS_S3_BUCKET}
      - API_KEY=${AI_SERVICE_API_KEY}
    ports:
      - "8000:8000"
    volumes:
      - ./ai-engine:/app
      - temp_data:/app/temp
    depends_on:
      - mongodb
    networks:
      - tender_network

# Networks
networks:
  tender_network:
    driver: bridge

# Volumes
volumes:
  mongodb_data:
    driver: local
  temp_data:
    driver: local