#!/usr/bin/env bash
# gniza4cp/lib/utils.sh — Core utility functions

die() {
    local code="${2:-$EXIT_FATAL}"
    echo "${C_RED}FATAL: $1${C_RESET}" >&2
    exit "$code"
}

require_root() {
    [[ $EUID -eq 0 ]] || die "This command must be run as root"
}

timestamp() {
    date -u +"%Y-%m-%dT%H%M%S"
}

human_size() {
    local bytes="$1"
    if (( bytes >= 1073741824 )); then
        local whole=$(( bytes / 1073741824 ))
        local frac=$(( (bytes % 1073741824) * 10 / 1073741824 ))
        printf "%d.%d GB" "$whole" "$frac"
    elif (( bytes >= 1048576 )); then
        local whole=$(( bytes / 1048576 ))
        local frac=$(( (bytes % 1048576) * 10 / 1048576 ))
        printf "%d.%d MB" "$whole" "$frac"
    elif (( bytes >= 1024 )); then
        local whole=$(( bytes / 1024 ))
        local frac=$(( (bytes % 1024) * 10 / 1024 ))
        printf "%d.%d KB" "$whole" "$frac"
    else
        printf "%d B" "$bytes"
    fi
}

human_duration() {
    local seconds="$1"
    if (( seconds >= 3600 )); then
        printf "%dh %dm %ds" $((seconds/3600)) $((seconds%3600/60)) $((seconds%60))
    elif (( seconds >= 60 )); then
        printf "%dm %ds" $((seconds/60)) $((seconds%60))
    else
        printf "%ds" "$seconds"
    fi
}

# Check if a command exists
require_cmd() {
    command -v "$1" &>/dev/null || die "Required command not found: $1"
}

validate_timestamp() {
    local ts="$1"
    if [[ ! "$ts" =~ ^[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{6}$ ]]; then
        log_error "Invalid timestamp format: $ts (expected YYYY-MM-DDTHHMMSS)"
        return 1
    fi
}

validate_account_name() {
    local name="$1"
    if [[ ! "$name" =~ ^[a-z][a-z0-9_-]{0,15}$ ]]; then
        log_error "Invalid account name: $name"
        return 1
    fi
}
