from funcs import generate_phash
import os

def find_duplicates(source_dir, target_dir, extensions, max_distance):
    """Remove duplicates in target_dir that are found in source_dir based on Hamming distance."""
    source_files = {}
    target_files = {}

    # Helper function to filter files by extension
    def filter_files(files):
        return [f for f in files if os.path.splitext(f)[1].lower() in extensions]

    # Build hash map of source directory
    for dirpath, _, filenames in os.walk(source_dir):
        for filename in filter_files(filenames):
            filepath = os.path.join(dirpath, filename)
            filehash = generate_phash(filepath, str=False)
            if filehash:
                source_files[filehash] = filepath

    # Build hash map of target directory and compare
    for dirpath, _, filenames in os.walk(target_dir):
        for filename in filter_files(filenames):
            filepath = os.path.join(dirpath, filename)
            filehash = generate_phash(filepath, str=False)
            if not filehash:
                continue

            # Check if this file is similar to any of the source files
            is_duplicate = False
            for source_hash in source_files.keys():
                distance = filehash - source_hash  # Hamming distance
                if distance <= max_distance:
                    is_duplicate = True
                    break  # Found a duplicate

            if is_duplicate:
                newpath = os.path.join('duplicates', filename)
                os.makedirs(os.path.dirname(newpath), exist_ok=True)
                os.rename(filepath, newpath)
                print(f"Moved duplicate: {filepath} to duplicates/ (distance: {distance})")
            else:
                target_files[filehash] = filepath

if __name__ == '__main__':
    # Paths to the directories
    source_dir = 'D:/Crawlers/media/Coomer/sadierayxo'
    target_dir = 'sorted/sadierayxo'

    # List of accepted extensions
    extensions = {'.png', '.jpg', '.jpeg', '.webp', '.gif'}

    # Maximum Hamming distance to consider as duplicates
    MAX_DISTANCE = 5  # Adjust this threshold as needed

    find_duplicates(source_dir, target_dir, extensions, MAX_DISTANCE)

    print("Duplicate removal process completed.")