#!/usr/bin/env bash
#
# Git pre-commit hook that runs all CI checks
# This hook dynamically reads the GitHub Actions workflow file and executes
# the same lint and test commands that run in CI.
#
# This hook is installed automatically by the Claude Code session-start hook
# To bypass this hook (not recommended), use: git commit --no-verify
#

set -e

echo "🔍 Running pre-commit checks..."
echo ""

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

# Function to print colored status messages
print_status() {
    echo -e "${GREEN}✓${NC} $1"
}

print_error() {
    echo -e "${RED}✗${NC} $1"
}

print_warning() {
    echo -e "${YELLOW}⚠${NC} $1"
}

print_info() {
    echo -e "${BLUE}ℹ${NC} $1"
}

# Check if uv is available
if ! command -v uv &> /dev/null; then
    print_error "uv is not installed. Please install uv: https://docs.astral.sh/uv/"
    exit 1
fi

# Get repository root
REPO_ROOT=$(git rev-parse --show-toplevel)
WORKFLOW_FILE="$REPO_ROOT/.github/workflows/ci.yml"
PARSER_SCRIPT="$REPO_ROOT/hooks/parse_ci_workflow.py"

# Check if workflow file exists
if [ ! -f "$WORKFLOW_FILE" ]; then
    print_error "CI workflow file not found: $WORKFLOW_FILE"
    exit 1
fi

# Check if parser script exists
if [ ! -f "$PARSER_SCRIPT" ]; then
    print_error "Workflow parser script not found: $PARSER_SCRIPT"
    exit 1
fi

print_info "Reading checks from GitHub Actions workflow: .github/workflows/ci.yml"
echo ""

# Track overall status
FAILED=0

# Extract and run lint commands
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo "📋 Lint Checks (from CI)"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo ""

# Read lint commands from workflow (bash 3.2 compatible)
LINT_COMMANDS=()
while IFS= read -r line; do
    LINT_COMMANDS+=("$line")
done < <(uv run python "$PARSER_SCRIPT" --job lint)

# Run each lint command
for cmd in "${LINT_COMMANDS[@]}"; do
    echo "Running: $cmd"
    if eval "$cmd"; then
        print_status "Passed: $cmd"
    else
        print_error "Failed: $cmd"
        FAILED=1
    fi
    echo ""
done

# Extract and run test commands
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo "🧪 Tests (from CI)"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo ""

# Read test commands from workflow (bash 3.2 compatible)
TEST_COMMANDS=()
while IFS= read -r line; do
    TEST_COMMANDS+=("$line")
done < <(uv run python "$PARSER_SCRIPT" --job test)

# Run each test command
for cmd in "${TEST_COMMANDS[@]}"; do
    echo "Running: $cmd"
    if eval "$cmd"; then
        print_status "Passed: $cmd"
    else
        print_error "Failed: $cmd"
        FAILED=1
    fi
    echo ""
done

echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"

# Exit with appropriate status
if [ $FAILED -eq 1 ]; then
    echo ""
    print_error "Pre-commit checks failed!"
    print_warning "Fix the issues above before committing"
    print_warning "To bypass this hook (not recommended): git commit --no-verify"
    echo ""
    exit 1
else
    echo ""
    print_status "All pre-commit checks passed! ✨"
    echo ""
    exit 0
fi
