#!/bin/bash
set -euo pipefail

remote_name="$1"

# --- Rust clippy for staged files in src-tauri ---
if git diff --cached --name-only | grep -q '^src-tauri/'; then
    echo "[pre-push] Running clippy on src-tauri..."
    cargo clippy --manifest-path ./src-tauri/Cargo.toml -- -D warnings || {
        echo "❌ Clippy found issues in src-tauri. Please fix them before pushing."
        exit 1
    }
fi

# --- JS/TS format check only for main repo ---
if git remote get-url "$remote_name" >/dev/null 2>&1; then
    remote_url=$(git remote get-url "$remote_name")
    if [[ "$remote_url" =~ github\.com[:/]+clash-verge-rev/clash-verge-rev(\.git)?$ ]]; then
        echo "[pre-push] Detected push to clash-verge-rev/clash-verge-rev ($remote_url)"
        echo "[pre-push] Running pnpm format:check..."
        if ! pnpm format:check; then
            echo "❌ Code format check failed. Please fix formatting before pushing."
            exit 1
        fi
    else
        echo "[pre-push] Not pushing to target repo. Skipping format check."
    fi
else
    echo "[pre-push] Remote '$remote_name' does not exist. Skipping format check."
fi

echo "[pre-push] All checks passed."
exit 0
