#!/bin/bash
# Kubernetes Logging Utilities
# File: logging-utils.sh

# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
PURPLE='\033[0;35m'
CYAN='\033[0;36m'
NC='\033[0m' # No Color

# Function for colored output
print_status() {
    echo -e "${BLUE}[INFO]${NC} $1"
}

print_success() {
    echo -e "${GREEN}[SUCCESS]${NC} $1"
}

print_warning() {
    echo -e "${YELLOW}[WARNING]${NC} $1"
}

print_error() {
    echo -e "${RED}[ERROR]${NC} $1"
}

print_debug() {
    echo -e "${PURPLE}[DEBUG]${NC} $1"
}

print_operation() {
    echo -e "${CYAN}[OPERATION]${NC} $1"
}

# Main logging function
log_operation() {
    local level=$1
    local message=$2
    local timestamp=$(date '+%Y-%m-%d %H:%M:%S')
    
    # Ensure log directory exists
    if [[ -n "$K8S_LOG_DIR" ]]; then
        mkdir -p "$K8S_LOG_DIR"
        
        # Log to main operations file
        echo "$timestamp [$level] $message" >> "$K8S_LOG_DIR/k8s-operations.log"
        
        # Log errors to separate error file
        if [[ "$level" == "ERROR" ]]; then
            echo "$timestamp [ERROR] $message" >> "$K8S_LOG_DIR/service-errors.log"
            print_error "$message"
        elif [[ "$level" == "WARNING" ]]; then
            print_warning "$message"
        elif [[ "$level" == "SUCCESS" ]]; then
            print_success "$message"
        elif [[ "$level" == "DEBUG" ]]; then
            print_debug "$message"
        elif [[ "$level" == "OPERATION" ]]; then
            print_operation "$message"
        else
            print_status "$message"
        fi
    else
        # Fallback if no log directory is set
        case $level in
            "ERROR")
                print_error "$message"
                ;;
            "WARNING")
                print_warning "$message"
                ;;
            "SUCCESS")
                print_success "$message"
                ;;
            "DEBUG")
                print_debug "$message"
                ;;
            "OPERATION")
                print_operation "$message"
                ;;
            *)
                print_status "$message"
                ;;
        esac
    fi
}

# Log kubectl command execution
log_kubectl_command() {
    local command="$1"
    local result="$2"
    local timestamp=$(date '+%Y-%m-%d %H:%M:%S')
    
    if [[ -n "$K8S_LOG_DIR" ]]; then
        echo "$timestamp [KUBECTL] $command" >> "$K8S_LOG_DIR/kubectl-commands.log"
        if [[ -n "$result" ]]; then
            echo "$timestamp [KUBECTL_RESULT] $result" >> "$K8S_LOG_DIR/kubectl-commands.log"
        fi
    fi
}

# Log dependency check results
log_dependency_check() {
    local service="$1"
    local status="$2"
    local details="$3"
    local timestamp=$(date '+%Y-%m-%d %H:%M:%S')
    
    if [[ -n "$K8S_LOG_DIR" ]]; then
        echo "$timestamp [DEPENDENCY] Service: $service, Status: $status, Details: $details" >> "$K8S_LOG_DIR/dependency-checks.log"
    fi
    
    if [[ "$status" == "READY" ]]; then
        log_operation "SUCCESS" "Dependency check passed for $service"
    elif [[ "$status" == "NOT_READY" ]]; then
        log_operation "WARNING" "Dependency check failed for $service: $details"
    else
        log_operation "ERROR" "Dependency check error for $service: $details"
    fi
}

# Show recent logs
show_recent_logs() {
    local log_type=${1:-operations}
    local lines=${2:-20}
    
    if [[ -z "$K8S_LOG_DIR" ]]; then
        echo "No log directory configured"
        return 1
    fi
    
    case $log_type in
        "operations"|"ops")
            if [[ -f "$K8S_LOG_DIR/k8s-operations.log" ]]; then
                echo "Recent operations (last $lines lines):"
                tail -n "$lines" "$K8S_LOG_DIR/k8s-operations.log"
            else
                echo "No operations log found"
            fi
            ;;
        "errors"|"err")
            if [[ -f "$K8S_LOG_DIR/service-errors.log" ]]; then
                echo "Recent errors (last $lines lines):"
                tail -n "$lines" "$K8S_LOG_DIR/service-errors.log"
            else
                echo "No error log found"
            fi
            ;;
        "kubectl"|"cmd")
            if [[ -f "$K8S_LOG_DIR/kubectl-commands.log" ]]; then
                echo "Recent kubectl commands (last $lines lines):"
                tail -n "$lines" "$K8S_LOG_DIR/kubectl-commands.log"
            else
                echo "No kubectl command log found"
            fi
            ;;
        "dependencies"|"deps")
            if [[ -f "$K8S_LOG_DIR/dependency-checks.log" ]]; then
                echo "Recent dependency checks (last $lines lines):"
                tail -n "$lines" "$K8S_LOG_DIR/dependency-checks.log"
            else
                echo "No dependency check log found"
            fi
            ;;
        *)
            echo "Available log types: operations, errors, kubectl, dependencies"
            return 1
            ;;
    esac
}

# Clear logs
clear_logs() {
    local log_type=${1:-all}
    
    if [[ -z "$K8S_LOG_DIR" ]]; then
        echo "No log directory configured"
        return 1
    fi
    
    case $log_type in
        "all")
            rm -f "$K8S_LOG_DIR"/*.log
            log_operation "INFO" "All logs cleared"
            ;;
        "operations"|"ops")
            rm -f "$K8S_LOG_DIR/k8s-operations.log"
            echo "Operations log cleared"
            ;;
        "errors"|"err")
            rm -f "$K8S_LOG_DIR/service-errors.log"
            echo "Error log cleared"
            ;;
        "kubectl"|"cmd")
            rm -f "$K8S_LOG_DIR/kubectl-commands.log"
            echo "Kubectl command log cleared"
            ;;
        "dependencies"|"deps")
            rm -f "$K8S_LOG_DIR/dependency-checks.log"
            echo "Dependency check log cleared"
            ;;
        *)
            echo "Available log types: all, operations, errors, kubectl, dependencies"
            return 1
            ;;
    esac
}

# Export functions for use in other scripts
if [[ -n "$ZSH_VERSION" ]]; then
    typeset -f log_operation log_kubectl_command log_dependency_check > /dev/null
    typeset -f show_recent_logs clear_logs > /dev/null
    typeset -f print_status print_success print_warning print_error print_debug print_operation > /dev/null
else
    export -f log_operation log_kubectl_command log_dependency_check
    export -f show_recent_logs clear_logs
    export -f print_status print_success print_warning print_error print_debug print_operation
fi