#!/usr/bin/env bash
# Loop CI Agent: Analyze time complexity (Big O notation) across the repo
# Scans all source files for O(n) annotations and generates a report

set -euo pipefail

REPORT_FILE="target/complexity-report.md"
SRC_DIR="src"

mkdir -p target

echo "# Time Complexity Analysis" > "$REPORT_FILE"
echo "" >> "$REPORT_FILE"
echo "Generated: $(date -u '+%Y-%m-%d %H:%M:%S UTC')" >> "$REPORT_FILE"
echo "" >> "$REPORT_FILE"

# Function to extract complexity annotations
extract_complexity() {
    local file="$1"
    local result=""
    while IFS=: read -r line_num content; do
        # Extract O(...) pattern
        complexity=$(echo "$content" | grep -oE "O\([^)]+\)" | head -1)
        if [ -n "$complexity" ]; then
            result="${result}| $file | $line_num | $complexity |\n"
        fi
    done < <(grep -n "O(" "$file" 2>/dev/null || true)
    echo -e "$result"
}

# Function to analyze function signatures
analyze_functions() {
    local file="$1"
    grep -n "fn " "$file" | grep -v "//" | while IFS=: read -r line_num content; do
        # Extract function name
        func_name=$(echo "$content" | grep -oE "fn [a-zA-Z_][a-zA-Z0-9_]*" | head -1 | sed 's/fn //')
        if [ -n "$func_name" ]; then
            echo "| $file | $line_num | $func_name |"
        fi
    done
}

echo "## Complexity Annotations" >> "$REPORT_FILE"
echo "" >> "$REPORT_FILE"
echo "| File | Line | Complexity |" >> "$REPORT_FILE"
echo "|------|------|------------|" >> "$REPORT_FILE"

total_annotations=0
for file in $(find "$SRC_DIR" \( -name "*.rs" -o -name "*.md" \) -type f); do
    annotations=$(extract_complexity "$file")
    if [ -n "$annotations" ]; then
        echo "$annotations" >> "$REPORT_FILE"
        count=$(echo "$annotations" | wc -l)
        total_annotations=$((total_annotations + count))
    fi
done

echo "" >> "$REPORT_FILE"
echo "**Total annotations: $total_annotations**" >> "$REPORT_FILE"
echo "" >> "$REPORT_FILE"

echo "## Function Inventory" >> "$REPORT_FILE"
echo "" >> "$REPORT_FILE"
echo "| File | Line | Function |" >> "$REPORT_FILE"
echo "|------|------|----------|" >> "$REPORT_FILE"

total_functions=0
for file in $(find "$SRC_DIR" -name "*.rs" -type f); do
    functions=$(analyze_functions "$file")
    if [ -n "$functions" ]; then
        echo "$functions" >> "$REPORT_FILE"
        count=$(echo "$functions" | wc -l)
        total_functions=$((total_functions + count))
    fi
done

echo "" >> "$REPORT_FILE"
echo "**Total functions: $total_functions**" >> "$REPORT_FILE"
echo "" >> "$REPORT_FILE"

# Summary
echo "## Summary" >> "$REPORT_FILE"
echo "" >> "$REPORT_FILE"
echo "- **Total files scanned:** $(find "$SRC_DIR" \( -name "*.rs" -o -name "*.md" \) -type f | wc -l)" >> "$REPORT_FILE"
echo "- **Total functions:** $total_functions" >> "$REPORT_FILE"
echo "- **Complexity annotations:** $total_annotations" >> "$REPORT_FILE"
echo "" >> "$REPORT_FILE"

# Complexity distribution
echo "## Complexity Distribution" >> "$REPORT_FILE"
echo "" >> "$REPORT_FILE"
echo "| Complexity | Count |" >> "$REPORT_FILE"
echo "|------------|-------|" >> "$REPORT_FILE"

for complexity in "O(1)" "O(log n)" "O(n)" "O(n log n)" "O(n²)" "O(n³)" "O(2^n)" "O(n!)"; do
    count=$(grep -r "$complexity" "$SRC_DIR" --include="*.rs" 2>/dev/null | wc -l)
    if [ "$count" -gt 0 ]; then
        echo "| $complexity | $count |" >> "$REPORT_FILE"
    fi
done

echo "" >> "$REPORT_FILE"
echo "## Recommendations" >> "$REPORT_FILE"
echo "" >> "$REPORT_FILE"

# Check for potential issues
echo "### High Complexity Functions" >> "$REPORT_FILE"
echo "" >> "$REPORT_FILE"
echo "Functions with O(n²) or higher:" >> "$REPORT_FILE"
echo "" >> "$REPORT_FILE"

for file in $(find "$SRC_DIR" -name "*.rs" -type f); do
    grep -n "O(n²)\|O(n³)\|O(2^n)\|O(n!)" "$file" 2>/dev/null | while IFS=: read -r line_num content; do
        echo "- \`$file:$line_num\`: $content" >> "$REPORT_FILE"
    done
done

echo "" >> "$REPORT_FILE"
echo "### Missing Annotations" >> "$REPORT_FILE"
echo "" >> "$REPORT_FILE"
echo "Functions without complexity annotations:" >> "$REPORT_FILE"
echo "" >> "$REPORT_FILE"

annotated_functions=$(grep -r "O(" "$SRC_DIR" --include="*.rs" 2>/dev/null | wc -l)
total_functions_found=$(grep -r "fn " "$SRC_DIR" --include="*.rs" 2>/dev/null | grep -v "//" | wc -l)

if [ "$annotated_functions" -lt "$total_functions_found" ]; then
    echo "Consider adding complexity annotations to improve documentation." >> "$REPORT_FILE"
fi

echo "" >> "$REPORT_FILE"
echo "---" >> "$REPORT_FILE"
echo "*Report generated by loop-ci-agent*" >> "$REPORT_FILE"

echo "Complexity report generated at $REPORT_FILE"
cat "$REPORT_FILE"
