#!/bin/bash

# RestoPOS Deployment Script
# This script can be used for manual deployment or as reference

set -e

echo "🚀 Starting RestoPOS Deployment..."

# Configuration
PROJECT_NAME="RestoPOS"
TIMESTAMP=$(date +"%Y%m%d_%H%M%S")
BACKUP_DIR="backup_$TIMESTAMP"

# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
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"
}

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

print_status "Checking PHP version..."
php -v | head -n 1

print_status "Checking Node.js version..."
node -v

print_status "Installing PHP dependencies..."
composer install --no-dev --optimize-autoloader --no-interaction

print_status "Installing Node.js dependencies..."
npm ci

print_status "Building assets..."
npm run build

print_status "Creating deployment package..."
mkdir -p deploy

# Copy files excluding development files
rsync -av --progress . deploy/ \
  --exclude='.git' \
  --exclude='node_modules' \
  --exclude='.env' \
  --exclude='.env.*' \
  --exclude='tests' \
  --exclude='storage/logs/*' \
  --exclude='storage/framework/cache/*' \
  --exclude='storage/framework/sessions/*' \
  --exclude='storage/framework/views/*' \
  --exclude='.github' \
  --exclude='docker' \
  --exclude='docker-compose.yml' \
  --exclude='Dockerfile' \
  --exclude='*.md' \
  --exclude='phpunit.xml' \
  --exclude='deploy.sh'

# Create necessary directories
print_status "Creating necessary directories..."
mkdir -p deploy/storage/logs
mkdir -p deploy/storage/framework/cache
mkdir -p deploy/storage/framework/sessions  
mkdir -p deploy/storage/framework/views
mkdir -p deploy/bootstrap/cache

# Set proper permissions
chmod -R 755 deploy/storage
chmod -R 755 deploy/bootstrap/cache

# Create .htaccess for Laravel if not exists
if [ ! -f deploy/public/.htaccess ]; then
    print_status "Creating .htaccess file..."
    cat > deploy/public/.htaccess << 'EOF'
<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews -Indexes
    </IfModule>

    RewriteEngine On

    # Handle Authorization Header
    RewriteCond %{HTTP:Authorization} .
    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

    # Redirect Trailing Slashes If Not A Folder...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} (.+)/$
    RewriteRule ^ %1 [L,R=301]

    # Send Requests To Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]
</IfModule>
EOF
fi

print_status "Creating deployment archive..."
cd deploy
tar -czf "../${PROJECT_NAME}_${TIMESTAMP}.tar.gz" .
cd ..

print_status "Deployment package created: ${PROJECT_NAME}_${TIMESTAMP}.tar.gz"
print_status "Deploy directory created: ./deploy/"

print_warning "Next steps:"
echo "1. Upload the deploy/ directory contents to your cPanel public_html or subdirectory"
echo "2. Create .env file on server with production settings"
echo "3. Run the following commands on your server:"
echo "   - php artisan migrate --force"
echo "   - php artisan config:cache"
echo "   - php artisan route:cache"
echo "   - php artisan view:cache"
echo "   - chmod -R 755 storage bootstrap/cache"

print_status "✅ Deployment package ready!"
