#!/bin/bash
# MORPH-SPEC Pre-Commit Hook: Spec Validation
# Validates that spec.md files have required sections

echo "🔍 Validating spec files..."

# Find modified spec.md files
SPEC_FILES=$(git diff --cached --name-only --diff-filter=ACM | grep 'spec\.md$')

if [ -z "$SPEC_FILES" ]; then
  echo "✓ No spec files modified"
  exit 0
fi

HAS_ERRORS=false

for spec_file in $SPEC_FILES; do
  echo "Checking: $spec_file"

  # Required sections
  REQUIRED_SECTIONS=(
    "## 📋 Metadata"
    "## 🎯 Overview"
    "## 🏗️ Technical Design"
    "## ✅ Acceptance Criteria"
  )

  for section in "${REQUIRED_SECTIONS[@]}"; do
    if ! grep -q "$section" "$spec_file"; then
      echo "  ❌ Missing section: $section"
      HAS_ERRORS=true
    fi
  done

  # Check if has at least one user story or requirement
  if ! grep -qi "user story\|requirement\|acceptance criteria" "$spec_file"; then
    echo "  ⚠️  Warning: No user stories or requirements found"
  fi
done

if [ "$HAS_ERRORS" = true ]; then
  echo ""
  echo "❌ COMMIT BLOCKED: spec.md files are incomplete"
  echo "   Add missing sections before committing"
  exit 1
fi

echo "✓ All spec files are valid"
exit 0
