#!/bin/bash

# Check if there are any changed files
if ! git diff --quiet || ! git diff --cached --quiet; then
    echo "No changed files detected"
    exit 0
fi

echo "Changed files detected"

# Create a unique branch name with timestamp
BRANCH_NAME="update-lockfile-$(date +%s)"

# Create and checkout new branch
git checkout -b "$BRANCH_NAME"

# Stage all changes
git add .

# Commit changes
git commit -m "Update lockfile"

# Push branch to remote
git push origin "$BRANCH_NAME"

curl -X POST \
    -H "Authorization: token $GITHUB_TOKEN" \
    -H "Accept: application/vnd.github.v3+json" \
    "https://api.github.com/repos/theorchard/$REPO_NAME/pulls" \
    -d "{\"title\":\"Update lockfile\",\"head\":\"$BRANCH_NAME\",\"base\":\"master\",\"body\":\"Automated lockfile update\"}"
