# pass_generate
# -------------
# Genera un password aleatorio y lo almacena en el password store.
# Imprime el password generado a stdout.
# Sale con exit code 1 si pass falla.
#
# USO (sourced):
#   source pass_generate.sh
#   pass_generate agentes/nuevo-token 32
#   pass_generate agentes/api-key         # default 24 chars

pass_generate() {
    local entry="$1"
    local length="${2:-24}"

    if [ -z "$entry" ]; then
        echo "pass_generate: se requiere nombre de entrada" >&2
        return 1
    fi

    local output
    output=$(pass generate -f -n "$entry" "$length" 2>&1)
    if [ $? -ne 0 ]; then
        echo "pass_generate: fallo al generar '$entry': $output" >&2
        return 1
    fi

    # pass generate imprime ANSI escape codes + header + password
    # Extraer ultima linea y limpiar escape codes
    echo "$output" | tail -1 | sed 's/\x1b\[[0-9;]*m//g'
}
