#!/bin/bash

# Lion POS - cPanel Post-Upload Setup Script
# Run this AFTER uploading files to cPanel via FTP
# Usage: bash setup-after-upload.sh

set -e

echo "🚀 Lion POS - cPanel Post-Upload Setup"
echo "======================================"
echo ""

# Step 1: Set Permissions
echo "1️⃣  Setting file permissions..."
chmod -R 755 .
chmod -R 777 storage/
chmod -R 777 bootstrap/cache/
echo "✅ Permissions set"

# Step 2: Create .env from cpanel config
echo ""
echo "2️⃣  Creating .env file..."
if [ -f ".env.cpanel" ]; then
    cp .env.cpanel .env
    echo "✅ .env file created"
else
    echo "⚠️  .env.cpanel not found, using .env.example"
    cp .env.example .env 2>/dev/null || echo "⚠️  No .env.example found"
fi

# Step 3: Install Dependencies
echo ""
echo "3️⃣  Installing PHP dependencies (this may take 2-3 minutes)..."
composer install --optimize-autoloader --no-dev
echo "✅ PHP dependencies installed"

# Step 4: Generate App Key
echo ""
echo "4️⃣  Generating application key..."
php artisan key:generate --force
echo "✅ Application key generated"

# Step 5: Run Migrations
echo ""
echo "5️⃣  Running database migrations..."
php artisan migrate --force
echo "✅ Database migrations completed"

# Step 6: Seed Database
echo ""
echo "6️⃣  Seeding database with demo data..."
php artisan db:seed
echo "✅ Database seeded"

# Step 7: Cache Configuration
echo ""
echo "7️⃣  Caching configuration..."
php artisan config:cache
php artisan route:cache
php artisan view:cache
echo "✅ Configuration cached"

# Step 8: Create Storage Symlink
echo ""
echo "8️⃣  Creating storage symlink..."
php artisan storage:link 2>/dev/null || true

# Step 9: Clear Cache
echo ""
echo "9️⃣  Final cache clearing..."
php artisan cache:clear
php artisan config:clear
php artisan cache:clear

# Final Summary
echo ""
echo "======================================"
echo "✨ Deployment Complete!"
echo "======================================"
echo ""
echo "📝 Your Application Details:"
echo "============================="
echo ""
echo "🌐 URL: https://lion.kivulakebreezeguesthouse.com"
echo "📊 Database: januspro_lion"
echo "👤 Database User: januspro_lionpro"
echo ""
echo "✅ Next Steps:"
echo "=============="
echo "1. Access your app at: https://lion.kivulakebreezeguesthouse.com"
echo "2. Test login with demo credentials (if seeded)"
echo "3. Monitor logs: tail -f storage/logs/laravel.log"
echo ""
echo "🔧 If you see errors:"
echo "====================="
echo "- Check storage/logs/laravel.log for details"
echo "- Ensure document root points to public/ folder"
echo "- Verify database credentials in .env"
echo "- Run: php artisan cache:clear"
echo ""
