#!/bin/bash

# 🔍 QUICK CODE SEARCH COMMANDS
# Copy-paste ready for exploring code

# ==============================================================================
# FIND THINGS (Core grep patterns)
# ==============================================================================

# What type definitions exist?
find_types() {
    echo "📋 All types defined:"
    find packages/types/src -name "*.ts" -exec grep -H "export interface\|export type" {} \;
}

# Find a specific type
find_type() {
    local type=$1
    echo "🔎 Finding type: $type"
    grep -r "export interface $type\|export type $type" packages/
}

# What services exist in a domain?
find_services() {
    local domain=$1
    echo "🔧 Services in $domain:"
    ls -1 apps/web/src/modules/$domain/services/ 2>/dev/null || echo "No services found"
}

# What components exist in a domain?
find_components() {
    local domain=$1
    echo "🎨 Components in $domain:"
    ls -1 apps/web/src/modules/$domain/components/ 2>/dev/null || echo "No components found"
}

# What endpoints exist?
find_endpoints() {
    local domain=$1
    echo "🌐 Endpoints in $domain:"
    grep -E "router\.(get|post|patch|delete)|fastify\.(get|post|patch|delete)" \
        apps/api/src/modules/$domain/${domain}.routes.ts 2>/dev/null || echo "No endpoints found"
}

# ==============================================================================
# TRACE THINGS (Follow imports and calls)
# ==============================================================================

# Who imports X?
find_importers() {
    local name=$1
    echo "📍 Who imports $name:"
    grep -r "import.*$name" apps/ packages/
}

# Who calls function X?
find_callers() {
    local func=$1
    echo "📞 Who calls $func:"
    grep -r "$func(" apps/
}

# What does X export?
find_exports() {
    local file=$1
    echo "📤 Exports from $file:"
    grep "export" "$file" | head -20
}

# Trace full import chain for component
trace_component() {
    local component=$1
    echo "🔗 Full chain for $component:"
    echo "1️⃣ Component definition:"
    find . -name "$component.vue" -exec cat {} \; | grep -A 5 "export\|<script"
    echo "2️⃣ Imported by:"
    grep -r "import.*$component" apps/
    echo "3️⃣ Uses services:"
    find . -name "$component.vue" -exec cat {} \; | grep "import.*Service"
}

# ==============================================================================
# VERIFY THINGS (Check consistency)
# ==============================================================================

# Do type definitions match API?
verify_type_api() {
    local type=$1
    echo "🔍 Checking if $type is used consistently:"
    echo "1️⃣ Type definition:"
    grep -A 10 "export interface $type" packages/types/src/*.ts
    echo "2️⃣ API returns:"
    grep -r "$type" apps/api/src/modules/ | grep "return\|:"
    echo "3️⃣ Frontend expects:"
    grep -r "$type" apps/web/src/modules/ | grep "interface\|type"
}

# What validation rules apply?
find_validation() {
    local type=$1
    echo "✅ Validation for $type:"
    grep -B 2 -A 10 "$type.*z\.|${type}Schema" apps/api/src/modules/*/planner.schemas.ts
}

# ==============================================================================
# USAGE
# ==============================================================================

# Print help
help_grep() {
    echo "🔍 CODE EXPLORATION COMMANDS"
    echo ""
    echo "FIND:"
    echo "  find_types                    - List all types"
    echo "  find_type <name>              - Find specific type"
    echo "  find_services <domain>        - List services in domain"
    echo "  find_components <domain>      - List components in domain"
    echo "  find_endpoints <domain>       - List endpoints in domain"
    echo ""
    echo "TRACE:"
    echo "  find_importers <name>         - Who imports X?"
    echo "  find_callers <func>           - Who calls X?"
    echo "  find_exports <file>           - What does X export?"
    echo "  trace_component <name>        - Full chain for component"
    echo ""
    echo "VERIFY:"
    echo "  verify_type_api <type>        - Type matches API?"
    echo "  find_validation <type>        - What validation rules?"
    echo ""
    echo "USAGE:"
    echo "  source scripts/grep-helpers.sh"
    echo "  find_types"
    echo "  find_endpoints planner"
    echo "  trace_component PlanCard"
}

# ==============================================================================
# QUICK ONE-LINERS (Just copy-paste into terminal)
# ==============================================================================

# All types
# grep -r "export interface\|export type" packages/types/src/

# All API endpoints
# grep -r "router\.\|fastify\." apps/api/src/modules/*/\*routes.ts

# All components
# find apps/web/src/modules -name "*.vue" | sort

# Who uses Plan type?
# grep -r "Plan" apps/ packages/ | grep -E "import|interface|type"

# What's in planner module?
# ls -la apps/web/src/modules/planner/

# Follow plannerService
# grep -r "plannerService" apps/web/src/

# Check endpoint definition
# grep -A 10 "router.get.*plans" apps/api/src/modules/planner/planner.routes.ts

# ==============================================================================
# AUTO-DISCOVERY (Smart exploration)
# ==============================================================================

# Given a component name, show everything about it
explore() {
    local target=$1
    echo "🔍 EXPLORING: $target"
    echo ""
    echo "1️⃣ FILE LOCATION:"
    find . -name "*$target*" -type f | head -5
    echo ""
    echo "2️⃣ DEFINITION:"
    find . -name "*$target*" -type f | xargs grep "export\|interface\|const" 2>/dev/null | head -5
    echo ""
    echo "3️⃣ IMPORTED BY:"
    grep -r "import.*$target" apps/ packages/ | cut -d: -f1 | sort -u
    echo ""
    echo "4️⃣ IMPORTS:"
    find . -name "*$target*" -type f | xargs grep "^import" 2>/dev/null | head -5
}

# ==============================================================================

# If called with arguments, run that function
if [ $# -gt 0 ]; then
    "$@"
else
    help_grep
fi
