version: '3.8'

services:
  # Nginx as API Gateway and Reverse Proxy
  nginx:
    container_name: nginx-gateway
    build:
      context: .
      dockerfile: docker/nginx/Dockerfile
    ports:
      - "7080:80"
      - "7443:443"
    volumes:
      - ./docker/nginx/conf.d/:/etc/nginx/conf.d/
      - ./docker/nginx/ssl:/etc/nginx/ssl
    depends_on:
      - auth-manamana
      - account-manamana
      - data-manamana
      - frontend-manamana
    networks:
      - manamana_network
    restart: unless-stopped

  # Frontend - Next.js Application
  frontend-manamana:

    container_name: frontend-manamana
    build:
      context: ./frontend
      dockerfile: ../docker/frontend/Dockerfile
      
    ports:
      - "3000:3000"
    environment:
      - NODE_ENV=production
      - NEXT_PUBLIC_API_URL=http://nginx/api/
      - NEXT_PUBLIC_WS_URL=ws://nginx/ws
    volumes:
      - ./frontend:/app
      - /app/node_modules
      - /app/.next
    networks:
      - manamana_network
    restart: unless-stopped

  # Authentication Service - FastAPI
  auth-manamana:
    container_name: auth-manamana
    build:
      context: ./backend/auth-service
      dockerfile: ../../docker/backend/Dockerfile
    ports:
      - "9001:8000"
    environment:
      - ENVIRONMENT=production
      - MONGODB_URI=mongodb://mongodb:27017/authdb
      - KAFKA_BOOTSTRAP_SERVERS=kafka:9092
      - JWT_SECRET_KEY=${JWT_SECRET_KEY:-your-secret-key}
      - REDIS_URL=redis://redis:6379
    volumes:
      - ./backend/auth-service:/app
    depends_on:
      - mongodb
      - kafka
      - redis
    networks:
      - manamana_network
    restart: unless-stopped
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:8000/health"]
      interval: 30s
      timeout: 10s
      retries: 3

  # Account Management Service - FastAPI
  account-manamana:
    container_name: account-manamana
    build:
      context: ./backend/account-service
      dockerfile: ../../docker/backend/Dockerfile
    ports:
      - "9002:8000"
    environment:
      - ENVIRONMENT=production
      - MONGODB_URI=mongodb://mongodb:27017/accountdb
      - KAFKA_BOOTSTRAP_SERVERS=kafka:9092
      - JWT_SECRET_KEY=${JWT_SECRET_KEY:-your-secret-key}
      - REDIS_URL=redis://redis:6379
    volumes:
      - ./backend/account-service:/app
      - ./backend/account-service/uploads:/app/uploads
    depends_on:
      - mongodb
      - kafka
      - redis
    networks:
      - manamana_network
    restart: unless-stopped
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:8000/health"]
      interval: 30s
      timeout: 10s
      retries: 3

  # Data Service - FastAPI
  data-manamana:
    container_name: data-manamana
    build:
      context: ./backend/data-service
      dockerfile: ../../docker/backend/Dockerfile
    ports:
      - "9003:8000"
    environment:
      - ENVIRONMENT=production
      - MONGODB_URI=mongodb://mongodb:27017/datadb
      - KAFKA_BOOTSTRAP_SERVERS=kafka:9092
      - JWT_SECRET_KEY=${JWT_SECRET_KEY:-your-secret-key}
      - REDIS_URL=redis://redis:6379
    volumes:
      - ./backend/data-service:/app
    depends_on:
      - mongodb
      - kafka
      - redis
    networks:
      - manamana_network
    restart: unless-stopped
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:8000/health"]
      interval: 30s
      timeout: 10s
      retries: 3

  # MongoDB Database
  mongodb:
    container_name: mongodb
    image: mongo:7.0
    ports:
      - "27037:27017"
    environment:
      - MONGO_INITDB_ROOT_USERNAME=admin
      - MONGO_INITDB_ROOT_PASSWORD=password123
      - MONGO_INITDB_DATABASE=pige_db
    volumes:
      - mongodb_data:/data/db
      - ./docker/database/init-mongo.js:/docker-entrypoint-initdb.d/init-mongo.js:ro
    networks:
      - manamana_network
    restart: unless-stopped
    command: mongod --auth

  # MongoDB Express - Database Admin UI
  mongo-express:
    container_name: mongo-express
    image: mongo-express:1.0.0
    ports:
      - "8083:8081"
    environment:
      - ME_CONFIG_MONGODB_ADMINUSERNAME=admin
      - ME_CONFIG_MONGODB_ADMINPASSWORD=password123
      - ME_CONFIG_MONGODB_SERVER=mongodb
      - ME_CONFIG_MONGODB_PORT=27017
      - ME_CONFIG_BASICAUTH_USERNAME=admin
      - ME_CONFIG_BASICAUTH_PASSWORD=admin
    depends_on:
      - mongodb
    networks:
      - manamana_network
    restart: unless-stopped

  # Redis - Caching and Session Store
  redis:
    container_name: redis
    image: redis:7.2-alpine 
    volumes:
      - redis_data:/data
    networks:
      - manamana_network
    restart: unless-stopped
    command: redis-server --appendonly yes

  # Zookeeper - Required for Kafka
  zookeeper:
    container_name: zookeeper
    image: confluentinc/cp-zookeeper:7.4.0
    environment:
      ZOOKEEPER_CLIENT_PORT: 2181
      ZOOKEEPER_TICK_TIME: 2000
    volumes:
      - zookeeper_data:/var/lib/zookeeper/data
      - zookeeper_logs:/var/lib/zookeeper/log
    networks:
      - manamana_network
    restart: unless-stopped

  # Kafka - Message Broker
  kafka:
    container_name: kafka
    image: confluentinc/cp-kafka:7.4.0
    depends_on:
      - zookeeper
    ports:
      - "9192:9092"
      - "9194:9094"
    environment:
      KAFKA_BROKER_ID: 1
      KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181
      KAFKA_LISTENER_SECURITY_PROTOCOL_MAP: PLAINTEXT:PLAINTEXT,PLAINTEXT_HOST:PLAINTEXT
      KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://kafka:29092,PLAINTEXT_HOST://localhost:9092
      KAFKA_INTER_BROKER_LISTENER_NAME: PLAINTEXT
      KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR: 1
      KAFKA_TRANSACTION_STATE_LOG_MIN_ISR: 1
      KAFKA_TRANSACTION_STATE_LOG_REPLICATION_FACTOR: 1
      KAFKA_AUTO_CREATE_TOPICS_ENABLE: 'true'
    volumes:
      - kafka_data:/var/lib/kafka/data
    networks:
      - manamana_network
    restart: unless-stopped
    healthcheck:
      test: ["CMD", "kafka-broker-api-versions", "--bootstrap-server", "localhost:9092"]
      interval: 30s
      timeout: 10s
      retries: 3

  # Kafka UI - Kafka Management Interface
  kafka-ui:
    container_name: kafka-ui
    image: provectuslabs/kafka-ui:latest
    ports:
      - "8180:8080"
    environment:
      KAFKA_CLUSTERS_0_NAME: local
      KAFKA_CLUSTERS_0_BOOTSTRAPSERVERS: kafka:29092
      KAFKA_CLUSTERS_0_ZOOKEEPER: zookeeper:2181
    depends_on:
      - kafka
      - zookeeper
    networks:
      - manamana_network
    restart: unless-stopped

  # Elasticsearch - Search and Analytics
  elasticsearch:
    container_name: elasticsearch
    image: docker.elastic.co/elasticsearch/elasticsearch:8.11.0
    environment:
      - discovery.type=single-node
      - xpack.security.enabled=false
      - "ES_JAVA_OPTS=-Xms512m -Xmx512m"
    ports:
      - "9200:9200"
    volumes:
      - elasticsearch_data:/usr/share/elasticsearch/data
    networks:
      - manamana_network
    restart: unless-stopped

  # Kibana - Elasticsearch Dashboard
  kibana:
    container_name: kibana
    image: docker.elastic.co/kibana/kibana:8.11.0
    ports:
      - "5601:5601"
    environment:
      - ELASTICSEARCH_HOSTS=http://elasticsearch:9200
    depends_on:
      - elasticsearch
    networks:
      - manamana_network
    restart: unless-stopped

  # Prometheus - Monitoring
  prometheus:
    container_name: prometheus
    image: prom/prometheus:latest
    ports:
      - "9090:9090"
    volumes:
      - ./docker/monitoring/prometheus.yml:/etc/prometheus/prometheus.yml
      - prometheus_data:/prometheus
    command:
      - '--config.file=/etc/prometheus/prometheus.yml'
      - '--storage.tsdb.path=/prometheus'
      - '--web.console.libraries=/etc/prometheus/console_libraries'
      - '--web.console.templates=/etc/prometheus/consoles'
      - '--web.enable-lifecycle'
    networks:
      - manamana_network
    restart: unless-stopped

  # Grafana - Monitoring Dashboard
  grafana:
    container_name: grafana
    image: grafana/grafana:latest
    ports:
      - "3002:3000"
    environment:
      - GF_SECURITY_ADMIN_PASSWORD=admin
    volumes:
      - grafana_data:/var/lib/grafana
    depends_on:
      - prometheus
    networks:
      - manamana_network
    restart: unless-stopped

# Networks
networks:
  manamana_network:
    driver: bridge
    ipam:
      config:
        - subnet: 172.33.0.0/16

# Volumes
volumes:
  mongodb_data:
    driver: local
  redis_data:
    driver: local
  kafka_data:
    driver: local
  zookeeper_data:
    driver: local
  zookeeper_logs:
    driver: local
  elasticsearch_data:
    driver: local
  prometheus_data:
    driver: local
  grafana_data:
    driver: local

 