#!/bin/bash
# AutoWorkflow Post-Commit Hook
# Runs on: PostToolUse for Bash (when git commit detected)
# Purpose: Trigger BLUEPRINT.md update reminder after feature commits
#
# This hook implements the on:feature_complete trigger
# It reminds Claude to update BLUEPRINT.md after successful commits

# Colors
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
CYAN='\033[0;36m'
BOLD='\033[1m'
DIM='\033[2m'
NC='\033[0m'

# Project directory (use env var if set, otherwise current directory)
PROJECT_DIR="${CLAUDE_PROJECT_DIR:-.}"

# State directory
STATE_DIR="$PROJECT_DIR/.claude/.autoworkflow"

# State files
PHASE_FILE="$STATE_DIR/phase"
TASK_TYPE_FILE="$STATE_DIR/task-type"

# Get task type
get_task_type() {
    if [ -f "$TASK_TYPE_FILE" ]; then
        cat "$TASK_TYPE_FILE"
    else
        echo "unknown"
    fi
}

# Check if task type requires BLUEPRINT update
requires_blueprint_update() {
    local task_type="$1"
    case "$task_type" in
        feature|refactor)
            return 0
            ;;
        *)
            return 1
            ;;
    esac
}

# Reset workflow state after commit
reset_workflow() {
    echo "IDLE" > "$PHASE_FILE"
    rm -f "$STATE_DIR/verify-iteration" 2>/dev/null
    rm -f "$STATE_DIR/verify-status" 2>/dev/null
    rm -f "$STATE_DIR/plan-approved" 2>/dev/null
    rm -f "$STATE_DIR/changed-files" 2>/dev/null
    rm -f "$STATE_DIR/session-resumed" 2>/dev/null
}

# Get last commit info
get_commit_info() {
    local commit_type=$(git log -1 --pretty=%s 2>/dev/null | cut -d'(' -f1 | cut -d':' -f1)
    local commit_scope=$(git log -1 --pretty=%s 2>/dev/null | sed -n 's/.*(\([^)]*\)).*/\1/p')
    local commit_msg=$(git log -1 --pretty=%s 2>/dev/null)
    local commit_hash=$(git log -1 --pretty=%h 2>/dev/null)
    local files_changed=$(git diff-tree --no-commit-id --name-only -r HEAD 2>/dev/null | wc -l | tr -d ' ')

    echo "$commit_type|$commit_scope|$commit_msg|$commit_hash|$files_changed"
}

# Main execution
main() {
    local task_type=$(get_task_type)
    local commit_info=$(get_commit_info)
    local commit_type=$(echo "$commit_info" | cut -d'|' -f1)
    local commit_scope=$(echo "$commit_info" | cut -d'|' -f2)
    local commit_msg=$(echo "$commit_info" | cut -d'|' -f3)
    local commit_hash=$(echo "$commit_info" | cut -d'|' -f4)
    local files_changed=$(echo "$commit_info" | cut -d'|' -f5)

    echo ""
    echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
    echo -e "${GREEN}${BOLD}✅ AUTOWORKFLOW: COMMIT COMPLETE${NC}"
    echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
    echo ""
    echo -e "  ${CYAN}Hash:${NC}    $commit_hash"
    echo -e "  ${CYAN}Message:${NC} $commit_msg"
    echo -e "  ${CYAN}Files:${NC}   $files_changed changed"
    echo ""

    # Check if BLUEPRINT update is needed
    if requires_blueprint_update "$task_type" || [ "$commit_type" = "feat" ] || [ "$commit_type" = "refactor" ]; then
        echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
        echo -e "${YELLOW}${BOLD}📘 AUTOWORKFLOW: UPDATE PHASE${NC}"
        echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
        echo ""
        echo "This commit may have added new features/routes/APIs."
        echo ""
        echo -e "${BOLD}Check if BLUEPRINT.md needs updating:${NC}"
        echo ""
        echo "  1. New routes/pages added?"
        echo "  2. New API endpoints added?"
        echo "  3. New features/components added?"
        echo "  4. Existing features modified?"
        echo ""

        # Check what might have changed
        local new_routes=$(git diff-tree --no-commit-id --name-only -r HEAD 2>/dev/null | grep -E "(pages|app|routes)" | head -3)
        local new_apis=$(git diff-tree --no-commit-id --name-only -r HEAD 2>/dev/null | grep -E "(api|endpoints|routes)" | head -3)
        local new_components=$(git diff-tree --no-commit-id --name-only -r HEAD 2>/dev/null | grep -E "components" | head -3)

        if [ -n "$new_routes" ]; then
            echo -e "${DIM}Potential new routes:${NC}"
            echo "$new_routes" | while read f; do echo -e "  ${DIM}- $f${NC}"; done
            echo ""
        fi

        if [ -n "$new_apis" ]; then
            echo -e "${DIM}Potential new APIs:${NC}"
            echo "$new_apis" | while read f; do echo -e "  ${DIM}- $f${NC}"; done
            echo ""
        fi

        if [ -n "$new_components" ]; then
            echo -e "${DIM}Potential new components:${NC}"
            echo "$new_components" | while read f; do echo -e "  ${DIM}- $f${NC}"; done
            echo ""
        fi

        echo -e "${BOLD}Action:${NC} Review and update instructions/BLUEPRINT.md if needed."
        echo ""
        echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
    fi

    # Reset workflow state
    reset_workflow

    echo ""
    echo -e "${GREEN}Workflow complete. Ready for next task.${NC}"
    echo ""
}

main
