#!/usr/bin/env bash

OMERON_LOG_FILE="${OMERON_LOG_FILE:-$HOME/.local/share/omeron/install.log}"
OMERON_LOG_LEVEL="${OMERON_LOG_LEVEL:-INFO}"

__log_init() {
  mkdir -p "$(dirname "$OMERON_LOG_FILE")"
}

__log_timestamp() {
  date '+%Y-%m-%d %H:%M:%S'
}

__log_write() {
  local level="$1"
  local message="$2"
  local timestamp
  timestamp="$(__log_timestamp)"

  __log_init
  printf '[%s] [%s] %s\n' "$timestamp" "$level" "$message" | tee -a "$OMERON_LOG_FILE"
}

log_info() { __log_write "INFO" "$1"; }
log_warn() { __log_write "WARN" "$1" >&2; }
log_error() { __log_write "ERROR" "$1" >&2; }
log_success() { __log_write "SUCCESS" "$1"; }
log_debug() {
  [[ "$OMERON_LOG_LEVEL" == "DEBUG" ]] && __log_write "DEBUG" "$1"
}

log_step() {
  local num="$1"
  local total="$2"
  local message="$3"
  printf '\n━━━ [%d/%d] %s ━━━\n' "$num" "$total" "$message"
  __log_write "STEP" "[$num/$total] $message"
}

log_section() {
  local message="$1"
  local line
  printf -v line '%*s' "${#message}" '' && line="${line// /━}"
  printf '\n%s\n%s\n%s\n' "$line" "$message" "$line"
  __log_write "SECTION" "$message"
}
