import os, funcs
from funcs import generate_phash

def get_username(image, ready_images):
    for ready_image in ready_images:
        if os.path.basename(image) in ready_image:
            ready_image = ready_image.replace('\\', '/')
            return ready_image.split('/')[1]
    return None
    
ready_images = funcs.get_files('ready_to_upload')
ready_images = [image for image in ready_images if not image.endswith('.mp4')]

sorted_images = funcs.get_files('sorted')
sorted_images = [image for image in sorted_images if not image.endswith('.mp4')]

os.makedirs('already_processed', exist_ok=True)

for image in sorted_images:
    image = image.replace('\\', '/')
    username = image.split('/')[1]
    filename = os.path.basename(image)
    
    for ready_image in ready_images:
        if filename in ready_image:
            username = get_username(image, ready_images)
            newpath = ready_image.replace('ready_to_upload', 'already_processed')
            os.makedirs(os.path.dirname(newpath), exist_ok=True)
            print(f'Moving {image} which is a match for {ready_image} to already_processed')
            os.rename(image, newpath)
            print(f'Moved {ready_image} to already_processed')
            break