#!/bin/bash

# GremlinModes Stash Script
# This script toggles GremlinModes files in/out of .gitignore
# allowing for clean commits without framework files

# Text formatting
BOLD='\033[1m'
GREMLIN='\033[1;38;2;31;204;0m'
GREEN='\033[0;32m'
BLUE='\033[0;34m'
RED='\033[0;31m'
YELLOW='\033[0;33m'
NC='\033[0m' # No Color

# Banner
echo -e "${BOLD}${GREMLIN}"
echo "   ▄▄ • ▄▄▄  ▄▄▄ .• ▌ ▄ ·. ▄▄▌  ▪   ▐ ▄ • ▌ ▄ ·.       ·▄▄▄▄  ▄▄▄ ..▄▄ · "
echo "  ▐█ ▀ ▪▀▄ █·▀▄.▀··██ ▐███▪██•  ██ •█▌▐█·██ ▐███▪▪     ██▪ ██ ▀▄.▀·▐█ ▀. "
echo "  ▄█ ▀█▄▐▀▀▄ ▐▀▀▪▄▐█ ▌▐▌▐█·██▪  ▐█·▐█▐▐▌▐█ ▌▐▌▐█· ▄█▀▄ ▐█· ▐█▌▐▀▀▪▄▄▀▀▀█▄"
echo "  ▐█▄▪▐█▐█•█▌▐█▄▄▌██ ██▌▐█▌▐█▌▐▌▐█▌██▐█▌██ ██▌▐█▌▐█▌.▐▌██. ██ ▐█▄▄▌▐█▄▪▐█"
echo "  ·▀▀▀▀ .▀  ▀ ▀▀▀ ▀▀  █▪▀▀▀.▀▀▀ ▀▀▀▀▀ █▪▀▀  █▪▀▀▀ ▀█▄▀▪▀▀▀▀▀•  ▀▀▀  ▀▀▀▀ "
echo -e "${NC}"
echo -e "${BOLD}GremlinModes Git Stash${NC}"
echo ""

# Check if we're in a git repository
if ! git rev-parse --git-dir > /dev/null 2>&1; then
    echo -e "${RED}Error: Not in a git repository!${NC}"
    echo -e "${YELLOW}Please run this script from within a git repository.${NC}"
    exit 1
fi

# Define the GremlinModes entries for .gitignore
GREMLIN_ENTRIES=(
    "# GremlinModes Framework Files"
    ".gremlinmodes/"
    ".roomodes"
    "gremlinmodes/"
    "agent.sh"
    "stash.sh"
)

# Create .gitignore if it doesn't exist
if [ ! -f ".gitignore" ]; then
    echo -e "${BLUE}Creating .gitignore file...${NC}"
    touch .gitignore
fi

# Function to check if GremlinModes entries are in .gitignore
is_stashed() {
    # Check if the header comment exists in .gitignore
    if grep -q "# GremlinModes Framework Files" .gitignore 2>/dev/null; then
        return 0  # true - is stashed
    else
        return 1  # false - is not stashed
    fi
}

# Function to add entries to .gitignore
stash_files() {
    echo -e "${BLUE}Stashing GremlinModes files...${NC}"
    
    # Add a newline if .gitignore doesn't end with one
    if [ -s ".gitignore" ] && [ "$(tail -c 1 .gitignore | wc -l)" -eq 0 ]; then
        echo "" >> .gitignore
    fi
    
    # Add all GremlinModes entries
    for entry in "${GREMLIN_ENTRIES[@]}"; do
        echo "$entry" >> .gitignore
    done
    
    echo -e "${GREEN}✓ GremlinModes files have been stashed (added to .gitignore)${NC}"
    echo ""
    echo -e "${BLUE}You can now safely:${NC}"
    echo "  1. Add your changes: git add ."
    echo "  2. Commit your changes: git commit -m \"your message\""
    echo "  3. Push to remote: git push"
    echo ""
    echo -e "${YELLOW}After pushing, run './stash.sh' again to unstash the files.${NC}"
}

# Function to remove entries from .gitignore
unstash_files() {
    echo -e "${BLUE}Unstashing GremlinModes files...${NC}"
    
    # Create a temporary file
    temp_file=$(mktemp)
    
    # Flag to track if we're in a GremlinModes block
    in_gremlin_block=false
    
    # Read .gitignore line by line
    while IFS= read -r line || [ -n "$line" ]; do
        # Check if this is the start of GremlinModes block
        if [[ "$line" == "# GremlinModes Framework Files" ]]; then
            in_gremlin_block=true
            continue
        fi
        
        # If we're in the GremlinModes block
        if [ "$in_gremlin_block" = true ]; then
            # Check if this line is a GremlinModes entry
            is_gremlin_entry=false
            for entry in "${GREMLIN_ENTRIES[@]:1}"; do  # Skip the comment header
                if [[ "$line" == "$entry" ]]; then
                    is_gremlin_entry=true
                    break
                fi
            done
            
            # If it's not a GremlinModes entry, we've exited the block
            if [ "$is_gremlin_entry" = false ] && [ -n "$line" ]; then
                in_gremlin_block=false
                echo "$line" >> "$temp_file"
            fi
            # If it is a GremlinModes entry, skip it (don't write to temp file)
        else
            # Not in GremlinModes block, keep the line
            echo "$line" >> "$temp_file"
        fi
    done < .gitignore
    
    # Replace .gitignore with the cleaned version
    mv "$temp_file" .gitignore
    
    # Remove .gitignore if it's now empty
    if [ ! -s ".gitignore" ]; then
        rm .gitignore
        echo -e "${BLUE}Removed empty .gitignore file${NC}"
    fi
    
    echo -e "${GREEN}✓ GremlinModes files have been unstashed (removed from .gitignore)${NC}"
    echo ""
    echo -e "${BLUE}GremlinModes files are now visible to git and coding agents.${NC}"
}

# Main logic
if is_stashed; then
    # Files are currently stashed, so unstash them
    echo -e "${YELLOW}GremlinModes files are currently stashed.${NC}"
    echo -e "${BLUE}Do you want to unstash them? (y/n)${NC}"
    read -p "> " response
    
    if [[ "$response" =~ ^[Yy]$ ]]; then
        unstash_files
    else
        echo -e "${YELLOW}Keeping files stashed.${NC}"
    fi
else
    # Files are not stashed, so stash them
    echo -e "${YELLOW}GremlinModes files are currently visible to git.${NC}"
    echo -e "${BLUE}Do you want to stash them? (y/n)${NC}"
    read -p "> " response
    
    if [[ "$response" =~ ^[Yy]$ ]]; then
        stash_files
    else
        echo -e "${YELLOW}Keeping files visible.${NC}"
    fi
fi

echo ""
echo -e "${GREMLIN}Made with 💚 mischief by gremlinlabs${NC}"