# ═══════════════════════════════════════════════════════════════════
# SVC Elektronik — Docker Compose
# Auto-setup: build → migrate → seed → run, semuanya otomatis
# Konfigurasi credentials di file .env (copy dari .env.docker)
# ═══════════════════════════════════════════════════════════════════

services:

  # ─── Aplikasi Laravel (PHP 8.2 + Apache) ─────────────────────────
  app:
    build:
      context: .
      dockerfile: Dockerfile
    image: svc-elektronik:latest
    container_name: svc-app
    restart: unless-stopped
    ports:
      - "${APP_PORT:-8000}:80"
    environment:
      # Application
      APP_NAME: "${APP_NAME:-Service Elektronik}"
      APP_ENV: "${APP_ENV:-local}"
      APP_DEBUG: "${APP_DEBUG:-true}"
      APP_URL: "${APP_URL:-http://localhost:8000}"
      APP_LOCALE: "${APP_LOCALE:-id}"

      # Database — host harus 'db' (nama service Docker), BUKAN 127.0.0.1
      DB_CONNECTION: mysql
      DB_HOST: db
      DB_PORT: 3306
      DB_DATABASE: "${DB_DATABASE:-svc}"
      DB_USERNAME: "${DB_USERNAME:-root}"
      DB_PASSWORD: "${DB_PASSWORD:-root}"

      # Auto-setup behavior
      AUTO_MIGRATE: "${AUTO_MIGRATE:-true}"
      AUTO_SEED: "${AUTO_SEED:-true}"
      AUTO_COMPOSER: "${AUTO_COMPOSER:-auto}"
      AUTO_NPM: "${AUTO_NPM:-auto}"

      # Session / Cache
      SESSION_DRIVER: file
      CACHE_STORE: file
      LOG_CHANNEL: stack
      LOG_LEVEL: "${LOG_LEVEL:-debug}"

    volumes:
      - ./:/var/www/html
      - /var/www/html/vendor          # anonymous volume preserves vendor from image
      - /var/www/html/node_modules
    depends_on:
      db:
        condition: service_healthy
    healthcheck:
      test: ["CMD", "curl", "-fsS", "http://localhost/up"]
      interval: 10s
      timeout: 5s
      retries: 10
      start_period: 60s
    networks:
      - svc-net

  # ─── MySQL 8 ─────────────────────────────────────────────────────
  db:
    image: mysql:8.0
    container_name: svc-db
    restart: unless-stopped
    environment:
      MYSQL_ROOT_PASSWORD: "${DB_PASSWORD:-root}"
      MYSQL_DATABASE: "${DB_DATABASE:-svc}"
      # Optional: buat user non-root jika DB_USERNAME bukan 'root'
      MYSQL_USER: "${DB_EXTRA_USER:-}"
      MYSQL_PASSWORD: "${DB_EXTRA_PASSWORD:-}"
    ports:
      - "${DB_PORT_EXTERNAL:-3307}:3306"
    volumes:
      - svc-db-data:/var/lib/mysql
      - ./docker/mysql/init:/docker-entrypoint-initdb.d:ro
    command: >
      --character-set-server=utf8mb4
      --collation-server=utf8mb4_unicode_ci
      --default-authentication-plugin=caching_sha2_password
    healthcheck:
      test: ["CMD-SHELL", "mysqladmin ping -h localhost -uroot -p$$MYSQL_ROOT_PASSWORD"]
      interval: 5s
      timeout: 10s
      retries: 30
      start_period: 30s
    networks:
      - svc-net

  # ─── phpMyAdmin (DB Management UI) ───────────────────────────────
  phpmyadmin:
    image: phpmyadmin:5
    container_name: svc-phpmyadmin
    restart: unless-stopped
    ports:
      - "${PMA_PORT:-8080}:80"
    environment:
      PMA_HOST: db
      PMA_PORT: 3306
      PMA_USER: root
      PMA_PASSWORD: "${DB_PASSWORD:-root}"
      UPLOAD_LIMIT: 100M
      MEMORY_LIMIT: 256M
    depends_on:
      db:
        condition: service_healthy
    networks:
      - svc-net

# ═══════════════════════════════════════════════════════════════════
volumes:
  svc-db-data:
    driver: local

networks:
  svc-net:
    driver: bridge
