#!/bin/bash

# Get the current branch name
CURRENT_BRANCH=$(git branch --show-current)

# Get the default branch (usually main or master)
DEFAULT_BRANCH=$(git remote show origin | grep 'HEAD branch' | cut -d' ' -f5)

# Push the current branch to origin
echo "Pushing $CURRENT_BRANCH to origin..."
git push -u origin "$CURRENT_BRANCH"

# Check if push was successful
if [ $? -ne 0 ]; then
    echo "Error: Failed to push branch"
    exit 1
fi

# Get the last commit's first line (title)
PR_TITLE=$(git log -1 --pretty=format:%s)

# Get the last commit's body (everything after the first line)
PR_BODY=$(git log -1 --pretty=format:%b)

# Create PR using the commit message
echo "Creating PR..."
echo "Title: $PR_TITLE"
echo "Body: $PR_BODY"

gh pr create \
  --base "$DEFAULT_BRANCH" \
  --head "$CURRENT_BRANCH" \
  --title "$PR_TITLE" \
  --body "$PR_BODY"
