Aligning Plane to Selected Cube face

Hi
I want to transfer the plan to the selected face
But since I want to do this from Python, I have a problem with the following code
I use the following code and I want to select the faces with Python

Thanks for guiding me


import bpy
import bmesh
from mathutils import Vector, Matrix
from random import randint






obj = bpy.data.objects['Plane'] 
me = obj.data
bm = bmesh.from_edit_mesh(me)
bm.faces[0].select = True 
bmesh.update_edit_mesh(me, True)


obj = bpy.data.objects['Cube'] 
me = obj.data
bm = bmesh.from_edit_mesh(me)
bm.faces[2].select = True 
bmesh.update_edit_mesh(me, True)





context = bpy.context  
scene = context.scene





A = bpy.data.objects['Plane']




B = bpy.data.objects['Cube']
me = B.data    

bm = bmesh.from_edit_mesh(me)


src_mw = A.matrix_world.copy()
src_bm = bmesh.from_edit_mesh(A.data)
src_face = src_bm.select_history.active
src_o = src_face.calc_center_median()
src_normal = src_face.normal
src_tan = src_face.calc_tangent_edge()



# This is the target, we change the sign of normal to stick face to face
dst_mw = B.matrix_world.copy()
dst_bm = bmesh.from_edit_mesh(B.data)
dst_face = dst_bm.select_history.active
dst_o = dst_face.calc_center_median()
dst_normal = -(dst_face.normal)
dst_tan = (dst_face.calc_tangent_edge())

vec2 = src_normal @ src_mw.inverted()
matrix_rotate = dst_normal.rotation_difference(vec2).to_matrix().to_4x4()

vec1 = src_tan @ src_mw.inverted()
dst_tan = dst_tan @ matrix_rotate.inverted()
mat_tmp = dst_tan.rotation_difference(vec1).to_matrix().to_4x4()
matrix_rotate = mat_tmp @ matrix_rotate
matrix_translation = Matrix.Translation(src_mw @ src_o)



# This line applied the matrix_translation and matrix_rotate
B.matrix_world = matrix_translation @ matrix_rotate.to_4x4()

# We need to recalculate these value since we change the matrix_world
dst_mw = B.matrix_world.copy()
dst_bm = bmesh.from_edit_mesh(B.data)
dst_face = dst_bm.select_history.active
dst_o = dst_face.calc_center_median()

# The following is telling blender to find a translation from face center to origin,
# And than apply it on world matrix 
# Be Careful, the order of the matrix multiplication change the result,
# We always put the transform matrix on "Left Hand Side" to perform the task
dif_mat = Matrix.Translation(B.location - dst_mw @ dst_o)
B.matrix_world = dif_mat @ B.matrix_world