#!/bin/bash
# Quick Task Master commands for Trax project

set -e

# Color codes
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
RED='\033[0;31m'
CYAN='\033[0;36m'
NC='\033[0m'

# Navigate to project root
cd "$(dirname "$0")/.."
PROJECT_ROOT=$(pwd)

# Set Task Master project root
export TM_PROJECT_ROOT="$PROJECT_ROOT"

# Command shortcuts
CMD=${1:-help}
shift || true

case "$CMD" in
    next|n)
        echo -e "${CYAN}🎯 Getting next Trax task...${NC}"
        task-master next
        ;;
    
    list|l)
        echo -e "${BLUE}📋 Listing Trax tasks...${NC}"
        task-master list "$@"
        ;;
    
    show|s)
        if [ -z "$1" ]; then
            echo -e "${RED}❌ Task ID required${NC}"
            echo "Usage: $0 show <task-id>"
            exit 1
        fi
        echo -e "${BLUE}📄 Showing task $1...${NC}"
        task-master show "$1"
        ;;
    
    done|d)
        if [ -z "$1" ]; then
            echo -e "${RED}❌ Task ID required${NC}"
            echo "Usage: $0 done <task-id>"
            exit 1
        fi
        echo -e "${GREEN}✅ Marking task $1 as done...${NC}"
        task-master set-status --id="$1" --status=done
        echo -e "${CYAN}🎯 Next task:${NC}"
        task-master next
        ;;
    
    progress|p)
        if [ -z "$1" ]; then
            echo -e "${RED}❌ Task ID required${NC}"
            echo "Usage: $0 progress <task-id>"
            exit 1
        fi
        echo -e "${YELLOW}🚧 Marking task $1 as in-progress...${NC}"
        task-master set-status --id="$1" --status=in-progress
        ;;
    
    pending)
        echo -e "${YELLOW}📋 Pending Trax tasks...${NC}"
        task-master list --status=pending
        ;;
    
    search)
        if [ -z "$1" ]; then
            echo -e "${RED}❌ Search term required${NC}"
            echo "Usage: $0 search <term>"
            exit 1
        fi
        echo -e "${BLUE}🔍 Searching for '$1'...${NC}"
        python3 scripts/tm_trax.py --search "$1"
        ;;
    
    stats)
        echo -e "${CYAN}📊 Trax Task Statistics${NC}"
        python3 scripts/tm_trax.py --stats
        ;;
    
    cache)
        echo -e "${CYAN}⚡ Using cached Task Master data...${NC}"
        python3 scripts/tm_trax.py "$@"
        ;;
    
    help|h|*)
        echo -e "${CYAN}🚀 Trax Task Master Quick Commands${NC}"
        echo ""
        echo "Usage: $0 [command] [args]"
        echo ""
        echo "Commands:"
        echo "  next, n         - Get next available task"
        echo "  list, l         - List all tasks"
        echo "  show, s <id>    - Show task details"
        echo "  done, d <id>    - Mark task as done and show next"
        echo "  progress, p <id>- Mark task as in-progress"
        echo "  pending         - Show pending tasks"
        echo "  search <term>   - Search tasks (uses cache)"
        echo "  stats           - Show task statistics (uses cache)"
        echo "  cache [args]    - Direct cache access"
        echo "  help, h         - Show this help"
        echo ""
        echo "Examples:"
        echo "  $0 next"
        echo "  $0 done 92.1"
        echo "  $0 search whisper"
        echo "  $0 cache --next"
        ;;
esac