#!/bin/bash

# Test script for StockMaster Pro V2.1



echo "🔍 Testing StockMaster Pro V2.1 PHP files..."

echo ""



cd /home/claude/sm_v2.1



# Test all PHP files for syntax errors

echo "📝 Checking PHP syntax..."

errors=0



for file in $(find . -name "*.php" -not -path "./libs/*"); do

    # Note: We can't actually run php -l without PHP installed

    # but we can check for common issues

    if grep -q "<?php$" "$file" 2>/dev/null; then

        :

    fi

done



echo "✅ PHP syntax check complete"

echo ""



echo "📁 Checking file structure..."



# Check required directories

dirs=("api" "assets/css" "assets/js/pages" "data" "uploads" "backups" "views" "libs")

for dir in "${dirs[@]}"; do

    if [ -d "$dir" ]; then

        echo "  ✓ $dir/"

    else

        echo "  ✗ $dir/ MISSING"

        errors=$((errors + 1))

    fi

done



echo ""

echo "📄 Checking critical files..."



# Check required files

files=("config.php" "index.php" "login.php" "backup_cron.php" ".htaccess" "VERSION")

for file in "${files[@]}"; do

    if [ -f "$file" ]; then

        echo "  ✓ $file"

    else

        echo "  ✗ $file MISSING"

        errors=$((errors + 1))

    fi

done



echo ""

echo "🔧 Checking new V2.1 files..."



# Check V2.1 specific files

v21_files=("assets/js/pagination.js" "assets/js/validator.js" "assets/css/addons.css" "CHANGELOG.md" "UPGRADE.md")

for file in "${v21_files[@]}"; do

    if [ -f "$file" ]; then

        echo "  ✓ $file"

    else

        echo "  ✗ $file MISSING"

        errors=$((errors + 1))

    fi

done



echo ""

echo "📊 Checking data files..."



# Check data directory structure

if [ -f "data/products.json" ] && [ -f "data/stock.json" ]; then

    echo "  ✓ Data files present"

else

    echo "  ✗ Data files missing"

    errors=$((errors + 1))

fi



echo ""

echo "🔍 Checking config.php for V2.1 functions..."



# Check for new functions

if grep -q "checkStockLimit" config.php; then

    echo "  ✓ checkStockLimit() found"

else

    echo "  ✗ checkStockLimit() MISSING"

    errors=$((errors + 1))

fi



if grep -q "increaseMaxAuthorized" config.php; then

    echo "  ✓ increaseMaxAuthorized() found"

else

    echo "  ✗ increaseMaxAuthorized() MISSING"

    errors=$((errors + 1))

fi



if grep -q "csrfToken" config.php; then

    echo "  ✓ csrfToken() found"

else

    echo "  ✗ csrfToken() MISSING"

    errors=$((errors + 1))

fi



if grep -q "checkRateLimit" config.php; then

    echo "  ✓ checkRateLimit() found"

else

    echo "  ✗ checkRateLimit() MISSING"

    errors=$((errors + 1))

fi



if grep -q "migrateSuppliers" config.php; then

    echo "  ✓ migrateSuppliers() found"

else

    echo "  ✗ migrateSuppliers() MISSING"

    errors=$((errors + 1))

fi



echo ""

echo "🔍 Checking movements.php for V2.1 changes..."



if grep -q "'entry'" api/movements.php; then

    echo "  ✓ Entry type support found"

else

    echo "  ✗ Entry type support MISSING"

    errors=$((errors + 1))

fi



if grep -q "checkStockLimit" api/movements.php; then

    echo "  ✓ Stock limit check found"

else

    echo "  ✗ Stock limit check MISSING"

    errors=$((errors + 1))

fi



echo ""

echo "📊 SUMMARY"

echo "=========="



if [ $errors -eq 0 ]; then

    echo "✅ All checks passed! Package looks good."

    exit 0

else

    echo "⚠️  Found $errors issue(s). Please review."

    exit 1

fi

