"""Module for processing and manipulating the Rizom Data Model class."""

import uuid
from typing import Tuple

from bpy.types import Context, Object

from rizomuvbridge_ultimate.rizom.script_link.rizom_cmds import RizomModel


def find_and_prepare_target_objects(
    context: Context, rizom_model: RizomModel
) -> Tuple[dict[str, Object], list[str]]:
    """Find objects with names that match what is stored in the Rizom model.

    Compare the objects in the scene with the ones in RizomUV. If a match is found
    in the Blender scene, save it to a dictionary with its name as the key, then rename it
    to a unique arbitrary name. This avoids name collisions when importing from Rizom,
    while still allowing objects to be matched by their original names.

    Args:
    ----
        context (Context): The current Blender context.
        rizom_model (RizomModel): The Rizom data model.

    Returns:
    -------
        Tuple[dict[str, Object], list[str]]: Dictionary of names mapping to the object in the scene
        and a list of missing objects that should be in the scene but couldn't be found.

    """
    scene_objects = {}
    missing_objects: list[str] = []
    for obj_name in rizom_model.loaded_objects:
        match = context.scene.objects.get(obj_name)

        if match:
            scene_objects[obj_name] = match
            match.name = str(uuid.uuid4())
        else:
            missing_objects.append(obj_name)

    return scene_objects, missing_objects
