#!/bin/bash
# Install TastyIgniter Default Theme

echo "🎨 Installing TastyIgniter Default Theme..."
echo ""

cd ~/public_html/restaurant.minra.top

echo "1. Installing theme via Composer..."
composer require tastyigniter/ti-theme-orange --no-interaction

if [ $? -eq 0 ]; then
    echo "   ✅ Theme installed successfully"
else
    echo "   ⚠️  Composer install failed, trying alternative method..."
    
    # Alternative: Clone from GitHub
    cd themes
    git clone https://github.com/tastyigniter/ti-theme-orange.git tastyigniter-orange
    cd ..
fi

echo ""
echo "2. Checking installed themes..."
ls -la themes/

echo ""
echo "3. Activating theme..."
php artisan tinker << 'EOF'
$themeName = 'tastyigniter-orange';

// Check if theme directory exists
$themePath = base_path("themes/{$themeName}");
if (!is_dir($themePath)) {
    echo "❌ Theme directory not found: {$themePath}\n";
    exit;
}

// Set active theme
DB::table('settings')->updateOrInsert(
    ['item' => 'active_theme'],
    ['sort' => 'core', 'value' => $themeName, 'updated_at' => now()]
);

// Register theme
DB::table('themes')->updateOrInsert(
    ['code' => $themeName],
    [
        'name' => 'TastyIgniter Orange',
        'description' => 'Default TastyIgniter theme',
        'version' => '1.0.0',
        'status' => 1,
        'is_default' => 1,
        'created_at' => now(),
        'updated_at' => now()
    ]
);

echo "✅ Theme activated: {$themeName}\n";
exit
EOF

echo ""
echo "4. Setting permissions..."
chmod -R 755 themes/

echo ""
echo "5. Clearing caches..."
php artisan config:clear
php artisan cache:clear
php artisan view:clear

echo ""
echo "6. Verifying..."
php artisan tinker --execute="echo 'Active theme: ' . (DB::table('settings')->where('item', 'active_theme')->value('value') ?: 'NOT SET');"

echo ""
echo "✅ Installation complete!"
echo ""
echo "Now visit: https://restaurant.minra.top/"
echo ""
