#!/bin/bash

# AIKit Commit Message Hook
# Validate commit messages follow conventional commit format

set -e

# Read the commit message
commit_msg_file="$1"
commit_msg=$(cat "$commit_msg_file")

echo "📝 Checking commit message format..."
echo "==================================="

# Check conventional commit pattern
# Pattern: type(scope): description
# Examples: feat(cli): add new command, fix(auth): resolve login issue
if ! echo "$commit_msg" | grep -qE "^(feat|fix|docs|style|refactor|test|chore|perf|ci|build|revert)(\(.+\))?: .{1,}"; then
    echo "⚠️  Commit message doesn't follow conventional commit format"
    echo ""
    echo "Expected format: type(scope): description"
    echo "Valid types: feat, fix, docs, style, refactor, test, chore, perf, ci, build, revert"
    echo ""
    echo "Examples:"
    echo "  feat(cli): add new command option"
    echo "  fix(auth): resolve login timeout issue"
    echo "  docs(readme): update installation instructions"
    echo ""
    echo "Current message: $commit_msg"
    echo ""
    read -p "Continue anyway? (y/N): " -n 1 -r
    echo
    if [[ ! $REPLY =~ ^[Yy]$ ]]; then
        echo "❌ Commit aborted. Please fix the commit message."
        exit 1
    fi
    echo "✅ Continuing with non-standard commit message..."
else
    echo "✅ Commit message follows conventional format"
fi

echo ""
exit 0