#!/bin/bash
#
# Generate traceability matrix in Markdown format
#

echo "# Requirements Traceability Matrix"
echo ""
echo "**Generated**: $(date '+%Y-%m-%d %H:%M:%S')"
echo ""
echo "| Requirement | Files Implementing | Status | Notes |"
echo "|-------------|--------------------|--------|-------|"

requirements=$(grep -E "^\| (UR|IR|DR|JA)-[0-9]+" README.md | sed -E 's/^\| ([A-Z]+-[0-9]+).*/\1/' | sort -u)

for req in $requirements; do
    files=$(grep -rl "@req: $req" src-tauri/ src/ 2>/dev/null | \
            sed 's|src-tauri/src/||; s|src/||' | \
            paste -sd, -)

    partial_files=$(grep -rl "@req-partial: $req" src-tauri/ src/ 2>/dev/null | wc -l)
    planned=$(grep -rl "@req-planned: $req" src-tauri/ src/ 2>/dev/null | wc -l)

    if [ -n "$files" ]; then
        status="✅ Done"
        notes=""
    elif [ "$partial_files" -gt 0 ]; then
        status="🔶 Partial"
        notes="Platform-specific"
    elif [ "$planned" -gt 0 ]; then
        status="📋 Planned"
        notes="Not implemented"
    else
        status="❌ Missing"
        notes="No implementation"
    fi

    echo "| $req | ${files:-N/A} | $status | $notes |"
done
