#!/bin/bash

# Debug Script for Quotation Issues
# This script helps identify and fix quotation-related problems

echo "🔍 Debugging Quotation System..."

# Check if PHP and Laravel are working
echo "1. Checking PHP and Laravel status..."
php artisan --version
php -v | head -1

# Check database connection
echo "2. Testing database connection..."
php artisan migrate:status | head -5

# Check routes
echo "3. Verifying quotation routes..."
php artisan route:list | grep quotation

# Check for JavaScript errors in logs
echo "4. Checking recent logs for errors..."
if [ -f "storage/logs/laravel.log" ]; then
    echo "Recent Laravel errors:"
    tail -20 storage/logs/laravel.log
else
    echo "No Laravel log file found"
fi

# Check file permissions
echo "5. Checking file permissions..."
ls -la public/js/quotation.js
ls -la resources/views/quotations/create.blade.php

# Test the AJAX endpoint directly
echo "6. Testing quotations endpoint..."
php artisan tinker --execute="
\$client = App\Models\Client::first();
if (\$client) {
    echo 'Testing with client ID: ' . \$client->id . PHP_EOL;
    \$events = App\Models\Event::where('client_id', \$client->id)->get();
    echo 'Found ' . \$events->count() . ' events for this client' . PHP_EOL;
    if (\$events->count() > 0) {
        echo 'Sample event: ' . \$events->first()->name . PHP_EOL;
    }
} else {
    echo 'No clients found in database' . PHP_EOL;
}
"

# Check jQuery and Bootstrap loading
echo "7. Checking asset files..."
if [ -f "public/js/quotation.js" ]; then
    echo "✅ quotation.js exists ($(wc -l < public/js/quotation.js) lines)"
    echo "Last modified: $(stat -c %y public/js/quotation.js)"
else
    echo "❌ quotation.js not found"
fi

# Check for common JavaScript issues
echo "8. Checking for JavaScript syntax issues..."
node -c public/js/quotation.js 2>/dev/null && echo "✅ JavaScript syntax is valid" || echo "❌ JavaScript syntax errors found"

# Check network connectivity to external CDNs
echo "9. Testing CDN connectivity..."
curl -s -o /dev/null -w "%{http_code}" "https://code.jquery.com/jquery-3.6.0.min.js" | grep -q "200" && echo "✅ jQuery CDN accessible" || echo "❌ jQuery CDN not accessible"
curl -s -o /dev/null -w "%{http_code}" "https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js" | grep -q "200" && echo "✅ Bootstrap CDN accessible" || echo "❌ Bootstrap CDN not accessible"

# Generate a test URL
echo "10. Generating test commands..."
echo ""
echo "🧪 Manual Testing Commands:"
echo "Test client events endpoint:"
echo "curl -X GET 'http://localhost/quotations/get-events-by-client/1' -H 'Accept: application/json' -H 'X-Requested-With: XMLHttpRequest'"
echo ""
echo "Test in browser console:"
echo "$.ajax({url: '/quotations/get-events-by-client/1', type: 'GET', dataType: 'json'}).done(function(data) { console.log('Success:', data); }).fail(function(xhr) { console.log('Error:', xhr.responseText); });"
echo ""
echo "📋 Browser Debugging Steps:"
echo "1. Open browser developer tools (F12)"
echo "2. Go to Console tab"
echo "3. Navigate to quotation create page"
echo "4. Select a client and watch for console messages"
echo "5. Check Network tab for failed AJAX requests"
echo ""
echo "🔧 Quick Fixes to Try:"
echo "1. Clear browser cache: Ctrl+Shift+R"
echo "2. Clear Laravel cache: php artisan cache:clear"
echo "3. Restart web server"
echo "4. Check .env file for correct APP_URL"
echo ""
echo "✅ Debug script completed!"
