#!/bin/bash

# Git username and PAT
GIT_USERNAME="dlarejsalvatore"
GIT_PAT="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"

# Base directory for all elite-* folders
BASE_DIR="/var/www/html"

# Logs
LOG_DIR="/var/www/html/scripts/logs_git_pull"
SUMMARY_LOG="${LOG_DIR}/summary.log"

# Create log directory if it doesn't exist
sudo mkdir -p "$LOG_DIR"
sudo chown "$USER:$USER" "$LOG_DIR"

# Clean previous summary
echo "Git Pull Summary - $(date)" > "$SUMMARY_LOG"
echo "=====================================" >> "$SUMMARY_LOG"

# Loop through all elite-* repos
for dir in "$BASE_DIR"/elite-*; do
  REPO_NAME=$(basename "$dir")

  # Skip specific folders
  if [[ "$REPO_NAME" == "elite-agatha" || "$REPO_NAME" == "elite-tabang" || "$REPO_NAME" == "elite-malis" ]]; then
    echo "Skipping $REPO_NAME"
    continue
  fi

  if [ -d "$dir/.git" ]; then
    echo "Processing git pull: $dir"

    # Get project name for log naming
    REPO_NAME=$(basename "$dir")
    STATUS_LOG="${LOG_DIR}/${REPO_NAME}_status.log"

     # Get and clean current remote URL
    sudo git -C "$dir" remote set-url origin "https://dlarejsalvatore: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@github.com/makiyow/elite-system.git"

    # Git reset --hard
    sudo git -C "$dir" reset --hard >> "$STATUS_LOG" 2>&1

    # Run Laravel migration and log result
    echo "Running migration..." >> "$STATUS_LOG"

    if sudo php "$dir/artisan" migrate --force >> "$STATUS_LOG" 2>&1; then
    	MIGRATION_STATUS="SUCCESS"
    else
    	MIGRATION_STATUS="FAILED"
    fi

    echo "Migration Status: $MIGRATION_STATUS" >> "$STATUS_LOG"

    # Append migration result to summary
    echo "  Migration: $MIGRATION_STATUS" >> "$SUMMARY_LOG"

    # Pull latest changes
    echo "Pulling latest changes..." >> "$STATUS_LOG"
    sudo git -C "$dir" pull >> "$STATUS_LOG" 2>&1

    # php artisan migrate
    # echo "Running migration..." >> "$STATUS_LOG"
    # sudo php "$dir/artisan" migrate --force >> "$STATUS_LOG" 2>&1

    # Run Laravel migration and log result
    echo "Running migration..." >> "$STATUS_LOG"

    if sudo php "$dir/artisan" migrate --force >> "$STATUS_LOG" 2>&1; then
    	MIGRATION_STATUS="SUCCESS"
    	else
    	MIGRATION_STATUS="FAILED"
    fi

    echo "Migration Status: $MIGRATION_STATUS" >> "$STATUS_LOG"

    # Append migration result to summary
    echo "  Migration: $MIGRATION_STATUS" >> "$SUMMARY_LOG"

    # php artisan optimize
    # echo "Running optimize..." >> "$STATUS_LOG"
    # echo sudo -S php "$dir/artisan" optimize >> "$STATUS_LOG" 2>&1

    # Git status
    echo "" >> "$STATUS_LOG"
    echo "Current Status:" >> "$STATUS_LOG"
    sudo git -C "$dir" status >> "$STATUS_LOG" 2>&1

    # Append summary
    CURRENT_BRANCH=$(sudo git -C "$dir" rev-parse --abbrev-ref HEAD)
    LATEST_COMMIT=$(sudo git -C "$dir" log -1 --pretty=format:"%h - %s (%cd)" --date=short)

    echo "[$REPO_NAME] on branch '$CURRENT_BRANCH'" >> "$SUMMARY_LOG"
    echo "  Last commit: $LATEST_COMMIT" >> "$SUMMARY_LOG"
    echo "" >> "$SUMMARY_LOG"
  else
    echo "Skipping $dir — not a Git repository"
  fi
done

echo "Logs saved in $LOG_DIR"
echo "Summary: $SUMMARY_LOG"
