#!/bin/bash

# 234Deals Deployment Script for Namecheap Shared Hosting
# Usage: ./deploy.sh

set -e # Exit on error

echo "🚀 Starting deployment for 234Deals..."
echo "=================================="

# 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_success() {
    echo -e "${GREEN}✓ $1${NC}"
}

print_error() {
    echo -e "${RED}✗ $1${NC}"
}

print_info() {
    echo -e "${YELLOW}→ $1${NC}"
}

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

# Step 1: Maintenance Mode
print_info "Enabling maintenance mode..."
php artisan down --render="errors::503" --secret="234deals-deploy" || true
print_success "Maintenance mode enabled"

# Step 2: Pull latest changes
print_info "Pulling latest changes from Git..."
if git pull origin main; then
    print_success "Git pull successful"
else
    print_error "Git pull failed"
    php artisan up
    exit 1
fi

# Step 3: Install/Update Composer dependencies
print_info "Installing Composer dependencies..."
if command -v composer &> /dev/null; then
    curl -sS https://getcomposer.org/installer | php
    php composer.phar install --no-dev --optimize-autoloader --no-interaction
    print_success "Composer dependencies installed"
else
    print_error "Composer not found, skipping..."
fi

# Step 4: Clear all caches
print_info "Clearing caches..."
php artisan optimize:clear
print_success "Caches cleared"

# Step 5: Cache configuration
print_info "Caching configuration..."
php artisan config:cache
php artisan route:cache
php artisan view:cache
print_success "Configuration cached"

# Step 6: Run migrations
# print_info "Running database migrations..."
# php artisan migrate --force
# print_success "Migrations completed"

# Step 7: Create storage link
print_info "Creating storage link..."
php artisan storage:link || print_info "Storage link already exists"
print_success "Storage link verified"

# Step 8: Set permissions
print_info "Setting correct permissions..."
chmod -R 755 storage bootstrap/cache 2>/dev/null || true
find storage -type f -exec chmod 664 {} \; 2>/dev/null || true
find bootstrap/cache -type f -exec chmod 664 {} \; 2>/dev/null || true
print_success "Permissions set"

# Step 9: Clear and cache again
print_info "Final cache clearing and caching..."
php artisan optimize:clear
php artisan config:cache
php artisan route:cache
php artisan view:cache
print_success "Final caching complete"

# Step 10: Optimize for production
print_info "Running production optimizations..."
php artisan optimize
print_success "Optimizations complete"

# Step 11: Disable maintenance mode
print_info "Disabling maintenance mode..."
php artisan up
print_success "Site is back online!"

echo "=================================="
echo -e "${GREEN}🎉 Deployment completed successfully!${NC}"
echo ""
echo "Access your site with bypass token during maintenance:"
echo "https://234deals.com/234deals-deploy"
echo ""
print_info "Don't forget to test your site!"
