#!/bin/bash

# PM Validation Script - Wrapper for Node.js implementation
# This wrapper maintains backward compatibility while delegating to the Node.js version

# Get the directory of this script
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"

# Check if Node.js is available
if command -v node >/dev/null 2>&1; then
  # Use the Node.js implementation
  node "$SCRIPT_DIR/validate.js"
  exit $?
else
  # Fallback to the original bash implementation if Node.js is not available
  echo "⚠️ Node.js not found, falling back to bash implementation"
  echo ""

  # Original bash implementation (preserved for fallback)
  echo "Validating PM System..."
  echo ""
  echo ""

  echo "🔍 Validating PM System"
  echo "======================="
  echo ""

  errors=0
  warnings=0

  # Check directory structure
  echo "📁 Directory Structure:"
  [ -d ".opencode" ] && echo "  ✅ .opencode directory exists" || { echo "  ❌ .opencode directory missing"; ((errors++)); }
  [ -d ".opencode/prds" ] && echo "  ✅ PRDs directory exists" || echo "  ⚠️ PRDs directory missing"
  [ -d ".opencode/epics" ] && echo "  ✅ Epics directory exists" || echo "  ⚠️ Epics directory missing"
  [ -d ".opencode/rules" ] && echo "  ✅ Rules directory exists" || echo "  ⚠️ Rules directory missing"
  echo ""

  # Check for orphaned files
  echo "🗂️ Data Integrity:"

  # Check epics have epic.md files
  for epic_dir in .opencode/epics/*/; do
    [ -d "$epic_dir" ] || continue
    if [ ! -f "$epic_dir/epic.md" ]; then
      echo "  ⚠️ Missing epic.md in $(basename "$epic_dir")"
      ((warnings++))
    fi
  done

  # Check for tasks without epics
  orphaned=$(find .opencode -name "[0-9]*.md" -not -path ".opencode/epics/*/*" 2>/dev/null | wc -l)
  [ $orphaned -gt 0 ] && echo "  ⚠️ Found $orphaned orphaned task files" && ((warnings++))

  # Check for broken references
  echo ""
  echo "🔗 Reference Check:"

  for task_file in .opencode/epics/*/[0-9]*.md; do
    [ -f "$task_file" ] || continue

    deps=$(grep "^depends_on:" "$task_file" | head -1 | sed 's/^depends_on: *\[//' | sed 's/\]//' | sed 's/,/ /g')
    if [ -n "$deps" ] && [ "$deps" != "depends_on:" ]; then
      epic_dir=$(dirname "$task_file")
      for dep in $deps; do
        if [ ! -f "$epic_dir/$dep.md" ]; then
          echo "  ⚠️ Task $(basename "$task_file" .md) references missing task: $dep"
          ((warnings++))
        fi
      done
    fi
  done

  [ $warnings -eq 0 ] && [ $errors -eq 0 ] && echo "  ✅ All references valid"

  # Check frontmatter
  echo ""
  echo "📝 Frontmatter Validation:"
  invalid=0

  for file in $(find .opencode -name "*.md" -path "*/epics/*" -o -path "*/prds/*" 2>/dev/null); do
    if ! grep -q "^---" "$file"; then
      echo "  ⚠️ Missing frontmatter: $(basename "$file")"
      ((invalid++))
    fi
  done

  [ $invalid -eq 0 ] && echo "  ✅ All files have frontmatter"

  # Summary
  echo ""
  echo "📊 Validation Summary:"
  echo "  Errors: $errors"
  echo "  Warnings: $warnings"
  echo "  Invalid files: $invalid"

  if [ $errors -eq 0 ] && [ $warnings -eq 0 ] && [ $invalid -eq 0 ]; then
    echo ""
    echo "✅ System is healthy!"
  else
    echo ""
    echo "💡 Run /pm:clean to fix some issues automatically"
  fi

  exit 0
fi
