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

ROOT="$(cd "$(dirname "$0")/.." && pwd)"

protected=(
  "app/Jobs/ImportWebsaleOrderJob.php"
  "app/Services/JtlApiService.php"
  "app/Console/Commands/WebsaleOrdersPullCommand.php"
  "routes/console.php"
  "routes/web.php"
  "config/jtl.php"
)

for file in "${protected[@]}"; do
  if [[ ! -f "$ROOT/$file" ]]; then
    echo "FEHLT: $file" >&2
    exit 1
  fi
done

find "$ROOT/app/Models/Datev" "$ROOT/app/Services/Datev" \
     "$ROOT/app/Console/Commands" "$ROOT/database/migrations" \
     "$ROOT/database/seeders" "$ROOT/config/datev.php" \
     -type f -name '*.php' -print0 \
  | xargs -0 -n1 php -l >/dev/null

echo "OK: DATEV-PHP-Dateien sind syntaktisch gültig."
echo "OK: Geschützte Importdateien sind vorhanden."

if [[ -f "$ROOT/docs/datev-protected-files.sha256" ]]; then
  (cd "$ROOT" && sha256sum -c docs/datev-protected-files.sha256)
  echo "OK: Bestehender Auftragsimport und JTL-Basisdateien sind bytegenau unverändert."
fi
