#!/bin/bash
# Simplified Taskmaster Workflow Script
# Uses CLI directly for fast access without cache complexity

set -e

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

# Function to print header
print_header() {
    echo ""
    echo -e "${CYAN}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
    echo -e "${CYAN}$1${NC}"
    echo -e "${CYAN}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
    echo ""
}

# Function to show help
show_help() {
    echo -e "${CYAN}Taskmaster Workflow Commands:${NC}"
    echo ""
    echo -e "${GREEN}Task Management:${NC}"
    echo "  $0 start <task-id>     - Start working on a task"
    echo "  $0 update <task-id> <message> - Update task progress"
    echo "  $0 complete <task-id>  - Mark task as complete"
    echo ""
    echo -e "${GREEN}Task Information:${NC}"
    echo "  $0 show <task-id>      - Show task details"
    echo "  $0 list               - List all tasks"
    echo "  $0 next               - Show next task to work on"
    echo ""
    echo -e "${GREEN}Tag Management:${NC}"
    echo "  $0 tags               - List available tags"
    echo "  $0 switch <tag>       - Switch to different tag"
    echo ""
    echo -e "${GREEN}Examples:${NC}"
    echo "  $0 start 15"
    echo "  $0 update 15 'Implemented core functionality'"
    echo "  $0 complete 15"
    echo "  $0 show 15"
}

# Function to start a task
start_task() {
    local task_id="$1"
    
    if [ -z "$task_id" ]; then
        echo -e "${RED}❌ Task ID required${NC}"
        echo "Usage: $0 start <task-id>"
        exit 1
    fi
    
    print_header "🚀 STARTING TASK: $task_id"
    
    # Show task details first
    echo -e "${BLUE}📄 Task Details:${NC}"
    task-master show "$task_id" 2>/dev/null || {
        echo -e "${RED}Task $task_id not found${NC}"
        exit 1
    }
    
    echo ""
    echo -e "${YELLOW}🚧 Setting status to in-progress...${NC}"
    task-master set-status --id="$task_id" --status=in-progress
    
    echo -e "${GREEN}✅ Task $task_id is now in progress!${NC}"
    echo ""
    echo -e "${CYAN}💡 Next steps:${NC}"
    echo "  - Work on the task implementation"
    echo "  - Use '$0 update <task-id> <message>' to log progress"
    echo "  - Use '$0 complete <task-id>' when complete"
}

# Function to update task progress
update_task() {
    local task_id="$1"
    local message="$2"
    
    if [ -z "$task_id" ]; then
        echo -e "${RED}❌ Task ID required${NC}"
        echo "Usage: $0 update <task-id> <message>"
        exit 1
    fi
    
    if [ -z "$message" ]; then
        echo -e "${RED}❌ Update message required${NC}"
        echo "Usage: $0 update <task-id> <message>"
        exit 1
    fi
    
    print_header "📝 UPDATING TASK: $task_id"
    
    echo -e "${BLUE}📝 Adding progress note: '$message'${NC}"
    task-master update-subtask --id="$task_id" --prompt="$message" 2>/dev/null || {
        echo -e "${YELLOW}⚠️  Could not update subtask, trying task update...${NC}"
        task-master update-task --id="$task_id" --prompt="$message" --append 2>/dev/null || {
            echo -e "${RED}❌ Failed to update task${NC}"
            exit 1
        }
    }
    
    echo -e "${GREEN}✅ Task updated successfully!${NC}"
}

# Function to complete a task
complete_task() {
    local task_id="$1"
    if [ -z "$task_id" ]; then
        echo -e "${RED}❌ Task ID required${NC}"
        echo "Usage: $0 complete <task-id>"
        exit 1
    fi
    
    print_header "✅ COMPLETING TASK: $task_id"
    
    # Show task details
    echo -e "${BLUE}📄 Final task review:${NC}"
    task-master show "$task_id" 2>/dev/null || {
        echo -e "${RED}Task $task_id not found${NC}"
        exit 1
    }
    
    echo ""
    echo -e "${GREEN}✅ Marking task as done...${NC}"
    task-master set-status --id="$task_id" --status=done
    
    echo -e "${GREEN}✅ Task $task_id completed successfully!${NC}"
}

# Function to show task details
show_task() {
    local task_id="$1"
    if [ -z "$task_id" ]; then
        echo -e "${RED}❌ Task ID required${NC}"
        echo "Usage: $0 show <task-id>"
        exit 1
    fi
    
    task-master show "$task_id"
}

# Function to list tasks
list_tasks() {
    task-master list
}

# Function to show next task
next_task() {
    task-master next
}

# Function to list tags
list_tags() {
    task-master tags
}

# Function to switch tags
switch_tag() {
    local tag="$1"
    if [ -z "$tag" ]; then
        echo -e "${RED}❌ Tag name required${NC}"
        echo "Usage: $0 switch <tag>"
        exit 1
    fi
    
    echo -e "${BLUE}🔄 Switching to tag: $tag${NC}"
    task-master use-tag "$tag"
    echo -e "${GREEN}✅ Switched to tag: $tag${NC}"
}

# Main execution
CMD=${1:-help}
shift || true

case "$CMD" in
    start)
        start_task "$1"
        ;;
    
    update)
        update_task "$1" "$2"
        ;;
    
    complete)
        complete_task "$1"
        ;;
    
    show)
        show_task "$1"
        ;;
    
    list)
        list_tasks
        ;;
    
    next)
        next_task
        ;;
    
    tags)
        list_tags
        ;;
    
    switch)
        switch_tag "$1"
        ;;
    
    help|h|*)
        show_help
        ;;
esac
