#!/usr/bin/env bash
# Local CI check for zlicenser-server, mirroring .github/workflows/ci.yml.
# Usage: ./test_ci.sh [--fix]
#   --fix   auto-run `cargo fmt` before checking (formats in place)
#
# Requires zlicenser-protocol to be cloned as a sibling directory:
#   git clone https://github.com/zal-analytics/zlicenser-protocol ../zlicenser-protocol

set -euo pipefail

REPO="$(cd "$(dirname "$0")" && pwd)"
PROTOCOL="$REPO/../zlicenser-protocol"
RED='\033[0;31m'; GRN='\033[0;32m'; YLW='\033[0;33m'; BLD='\033[1m'; RST='\033[0m'
FAILED=(); SKIPPED=(); FIX=0

for arg in "$@"; do
  case "$arg" in
    --fix) FIX=1 ;;
    *) echo "Unknown argument: $arg"; exit 1 ;;
  esac
done

hr()   { printf '%*s\n' 72 '' | tr ' ' '-'; }
step() {
  local label="$1"; shift
  printf "  ${BLD}%-28s${RST}" "$label"
  local out
  if out=$(eval "$@" 2>&1); then echo -e "${GRN}PASS${RST}"
  else echo -e "${RED}FAIL${RST}"; echo "$out" | sed 's/^/    /'; return 1; fi
}
check_tool() {
  if ! command -v "$1" &>/dev/null; then
    echo -e "  ${YLW}SKIP${RST}  $1 not installed  -->  cargo install $1"; return 1
  fi
}

hr
echo -e "${BLD}zlicenser-server CI  $(date '+%Y-%m-%d %H:%M:%S')${RST}"
echo -e "  $REPO"
hr

# zlicenser-protocol must exist as a sibling due to [patch.crates-io] in Cargo.toml
if [[ ! -d "$PROTOCOL" ]]; then
  echo -e "${RED}ERROR: zlicenser-protocol not found at $PROTOCOL${RST}"
  echo -e "  Clone it first:"
  echo -e "  ${BLD}git clone https://github.com/zal-analytics/zlicenser-protocol ../zlicenser-protocol${RST}"
  exit 1
fi

# --all-features enables storage-postgres which starts a testcontainer (requires Podman/Docker)
PODMAN_SOCK="${XDG_RUNTIME_DIR:-/run/user/$(id -u)}/podman/podman.sock"
DOCKER_SOCK="/var/run/docker.sock"
if [[ -z "${DOCKER_HOST:-}" ]] && [[ ! -S "$PODMAN_SOCK" ]] && [[ ! -S "$DOCKER_SOCK" ]]; then
  echo -e "${YLW}WARNING: no container socket found — test --all-features will fail (postgres testcontainer needs Podman/Docker)${RST}"
  echo -e "  Start Podman: ${BLD}systemctl --user start podman.socket${RST}"
fi

[[ $FIX -eq 1 ]] && (cd "$REPO" && cargo fmt) && echo -e "  ${GRN}fmt (auto-fixed)${RST}" || true

step "fmt"                               "cd '$REPO' && cargo fmt --check"                                                       || FAILED+=(fmt)
step "clippy --all-features"             "cd '$REPO' && cargo clippy --workspace --all-features -- -D warnings"                  || FAILED+=(clippy-all)
step "clippy -p server --no-defaults"    "cd '$REPO' && cargo clippy -p zlicenser-server --no-default-features -- -D warnings"   || FAILED+=(clippy-none)
step "test"                              "cd '$REPO' && cargo test -p zlicenser-server"                                          || FAILED+=(test)
step "test --all-features"               "cd '$REPO' && cargo test -p zlicenser-server --all-features"                          || FAILED+=(test-all)
step "test --no-defaults"                "cd '$REPO' && cargo test -p zlicenser-server --no-default-features"                    || FAILED+=(test-none)

if check_tool cargo-deny;  then step "deny"  "cd '$REPO' && cargo deny check"  || FAILED+=(deny);  else SKIPPED+=(deny);  fi
if check_tool cargo-audit; then step "audit" "cd '$REPO' && cargo audit"        || FAILED+=(audit); else SKIPPED+=(audit); fi

hr
[[ ${#SKIPPED[@]} -gt 0 ]] && echo -e "${YLW}Skipped:${RST} ${SKIPPED[*]}"
if [[ ${#FAILED[@]} -eq 0 ]]; then
  echo -e "${GRN}${BLD}All checks passed.${RST}"
  exit 0
else
  echo -e "${RED}${BLD}Failed: ${FAILED[*]}${RST}"
  exit 1
fi
