#!/bin/bash

# RestoPOS Post-Deployment Script
# Run this script on your cPanel server after FTP deployment

set -e

echo "🚀 Starting RestoPOS Post-Deployment Setup..."

# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color

# Function to print colored output
print_status() {
    echo -e "${GREEN}[INFO]${NC} $1"
}

print_warning() {
    echo -e "${YELLOW}[WARNING]${NC} $1"
}

print_error() {
    echo -e "${RED}[ERROR]${NC} $1"
}

print_step() {
    echo -e "${BLUE}[STEP]${NC} $1"
}

# Check if we're in the right directory
if [ ! -f "artisan" ]; then
    print_error "Laravel artisan file not found. Please run this script from your application directory."
    exit 1
fi

print_status "RestoPOS Post-Deployment Setup Started"
echo "=========================================="

# Step 1: Check .env file
print_step "1. Checking environment configuration..."
if [ ! -f ".env" ]; then
    print_warning ".env file not found!"
    echo "Please create .env file from env.production.template"
    echo "cp env.production.template .env"
    echo "Then edit .env with your database credentials"
    exit 1
fi
print_status ".env file found ✓"

# Step 2: Test database connection
print_step "2. Testing database connection..."
if php artisan migrate:status > /dev/null 2>&1; then
    print_status "Database connection successful ✓"
else
    print_error "Database connection failed!"
    echo "Please check your database configuration in .env file"
    echo "Refer to DATABASE_SETUP.md for setup instructions"
    exit 1
fi

# Step 3: Clear caches
print_step "3. Clearing application caches..."
php artisan config:clear
php artisan cache:clear
php artisan route:clear
php artisan view:clear
print_status "Caches cleared ✓"

# Step 4: Run migrations
print_step "4. Running database migrations..."
php artisan migrate --force
print_status "Migrations completed ✓"

# Step 5: Seed database (optional)
print_step "5. Database seeding (optional)..."
read -p "Do you want to seed the database? (y/N): " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
    php artisan db:seed --force
    print_status "Database seeded ✓"
else
    print_warning "Database seeding skipped"
fi

# Step 6: Optimize application
print_step "6. Optimizing application..."
php artisan config:cache
php artisan route:cache
php artisan view:cache
print_status "Application optimized ✓"

# Step 7: Set permissions
print_step "7. Setting file permissions..."
chmod -R 755 storage
chmod -R 755 bootstrap/cache

# Check if we can write to storage
if [ -w "storage" ]; then
    print_status "Storage permissions set correctly ✓"
else
    print_warning "Storage directory may not be writable"
    echo "You may need to run: chmod -R 755 storage"
fi

# Step 8: Final checks
print_step "8. Running final checks..."

# Check if key is generated
if grep -q "APP_KEY=base64:" .env; then
    print_status "Application key is set ✓"
else
    print_warning "Generating application key..."
    php artisan key:generate --force
    print_status "Application key generated ✓"
fi

# Test basic artisan commands
if php artisan --version > /dev/null 2>&1; then
    print_status "Laravel is working correctly ✓"
else
    print_error "Laravel setup has issues"
fi

echo ""
echo "=========================================="
print_status "🎉 RestoPOS Post-Deployment Setup Complete!"
echo ""
print_status "Your RestoPOS application is now ready to use!"
echo ""
print_warning "Next steps:"
echo "1. Access your application in the browser"
echo "2. Test login functionality"
echo "3. Verify all features are working"
echo ""
print_warning "If you encounter any issues:"
echo "1. Check storage/logs/laravel.log for errors"
echo "2. Verify database connection"
echo "3. Check file permissions"
echo ""
echo "🚀 Happy using RestoPOS!"
