#!/bin/bash
# TinyTorch Pre-Push Hook
# Prevents accidental pushes to the main TinyTorch repository

UPSTREAM_REPO="github.com/harvard-edge/cs249r_book"
PROTECTED_BRANCHES="main dev master"

# Check if we're pushing to the upstream repo
remote_url=$(git remote get-url --push "$1" 2>/dev/null)

if echo "$remote_url" | grep -q "$UPSTREAM_REPO"; then
    # Check if we're pushing to a protected branch
    while read local_ref local_sha remote_ref remote_sha; do
        branch_name=$(echo "$remote_ref" | sed 's/refs\/heads\///')

        for protected in $PROTECTED_BRANCHES; do
            if [ "$branch_name" = "$protected" ]; then
                echo "❌ ERROR: Pushing to $branch_name on $UPSTREAM_REPO is not allowed!"
                echo ""
                echo "This is the upstream TinyTorch repository, not your personal fork."
                echo ""
                echo "If you're a student:"
                echo "  1. Create your own fork: https://github.com/harvard-edge/cs249r_book/fork"
                echo "  2. Add your fork as a remote: git remote add my-fork <your-fork-url>"
                echo "  3. Push to your fork: git push my-fork $branch_name"
                echo ""
                echo "If you're a contributor:"
                echo "  1. Create a feature branch: git checkout -b feature/my-feature"
                echo "  2. Push your branch: git push origin feature/my-feature"
                echo "  3. Open a Pull Request on GitHub"
                echo ""
                exit 1
            fi
        done
    done

    echo "✅ Pushing to feature branch is allowed"
fi

exit 0
