name: laahtech-school

# LaaHTech School - standalone B2B deployment
# Single server, no reverse proxy.

services:

  # App (nginx + PHP-FPM + Moodle cron - unified container)
  app:
    image: registry.gitlab.com/laahtech-group/laahtech/laahtech-school:latest
    platform: linux/amd64
    container_name: laahtech-school-app
    entrypoint:
      - /bin/sh
      - -lc
      - |
        export IOMAD_DB_HOST="${SCHOOL_DB_HOST:-mariadb}"
        export IOMAD_DB_PORT="${SCHOOL_DB_PORT:-3306}"
        export IOMAD_DB_NAME="${SCHOOL_DB_NAME:-moodle}"
        export IOMAD_DB_USERNAME="${SCHOOL_DB_USERNAME:-schooluser}"
        export IOMAD_DB_PASSWORD="${SCHOOL_DB_PASSWORD:-schoolpass}"
        if [ -f /var/www/html/config.php ] && [ -n "${DOMAIN}" ]; then
          sed -i "s#wwwroot[[:space:]]*=[[:space:]]*'.*';#wwwroot   = 'https://${DOMAIN}';#" /var/www/html/config.php || true
        fi
        exec /entrypoint.sh
    ports:
      - "80:80"
      - "443:443"

    environment:
      DOMAIN: ${DOMAIN:-school.laahtech.test}

      MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD:-rootpass}

      SCHOOL_DB_HOST: ${SCHOOL_DB_HOST:-mariadb}
      SCHOOL_DB_PORT: ${SCHOOL_DB_PORT:-3306}
      SCHOOL_DB_NAME: ${SCHOOL_DB_NAME:-moodle}
      SCHOOL_DB_USERNAME: ${SCHOOL_DB_USERNAME:-schooluser}
      SCHOOL_DB_PASSWORD: ${SCHOOL_DB_PASSWORD:-schoolpass}

      PHP_MEMORY_LIMIT: ${PHP_MEMORY_LIMIT:-512M}
      PHP_MAX_EXECUTION_TIME: ${PHP_MAX_EXECUTION_TIME:-300}
      PHP_UPLOAD_MAX_FILESIZE: ${PHP_UPLOAD_MAX_FILESIZE:-512M}
      PHP_POST_MAX_SIZE: ${PHP_POST_MAX_SIZE:-512M}

    volumes:
      - schooldata:/var/moodledata
      - ssl_certs:/etc/nginx/ssl
      - certbot_webroot:/var/www/certbot

    depends_on:
      mariadb:
        condition: service_healthy

    networks:
      - school

    restart: unless-stopped

  # MariaDB
  mariadb:
    image: mariadb:10.11
    container_name: laahtech-school-db

    environment:
      MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD:-rootpass}

    volumes:
      - mariadb_data:/var/lib/mysql
      - ./docker/mariadb.cnf:/etc/mysql/conf.d/moodle.cnf:ro

    ports:
      - "127.0.0.1:${MARIADB_PORT:-3307}:3306"

    healthcheck:
      test: ["CMD", "mariadb-admin", "ping", "-h", "localhost", "-u", "root", "-p${MYSQL_ROOT_PASSWORD}"]
      interval: 10s
      timeout: 5s
      retries: 5
      start_period: 30s

    networks:
      - school

    restart: unless-stopped

  # Certbot - optional SSL setup
  certbot:
    image: certbot/certbot:latest
    container_name: laahtech-school-certbot

    volumes:
      - ssl_certs:/etc/letsencrypt
      - certbot_webroot:/var/www/certbot

    profiles: ["certbot"]

    entrypoint: >
      sh -c "certbot certonly
        --webroot
        --webroot-path=/var/www/certbot
        --email ${CERTBOT_EMAIL:-dev@laahtech.test}
        --agree-tos
        --no-eff-email
        --non-interactive
        -d ${DOMAIN}"

volumes:
  mariadb_data:
  schooldata:
  ssl_certs:
  certbot_webroot:

networks:
  school:
    driver: bridge
