#!/bin/bash

# Script to check if any new files are being added to the public/ folder
# New assets should be uploaded to Cloudflare R2 instead

set -e

# Change to the repository root
cd "$(git rev-parse --show-toplevel)"

# Get the list of allowed files from git (files committed in HEAD)
ALLOWED_FILES=$(git ls-tree -r HEAD --name-only ui/app-ui/public 2>/dev/null | sort)

# Get currently staged files in public/ (only additions)
STAGED_FILES=$(git diff --cached --name-only --diff-filter=A ui/app-ui/public 2>/dev/null | sort)

# If no new files are staged, exit successfully
if [ -z "$STAGED_FILES" ]; then
  exit 0
fi

# Check if any staged files are not in the allowed list
NEW_FILES=$(comm -13 <(echo "$ALLOWED_FILES") <(echo "$STAGED_FILES"))

if [ -n "$NEW_FILES" ]; then
  echo "❌ ERROR: New assets cannot be added to the public/ folder."
  echo ""
  echo "The following files are not allowed:"
  echo "$NEW_FILES" | sed 's/^/  - /'
  echo ""
  echo "📦 New assets must be uploaded to Cloudflare R2 instead."
  exit 1
fi

exit 0
