#!/usr/bin/env bash
# Update references to renamed documentation files
# Run AFTER renaming docs to lowercase

set -e

PROJECT_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
cd "$PROJECT_ROOT"

echo "=== Updating documentation references ==="
echo ""

# Backup function
backup_file() {
    local file=$1
    if [ -f "$file" ]; then
        cp "$file" "${file}.bak"
        echo "✓ Backed up: $file"
    fi
}

# Update function with display
update_refs() {
    local file=$1
    local search=$2
    local replace=$3

    if [ -f "$file" ]; then
        if grep -q "$search" "$file" 2>/dev/null; then
            backup_file "$file"
            sed -i '' "s|$search|$replace|g" "$file"
            echo "✓ Updated: $file"
            echo "  ├─ $search → $replace"
        fi
    fi
}

echo "Step 1: Update root-level doc references"
echo "-----------------------------------------"

# Find all markdown files and update references
find . -name "*.md" -type f -not -path "./target/*" -not -path "./.git/*" -not -path "./node_modules/*" -not -name "*.bak" | while read -r file; do
    # Root level docs
    update_refs "$file" 'docs/BUILD\.md' 'docs/build.md'
    update_refs "$file" 'BUILD\.md' 'build.md'

    update_refs "$file" 'docs/CONFIGURATION\.md' 'docs/configuration.md'
    update_refs "$file" 'CONFIGURATION\.md' 'configuration.md'

    update_refs "$file" 'docs/DEVELOPMENT\.md' 'docs/development.md'
    update_refs "$file" 'DEVELOPMENT\.md' 'development.md'

    update_refs "$file" 'docs/FIELD_TYPES\.md' 'docs/field-types.md'
    update_refs "$file" 'docs/field_types\.md' 'docs/field-types.md'
    update_refs "$file" 'FIELD_TYPES\.md' 'field-types.md'
    update_refs "$file" 'field_types\.md' 'field-types.md'

    update_refs "$file" 'docs/INSTALLATION\.md' 'docs/installation.md'
    update_refs "$file" 'INSTALLATION\.md' 'installation.md'

    update_refs "$file" 'docs/NICKEL\.md' 'docs/nickel.md'
    update_refs "$file" 'NICKEL\.md' 'nickel.md'

    update_refs "$file" 'docs/PRE-COMMIT-SETUP\.md' 'docs/pre-commit-setup.md'
    update_refs "$file" 'PRE-COMMIT-SETUP\.md' 'pre-commit-setup.md'

    update_refs "$file" 'docs/RELEASE\.md' 'docs/release.md'
    update_refs "$file" 'RELEASE\.md' 'release.md'
done

echo ""
echo "Step 2: Update agent/ doc references"
echo "-------------------------------------"

find . -name "*.md" -type f -not -path "./target/*" -not -path "./.git/*" -not -path "./node_modules/*" -not -name "*.bak" | while read -r file; do
    update_refs "$file" 'docs/agent/GETTING_STARTED\.md' 'docs/agent/getting-started.md'
    update_refs "$file" 'docs/agent/getting_started\.md' 'docs/agent/getting-started.md'
    update_refs "$file" 'agent/GETTING_STARTED\.md' 'agent/getting-started.md'
    update_refs "$file" 'agent/getting_started\.md' 'agent/getting-started.md'
    update_refs "$file" 'GETTING_STARTED\.md' 'getting-started.md'
    update_refs "$file" 'getting_started\.md' 'getting-started.md'

    update_refs "$file" 'docs/agent/LLM_PROVIDERS\.md' 'docs/agent/llm-providers.md'
    update_refs "$file" 'docs/agent/llm_providers\.md' 'docs/agent/llm-providers.md'
    update_refs "$file" 'agent/LLM_PROVIDERS\.md' 'agent/llm-providers.md'
    update_refs "$file" 'agent/llm_providers\.md' 'agent/llm-providers.md'
    update_refs "$file" 'LLM_PROVIDERS\.md' 'llm-providers.md'
    update_refs "$file" 'llm_providers\.md' 'llm-providers.md'

    update_refs "$file" 'docs/agent/LLM_INTEGRATION\.md' 'docs/agent/llm-integration.md'
    update_refs "$file" 'LLM_INTEGRATION\.md' 'llm-integration.md'
    update_refs "$file" 'typedialog-ag-core/LLM_INTEGRATION\.md' 'agent/llm-integration.md'
done

echo ""
echo "Step 3: Update encryption/ doc references"
echo "------------------------------------------"

find . -name "*.md" -type f -not -path "./target/*" -not -path "./.git/*" -not -path "./node_modules/*" -not -name "*.bak" | while read -r file; do
    update_refs "$file" 'docs/encryption/ENCRYPTION-QUICK-START\.md' 'docs/encryption/encryption-quick-start.md'
    update_refs "$file" 'encryption/ENCRYPTION-QUICK-START\.md' 'encryption/encryption-quick-start.md'
    update_refs "$file" 'ENCRYPTION-QUICK-START\.md' 'encryption-quick-start.md'

    update_refs "$file" 'docs/encryption/ENCRYPTION-SERVICES-SETUP\.md' 'docs/encryption/encryption-services-setup.md'
    update_refs "$file" 'encryption/ENCRYPTION-SERVICES-SETUP\.md' 'encryption/encryption-services-setup.md'
    update_refs "$file" 'ENCRYPTION-SERVICES-SETUP\.md' 'encryption-services-setup.md'

    update_refs "$file" 'docs/encryption/ENCRYPTION-UNIFIED-ARCHITECTURE\.md' 'docs/encryption/encryption-unified-architecture.md'
    update_refs "$file" 'encryption/ENCRYPTION-UNIFIED-ARCHITECTURE\.md' 'encryption/encryption-unified-architecture.md'
    update_refs "$file" 'ENCRYPTION-UNIFIED-ARCHITECTURE\.md' 'encryption-unified-architecture.md'
done

echo ""
echo "Step 4: Update examples/ doc references"
echo "---------------------------------------"

find . -name "*.md" -type f -not -path "./target/*" -not -path "./.git/*" -not -path "./node_modules/*" -not -name "*.bak" | while read -r file; do
    update_refs "$file" 'SOPS-DEMO\.md' 'sops-demo.md'
    update_refs "$file" 'TEST-SOPS-INTEGRATION\.md' 'test-sops-integration.md'
    update_refs "$file" 'INTEGRATION_GUIDE\.md' 'integration-guide.md'
done

echo ""
echo "Step 5: Check for remaining uppercase doc references"
echo "-----------------------------------------------------"

remaining=$(grep -rcE "BUILD\.md|CONFIGURATION\.md|DEVELOPMENT\.md|FIELD_TYPES\.md|INSTALLATION\.md|NICKEL\.md|PRE-COMMIT-SETUP\.md|RELEASE\.md|GETTING_STARTED\.md|LLM_PROVIDERS\.md|LLM_INTEGRATION\.md|ENCRYPTION-QUICK-START\.md|ENCRYPTION-SERVICES-SETUP\.md|ENCRYPTION-UNIFIED-ARCHITECTURE\.md|SOPS-DEMO\.md|TEST-SOPS-INTEGRATION\.md|INTEGRATION_GUIDE\.md" \
    --include="*.md" --include="*.toml" --include="*.rs" \
    --exclude-dir=target --exclude-dir=.git --exclude-dir=node_modules \
    --exclude="*.bak" . 2>/dev/null | grep -cv "update-docs-refs.sh" || echo "0")

if [ "$remaining" -gt 0 ]; then
    echo "⚠️  Found $remaining remaining uppercase doc references:"
    grep -rE "BUILD\.md|CONFIGURATION\.md|DEVELOPMENT\.md|FIELD_TYPES\.md|INSTALLATION\.md|NICKEL\.md|PRE-COMMIT-SETUP\.md|RELEASE\.md|GETTING_STARTED\.md|LLM_PROVIDERS\.md|LLM_INTEGRATION\.md|ENCRYPTION-QUICK-START\.md|ENCRYPTION-SERVICES-SETUP\.md|ENCRYPTION-UNIFIED-ARCHITECTURE\.md|SOPS-DEMO\.md|TEST-SOPS-INTEGRATION\.md|INTEGRATION_GUIDE\.md" \
        --include="*.md" --include="*.toml" --include="*.rs" \
        --exclude-dir=target --exclude-dir=.git --exclude-dir=node_modules \
        --exclude="*.bak" . 2>/dev/null | grep -v "update-docs-refs.sh" | head -10
else
    echo "✓ No remaining uppercase doc references found"
fi

echo ""
echo "Step 5: Cleanup backups (optional)"
echo "-----------------------------------"
backup_count=$(find . -name "*.bak" -type f | wc -l)
echo "Found $backup_count backup files"
echo "To remove backups: find . -name '*.bak' -type f -delete"

echo ""
echo "=== Summary ===="
echo "✓ Updated references to lowercase doc filenames"
echo "✓ Checked for remaining uppercase references"
echo ""
echo "Next steps:"
echo "1. Review changes: git diff"
echo "2. Test that links work"
echo "3. Remove backups: find . -name '*.bak' -delete"
echo "4. Commit: git add -A && git commit -m 'docs: rename files to lowercase'"
