#!/bin/bash

# NetGescon Testing Script
# Quick commands for testing the dashboard

echo "🚀 NetGescon Dashboard Testing Helper"
echo "====================================="

echo ""
echo "📋 Available Commands:"
echo "1. Check server status"
echo "2. Create test user (if not exists)"
echo "3. Create test stabile data"
echo "4. Open dashboard in browser"
echo "5. Check database status"
echo "6. Run migrations"
echo "7. Clear Laravel cache"

echo ""
read -p "Enter command number (1-7): " choice

case $choice in
    1)
        echo "🔍 Checking server status..."
        curl -I http://localhost:8000/ 2>/dev/null | head -1 || echo "❌ Server not running"
        ;;
    2)
        echo "👤 Creating test user..."
        cd netgescon-laravel
        php artisan db:seed --class=TestUserSeeder
        echo "✅ User: admin@netgescon.test / password"
        ;;
    3)
        echo "🏢 Creating test stabile..."
        cd netgescon-laravel
        php -r "
        require_once 'vendor/autoload.php';
        \$app = require_once 'bootstrap/app.php';
        \$app->make('Illuminate\Contracts\Console\Kernel')->bootstrap();
        try {
            \$stabile = \App\Models\Stabile::create([
                'denominazione' => 'Condominio Villa Serena - Test',
                'indirizzo' => 'Via Roma, 123',
                'citta' => 'Roma',
                'cap' => '00100',
                'provincia' => 'RM',
                'codice_fiscale' => '80012345678',
                'note' => 'Condominio di test per validazione dashboard'
            ]);
            echo '✅ Stabile created with ID: ' . \$stabile->id . PHP_EOL;
        } catch (Exception \$e) {
            echo '❌ Error: ' . \$e->getMessage() . PHP_EOL;
        }
        "
        ;;
    4)
        echo "🌐 Opening dashboard..."
        if command -v xdg-open > /dev/null; then
            xdg-open http://localhost:8000/admin/stabili
        elif command -v open > /dev/null; then
            open http://localhost:8000/admin/stabili
        else
            echo "📱 Open browser manually: http://localhost:8000/admin/stabili"
        fi
        ;;
    5)
        echo "💾 Checking database..."
        cd netgescon-laravel
        php artisan migrate:status | tail -10
        echo ""
        php -r "
        require_once 'vendor/autoload.php';
        \$app = require_once 'bootstrap/app.php';
        \$app->make('Illuminate\Contracts\Console\Kernel')->bootstrap();
        echo 'Users: ' . \App\Models\User::count() . PHP_EOL;
        echo 'Stabili: ' . \App\Models\Stabile::count() . PHP_EOL;
        "
        ;;
    6)
        echo "🔄 Running migrations..."
        cd netgescon-laravel
        php artisan migrate
        ;;
    7)
        echo "🧹 Clearing cache..."
        cd netgescon-laravel
        php artisan route:clear
        php artisan config:clear
        php artisan view:clear
        php artisan cache:clear
        echo "✅ Cache cleared"
        ;;
    *)
        echo "❌ Invalid choice"
        ;;
esac

echo ""
echo "🏁 Done!"
