#!/bin/bash

# Sky Art Shop Workspace Organization Script
# This script organizes all scattered files into their appropriate folders

echo "🔧 Organizing Sky Art Shop Workspace..."

# Create organized folder structure
mkdir -p docs/database
mkdir -p docs/frontend  
mkdir -p docs/mobile
mkdir -p docs/performance
mkdir -p docs/fixes
mkdir -p docs/development
mkdir -p docs/project-logs
mkdir -p logs

# Move database related files
echo "📊 Organizing database files..."
mv DATABASE_*.* docs/database/ 2>/dev/null || true

# Move frontend related files  
echo "🎨 Organizing frontend files..."
mv FRONTEND_*.* docs/frontend/ 2>/dev/null || true
mv MOBILE_*.* docs/mobile/ 2>/dev/null || true
mv NAVBAR_*.* docs/frontend/ 2>/dev/null || true

# Move performance files
echo "⚡ Organizing performance files..."
mv PERFORMANCE_*.* docs/performance/ 2>/dev/null || true

# Move fix documentation
echo "🔧 Organizing fix documentation..."
mv FIXES_*.* docs/fixes/ 2>/dev/null || true
mv *_COMPLETE.txt docs/fixes/ 2>/dev/null || true
mv REFACTORING_*.* docs/fixes/ 2>/dev/null || true

# Move general project logs
echo "📋 Organizing project logs..."
mv CACHE_*.* docs/project-logs/ 2>/dev/null || true
mv DEEP_DEBUG_*.* docs/development/ 2>/dev/null || true
mv DIAGNOSIS_*.* docs/development/ 2>/dev/null || true
mv PROJECT_README.md docs/project-logs/ 2>/dev/null || true
mv PORTFOLIO_*.* docs/project-logs/ 2>/dev/null || true
mv PROBLEM_*.* docs/project-logs/ 2>/dev/null || true
mv ROOT_CAUSE_*.* docs/project-logs/ 2>/dev/null || true

# Move any remaining .txt and .md files (except README.md)
echo "📝 Moving remaining documentation files..."
find . -maxdepth 1 -name "*.txt" ! -name "README.md" -exec mv {} docs/project-logs/ \; 2>/dev/null || true
find . -maxdepth 1 -name "*.md" ! -name "README.md" -exec mv {} docs/project-logs/ \; 2>/dev/null || true

# Move shell scripts (except this one)
echo "🔨 Organizing shell scripts..."
find . -maxdepth 1 -name "*.sh" ! -name "organize-workspace.sh" -exec mv {} scripts/ \; 2>/dev/null || true

# Clean up any corrupted files
echo "🧹 Cleaning up corrupted files..."
rm -f "t\"" "tatus" "ystemctl*" "*successfully\"" "organized*" "ac" 2>/dev/null || true

# Show final structure
echo ""
echo "✅ Workspace organization complete!"
echo ""
echo "📁 Organized Structure:"
echo "├── backend/          - Server-side code and APIs"
echo "├── website/          - Frontend code and assets"  
echo "├── config/           - Configuration files"
echo "├── scripts/          - Shell scripts and automation"
echo "├── docs/"
echo "│   ├── database/     - Database related documentation"
echo "│   ├── frontend/     - Frontend fixes and updates"
echo "│   ├── mobile/       - Mobile optimization docs"  
echo "│   ├── performance/  - Performance optimization docs"
echo "│   ├── fixes/        - Bug fixes and refactoring docs"
echo "│   ├── development/  - Development logs and debug info"
echo "│   └── project-logs/ - General project documentation"
echo "├── .env              - Environment variables"
echo "├── .gitignore        - Git ignore rules"
echo "└── README.md         - Main project documentation"
echo ""
echo "🎉 All files have been organized into their respective folders!"