#!/bin/bash
# check-assets.sh - Validate all referenced images exist
# Usage: ./check-assets.sh

set -e

WEBSITE_DIR="/media/pts/Website/SkyArtShop/website"
BACKEND_DIR="/media/pts/Website/SkyArtShop/backend"
DB_NAME="skyartshop"
DB_USER="skyartapp"

echo "🔍 SkyArtShop Asset Validation"
echo "================================"
echo ""

MISSING_COUNT=0
TOTAL_COUNT=0

# Function to check if file exists
check_file() {
    local file="$1"
    local source="$2"
    
    TOTAL_COUNT=$((TOTAL_COUNT + 1))
    
    if [ -f "${WEBSITE_DIR}${file}" ] || [ -L "${WEBSITE_DIR}${file}" ]; then
        echo "✅ ${file}"
        return 0
    else
        echo "❌ Missing: ${file} (referenced in ${source})"
        MISSING_COUNT=$((MISSING_COUNT + 1))
        return 1
    fi
}

# Check critical images
echo "📋 Checking Critical Images..."
echo "-------------------------------"
check_file "/assets/images/hero-image.jpg" "home.html"
check_file "/assets/images/inspiration.jpg" "home.html"
check_file "/assets/images/placeholder.jpg" "multiple pages"
check_file "/assets/images/products/placeholder.jpg" "product pages"
echo ""

# Check HTML image references
echo "📄 Checking HTML Image References..."
echo "-------------------------------------"
if [ -d "${WEBSITE_DIR}/public" ]; then
    while IFS= read -r line; do
        # Extract image path from src attribute
        img=$(echo "$line" | sed -E 's/.*src="([^"]*\.(jpg|jpeg|png|gif|svg))".*/\1/')
        file=$(echo "$line" | cut -d':' -f1)
        
        if [ -n "$img" ] && [ "$img" != "$line" ]; then
            check_file "$img" "$(basename $file)"
        fi
    done < <(grep -roh 'src="[^"]*\.\(jpg\|jpeg\|png\|gif\|svg\)' "${WEBSITE_DIR}/public/"*.html 2>/dev/null || true)
fi
echo ""

# Check database image references
echo "🗄️  Checking Database Product Images..."
echo "----------------------------------------"
if command -v psql &> /dev/null; then
    while IFS= read -r img; do
        img=$(echo "$img" | xargs) # trim whitespace
        if [ -n "$img" ]; then
            check_file "$img" "database products table"
        fi
    done < <(PGPASSWORD="${DB_PASSWORD}" psql -U "$DB_USER" -d "$DB_NAME" -t -c "SELECT DISTINCT imageurl FROM products WHERE imageurl != '' AND imageurl IS NOT NULL;" 2>/dev/null || echo "")
else
    echo "⚠️  psql not available - skipping database check"
fi
echo ""

# Check uploads directory
echo "📁 Checking Uploads Directory..."
echo "---------------------------------"
UPLOAD_DIR="${WEBSITE_DIR}/uploads"
if [ -d "$UPLOAD_DIR" ]; then
    UPLOAD_COUNT=$(find "$UPLOAD_DIR" -type f | wc -l)
    UPLOAD_SIZE=$(du -sh "$UPLOAD_DIR" | cut -f1)
    echo "✅ Uploads directory exists"
    echo "   Files: $UPLOAD_COUNT"
    echo "   Size: $UPLOAD_SIZE"
else
    echo "❌ Uploads directory missing: $UPLOAD_DIR"
    MISSING_COUNT=$((MISSING_COUNT + 1))
fi
echo ""

# Summary
echo "📊 Summary"
echo "=========="
echo "Total images checked: $TOTAL_COUNT"
echo "Missing images: $MISSING_COUNT"
echo ""

if [ $MISSING_COUNT -eq 0 ]; then
    echo "✅ All assets validated successfully!"
    exit 0
else
    echo "⚠️  Found $MISSING_COUNT missing asset(s)"
    echo ""
    echo "💡 Suggestions:"
    echo "   1. Create symbolic links for placeholder images"
    echo "   2. Add real images to replace placeholders"
    echo "   3. Update database references to use existing images"
    exit 1
fi
