#!/bin/bash
# pre-commit hook - Block commits on protected branches
# Works in both main repo and worktrees.
# Install: ./scripts/install-hooks.sh

RED='\033[0;31m'
YELLOW='\033[1;33m'
NC='\033[0m'

CURRENT_BRANCH=$(git rev-parse --abbrev-ref HEAD)

# Block commits on protected branches
PROTECTED_BRANCHES="dev main master"

for branch in $PROTECTED_BRANCHES; do
    if [[ "$CURRENT_BRANCH" == "$branch" ]]; then
        echo -e "${RED}=================================================${NC}"
        echo -e "${RED}ERROR: Direct commits to '$branch' are not allowed${NC}"
        echo -e "${RED}=================================================${NC}"
        echo ""
        echo "Create an isolated worktree for your changes:"
        echo "  ./scripts/start-feature.sh feature <description>"
        echo ""
        echo "Or manually:"
        echo "  git worktree add .worktrees/<desc> -b feature/<desc> origin/dev"
        echo ""
        exit 1
    fi
done

# Warn if branch doesn't follow naming convention
if ! echo "$CURRENT_BRANCH" | grep -qE '^(feature|bugfix|hotfix|chore)/'; then
    echo -e "${YELLOW}WARNING: Branch '$CURRENT_BRANCH' doesn't follow naming convention${NC}"
    echo -e "${YELLOW}Expected: feature|bugfix|hotfix|chore/<description>${NC}"
    echo ""
fi

exit 0
