##############################################################
# GMLM Platform — Docker Compose
# Per-customer isolated deployment stack
#
# Services:
#   app     — PHP-FPM + Nginx (application)
#   mysql   — MySQL 8.0 (isolated customer database)
#   redis   — Redis 7 (cache, sessions, queues)
#
# Usage:
#   docker compose up -d
#   docker compose exec app php artisan gmlm:provision
##############################################################

name: gmlm-${DEPLOYMENT_ID:-customer}

services:

  # ── Application (PHP-FPM + Nginx + Horizon + Scheduler) ────
  app:
    build:
      context: ..
      dockerfile: docker/Dockerfile
      target: production
    image: gmlm-platform:${GMLM_VERSION:-latest}
    container_name: gmlm-app-${DEPLOYMENT_ID:-default}
    restart: unless-stopped
    environment:
      APP_ENV:          ${APP_ENV:-production}
      APP_KEY:          ${APP_KEY}
      APP_URL:          ${APP_URL}
      APP_DEBUG:        "false"
      APP_INSTALLED:    ${APP_INSTALLED:-false}

      DB_HOST:          mysql
      DB_PORT:          3306
      DB_DATABASE:      ${DB_DATABASE:-gmlm_db}
      DB_USERNAME:      ${DB_USERNAME:-gmlm_user}
      DB_PASSWORD:      ${DB_PASSWORD}

      REDIS_HOST:       redis
      REDIS_PORT:       6379
      REDIS_PASSWORD:   ${REDIS_PASSWORD:-}

      QUEUE_CONNECTION: redis
      CACHE_DRIVER:     redis
      SESSION_DRIVER:   redis

      MAIL_MAILER:      ${MAIL_MAILER:-log}
      MAIL_HOST:        ${MAIL_HOST:-}
      MAIL_PORT:        ${MAIL_PORT:-587}
      MAIL_USERNAME:    ${MAIL_USERNAME:-}
      MAIL_PASSWORD:    ${MAIL_PASSWORD:-}
      MAIL_ENCRYPTION:  ${MAIL_ENCRYPTION:-tls}

      GMLM_LICENSE_KEY:    ${GMLM_LICENSE_KEY}
      GMLM_VERSION:        ${GMLM_VERSION:-1.0.0}
      GMLM_UPDATE_SERVER:  ${GMLM_UPDATE_SERVER:-https://updates.globalmlmsoftware.com}
      GMLM_BASE_CURRENCY:  ${GMLM_BASE_CURRENCY:-USD}

    volumes:
      - app_storage:/var/www/html/storage
      - ../:/var/www/html:ro    # Read-only core — writeable storage is the volume
      - ${SSL_CERT_PATH:-./docker/nginx/certs}:/etc/nginx/certs:ro
    ports:
      - "${HTTP_PORT:-80}:80"
      - "${HTTPS_PORT:-443}:443"
    depends_on:
      mysql:
        condition: service_healthy
      redis:
        condition: service_healthy
    networks:
      - gmlm-network
    healthcheck:
      test: ["CMD", "curl", "-sf", "http://localhost/api/v1/health"]
      interval: 30s
      timeout: 10s
      retries: 5
      start_period: 60s
    logging:
      driver: "json-file"
      options:
        max-size: "50m"
        max-file: "5"

  # ── MySQL 8 — Isolated customer database ────────────────────
  mysql:
    image: mysql:8.0
    container_name: gmlm-mysql-${DEPLOYMENT_ID:-default}
    restart: unless-stopped
    environment:
      MYSQL_ROOT_PASSWORD: ${DB_ROOT_PASSWORD}
      MYSQL_DATABASE:      ${DB_DATABASE:-gmlm_db}
      MYSQL_USER:          ${DB_USERNAME:-gmlm_user}
      MYSQL_PASSWORD:      ${DB_PASSWORD}
    command: >
      --character-set-server=utf8mb4
      --collation-server=utf8mb4_unicode_ci
      --innodb-buffer-pool-size=512M
      --innodb-log-file-size=128M
      --innodb-flush-log-at-trx-commit=1
      --innodb-flush-method=O_DIRECT
      --max-connections=200
      --slow-query-log=1
      --slow-query-log-file=/var/log/mysql/slow.log
      --long-query-time=1
      --binlog-expire-logs-seconds=604800
    volumes:
      - mysql_data:/var/lib/mysql
      - mysql_logs:/var/log/mysql
      - ./mysql/init:/docker-entrypoint-initdb.d:ro
    networks:
      - gmlm-network
    healthcheck:
      test: ["CMD", "mysqladmin", "ping", "-h", "localhost", "-u", "${DB_USERNAME:-gmlm_user}", "-p${DB_PASSWORD}"]
      interval: 10s
      timeout: 5s
      retries: 10
      start_period: 30s
    logging:
      driver: "json-file"
      options:
        max-size: "20m"
        max-file: "3"

  # ── Redis 7 — Cache, Sessions, Queues ────────────────────────
  redis:
    image: redis:7-alpine
    container_name: gmlm-redis-${DEPLOYMENT_ID:-default}
    restart: unless-stopped
    command: >
      redis-server
      --appendonly yes
      --appendfsync everysec
      --maxmemory 256mb
      --maxmemory-policy allkeys-lru
      ${REDIS_PASSWORD:+--requirepass $REDIS_PASSWORD}
    volumes:
      - redis_data:/data
    networks:
      - gmlm-network
    healthcheck:
      test: ["CMD", "redis-cli", "ping"]
      interval: 10s
      timeout: 5s
      retries: 5
    logging:
      driver: "json-file"
      options:
        max-size: "10m"
        max-file: "3"

# ── Named Volumes ────────────────────────────────────────────
volumes:
  app_storage:
    name: gmlm-storage-${DEPLOYMENT_ID:-default}
    driver: local
  mysql_data:
    name: gmlm-mysql-${DEPLOYMENT_ID:-default}
    driver: local
  mysql_logs:
    name: gmlm-mysql-logs-${DEPLOYMENT_ID:-default}
    driver: local
  redis_data:
    name: gmlm-redis-${DEPLOYMENT_ID:-default}
    driver: local

# ── Network ──────────────────────────────────────────────────
networks:
  gmlm-network:
    name: gmlm-net-${DEPLOYMENT_ID:-default}
    driver: bridge
