#!/bin/bash

################################################################################
# Migrate Storage Files to Public Directory
# Purpose: Move files from storage/app/public to public/storage for cPanel compatibility
# Usage: ./scripts/migrate-storage-to-public.sh
################################################################################

set -e

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

echo "════════════════════════════════════════════════════════════════════════════"
echo "  STORAGE MIGRATION TO PUBLIC DIRECTORY"
echo "════════════════════════════════════════════════════════════════════════════"
echo ""

cd "$PROJECT_ROOT"

# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color

print_ok() {
    echo -e "${GREEN}✓${NC} $1"
}

print_error() {
    echo -e "${RED}✗${NC} $1"
}

print_warning() {
    echo -e "${YELLOW}⚠${NC} $1"
}

print_info() {
    echo -e "${BLUE}ℹ${NC} $1"
}

echo "This script will:"
echo "  1. Create public/storage directory structure"
echo "  2. Copy files from storage/app/public to public/storage"
echo "  3. Set correct permissions (755 directories, 644 files)"
echo "  4. Preserve original files in storage/app/public"
echo ""

# Check if storage/app/public exists and has files
if [ ! -d "storage/app/public" ]; then
    print_warning "storage/app/public does not exist - nothing to migrate"
    exit 0
fi

FILE_COUNT=$(find storage/app/public -type f | wc -l)
if [ "$FILE_COUNT" -eq 0 ]; then
    print_warning "storage/app/public has no files - nothing to migrate"
    exit 0
fi

print_info "Found $FILE_COUNT files to migrate"
echo ""

# Remove symlink if it exists (check this FIRST before directory check)
if [ -L "public/storage" ]; then
    print_warning "Removing symlink at public/storage..."
    rm -f public/storage
    print_ok "Symlink removed"
fi

# Create public/storage directory if it doesn't exist
if [ ! -d "public/storage" ]; then
    print_info "Creating public/storage directory..."
    mkdir -p public/storage
    chmod 755 public/storage
    print_ok "Created public/storage"
else
    print_info "public/storage directory already exists"
fi

echo ""
print_info "Copying files from storage/app/public to public/storage..."
echo ""

# Copy files recursively, preserving structure
rsync -av --progress storage/app/public/ public/storage/

echo ""
print_info "Setting correct permissions..."

# Set directory permissions to 755
find public/storage -type d -exec chmod 755 {} \;

# Set file permissions to 644
find public/storage -type f -exec chmod 644 {} \;

print_ok "Permissions set successfully"
echo ""

# Verify migration
MIGRATED_COUNT=$(find public/storage -type f | wc -l)
print_ok "Migration complete: $MIGRATED_COUNT files in public/storage"

# Show directory structure
echo ""
print_info "Directory structure:"
ls -lAh public/storage | head -20

echo ""
print_info "Sample file permissions:"
find public/storage -type f | head -5 | while read -r file; do
    ls -lh "$file"
done

echo ""
echo "════════════════════════════════════════════════════════════════════════════"
print_ok "MIGRATION COMPLETED SUCCESSFULLY"
echo "════════════════════════════════════════════════════════════════════════════"
echo ""
echo "Next steps:"
echo "  1. Test file access: curl \$APP_URL/storage/test.txt"
echo "  2. Upload new file via admin to test direct storage"
echo "  3. If everything works, you can remove storage/app/public (optional)"
echo ""
