#!/bin/bash

# 🔍 CODE INDEX GENERATOR
# Indexes all types, services, stores, endpoints in the codebase
# This is your source of truth for what exists

set -e

WORKSPACE="/Users/floriansola/Documents/MyRoadTrip"
INDEX_FILE="$WORKSPACE/.code-index.txt"

echo "🔍 Generating Code Index..."
echo "Generated: $(date)" > "$INDEX_FILE"
echo "" >> "$INDEX_FILE"

# ============================================================================
# TYPES INDEX
# ============================================================================
echo "📋 TYPES DEFINED" >> "$INDEX_FILE"
echo "=================================================================================" >> "$INDEX_FILE"

find "$WORKSPACE/packages/types/src" -name "*.types.ts" 2>/dev/null | while read file; do
    echo "" >> "$INDEX_FILE"
    echo "FILE: ${file#$WORKSPACE/}" >> "$INDEX_FILE"
    echo "---" >> "$INDEX_FILE"
    grep "export interface\|export type" "$file" >> "$INDEX_FILE"
done

# ============================================================================
# COMPONENTS INDEX
# ============================================================================
echo "" >> "$INDEX_FILE"
echo "🎨 COMPONENTS" >> "$INDEX_FILE"
echo "=================================================================================" >> "$INDEX_FILE"

find "$WORKSPACE/apps/web/src/modules" -name "*.vue" -type f 2>/dev/null | sort | while read file; do
    module=$(echo "$file" | cut -d/ -f10)
    component=$(basename "$file" .vue)
    echo "  $module: $component" >> "$INDEX_FILE"
done

# ============================================================================
# SERVICES INDEX
# ============================================================================
echo "" >> "$INDEX_FILE"
echo "🔧 SERVICES" >> "$INDEX_FILE"
echo "=================================================================================" >> "$INDEX_FILE"

find "$WORKSPACE/apps/web/src/modules" -name "*Service.ts" -type f 2>/dev/null | sort | while read file; do
    echo "" >> "$INDEX_FILE"
    echo "FILE: ${file#$WORKSPACE/}" >> "$INDEX_FILE"
    echo "Exports:" >> "$INDEX_FILE"
    grep "export" "$file" | head -10 >> "$INDEX_FILE"
done

# ============================================================================
# STORES INDEX
# ============================================================================
echo "" >> "$INDEX_FILE"
echo "📦 STORES (Pinia)" >> "$INDEX_FILE"
echo "=================================================================================" >> "$INDEX_FILE"

find "$WORKSPACE/apps/web/src/modules" -name "*Store.ts" -type f 2>/dev/null | sort | while read file; do
    echo "" >> "$INDEX_FILE"
    echo "FILE: ${file#$WORKSPACE/}" >> "$INDEX_FILE"
    echo "Exports:" >> "$INDEX_FILE"
    grep "export" "$file" | head -15 >> "$INDEX_FILE"
done

# ============================================================================
# API ENDPOINTS INDEX
# ============================================================================
echo "" >> "$INDEX_FILE"
echo "🌐 API ENDPOINTS" >> "$INDEX_FILE"
echo "=================================================================================" >> "$INDEX_FILE"

find "$WORKSPACE/apps/api/src/modules" -name "*routes.ts" -type f 2>/dev/null | sort | while read file; do
    echo "" >> "$INDEX_FILE"
    echo "FILE: ${file#$WORKSPACE/}" >> "$INDEX_FILE"
    echo "Endpoints:" >> "$INDEX_FILE"
    grep -E "router\.(get|post|patch|delete|put)|fastify\.(get|post|patch|delete|put)" "$file" | head -20 >> "$INDEX_FILE"
done

# ============================================================================
# BACKEND SERVICES INDEX
# ============================================================================
echo "" >> "$INDEX_FILE"
echo "⚙️ BACKEND SERVICES" >> "$INDEX_FILE"
echo "=================================================================================" >> "$INDEX_FILE"

find "$WORKSPACE/apps/api/src/modules" -name "*service.ts" -type f 2>/dev/null | sort | while read file; do
    echo "" >> "$INDEX_FILE"
    echo "FILE: ${file#$WORKSPACE/}" >> "$INDEX_FILE"
    echo "Methods:" >> "$INDEX_FILE"
    grep -E "^\s*(async |public |private )?[a-zA-Z]+\(" "$file" | grep -v "constructor\|super" | head -20 >> "$INDEX_FILE"
done

# ============================================================================
# IMPORTS GRAPH
# ============================================================================
echo "" >> "$INDEX_FILE"
echo "🔗 IMPORT CHAINS (What imports what)" >> "$INDEX_FILE"
echo "=================================================================================" >> "$INDEX_FILE"

echo "" >> "$INDEX_FILE"
echo "plannerService imported by:" >> "$INDEX_FILE"
grep -r "import.*plannerService" "$WORKSPACE/apps/web/src" 2>/dev/null | cut -d: -f1 | sort -u >> "$INDEX_FILE"

echo "" >> "$INDEX_FILE"
echo "plannerStore imported by:" >> "$INDEX_FILE"
grep -r "import.*plannerStore" "$WORKSPACE/apps/web/src" 2>/dev/null | cut -d: -f1 | sort -u >> "$INDEX_FILE"

echo "" >> "$INDEX_FILE"
echo "Plan type imported by:" >> "$INDEX_FILE"
grep -r "import.*Plan.*from.*types" "$WORKSPACE/apps" 2>/dev/null | cut -d: -f1 | sort -u >> "$INDEX_FILE"

# ============================================================================
# SUMMARY
# ============================================================================
echo "" >> "$INDEX_FILE"
echo "📊 SUMMARY" >> "$INDEX_FILE"
echo "=================================================================================" >> "$INDEX_FILE"
echo "" >> "$INDEX_FILE"
echo "Total types: $(grep -r "export interface\|export type" "$WORKSPACE/packages/types/src" 2>/dev/null | wc -l)" >> "$INDEX_FILE"
echo "Total components: $(find "$WORKSPACE/apps/web/src/modules" -name "*.vue" 2>/dev/null | wc -l)" >> "$INDEX_FILE"
echo "Total services: $(find "$WORKSPACE/apps/web/src/modules" -name "*Service.ts" 2>/dev/null | wc -l)" >> "$INDEX_FILE"
echo "Total stores: $(find "$WORKSPACE/apps/web/src/modules" -name "*Store.ts" 2>/dev/null | wc -l)" >> "$INDEX_FILE"
echo "Total API modules: $(find "$WORKSPACE/apps/api/src/modules" -type d -mindepth 1 -maxdepth 1 2>/dev/null | wc -l)" >> "$INDEX_FILE"

echo "✅ Code index generated: $INDEX_FILE"
echo "   View it: cat $INDEX_FILE"
echo "   Size: $(du -h $INDEX_FILE | cut -f1)"
