#!/bin/sh
# RefineID Windows commit gate.
#
# Enforces the local format, lint, and unit-test quality gate before committing.
# Enable it once per clone with:
#
#     git config core.hooksPath .githooks
#
# Formatting, clippy lints (both host and Windows cross-target), and unit tests
# run on commit so that bad quality code is caught before pushing to GitHub.
# Markdown links must stay inside the repository: a link into a sibling
# checkout resolves only on a developer's disk and is broken on GitHub.

set -e

if git ls-files | grep -i -E '(^|/)WHATSUP\.md$' >/dev/null; then
    echo "pre-commit: error: WHATSUP.md files are forbidden."
    exit 1
fi

# Stamp the workspace version to the UTC day when the day changed. The
# manifest carries CalVer YY.M.D; the apps and packages add the time bucket
# at build.
root=$(git rev-parse --show-toplevel)
manifest="$root/Cargo.toml"
yy=$(date -u +%y); yy=${yy#0}
mo=$(date -u +%m); mo=${mo#0}
dd=$(date -u +%d); dd=${dd#0}
today="$yy.$mo.$dd"
current=$(awk -F'"' '/^version = / { print $2; exit }' "$manifest")
if [ "$current" != "$today" ]; then
    sed -i.bak "s|^version = \"$current\"|version = \"$today\"|" "$manifest"
    rm -f "$manifest.bak"
    cargo check --workspace --quiet
    git add "$manifest" "$root/Cargo.lock"
    echo "pre-commit: workspace version $current -> $today (UTC day change)"
fi

# A relative link escapes the repository when it climbs more directories
# than its file is deep. Cross-repository references use the GitHub URL.
echo "pre-commit: markdown links stay inside the repository"
link_open='](' # markdown inline link opener preceding the target
parent_dir='../'
escaping=$(git ls-files -z -- '*.md' | xargs -0 awk \
    -v open_len="${#link_open}" -v parent_len="${#parent_dir}" '
    FNR == 1 { depth = gsub("/", "/", FILENAME) }
    {
        line = $0
        while (match(line, /\]\((\.\.\/)+/)) {
            climb = (RLENGTH - open_len) / parent_len
            if (climb > depth) {
                target = substr(line, RSTART + open_len)
                sub(/\).*$/, "", target)
                print FILENAME ":" FNR ": " target
            }
            line = substr(line, RSTART + RLENGTH)
        }
    }')
if [ -n "$escaping" ]; then
    echo "pre-commit: error: relative links leave the repository; link the GitHub URL instead:" >&2
    echo "$escaping" >&2
    exit 1
fi

echo "pre-commit: cargo fmt --all --check"
cargo fmt --all --check

echo "pre-commit: dotnet csharpier check"
dotnet tool restore >/dev/null
dotnet csharpier check .

echo "pre-commit: cargo clippy (native host)"
cargo clippy --workspace --all-targets -- -D warnings

if rustup target list --installed | grep -q "x86_64-pc-windows-msvc"; then
    echo "pre-commit: cargo clippy (x86_64-pc-windows-msvc)"
    cargo clippy --target x86_64-pc-windows-msvc --workspace --all-targets -- -D warnings
fi

echo "pre-commit: cargo test -p refineid-lib-core"
cargo test -p refineid-lib-core

echo "pre-commit: ok (all quality checks passed)"
