#!/usr/bin/env bash
set -u

OUT_BASE="${1:-$HOME}"
TS="$(date +%Y%m%d-%H%M%S)"
OUT="$OUT_BASE/shift-planner-inventory-$TS"
mkdir -p "$OUT"

find_repo() {
  for p in /opt/shift-planner /srv/shift-planner /var/www/shift-planner; do
    if [ -d "$p/.git" ]; then
      printf '%s\n' "$p"
      return 0
    fi
  done
  return 1
}

REPO="${SHIFT_PLANNER_REPO:-$(find_repo 2>/dev/null || true)}"

{
  echo "=== SYSTEM ==="
  date -Is
  hostnamectl 2>/dev/null || true
  uname -a
  echo
  ip -br addr 2>/dev/null || true
  echo
  df -h
  echo
  free -h 2>/dev/null || true
} > "$OUT/system.txt" 2>&1

if [ -n "${REPO:-}" ] && [ -d "$REPO/.git" ]; then
  {
    git -C "$REPO" status --short 2>&1 || true
    echo
    git -C "$REPO" log -1 --decorate --oneline 2>&1 || true
    echo
    git -C "$REPO" branch -avv 2>&1 || true
    echo
    git -C "$REPO" tag --sort=-version:refname | head -50 2>&1 || true
    echo
    git -C "$REPO" remote -v 2>&1 || true
  } > "$OUT/git.txt"

  git -C "$REPO" bundle create "$OUT/repository.bundle" --all >"$OUT/git-bundle.log" 2>&1 || true

  find "$REPO" -maxdepth 3 -type f     \( -name "*.py" -o -name "*.html" -o -name "*.js" -o -name "*.css" -o -name "*.sql" -o -name "*.md" \)     -printf '%P\n' 2>/dev/null | sort > "$OUT/source-files.txt"
fi

{
  systemctl list-unit-files --type=service 2>/dev/null | grep -Ei 'shift|planner|ndm' || true
  echo
  for svc in shift-planner.service ndm-auth.service; do
    echo "=== $svc ==="
    systemctl cat "$svc" 2>/dev/null || true
    systemctl status "$svc" --no-pager 2>/dev/null || true
    echo
  done
} > "$OUT/services.txt" 2>&1

grep -RniE 'shift-planner|8095|Pracovn' /etc/nginx 2>/dev/null > "$OUT/nginx.txt" || true

DB_LIST="$OUT/databases.txt"
: > "$DB_LIST"
for root in "${REPO:-/nonexistent}" /var/lib /opt; do
  [ -d "$root" ] || continue
  find "$root" -maxdepth 4 -type f \( -name "*.sqlite" -o -name "*.db" -o -name "*.sqlite3" \)     2>/dev/null | grep -Ei 'shift|planner|work|timesheet|ndm' >> "$DB_LIST" || true
done
sort -u -o "$DB_LIST" "$DB_LIST"

mkdir -p "$OUT/sqlite"
while IFS= read -r db; do
  [ -f "$db" ] || continue
  name="$(printf '%s' "$db" | sed 's#/#_#g; s#^_##')"
  {
    echo "DB: $db"
    ls -lh "$db"
    if command -v sqlite3 >/dev/null 2>&1; then
      echo "=== INTEGRITY ==="
      sqlite3 "$db" 'PRAGMA integrity_check;' 2>&1 || true
      echo "=== TABLES ==="
      sqlite3 "$db" '.tables' 2>&1 || true
      echo "=== SCHEMA ==="
      sqlite3 "$db" '.schema' 2>&1 || true
      echo "=== COUNTS ==="
      for t in $(sqlite3 "$db" "SELECT name FROM sqlite_master WHERE type='table' AND name NOT LIKE 'sqlite_%';" 2>/dev/null); do
        printf '%s: ' "$t"
        sqlite3 "$db" "SELECT COUNT(*) FROM \"$t\";" 2>/dev/null || true
      done
    fi
  } > "$OUT/sqlite/$name.txt" 2>&1
done < "$DB_LIST"

if [ -n "${REPO:-}" ] && [ -d "$REPO" ]; then
  grep -RniE     'CREATE TABLE|ALTER TABLE|persons|users|departments|workplaces|organizational_roles|permissions|work_evidence_mode|timesheets|vacation|employee_workplaces|leader_person_id'     "$REPO" --include='*.py' --include='*.sql' --include='*.md' 2>/dev/null | head -3000 > "$OUT/domain-hints.txt" || true
fi

{
  [ -n "${REPO:-}" ] && ls -ld "$REPO" "$REPO"/data 2>/dev/null || true
  echo
  ss -lntup 2>/dev/null | grep -E ':(80|443|8095)\b' || true
} > "$OUT/runtime.txt"

tar -C "$OUT_BASE" -czf "$OUT_BASE/shift-planner-inventory-$TS.tar.gz" "$(basename "$OUT")"
printf 'Created: %s\n' "$OUT_BASE/shift-planner-inventory-$TS.tar.gz"
