#!/usr/bin/env sh

# Run format check first (capture exit code without breaking script)
set +e
yarn format:check
FORMAT_EXIT=$?
set -e

# If format check failed, run format to auto-fix
if [ $FORMAT_EXIT -ne 0 ]; then
  echo "Formatting issues detected. Running format to auto-fix..."
  yarn format
fi

# Run lint check (capture exit code without breaking script)
set +e
yarn lint
LINT_EXIT=$?
set -e

# If lint check failed, run lint:fix to auto-fix
if [ $LINT_EXIT -ne 0 ]; then
  echo "Linting issues detected. Running lint:fix to auto-fix..."
  yarn lint:fix
fi

# Run TypeScript compile check (capture exit code without breaking script)
set +e
yarn compile
COMPILE_EXIT=$?
set -e

# Run Rust compile checks for both the core and Tauri codebases
set +e
yarn rust:check
RUST_CHECK_EXIT=$?
set -e

# Exit with error if any command still fails after fixes
if [ $FORMAT_EXIT -ne 0 ] || [ $LINT_EXIT -ne 0 ] || [ $COMPILE_EXIT -ne 0 ] || [ $RUST_CHECK_EXIT -ne 0 ]; then
  echo "Pre-push checks failed. Please fix format (Prettier + cargo fmt for core and Tauri), lint, TypeScript, and/or Rust errors before pushing."
  exit 1
fi
