# Custom Fish prompt - simple version
# Configure via: prompt-config

function _prompt_git_branch
    git branch --show-current 2>/dev/null
end

function _prompt_git_icon
    set -l remotes (git remote -v 2>/dev/null | awk '{print $2}')
    test -z "$remotes"; and return

    for remote in $remotes
        if not string match -q "*github.com*" $remote
            printf '\ue702'
            return
        end
    end
    printf '\uf113'
end

function _prompt_git_status
    git rev-parse --git-dir >/dev/null 2>&1; or return

    set -l staged (git diff --cached --numstat 2>/dev/null | wc -l)
    set -l modified (git diff --numstat 2>/dev/null | wc -l)
    set -l untracked (git ls-files --others --exclude-standard 2>/dev/null | wc -l)
    set -l stashed (git stash list 2>/dev/null | wc -l)

    set -l ahead_behind (git rev-list --left-right --count @{upstream}...HEAD 2>/dev/null)
    set -l ahead (echo $ahead_behind | awk '{print $2}')
    set -l behind (echo $ahead_behind | awk '{print $1}')

    set -l status_parts

    if test "$staged" -gt 0
        set status_parts $status_parts "++($staged)"
    end

    if test "$modified" -gt 0
        set status_parts $status_parts "!($modified)"
    end

    if test "$untracked" -gt 0
        set status_parts $status_parts "?($untracked)"
    end

    if test "$stashed" -gt 0
        set status_parts $status_parts "\$"
    end

    if test -n "$ahead" -a "$ahead" -gt 0
        if test -n "$behind" -a "$behind" -gt 0
            set status_parts $status_parts "⇕[⇡($ahead)⇣($behind)]"
        else
            set status_parts $status_parts "⇡($ahead)"
        end
    else if test -n "$behind" -a "$behind" -gt 0
        set status_parts $status_parts "⇣($behind)"
    end

    if test -z "$status_parts"
        echo "✓"
    else
        string join "" $status_parts
    end
end

function _prompt_mode_text
    switch $fish_bind_mode
        case default
            echo "N"
        case insert
            echo "I"
        case visual
            echo "V"
        case replace-one
            echo "R"
        case '*'
            echo "$fish_bind_mode"
    end
end

function _prompt_mode_color
    switch $fish_bind_mode
        case default
            echo $prompt_color_error
        case insert
            echo $prompt_color_tertiary_fixed
        case visual
            echo $prompt_color_primary_fixed
        case replace-one
            echo $prompt_color_error
        case '*'
            echo $prompt_color_tertiary_fixed
    end
end


function fish_prompt
    # keep vertical spacing consistent (including empty Enter)
    echo

    _prompt_config

    set -l bg $prompt_color_surface
    set -l left_parts

    # vi mode indicator pill on left
    set -l mode_text (_prompt_mode_text)
    set -l mode_color (_prompt_mode_color)
    set left_parts $left_parts (_prompt_segment "$mode_text" $bg $mode_color)

    # Username
    if test "$prompt_show_username" = true
        set left_parts $left_parts (_prompt_segment "$prompt_user_symbol $USER" $bg $prompt_color_tertiary_fixed)
    end

    # Directory
    set -l dir (_prompt_directory)
    set left_parts $left_parts (_prompt_segment "$dir" $bg $prompt_color_primary)

    # Git branch with dynamic icon
    set -l branch (_prompt_git_branch)
    if test -n "$branch"
        set -l git_icon (_prompt_git_icon)
        set left_parts $left_parts (_prompt_segment "$git_icon $branch" $bg $prompt_color_primary_fixed)
    end

    # Git status
    set -l git_status (_prompt_git_status)
    if test -n "$git_status"
        set -l fg_color $prompt_color_error
        if test "$git_status" = "✓"
            set fg_color $prompt_color_tertiary_fixed
        end
        set left_parts $left_parts (_prompt_segment "$git_status" $bg $fg_color)
    end

    set -l left_line (string join " " $left_parts)

    # Right-aligned time pill
    set -l right_line ""
    if test "$prompt_show_time" = true
        set -l time_str (date "+$prompt_time_format")
        set right_line (_prompt_segment "󰴈 $time_str" $bg $prompt_color_secondary)
    end

    if test -n "$right_line"
        set -l cols $COLUMNS
        test -z "$cols"; and set cols (tput cols 2>/dev/null)
        test -z "$cols"; and set cols 80

        set -l left_width (string length -V -- $left_line)
        set -l right_width (string length -V -- $right_line)
        set -l gap (math $cols - $left_width - $right_width)

        if test $gap -lt 1
            set gap 1
        end

        set -l spaces (string repeat -n $gap " ")
        echo "$left_line$spaces$right_line"
    else
        echo $left_line
    end

    # Second line: input prompt
    set -l tertiary_clean (string replace -a "#" "" $prompt_color_tertiary)
    set tertiary_clean (string replace -a '"' '' $tertiary_clean)
    set_color $tertiary_clean
    echo -n "$prompt_symbol "
    set_color normal
end
