services:
  mongodb:
    image: mongo:7
    container_name: meshitrack-mongodb
    restart: unless-stopped
    ports:
      - '27017:27017'
    environment:
      MONGO_INITDB_ROOT_USERNAME: meshitrack
      MONGO_INITDB_ROOT_PASSWORD: ${MONGO_PASSWORD:-devpassword}
      MONGO_INITDB_DATABASE: meshitrack
    volumes:
      - mongo-data:/data/db
      - ./mongo/init-replica.js:/docker-entrypoint-initdb.d/init-replica.js:ro
      - ./mongo/keyfile:/tmp/mongo-keyfile:ro
      - ./mongo/docker-entrypoint-init.sh:/usr/local/bin/docker-entrypoint-init.sh:ro
    entrypoint: ['/bin/bash', '/usr/local/bin/docker-entrypoint-init.sh']
    command: ['--replSet', 'rs0', '--bind_ip_all', '--keyFile', '/etc/mongodb/keyfile']
    healthcheck:
      test: ['CMD', 'mongosh', '--eval', "db.adminCommand('ping')"]
      interval: 10s
      timeout: 5s
      retries: 5

  keycloak:
    image: quay.io/keycloak/keycloak:24.0
    container_name: meshitrack-keycloak
    restart: unless-stopped
    ports:
      - '8080:8080'
    environment:
      KC_DB: dev-file
      KEYCLOAK_ADMIN: admin
      KEYCLOAK_ADMIN_PASSWORD: ${KC_ADMIN_PASSWORD:-admin}
      KC_HEALTH_ENABLED: 'true'
      KC_HOSTNAME_URL: http://localhost:8080
    command: start-dev --import-realm
    volumes:
      - keycloak-data:/opt/keycloak/data
      - ./keycloak/realm-export.json:/opt/keycloak/data/import/realm-export.json:ro
    healthcheck:
      test:
        [
          'CMD',
          'bash',
          '-c',
          "exec 3<>/dev/tcp/127.0.0.1/8080 && echo -e 'GET /health/ready HTTP/1.0\\r\\n\\r\\n' >&3 && cat <&3 | grep -q 'UP'",
        ]
      interval: 15s
      timeout: 10s
      retries: 15
      start_period: 60s

  mongo-init:
    image: mongo:7
    container_name: meshitrack-mongo-init
    restart: on-failure
    depends_on:
      mongodb:
        condition: service_healthy
    entrypoint: >
      mongosh --host mongodb:27017
        --username meshitrack
        --password ${MONGO_PASSWORD:-devpassword}
        --authenticationDatabase admin
        --eval "
          try { rs.initiate({_id:'rs0',members:[{_id:0,host:'mongodb:27017'}]}) }
          catch(e) { if(e.codeName !== 'AlreadyInitialized') throw e }
        "

  api:
    build:
      context: ..
      dockerfile: docker/Dockerfile.api
      target: development
    container_name: meshitrack-api
    restart: unless-stopped
    ports:
      - '3001:3001'
    depends_on:
      mongo-init:
        condition: service_completed_successfully
      keycloak:
        condition: service_healthy
    environment:
      NODE_ENV: development
      PORT: 3001
      MONGODB_URI: mongodb://meshitrack:${MONGO_PASSWORD:-devpassword}@mongodb:27017/meshitrack?authSource=admin&replicaSet=rs0
      KEYCLOAK_URL: http://keycloak:8080
      KEYCLOAK_ISSUER_URL: http://localhost:8080
      KEYCLOAK_REALM: meshitrack
      KEYCLOAK_CLIENT_ID: meshitrack-api
      CORS_ORIGIN: http://localhost:3000
    volumes:
      - ../packages/api/src:/app/packages/api/src
      - ../packages/shared/src:/app/packages/shared/src

  web:
    build:
      context: ..
      dockerfile: docker/Dockerfile.web
      target: development
    container_name: meshitrack-web
    restart: unless-stopped
    ports:
      - '3000:3000'
    depends_on:
      - api
    environment:
      NODE_ENV: development
      NEXT_PUBLIC_API_URL: http://localhost:3001/api/v1
      NEXT_PUBLIC_KEYCLOAK_URL: http://localhost:8080
      NEXT_PUBLIC_KEYCLOAK_REALM: meshitrack
      NEXT_PUBLIC_KEYCLOAK_CLIENT_ID: meshitrack-web
      KEYCLOAK_URL: http://keycloak:8080
      KEYCLOAK_REALM: meshitrack
      KEYCLOAK_CLIENT_ID: meshitrack-web
      KEYCLOAK_CLIENT_SECRET: dev-web-client-secret
      NEXTAUTH_URL: http://localhost:3000
      NEXTAUTH_SECRET: ${NEXTAUTH_SECRET:-dev-secret-change-in-production}
    volumes:
      - ../packages/web/src:/app/packages/web/src

  mongo-express:
    image: mongo-express
    container_name: meshitrack-mongo-express
    ports:
      - '8081:8081'
    depends_on:
      mongodb:
        condition: service_healthy
    environment:
      ME_CONFIG_MONGODB_ADMINUSERNAME: meshitrack
      ME_CONFIG_MONGODB_ADMINPASSWORD: ${MONGO_PASSWORD:-devpassword}
      ME_CONFIG_MONGODB_URL: mongodb://meshitrack:${MONGO_PASSWORD:-devpassword}@mongodb:27017/
    profiles:
      - dev

volumes:
  mongo-data:
  keycloak-data:
