#!/bin/bash

if [[ -z "${WELCOME_SCRIPT_PATH}" ]]; then
  WELCOME_SCRIPT_PATH="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." &>/dev/null && pwd)"
fi

#region INCLUDES
. "${WELCOME_SCRIPT_PATH}/config/config_colors.sh"
#endregion

# Séparateur
SEP="█►"

get_git_status() {
    local branch status current_bg current_color project upstream behind
    branch=$(git branch --show-current 2>/dev/null)
    [[ -z "$branch" ]] && return

    project=$(basename "$(git rev-parse --show-toplevel 2>/dev/null)" 2>/dev/null)

    current_bg="$(color_secure "$COLOR_DARK_GREEN_BG")"
    current_color="$(color_secure "$COLOR_DARK_GREEN")"
    status="🆗"

    if [[ -n $(git status --porcelain 2>/dev/null) ]]; then
        current_bg="$(color_secure "$COLOR_LIGHT_RED_BG")"
        # shellcheck disable=SC2086
        current_color="$(color_secure "$COLOR_LIGHT_RED")"
        status="🔄"
    fi

    # Affichage du premier bloc
    echo -ne "$(color_secure "$COLOR_DARK_GRAY")${current_bg}${SEP}$(color_secure "$COLOR_WHITE") [${project}/${branch}]${status}"

    # --- SEGMENT DYNAMIQUE (BEHIND / PULL) ---
    # On vérifie si on a un upstream (une branche distante)
    upstream=$(git rev-parse --abbrev-ref --symbolic-full-name "@{u}" 2>/dev/null)
    if [[ -n "$upstream" ]]; then
        # On compte le nombre de commits de retard
        behind=$(git rev-list --count HEAD.."$upstream" 2>/dev/null)

        if [[ "$behind" -gt 0 ]]; then
            # Raccord entre le segment précédent et le segment orange
            # On utilise la couleur de fond du précédent pour le texte du SEP
            echo -ne "${current_color}$(color_secure "$COLOR_ORANGE_BG")${SEP}$(color_secure "$COLOR_WHITE") pull $behind ✨"#🆙

            # On met à jour les variables pour le SEP de fin
            # shellcheck disable=SC2086
            current_bg="$(color_secure $COLOR_ORANGE_BG)"
            current_color="$(color_secure "$COLOR_ORANGE")"
        fi
    fi

    # --- FIN DU BLOC ---
    echo -ne "${current_bg}${NONE}${current_color}${SEP}${NONE}"
}

get_git_status_OK() {
    local branch status current_bg current_color project
    branch=$(git branch --show-current 2>/dev/null)
    [[ -z "$branch" ]] && return

    project=$(basename "$(git rev-parse --show-toplevel 2>/dev/null)" 2>/dev/null)

    # Initialisation des couleurs (On utilise des variables propres)
    current_bg="$(color_secure "$COLOR_DARK_GREEN_BG")"
    current_color="$(color_secure "$COLOR_DARK_GREEN")"
    status="🆗"

    if [[ -n $(git status --porcelain 2>/dev/null) ]]; then
        current_bg="$(color_secure "$COLOR_LIGHT_RED_BG")"
        current_color="$(color_secure "$COLOR_LIGHT_RED")"
        status="🔄"
    fi

    echo -ne "$(color_secure "$COLOR_DARK_GRAY")${current_bg}${SEP}$(color_secure "$COLOR_WHITE") TEST[${project}/${branch}]${status}${current_bg}${NONE}${current_color}${SEP}${NONE}"
}

set_ps1() {
    local EXIT P1 P2 GIT_PART ARROW_C
    EXIT="$?"

    # Segment 1 : User (Bleu) - Ajout des  
    P1="$(color_secure "$COLOR_LIGHT_BLUE_BG")$(color_secure "$COLOR_WHITE") <\u@$(hostname -f)>${NONE}"

    # Segment 2 : Chemin (Gris) avec raccord Bleu
    P2="$(color_secure "$COLOR_LIGHT_BLUE")$(color_secure "$COLOR_DARK_GRAY_BG")${SEP}$(color_secure "$COLOR_WHITE") \w"

    # Segment 3 : Git
    GIT_PART=$(get_git_status)

    if [[ -n "$GIT_PART" ]]; then
        # On ne met pas d'espace avant ${GIT_PART} car le SEP s'en occupe
        PS1="${P1}${P2} ${GIT_PART}"
    else
        PS1="${P1}${P2} ${NONE}$(color_secure "$COLOR_DARK_GRAY")${SEP}${NONE}"
    fi

    # Ligne 2 : Flèche de retour
    ARROW_C="$(color_secure "$COLOR_GREEN")"
    [[ $EXIT != 0 ]] && ARROW_C="$(color_secure "$COLOR_LIGHT_RED")"

    PS1+="\n${ARROW_C}└─${NONE} "
}

PROMPT_COMMAND=set_ps1
