#!/bin/bash
# ── violet-chaton : fonctions partagées ───────────────────────────────────────

# ── Palette violet-chaton v2 (source de verite : themes/palette.sh) ──────────
_LIB_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
_PALETTE="$_LIB_DIR/../themes/palette.sh"
if [ -f "$_PALETTE" ]; then
    source "$_PALETTE"
fi

PINK="${VC_ANSI_MAGENTA:-\033[38;2;255;77;166m}"
PURPLE="${VC_ANSI_LILAC:-\033[38;2;201;160;255m}"
CYAN="${VC_ANSI_LAVANDE:-\033[38;2;164;180;255m}"
GREEN="${VC_ANSI_MITSURI:-\033[38;2;154;219;168m}"
YELLOW="${VC_ANSI_CHAMPAGNE:-\033[38;2;232;200;122m}"
RED="${VC_ANSI_DANGER:-\033[38;2;242;92;122m}"
MUTED="${VC_ANSI_MUTED:-\033[38;2;113;102;134m}"
TEXT="${VC_ANSI_TEXT:-\033[38;2;240;234;248m}"
BOLD="${VC_ANSI_BOLD:-\033[1m}"
RESET="${VC_ANSI_RESET:-\033[0m}"

# ── Log ────────────────────────────────────────────────────────────────────────
# INSTALL_LOG peut être pré-exporté par install.sh pour que tous les sous-scripts
# partagent le même fichier. Sinon, chaque script crée le sien.
: "${INSTALL_LOG:=$HOME/violet-chaton-install-$(date +%Y%m%d-%H%M%S).log}"
export INSTALL_LOG

_log() {
    printf '[%s] %s\n' "$(date '+%H:%M:%S')" "$1" >> "$INSTALL_LOG"
}

# ── Bannières ─────────────────────────────────────────────────────────────────
banner() {
    echo ""
    echo -e "${PINK}${BOLD}  ╔══════════════════════════════════════════════════════════╗${RESET}"
    echo -e "${PINK}${BOLD}  ║                                                          ║${RESET}"
    echo -e "${PINK}${BOLD}  ║  ${PURPLE}░░  violet-chaton — setup automatique  ░░${PINK}   q:D        ║${RESET}"
    echo -e "${PINK}${BOLD}  ║                                                          ║${RESET}"
    echo -e "${PINK}${BOLD}  ╚══════════════════════════════════════════════════════════╝${RESET}"
    echo ""
    _log "════════════════════════════════════════"
    _log "  violet-chaton — installation démarrée"
    _log "════════════════════════════════════════"
}

banner_done() {
    _log ""
    _log "════════════════════════════════════════"
    _log "  Installation terminée"
    _log "════════════════════════════════════════"
    echo ""
    echo -e "${CYAN}${BOLD}  ╔══════════════════════════════════════════════════════════╗${RESET}"
    echo -e "${CYAN}${BOLD}  ║                                                          ║${RESET}"
    echo -e "${CYAN}${BOLD}  ║       ${GREEN}✓  Installation terminée avec succès !${CYAN}            ║${RESET}"
    echo -e "${CYAN}${BOLD}  ║   ${MUTED}Ouvre un nouveau terminal pour voir le résultat.${CYAN}      ║${RESET}"
    echo -e "${CYAN}${BOLD}  ║                                                          ║${RESET}"
    echo -e "${CYAN}${BOLD}  ╚══════════════════════════════════════════════════════════╝${RESET}"
    echo ""
    echo -e "  ${MUTED}Log complet :${RESET} ${TEXT}$INSTALL_LOG${RESET}"
    echo ""
}

# ── Fonctions de sortie (terminal + log) ───────────────────────────────────────
section() {
    echo ""
    echo -e "${PURPLE}${BOLD}  ┌─ $1 ${RESET}"
    _log ""
    _log "── $1"
}

step() {
    echo -e "${MUTED}  │  ${CYAN}→${RESET} $1"
    _log "   → $1"
}

ok() {
    echo -e "${MUTED}  │  ${GREEN}✓${RESET} $1"
    _log "   ✓ $1"
}

fail() {
    echo -e "${MUTED}  │  ${RED}✗${RESET} $1"
    _log "   ✗ ERREUR : $1"
}

warn() {
    echo -e "${MUTED}  │  ${YELLOW}!${RESET} $1"
    _log "   ! AVERT  : $1"
}

info() {
    echo -e "${MUTED}  │    ${MUTED}$1${RESET}"
    _log "     $1"
}

# ── Utilitaires ───────────────────────────────────────────────────────────────
has_cmd() {
    command -v "$1" &>/dev/null
}

ensure_dir() {
    mkdir -p "$1"
}

backup_dir() {
    local dir="$1"
    [ -d "$dir" ] || return 0
    # BACKUP_DIR est défini par 03-deploy-configs.sh
    [ -z "$BACKUP_DIR" ] && return 0
    local rel="${dir#"$HOME/"}"
    ensure_dir "$BACKUP_DIR/$rel"
    cp -r "$dir/." "$BACKUP_DIR/$rel/" 2>/dev/null
}

show_summary() {
    local log="$INSTALL_LOG"
    [ -f "$log" ] || return

    local successes errors warnings
    successes=$(grep -c "   ✓ " "$log" 2>/dev/null) || true; successes=${successes:-0}
    errors=$(grep -c " ✗ ERREUR" "$log" 2>/dev/null) || true; errors=${errors:-0}
    warnings=$(grep -c " ! AVERT" "$log" 2>/dev/null) || true; warnings=${warnings:-0}

    echo -e "${PURPLE}${BOLD}  ┌─ Résumé ────────────────────────────────────────────────────${RESET}"
    echo -e "  │  ${GREEN}✓ ${successes} réussis${RESET}   ${YELLOW}! ${warnings} avertissements${RESET}   ${RED}✗ ${errors} erreurs${RESET}"

    if [ "${errors:-0}" -gt 0 ] 2>/dev/null; then
        echo -e "  │"
        echo -e "  │  ${RED}Erreurs :${RESET}"
        grep " ✗ ERREUR" "$log" | sed 's/.*✗ ERREUR : //' | while IFS= read -r line; do
            echo -e "  │    ${RED}✗${RESET} $line"
        done
    fi

    if [ "${warnings:-0}" -gt 0 ] 2>/dev/null; then
        echo -e "  │"
        echo -e "  │  ${YELLOW}Avertissements :${RESET}"
        grep " ! AVERT" "$log" | sed 's/.*! AVERT  : //' | while IFS= read -r line; do
            echo -e "  │    ${YELLOW}!${RESET} $line"
        done
    fi
    echo ""
}
