#!/usr/bin/env bash
# Update file references after reorganization
# Run AFTER git mv commands

set -e

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

echo "=== Updating references after reorganization ==="
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 README.md"
echo "------------------------"

# Update imgs/ → assets/ in README.md
update_refs "README.md" 'src="imgs/' 'src="assets/'
update_refs "README.md" 'href="imgs/' 'href="assets/'

# Update INSTALLATION.md → installation.md (uppercase to lowercase)
update_refs "README.md" 'docs/INSTALLATION\.md' 'docs/installation.md'
update_refs "README.md" 'docs/DEPLOYMENT\.md' 'docs/deployment.md'
update_refs "README.md" 'docs/QUICKSTART\.md' 'docs/quickstart.md'

echo ""
echo "Step 2: Update assets/ files (formerly imgs/)"
echo "----------------------------------------------"

# Update all files in assets/ directory
if [ -d "assets" ]; then
    for file in assets/*.md assets/*.html; do
        [ -f "$file" ] || continue

        # Update /imgs/ → /assets/
        update_refs "$file" '/imgs/' '/assets/'
        update_refs "$file" 'href="imgs/' 'href="assets/'
        update_refs "$file" 'src="imgs/' 'src="assets/'
    done
fi

echo ""
echo "Step 3: Update docs/ cross-references"
echo "--------------------------------------"

# Update any cross-references in docs/
if [ -d "docs" ]; then
    for file in docs/*.md; do
        [ -f "$file" ] || continue

        # Update uppercase references to lowercase
        update_refs "$file" '\.\./INSTALLATION\.md' '../installation.md'
        update_refs "$file" '\.\./DEPLOYMENT\.md' '../deployment.md'
        update_refs "$file" '\.\./QUICKSTART\.md' '../quickstart.md'
        update_refs "$file" 'INSTALLATION\.md' 'installation.md'
        update_refs "$file" 'DEPLOYMENT\.md' 'deployment.md'
        update_refs "$file" 'QUICKSTART\.md' 'quickstart.md'

        # Update imgs/ → assets/
        update_refs "$file" '\.\./imgs/' '../assets/'
        update_refs "$file" '/imgs/' '/assets/'
    done
fi

echo ""
echo "Step 4: Check for remaining old references"
echo "-------------------------------------------"

# Search for any remaining references (excluding backups, target, .git)
echo "Searching for remaining 'imgs/' references..."
remaining_imgs=$(grep -r "imgs/" --include="*.md" --include="*.html" --include="*.toml" \
    --exclude-dir=target --exclude-dir=.git --exclude-dir=node_modules \
    --exclude="*.bak" . 2>/dev/null | grep -cv ".coder" || echo "0")

if [ "$remaining_imgs" -gt 0 ]; then
    echo "⚠️  Found $remaining_imgs remaining 'imgs/' references:"
    grep -r "imgs/" --include="*.md" --include="*.html" --include="*.toml" \
        --exclude-dir=target --exclude-dir=.git --exclude-dir=node_modules \
        --exclude="*.bak" . 2>/dev/null | grep -v ".coder" | head -10
else
    echo "✓ No remaining 'imgs/' references found"
fi

echo ""
echo "Searching for remaining uppercase doc references..."
remaining_docs=$(grep -rE "INSTALLATION\.md|DEPLOYMENT\.md|QUICKSTART\.md" \
    --include="*.md" --include="*.toml" --include="*.rs" \
    --exclude-dir=target --exclude-dir=.git --exclude-dir=node_modules \
    --exclude="*.bak" . 2>/dev/null | grep -cv ".coder" || echo "0")

if [ "$remaining_docs" -gt 0 ]; then
    echo "⚠️  Found $remaining_docs remaining uppercase doc references:"
    grep -rE "INSTALLATION\.md|DEPLOYMENT\.md|QUICKSTART\.md" \
        --include="*.md" --include="*.toml" --include="*.rs" \
        --exclude-dir=target --exclude-dir=.git --exclude-dir=node_modules \
        --exclude="*.bak" . 2>/dev/null | grep -v ".coder" | head -10
else
    echo "✓ No remaining uppercase doc references found"
fi

echo ""
echo "Step 5: Cleanup backups (optional)"
echo "-----------------------------------"
echo "Backup files created with .bak extension"
echo "To remove backups: find . -name '*.bak' -type f -delete"
echo ""

backup_count=$(find . -name "*.bak" -type f | wc -l)
echo "Found $backup_count backup files"

echo ""
echo "=== Summary ==="
echo "✓ Updated references in README.md"
echo "✓ Updated references in assets/"
echo "✓ Updated references in docs/"
echo "✓ Checked for remaining old 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: reorganize structure and update references'"
