#!/bin/bash
# Git workflow helper script

set -e

# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color

# Functions
print_header() {
    echo -e "${BLUE}$1${NC}"
}

print_success() {
    echo -e "${GREEN}✅ $1${NC}"
}

print_warning() {
    echo -e "${YELLOW}⚠️  $1${NC}"
}

print_error() {
    echo -e "${RED}❌ $1${NC}"
}

# Check if we're in a git repository
if [ ! -d ".git" ]; then
    print_error "Not in a git repository"
    exit 1
fi

# Main menu
echo "🔧 Git Workflow Helper"
echo "======================"
echo "1) Create feature branch"
echo "2) Commit changes"
echo "3) Push to GitHub"
echo "4) Create release"
echo "5) Clean up branches"
echo "6) Show status"
echo "0) Exit"
echo ""

read -p "Choose an option (0-6): " choice

case $choice in
    1)
        print_header "Creating feature branch"
        read -p "Enter feature name (e.g., helpbox-improvements): " feature_name
        
        if [ -z "$feature_name" ]; then
            print_error "Feature name cannot be empty"
            exit 1
        fi
        
        # Check if we're on main/develop
        current_branch=$(git branch --show-current)
        if [ "$current_branch" != "main" ] && [ "$current_branch" != "develop" ]; then
            print_warning "You're not on main or develop branch. Current: $current_branch"
            read -p "Continue anyway? (y/N): " continue_anyway
            if [ "$continue_anyway" != "y" ] && [ "$continue_anyway" != "Y" ]; then
                exit 0
            fi
        fi
        
        branch_name="feature/$feature_name"
        git checkout -b "$branch_name"
        print_success "Created and switched to branch: $branch_name"
        ;;
        
    2)
        print_header "Committing changes"
        
        # Check for changes
        if git diff --quiet && git diff --cached --quiet; then
            print_warning "No changes to commit"
            exit 0
        fi
        
        # Show status
        git status
        echo ""
        
        # Commit type
        echo "Select commit type:"
        echo "1) feat: New feature"
        echo "2) fix: Bug fix"
        echo "3) docs: Documentation"
        echo "4) style: Code style changes"
        echo "5) refactor: Code refactoring"
        echo "6) test: Add or update tests"
        echo "7) chore: Maintenance tasks"
        
        read -p "Choose type (1-7): " commit_type_choice
        
        case $commit_type_choice in
            1) commit_type="feat" ;;
            2) commit_type="fix" ;;
            3) commit_type="docs" ;;
            4) commit_type="style" ;;
            5) commit_type="refactor" ;;
            6) commit_type="test" ;;
            7) commit_type="chore" ;;
            *) print_error "Invalid choice"; exit 1 ;;
        esac
        
        read -p "Enter commit message: " commit_message
        
        if [ -z "$commit_message" ]; then
            print_error "Commit message cannot be empty"
            exit 1
        fi
        
        git add .
        git commit -m "$commit_type: $commit_message"
        print_success "Changes committed: $commit_type: $commit_message"
        ;;
        
    3)
        print_header "Pushing to GitHub"
        current_branch=$(git branch --show-current)
        
        # Check if upstream exists
        if ! git rev-parse --verify "origin/$current_branch" > /dev/null 2>&1; then
            print_warning "Upstream branch doesn't exist. Setting up..."
            git push -u origin "$current_branch"
        else
            git push
        fi
        
        print_success "Pushed to origin/$current_branch"
        
        # Suggest creating PR
        if [ "$current_branch" != "main" ] && [ "$current_branch" != "develop" ]; then
            print_warning "Consider creating a Pull Request on GitHub"
            echo "URL: https://github.com/yourusername/stiftung-starter/compare/$current_branch"
        fi
        ;;
        
    4)
        print_header "Creating release"
        
        # Check if on main branch
        current_branch=$(git branch --show-current)
        if [ "$current_branch" != "main" ]; then
            print_error "Must be on main branch to create release"
            exit 1
        fi
        
        # Get last tag
        last_tag=$(git describe --tags --abbrev=0 2>/dev/null || echo "v0.0.0")
        print_warning "Last tag: $last_tag"
        
        read -p "Enter new version (e.g., v2.1.0): " new_version
        
        if [ -z "$new_version" ]; then
            print_error "Version cannot be empty"
            exit 1
        fi
        
        # Create tag
        git tag -a "$new_version" -m "Release $new_version"
        git push origin "$new_version"
        
        print_success "Created and pushed tag: $new_version"
        ;;
        
    5)
        print_header "Cleaning up branches"
        
        # Show merged branches
        echo "Merged branches that can be deleted:"
        git branch --merged main | grep -v "main\|develop\|\*" | sed 's/^/  /'
        echo ""
        
        read -p "Delete merged branches? (y/N): " delete_merged
        if [ "$delete_merged" = "y" ] || [ "$delete_merged" = "Y" ]; then
            git branch --merged main | grep -v "main\|develop\|\*" | xargs -n 1 git branch -d
            print_success "Deleted merged branches"
        fi
        
        # Clean remote tracking branches
        git remote prune origin
        print_success "Cleaned remote tracking branches"
        ;;
        
    6)
        print_header "Repository Status"
        
        # Current branch
        current_branch=$(git branch --show-current)
        echo "📍 Current branch: $current_branch"
        
        # Uncommitted changes
        if ! git diff --quiet || ! git diff --cached --quiet; then
            print_warning "You have uncommitted changes"
        else
            print_success "Working directory is clean"
        fi
        
        # Unpushed commits
        if [ -n "$(git log origin/$current_branch..$current_branch 2>/dev/null)" ]; then
            print_warning "You have unpushed commits"
        else
            print_success "All commits are pushed"
        fi
        
        # Show recent commits
        echo ""
        echo "📝 Recent commits:"
        git log --oneline -5
        
        # Show branches
        echo ""
        echo "🌿 Branches:"
        git branch -v
        ;;
        
    0)
        print_success "Goodbye!"
        exit 0
        ;;
        
    *)
        print_error "Invalid option"
        exit 1
        ;;
esac
