from pathlib import Path


def build_list_header(csv_path: Path, last_modified: str) -> str:
    return f'Commands file: {csv_path}\nLast updated: {last_modified}\n'


def generate_rem_bash_function() -> str:
    return '''
### AUTO GENERATED BY REMS TOOL ###
rem() {
    if [ -z "$1" ]; then
        last_cmd=$(HISTTIMEFORMAT= history 2 | head -1 | sed 's/^[ ]*[0-9]*[ ]*//')
        pyrems store "$last_cmd"
    else
        last_cmd=$(HISTTIMEFORMAT= history 2 | head -1 | sed 's/^[ ]*[0-9]*[ ]*//')
        pyrems store "$last_cmd" --note "$*"
    fi
}

rems() {
    if [ $# -eq 0 ]; then
        local output=$(pyrems select)
        local exit_code=$?

        if [ $exit_code -eq 0 ] && [ -n "$output" ]; then
            local cmd=$(echo "$output" | head -1)
            if [ -n "$cmd" ]; then
                history -s "$cmd"
            fi
            echo "$output"
        fi
        return $exit_code
    else
        pyrems "$@"
    fi
}'''

