# bash_log
# --------
# Funciones de logging con colores para scripts.
# Incluye: log, success, info, warning, error, debug, progress.
#
# USO (sourced):
#   source bash_log.sh
#   bash_log_init
#   success "Operacion completada"
#   info "Procesando..."
#   error "Algo fallo"

SCRIPT_DIR_BASH_LOG="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
source "$SCRIPT_DIR_BASH_LOG/bash_colors.sh"
bash_colors

bash_log_init() {
    export ERROR_LOG_FILE="${ERROR_LOG_FILE:-/tmp/script-errors-$(date +%Y%m%d).log}"
    export DEBUG_MODE="${DEBUG_MODE:-0}"
}

log() {
    local level="$1"
    shift
    local message="$@"
    local timestamp
    timestamp=$(date '+%Y-%m-%d %H:%M:%S')
    echo "[$timestamp] [$level] $message" >> "${ERROR_LOG_FILE:-/tmp/script-errors.log}"

    if [ "${DEBUG_MODE:-0}" = "1" ]; then
        echo -e "${GRAY}[$timestamp] [$level] $message${NC}" >&2
    fi
}

success() {
    echo -e "${GREEN}${CHECKMARK} $*${NC}"
    log "SUCCESS" "$*"
}

info() {
    echo -e "${BLUE}${INFO} $*${NC}"
    log "INFO" "$*"
}

warning() {
    echo -e "${YELLOW}${WARNING} $*${NC}"
    log "WARNING" "$*"
}

error() {
    echo -e "${RED}${CROSS} Error: $*${NC}" >&2
    log "ERROR" "$*"
}

debug() {
    if [ "${DEBUG_MODE:-0}" = "1" ]; then
        echo -e "${GRAY}[DEBUG] $*${NC}" >&2
    fi
    log "DEBUG" "$*"
}

progress() {
    echo -e "${CYAN}${ARROW} $*${NC}"
}
