#!/usr/bin/env bash
# Common logging functions for scripts
# Usage: source "$SCRIPT_DIR/lib/common/logging.sh"

# Source colors if not already sourced
[ -z "${RED:-}" ] && source "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/colors.sh"

# Logging levels (idempotent & safe to re-source)
: "${LOG_LEVEL_DEBUG:=0}"
: "${LOG_LEVEL_INFO:=1}"
: "${LOG_LEVEL_WARN:=2}"
: "${LOG_LEVEL_ERROR:=0}"
# Mark as readonly if not already
readonly LOG_LEVEL_DEBUG 2>/dev/null || true
readonly LOG_LEVEL_INFO  2>/dev/null || true
readonly LOG_LEVEL_WARN  2>/dev/null || true
readonly LOG_LEVEL_ERROR 2>/dev/null || true

# Default log level
: "${LOG_LEVEL:=$LOG_LEVEL_INFO}"

# Logging functions
log_debug() {
    [ "$LOG_LEVEL" -le "$LOG_LEVEL_DEBUG" ] && echo -e "${CYAN}[DEBUG]${NC} $*" >&2
}

log_info() {
    [ "$LOG_LEVEL" -le "$LOG_LEVEL_INFO" ] && echo -e "${GREEN}[INFO]${NC} $*"
}

log_warn() {
    [ "$LOG_LEVEL" -le "$LOG_LEVEL_WARN" ] && echo -e "${YELLOW}[WARN]${NC} $*" >&2
}

log_error() {
    [ "$LOG_LEVEL" -le "$LOG_LEVEL_ERROR" ] && echo -e "${RED}[ERROR]${NC} $*" >&2
}

log_success() {
    echo -e "${GREEN}✓${NC} $*"
}

log_failure() {
    echo -e "${RED}✗${NC} $*"
}

log_section() {
    local title="$1"
    echo ""
    echo -e "${BOLD}${BLUE}════════════════════════════════════════════════════════════════${NC}"
    echo -e "${BOLD}${BLUE}  ${title}${NC}"
    echo -e "${BOLD}${BLUE}════════════════════════════════════════════════════════════════${NC}"
    echo ""
}

log_subsection() {
    local title="$1"
    echo ""
    echo -e "${CYAN}${title}${NC}"
    echo "$(printf '─%.0s' {1..60})"
    echo ""
}

# Print separator line
print_separator() {
    local char="${1:-=}"
    local width="${2:-80}"
    printf "%.0s${char}" $(seq 1 "$width")
    echo
}

