#!/usr/bin/env bash
set -euo pipefail

ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
failed=0

check() {
  local name="$1" command="$2"
  if bash -lc "$command" >/dev/null 2>&1; then
    printf '[OK] %s\n' "$name"
  else
    printf '[FAIL] %s\n' "$name"
    failed=1
  fi
}

check "PHP 8.3+" "php -r 'exit(version_compare(PHP_VERSION, \"8.3\", \">=\") ? 0 : 1);'"
check "PHP intl" "php -m | grep -qi '^intl$'"
check "PHP mbstring" "php -m | grep -qi '^mbstring$'"
check "PHP mysqli" "php -m | grep -qi '^mysqli$'"
check "Node 22+" "node -e 'process.exit(Number(process.versions.node.split(\".\")[0]) >= 22 ? 0 : 1)'"
check "MariaDB/MySQL client" "command -v mariadb || command -v mysql"
check "Composer" "command -v composer"
check "Web writable" "test -w '$ROOT/web/writable'"
check "Runtime workspace" "test -w '$ROOT/runtime/workspaces'"
check "Runtime artifacts" "test -w '$ROOT/runtime/artifacts'"

if command -v flutter >/dev/null 2>&1; then
  check "Flutter" "flutter --version"
else
  printf '[INFO] Flutter 없음: DRY_RUN=true만 사용할 수 있습니다.\n'
fi

exit "$failed"
