#!/bin/bash
set -e

git fetch origin develop --depth 1

# Get TypeScript files changed since the comparison point (matches GitHub's file count)
FILES=$(git diff --name-only $(git merge-base origin/develop HEAD)..HEAD | grep -E '\.tsx?$' | tr '\n' ' ')
echo "mergebase: $(git merge-base origin/develop HEAD)"

if [ -z "$FILES" ]; then
  echo "No TypeScript files changed."
  exit 0
fi

echo "Checking only these files:"
echo "$FILES"

ERRORS=$(bun run tsc:strict 2>&1 || true)

MATCHING_ERRORS=""
for FILE in $FILES; do
  MATCH=$(echo "$ERRORS" | grep -E "^$FILE" || true)
  if [ -n "$MATCH" ]; then
    MATCHING_ERRORS="${MATCHING_ERRORS}${MATCH}"$'\n'
  fi
done

if [ -n "$MATCHING_ERRORS" ]; then
  echo "Errors in PR-modified files:"
  echo "$MATCHING_ERRORS"
  exit 1
else
  echo "No type errors in PR files."
fi
