#!/bin/bash

SCHEMA="st1.schema.json"
VALIDATOR=$(command -v ajv)

if [ -z "$VALIDATOR" ]; then
  echo "❌ ajv-cli is not installed. Install it with: npm install -g ajv-cli"
  exit 1
fi

echo "🔍 Validating ST1 JSON files using $SCHEMA..."
echo

valid_count=0
invalid_count=0
invalid_files=()

for file in *.json; do
  if [ "$file" != "$SCHEMA" ]; then
    echo "🧪 Validating $file..."
    ajv validate -s "$SCHEMA" -d "$file" --strict=false
    if [ $? -eq 0 ]; then
      echo "✅ $file is valid."
      ((valid_count++))
    else
      echo "❌ $file is INVALID."
      ((invalid_count++))
      invalid_files+=("$file")
    fi
    echo "---------------------------"
  fi
done

# Summary
echo ""
echo "📊 Validation Summary:"
echo "✅ Valid files:   $valid_count"
echo "❌ Invalid files: $invalid_count"

if [ $invalid_count -gt 0 ]; then
  echo "🚫 Invalid file list:"
  for f in "${invalid_files[@]}"; do
    echo "   - $f"
  done
fi
