#!/bin/bash

# Enable strict error handling
set -euo pipefail

# Define variables
PROJECT_DIR="/var/www/starkehardware.com"
BRANCH="${1:-main}" # Default branch is 'develop' if not passed as an argument
USER="www-data"
GROUP="www-data"

echo "$(date +'%Y-%m-%d %H:%M:%S') - Starting deployment for branch '$BRANCH'..."

# Ensure the project directory exists
if [ ! -d "$PROJECT_DIR" ]; then
  echo "Error: Project directory '$PROJECT_DIR' does not exist!"
  exit 1
fi

# Navigate to the project directory
cd "$PROJECT_DIR"

# Step 1: Fix ownership and permissions for .git folder
echo "$(date +'%Y-%m-%d %H:%M:%S') - Setting permissions for .git directory..."
sudo chown -R "$(whoami)":"$(whoami)" .git
sudo chmod -R 775 .git
git config --global --add safe.directory "$PROJECT_DIR"

# Step 2: Stash local changes, reset, and pull the latest code
echo "$(date +'%Y-%m-%d %H:%M:%S') - Pulling latest code from branch '$BRANCH'..."
git reset --hard HEAD
git checkout "$BRANCH"
git pull origin "$BRANCH"

# Step 3: Install PHP dependencies
if [ -f "composer.json" ]; then
  echo "$(date +'%Y-%m-%d %H:%M:%S') - Installing PHP dependencies..."
  composer install --no-ansi --no-interaction --no-scripts --no-progress --prefer-dist --ignore-platform-reqs
else
  echo "$(date +'%Y-%m-%d %H:%M:%S') - Skipping Composer install, no composer.json found."
fi

# Step 4: Run Laravel migrations
if [ -f "artisan" ]; then
  echo "$(date +'%Y-%m-%d %H:%M:%S') - Running database migrations..."
  php artisan migrate --force
else
  echo "$(date +'%Y-%m-%d %H:%M:%S') - Skipping migrations, no artisan file found."
fi

# Step 5: Install Node.js dependencies and build assets
if [ -f "package.json" ]; then
  echo "$(date +'%Y-%m-%d %H:%M:%S') - Preparing to build frontend assets..."
  
  # Set correct ownership for Node.js dependencies folder
  echo "$(date +'%Y-%m-%d %H:%M:%S') - Setting permissions for node_modules directory..."
  sudo chown -R "$(whoami)":"$(whoami)" "$PROJECT_DIR/node_modules" || true
  sudo chmod -R 775 "$PROJECT_DIR/node_modules" || true
  
  echo "$(date +'%Y-%m-%d %H:%M:%S') - Installing Node.js dependencies..."
  npm install --legacy-peer-deps

  echo "$(date +'%Y-%m-%d %H:%M:%S') - Building frontend assets..."
  npm run production
else
  echo "$(date +'%Y-%m-%d %H:%M:%S') - Skipping frontend build, no package.json found."
fi

# Step 6: Clear and cache Laravel configurations
if [ -f "artisan" ]; then
  echo "$(date +'%Y-%m-%d %H:%M:%S') - Clearing and caching Laravel configurations..."
  php artisan optimize:clear
  php artisan config:cache
  php artisan route:cache
  php artisan view:cache
else
  echo "$(date +'%Y-%m-%d %H:%M:%S') - Skipping Laravel optimization, no artisan file found."
fi

# Step 7: Set proper permissions
echo "$(date +'%Y-%m-%d %H:%M:%S') - Setting file permissions..."
sudo chown -R "$USER":"$GROUP" "$PROJECT_DIR"
sudo chown -R "$USER":"$GROUP" "$PROJECT_DIR/storage" "$PROJECT_DIR/bootstrap/cache"
sudo chmod -R 755 "$PROJECT_DIR"
sudo chmod -R 777 "$PROJECT_DIR/storage" "$PROJECT_DIR/bootstrap/cache"
sudo chmod -R 777 "$PROJECT_DIR/storage/logs"
sudo chmod -R 777 "$PROJECT_DIR/storage/framework/views"
sudo chmod -R 777 "$PROJECT_DIR/storage/framework/sessions"

echo "$(date +'%Y-%m-%d %H:%M:%S') - Deployment completed successfully for branch '$BRANCH'."
