REPO_BASE="/var/www/vhosts/adp.dev.portalpeople.co.uk/dev/NGauge_modules"
REPOS=("ngauge-cart" "ngauge-caterpoint" "ngauge-datadash" "ngauge-indicator" "ngauge-zupa")
VERSIONS=("v1.0.3" "v1.4.7" "v1.2.4" "v1.0.7" "v1.0.6")
DESCS=("fix cart routes and payload handling" "improve caterpoint cart import mapping" "document multi-connection workflow" "add gs1 parser documentation" "add inbox import browser")
WEBHOOK_SCRIPT="/var/www/vhosts/adp.dev.portalpeople.co.uk/dev/NGauge_modules/statis-webhook/trigger-webhook.php"

for i in "${!REPOS[@]}"; do
    REPO="${REPOS[$i]}"
    VERSION="${VERSIONS[$i]}"
    DESC="${DESCS[$i]}"
    REPO_PATH="$REPO_BASE/$REPO"
    
    echo "Processing $REPO..."
    if [ ! -d "$REPO_PATH" ]; then
        echo "Error: Directory $REPO_PATH does not exist."
        continue
    fi
    
    cd "$REPO_PATH" || continue
    
    git status --short
    git add -A
    
    if ! git commit -m "$VERSION - $DESC"; then
        if [ -z "$(git status --short)" ]; then
             echo "Nothing to commit for $REPO."
        else
             echo "Commit failed for $REPO."
             continue
        fi
    fi
    COMMIT_HASH=$(git rev-parse HEAD)
    
    if ! git tag "$VERSION"; then
        echo "Warning: Tag $VERSION already exists or failed for $REPO."
    fi
    
    if ! git push; then
        echo "Error: git push failed for $REPO."
        continue
    fi
    
    if ! git push origin "$VERSION"; then
        echo "Error: git push tag failed for $REPO."
        continue
    fi
    
    echo "Triggering publish for $REPO..."
    php "$WEBHOOK_SCRIPT" --path="$REPO_PATH"
    
    echo "RESULT: $REPO | Hash: $COMMIT_HASH | Status: Success"
done
