#!/bin/bash

# Workflow Management Script
# Allows switching between basic and enhanced workflows

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

# Configuration
PROJECT_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
WORKFLOW_CONFIG="$PROJECT_ROOT/.taskmaster/workflow.json"

# Help function
show_help() {
    echo "Usage: $0 [basic|enhanced|status]"
    echo ""
    echo "Manages workflow configuration for the project:"
    echo "  basic     Switch to basic workflow (flexible, no strict quality gates)"
    echo "  enhanced  Switch to enhanced workflow (strict TDD, LOC, UTC compliance)"
    echo "  status    Show current workflow configuration"
    echo ""
    echo "Examples:"
    echo "  $0 basic              # Switch to basic workflow"
    echo "  $0 enhanced           # Switch to enhanced workflow"
    echo "  $0 status             # Show current workflow"
}

# Function to create workflow config directory
ensure_config_dir() {
    mkdir -p "$(dirname "$WORKFLOW_CONFIG")"
}

# Function to get current workflow
get_current_workflow() {
    if [[ -f "$WORKFLOW_CONFIG" ]]; then
        jq -r '.workflow' "$WORKFLOW_CONFIG" 2>/dev/null || echo "basic"
    else
        echo "basic"
    fi
}

# Function to set workflow
set_workflow() {
    local workflow="$1"
    
    ensure_config_dir
    
    cat > "$WORKFLOW_CONFIG" << EOF
{
    "workflow": "$workflow",
    "last_updated": "$(date -u +"%Y-%m-%dT%H:%M:%SZ")",
    "description": "$workflow workflow configuration"
}
EOF
    
    echo -e "${GREEN}✅ Workflow switched to: $workflow${NC}"
}

# Function to show workflow status
show_workflow_status() {
    local current_workflow=$(get_current_workflow)
    
    echo -e "${BLUE}📋 Current Workflow Configuration:${NC}"
    echo -e "  ${BLUE}• Active workflow:${NC} $current_workflow"
    
    if [[ -f "$WORKFLOW_CONFIG" ]]; then
        local last_updated=$(jq -r '.last_updated' "$WORKFLOW_CONFIG" 2>/dev/null || echo "unknown")
        echo -e "  ${BLUE}• Last updated:${NC} $last_updated"
    fi
    
    echo ""
    echo -e "${BLUE}📚 Workflow Descriptions:${NC}"
    
    if [[ "$current_workflow" == "enhanced" ]]; then
        echo -e "  ${GREEN}✅ Enhanced Workflow (Active)${NC}"
        echo -e "    • Strict TDD compliance required"
        echo -e "    • LOC rules enforced (300/350 line limits)"
        echo -e "    • UTC timestamp compliance mandatory"
        echo -e "    • Automatic quality gates prevent task completion"
        echo -e "    • Use: ./scripts/validate_quality.sh <task-id>"
    else
        echo -e "  ${YELLOW}⚠️  Basic Workflow (Active)${NC}"
        echo -e "    • Flexible development approach"
        echo -e "    • No strict quality gates"
        echo -e "    • Suitable for prototyping and experimentation"
        echo -e "    • Manual quality checks recommended"
    fi
    
    echo ""
    echo -e "  ${BLUE}Basic Workflow:${NC}"
    echo -e "    • Flexible development approach"
    echo -e "    • No strict quality gates"
    echo -e "    • Suitable for prototyping and experimentation"
    echo -e "    • Manual quality checks recommended"
    
    echo ""
    echo -e "  ${BLUE}Enhanced Workflow:${NC}"
    echo -e "    • Strict TDD compliance required"
    echo -e "    • LOC rules enforced (300/350 line limits)"
    echo -e "    • UTC timestamp compliance mandatory"
    echo -e "    • Automatic quality gates prevent task completion"
    echo -e "    • Use: ./scripts/validate_quality.sh <task-id>"
    
    echo ""
    echo -e "${BLUE}🔧 Quick Commands:${NC}"
    echo -e "  ${BLUE}• Switch to basic:${NC} $0 basic"
    echo -e "  ${BLUE}• Switch to enhanced:${NC} $0 enhanced"
    echo -e "  ${BLUE}• Check status:${NC} $0 status"
}

# Function to validate workflow choice
validate_workflow() {
    local workflow="$1"
    
    case "$workflow" in
        basic|enhanced)
            return 0
            ;;
        *)
            echo -e "${RED}Error: Invalid workflow '$workflow'${NC}"
            echo -e "${YELLOW}Valid options: basic, enhanced${NC}"
            return 1
            ;;
    esac
}

# Function to show workflow transition message
show_transition_message() {
    local from_workflow="$1"
    local to_workflow="$2"
    
    echo ""
    echo -e "${BLUE}🔄 Workflow Transition: $from_workflow → $to_workflow${NC}"
    
    if [[ "$to_workflow" == "enhanced" ]]; then
        echo -e "${YELLOW}⚠️  Switching to Enhanced Workflow${NC}"
        echo -e "${YELLOW}  This enables strict quality gates:${NC}"
        echo -e "    • TDD compliance required"
        echo -e "    • LOC rules enforced"
        echo -e "    • UTC timestamp compliance mandatory"
        echo -e "    • Task completion blocked if rules violated"
        echo ""
        echo -e "${BLUE}💡 Next Steps:${NC}"
        echo -e "  • Use ./scripts/validate_quality.sh <task-id> for quality checks"
        echo -e "  • Follow the enhanced 5-step workflow in dev_workflow.mdc"
        echo -e "  • Run validation before task completion"
    else
        echo -e "${YELLOW}⚠️  Switching to Basic Workflow${NC}"
        echo -e "${YELLOW}  This disables strict quality gates:${NC}"
        echo -e "    • Flexible development approach"
        echo -e "    • No automatic blocking"
        echo -e "    • Manual quality checks recommended"
        echo ""
        echo -e "${BLUE}💡 Next Steps:${NC}"
        echo -e "  • Follow the basic workflow in dev_workflow.mdc"
        echo -e "  • Run manual quality checks as needed"
        echo -e "  • Consider switching back to enhanced for production code"
    fi
}

# Main function
main() {
    local action="$1"
    
    case "$action" in
        basic)
            if validate_workflow "$action"; then
                local current=$(get_current_workflow)
                if [[ "$current" != "$action" ]]; then
                    show_transition_message "$current" "$action"
                    set_workflow "$action"
                else
                    echo -e "${YELLOW}⚠️  Already using basic workflow${NC}"
                fi
            else
                exit 1
            fi
            ;;
        enhanced)
            if validate_workflow "$action"; then
                local current=$(get_current_workflow)
                if [[ "$current" != "$action" ]]; then
                    show_transition_message "$current" "$action"
                    set_workflow "$action"
                else
                    echo -e "${YELLOW}⚠️  Already using enhanced workflow${NC}"
                fi
            else
                exit 1
            fi
            ;;
        status)
            show_workflow_status
            ;;
        --help|-h|help)
            show_help
            ;;
        "")
            show_workflow_status
            ;;
        *)
            echo -e "${RED}Error: Unknown action '$action'${NC}"
            show_help
            exit 1
            ;;
    esac
}

# Run main function
main "$@"
