# ─────────────────────────────────────────────
# Infrastructure services (always-on, no profile)
# Run independently: docker compose up -d
# ─────────────────────────────────────────────
services:
  postgres:
    image: postgres:17-alpine
    container_name: sso-postgres
    restart: unless-stopped
    ports:
      - '${POSTGRES_PORT:-5432}:5432'
    environment:
      POSTGRES_USER: ${DB_USER:-postgres}
      POSTGRES_PASSWORD: ${DB_PASSWORD:-postgres}
      POSTGRES_DB: ${DB_NAME:-sso}
    volumes:
      - postgres_data:/var/lib/postgresql/data
    networks:
      - sso-network
    healthcheck:
      test: ['CMD-SHELL', 'pg_isready -U ${DB_USER:-postgres}']
      interval: 10s
      timeout: 5s
      retries: 5

  mongo:
    image: mongo:7
    container_name: sso-mongo
    restart: unless-stopped
    ports:
      - '${MONGO_PORT:-27017}:27017'
    environment:
      MONGO_INITDB_DATABASE: ${MONGODB_AUDIT_DATABASE:-sso_audit}
    volumes:
      - mongo_data:/data/db
    networks:
      - sso-network
    healthcheck:
      test: ['CMD', 'mongosh', '--eval', 'db.adminCommand({ping:1})']
      interval: 10s
      timeout: 5s
      retries: 5

  redis:
    image: redis:7-alpine
    container_name: sso-redis
    restart: unless-stopped
    ports:
      - '${REDIS_PORT:-6379}:6379'
    volumes:
      - redis_data:/data
    command: redis-server --appendonly yes
    networks:
      - sso-network
    healthcheck:
      test: ['CMD', 'redis-cli', 'ping']
      interval: 10s
      timeout: 5s
      retries: 3

  minio:
    image: minio/minio:RELEASE.2025-04-22T22-12-26Z
    container_name: sso-minio
    restart: unless-stopped
    ports:
      - '${MINIO_PORT:-9000}:9000'
      - '9001:9001'
    environment:
      MINIO_ROOT_USER: ${MINIO_ACCESS_KEY:-minioadmin}
      MINIO_ROOT_PASSWORD: ${MINIO_SECRET_KEY:-minioadmin123}
    volumes:
      - minio_data:/data
    command: server /data --console-address ':9001'
    networks:
      - sso-network
    healthcheck:
      test: ['CMD', 'curl', '-f', 'http://localhost:9000/minio/health/live']
      interval: 30s
      timeout: 10s
      retries: 3

  # ─────────────────────────────────────────────
  # Application (dev / staging / prod profile)
  # Run: docker compose --profile dev up app-dev -d
  #      docker compose --profile staging up app-staging -d
  #      docker compose --profile prod up app-prod -d
  # ─────────────────────────────────────────────
  app-dev:
    profiles: ['dev']
    build:
      context: .
      dockerfile: Dockerfile
      target: development
    container_name: sso-dev
    restart: unless-stopped
    ports:
      - '3001:3001'
    env_file:
      - .env
    environment:
      - DB_HOST=postgres
      - REDIS_HOST=redis
      - MINIO_ENDPOINT=minio
      - MONGODB_AUDIT_URL=mongodb://mongo:27017
      - CHOKIDAR_USEPOLLING=true
    volumes:
      - .:/usr/src/app
      - /usr/src/app/node_modules
    networks:
      - sso-network
    depends_on:
      postgres:
        condition: service_healthy
      mongo:
        condition: service_healthy
      redis:
        condition: service_healthy
      minio:
        condition: service_healthy
    healthcheck:
      test: ['CMD', 'curl', '-f', 'http://localhost:3001/health']
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 40s

  app-staging:
    profiles: ['staging']
    build:
      context: .
      dockerfile: Dockerfile
      target: production
    restart: unless-stopped
    ports:
      - '3002:3001'
    env_file:
      - .env.staging
    environment:
      - DB_HOST=postgres
      - REDIS_HOST=redis
      - MINIO_ENDPOINT=minio
      - MONGODB_AUDIT_URL=mongodb://mongo:27017
    networks:
      - sso-network
    depends_on:
      postgres:
        condition: service_healthy
      mongo:
        condition: service_healthy
      redis:
        condition: service_healthy
      minio:
        condition: service_healthy
    healthcheck:
      test: ['CMD', 'curl', '-f', 'http://localhost:3001/health']
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 40s

  app-prod:
    profiles: ['prod']
    build:
      context: .
      dockerfile: Dockerfile
      target: production
    restart: unless-stopped
    ports:
      - '3003:3001'
    env_file:
      - .env.prod
    environment:
      - DB_HOST=postgres
      - REDIS_HOST=redis
      - MINIO_ENDPOINT=minio
      - MONGODB_AUDIT_URL=mongodb://mongo:27017
    networks:
      - sso-network
    depends_on:
      postgres:
        condition: service_healthy
      mongo:
        condition: service_healthy
      redis:
        condition: service_healthy
      minio:
        condition: service_healthy
    healthcheck:
      test: ['CMD', 'curl', '-f', 'http://localhost:3001/health']
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 40s

networks:
  sso-network:
    driver: bridge

volumes:
  postgres_data:
  mongo_data:
  redis_data:
  minio_data:
