#!/usr/bin/env bash
set -euo pipefail

# Claude Code Hook: File Logger
# 
# Purpose: Simple demonstration of hook functionality
# Trigger: PreToolUse for Edit, Write, MultiEdit tools
# Blocking: No - just logs activity
#
# This hook demonstrates basic hook functionality by logging file operations


##################################
# Configuration
##################################
HOOK_NAME="file-logger"
LOG_FILE="$HOME/.claude/logs/file-logger.log"

# Ensure log directory exists with secure permissions
mkdir -p "$(dirname "$LOG_FILE")"
chmod 700 "$(dirname "$LOG_FILE")"

# Create log file with restrictive permissions if it doesn't exist
touch "$LOG_FILE"
chmod 600 "$LOG_FILE"

##################################
# Logging Functions
##################################
log() {
    echo "[$(date +'%Y-%m-%d %H:%M:%S')] [$HOOK_NAME] $*" | tee -a "$LOG_FILE"
}

##################################
# Dependency Validation
##################################
validate_hook_dependencies() {
    local deps=("wc" "file")
    local missing=()
    
    for dep in "${deps[@]}"; do
        if ! command -v "$dep" &> /dev/null; then
            missing+=("$dep")
        fi
    done
    
    if [[ ${#missing[@]} -gt 0 ]]; then
        log "ERROR: Missing required dependencies: ${missing[*]}"
        echo "Install missing tools and retry"
        exit 1
    fi
}

##################################
# Main Hook Logic
##################################
main() {
    # Validate dependencies first
    validate_hook_dependencies
    local tool_name="${CLAUDE_TOOL:-unknown}"
    local file_path="${CLAUDE_FILE:-unknown}"
    
    log "Hook triggered!"
    log "Tool: $tool_name"
    log "File: $file_path"
    
    # Only process file modification tools
    case "$tool_name" in
        "Edit"|"Write"|"MultiEdit")
            log "Processing file modification tool: $tool_name"
            ;;
        *)
            log "Skipping non-file tool: $tool_name"
            exit 0
            ;;
    esac
    
    # Get basic file info if file exists
    if [[ -n "$file_path" ]] && [[ "$file_path" != "unknown" ]] && [[ -f "$file_path" ]]; then
        local file_size=$(wc -c < "$file_path" 2>/dev/null || echo "0")
        local file_lines=$(wc -l < "$file_path" 2>/dev/null || echo "0")
        
        log "File size: $file_size bytes"
        log "File lines: $file_lines"
        log "File type: $(file -b "$file_path" 2>/dev/null || echo "unknown")"
    else
        log "File does not exist yet or path unknown"
    fi
    
    # Always allow the operation to proceed
    log "Operation allowed - no blocking behavior"
    exit 0
}

##################################
# Execute Main Function
##################################
main "$@"