#!/bin/bash
# Organize Remaining Root Directory Files
# Moves remaining files from root to appropriate locations

set -euo pipefail

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

# Colors
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m'

log() {
    echo -e "${BLUE}[$(date +'%Y-%m-%d %H:%M:%S')]${NC} $1"
}

success() {
    echo -e "${GREEN}[OK]${NC} $1"
}

move_file() {
    local src="$1"
    local dest="$2"
    
    if [ ! -f "$src" ]; then
        return 1
    fi
    
    local dest_dir=$(dirname "$dest")
    mkdir -p "$dest_dir"
    
    if mv "$src" "$dest" 2>/dev/null; then
        success "Moved: $src -> $dest"
        return 0
    else
        return 1
    fi
}

log "Organizing remaining root directory files..."

# Configuration/Setup guides → docs/04-configuration/
log "Moving configuration guides..."
move_file "CLOUDFLARE_API_SETUP.md" "docs/04-configuration/CLOUDFLARE_API_SETUP.md"
move_file "CLOUDFLARE_TUNNEL_INSTALL_NOW.md" "docs/04-configuration/CLOUDFLARE_TUNNEL_INSTALL_NOW.md"
move_file "SETUP_TUNNEL_NOW.md" "docs/04-configuration/SETUP_TUNNEL_NOW.md"
move_file "TUNNEL_TOKEN_INSTALL.md" "docs/04-configuration/TUNNEL_TOKEN_INSTALL.md"
move_file "TUNNEL_CONFIG_VERIFIED.md" "docs/04-configuration/TUNNEL_CONFIG_VERIFIED.md"
move_file "ALI_RPC_PORT_FORWARDING_CONFIG.md" "docs/04-configuration/ALI_RPC_PORT_FORWARDING_CONFIG.md"
move_file "NGINX_CONFIGURATIONS_VMIDS_2400-2508.md" "docs/04-configuration/NGINX_CONFIGURATIONS_VMIDS_2400-2508.md"

# Troubleshooting guides → docs/09-troubleshooting/
log "Moving troubleshooting guides..."
move_file "NO_SSH_ACCESS_SOLUTION.md" "docs/09-troubleshooting/NO_SSH_ACCESS_SOLUTION.md"
move_file "TROUBLESHOOT_CONNECTION.md" "docs/09-troubleshooting/TROUBLESHOOT_CONNECTION.md"
move_file "FIX_TUNNEL_ALTERNATIVES.md" "docs/09-troubleshooting/FIX_TUNNEL_ALTERNATIVES.md"
move_file "TUNNEL_SOLUTIONS.md" "docs/09-troubleshooting/TUNNEL_SOLUTIONS.md"
move_file "R630-04-AUTHENTICATION-ISSUE.md" "docs/09-troubleshooting/R630-04-AUTHENTICATION-ISSUE.md"
move_file "R630-04-CONSOLE-ACCESS-GUIDE.md" "docs/09-troubleshooting/R630-04-CONSOLE-ACCESS-GUIDE.md"
move_file "R630-04-PROXMOX-TROUBLESHOOTING.md" "docs/09-troubleshooting/R630-04-PROXMOX-TROUBLESHOOTING.md"
move_file "fix-ssh-key-issue.md" "docs/09-troubleshooting/fix-ssh-key-issue.md"
move_file "ssh-r630-04-options.md" "docs/09-troubleshooting/ssh-r630-04-options.md"

# Quick start guides → docs/01-getting-started/
log "Moving quick start guides..."
move_file "LIST_VMS_QUICK_START.md" "docs/01-getting-started/LIST_VMS_QUICK_START.md"
move_file "LIST_VMS_README.md" "docs/01-getting-started/LIST_VMS_README.md"
move_file "THIRDWEB_RPC_CLOUDFLARE_QUICKSTART.md" "docs/01-getting-started/THIRDWEB_RPC_CLOUDFLARE_QUICKSTART.md"
move_file "THIRDWEB_RPC_QUICKSTART.md" "docs/01-getting-started/THIRDWEB_RPC_QUICKSTART.md"
move_file "THIRDWEB_RPC_NEXT_STEPS.md" "docs/01-getting-started/THIRDWEB_RPC_NEXT_STEPS.md"
move_file "REMINING_STEPS_QUICK_REFERENCE.md" "docs/01-getting-started/REMINING_STEPS_QUICK_REFERENCE.md"

# Reports/Analyses → reports/
log "Moving reports and analyses..."
move_file "COMPREHENSIVE_PROJECT_REVIEW.md" "reports/COMPREHENSIVE_PROJECT_REVIEW.md"
move_file "R630-02_CONTAINERS_AND_SERVICES_REVIEW.md" "reports/R630-02_CONTAINERS_AND_SERVICES_REVIEW.md"
move_file "DNS_CONFLICT_RESOLUTION.md" "reports/analyses/DNS_CONFLICT_RESOLUTION.md"
move_file "MIM4U_DOMAIN_CONFLICT.md" "reports/analyses/MIM4U_DOMAIN_CONFLICT.md"
move_file "ECOSYSTEM_IMPROVEMENT_PLAN.md" "reports/ECOSYSTEM_IMPROVEMENT_PLAN.md"
move_file "ALL_TUNNELS_DOWN.md" "reports/status/ALL_TUNNELS_DOWN.md"

# Cleanup reports → reports/
log "Moving cleanup reports..."
move_file "CLEANUP_COMPLETE_SUMMARY.md" "reports/CLEANUP_COMPLETE_SUMMARY.md"
move_file "CLEANUP_RESULTS.md" "reports/CLEANUP_RESULTS.md"
move_file "MARKDOWN_CLEANUP_QUICK_START.md" "reports/MARKDOWN_CLEANUP_QUICK_START.md"

# Reference/Notes → docs/11-references/
log "Moving reference files..."
mkdir -p "docs/11-references"
move_file "CHAIN138_TOKEN_ADDRESSES.md" "docs/11-references/CHAIN138_TOKEN_ADDRESSES.md"
move_file "OMADA_AUTH_NOTE.md" "docs/11-references/OMADA_AUTH_NOTE.md"
move_file "OMADA_QUERY_INSTRUCTIONS.md" "docs/11-references/OMADA_QUERY_INSTRUCTIONS.md"
move_file "GET_EMAIL_FROM_API.md" "docs/11-references/GET_EMAIL_FROM_API.md"
move_file "76.53.10.34_CONNECTION_EXPLANATION.md" "docs/11-references/76.53.10.34_CONNECTION_EXPLANATION.md"
move_file "README_EXPLORER_SUBMODULE.md" "docs/11-references/README_EXPLORER_SUBMODULE.md"

log "Organization complete!"
log "Remaining files in root:"
find . -maxdepth 1 -name "*.md" -type f | sort
