services:
  db:
    image: postgres:latest
    restart: unless-stopped
    env_file:
      - .env
    volumes:
      - postgres_data:/var/lib/postgresql/data
    ports:
      - "5432:5432"
    environment:
      POSTGRES_DB: ${DB_NAME}
      POSTGRES_USER: ${DB_USER}
      POSTGRES_PASSWORD: ${DB_PASSWORD}

  redis:
    image: redis:latest
    restart: unless-stopped
    ports:
      - "6379:6379"
    volumes:
      - redis:/data

  openldap:
    image: osixia/openldap:latest
    container_name: openldap
    restart: unless-stopped
    environment:
      LDAP_ORGANISATION: "Schule"
      LDAP_DOMAIN: "Schule.intern"
      LDAP_ADMIN_PASSWORD: ${LDAP_ADMIN_PASSWORD}
    ports:
      - "389:389"
      - "636:636"
    volumes:
      - openldap_data:/var/lib/ldap
      - openldap_config:/etc/ldap/slapd.d
      # Custom LDAP configuration
      - ./crates/ldap/src/users.ldif:/container/service/slapd/assets/config/bootstrap/ldif/custom/users.ldif

    healthcheck:
      test:
        [
          "CMD",
          "ldapsearch",
          "-x",
          "-H",
          "ldap://localhost",
          "-b",
          "dc=Schule,dc=intern",
        ]
      interval: 30s
      retries: 3

  backend:
    build:
      context: .
      dockerfile: ./backend.Dockerfile
    image: peer-group-grading:latest
    restart: unless-stopped
    environment:
      DB_NAME: ${DB_NAME}
      DB_USER: ${DB_USER}
      DB_PASSWORD: ${DB_PASSWORD}
      DB_HOST: db
      DB_PORT: 5432
      REDIS_HOST: redis
      REDIS_PORT: 6379
    ports:
      - "8080:8080"
    depends_on:
      - db
      - redis

volumes:
  postgres_data:
  redis:
  openldap_data:
  openldap_config:
