import os
import shutil
import random
import uuid

def shuffle_images_and_create_hierarchy(base_dir):
    """
    Shuffles images in a base directory and creates a 2-level hierarchy with random names.

    Args:
        base_dir (str): The path to the base directory containing images.
    """
    try:
        image_files = [f for f in os.listdir(base_dir) if f.lower().endswith(('.png', '.jpg', '.jpeg', '.gif', '.bmp', '.webp'))]
        random.shuffle(image_files)

        for image_file in image_files:
            source_path = os.path.join(base_dir, image_file)

            # Generate random names for directories and files
            random_dir1 = str(uuid.uuid4())
            random_dir2 = str(uuid.uuid4())
            random_filename = str(uuid.uuid4()) + os.path.splitext(image_file)[1]  # Keep original extension

            # Create the destination directory structure
            dest_dir1 = os.path.join(base_dir, random_dir1)
            dest_dir2 = os.path.join(dest_dir1, random_dir2)

            os.makedirs(dest_dir2, exist_ok=True)

            # Create the destination file path
            dest_path = os.path.join(dest_dir2, random_filename)

            # Move the image to the new location
            shutil.move(source_path, dest_path)

        print(f"Successfully shuffled and moved {len(image_files)} images.")

    except FileNotFoundError:
        print(f"Error: Directory '{base_dir}' not found.")
    except Exception as e:
        print(f"An error occurred: {e}")

if __name__ == "__main__":
    base_directory = input("Enter the path to the base directory containing images: ")

    if not os.path.exists(base_directory):
        print(f"Error: '{base_directory}' does not exist.")
    elif not os.path.isdir(base_directory):
        print(f"Error: '{base_directory}' is not a directory.")
    else:
        shuffle_images_and_create_hierarchy(base_directory)

