from funcs import get_files  # Assuming this is defined elsewhere
from PIL import Image
import imagehash
import config
import os

def generate_image_phash(filepath, hash_size=8):
    try:
        # Open the image using PIL
        pil_image = Image.open(filepath)
        
        # Compute pHash using the imagehash library
        phash = imagehash.phash(pil_image, hash_size=hash_size)
        return phash
    except Exception as e:
        print(f"Error processing image {filepath}: {e}")
        return None

def are_phashes_duplicates(phash1, phash2, threshold=5):
    try:
        # Compute the Hamming distance between the pHashes
        distance = phash1 - phash2
        return distance <= threshold
    except TypeError as e:
        print(f"Error comparing pHashes: {e}")
        return False

def get_media_by_phash(phash, username, existing_medias, threshold=5):
    for media in existing_medias:
        existing_phash_str = media[1]

        # existing_username = media[2]
        # if existing_username != username:
        #     continue  # Only compare with the same user's media
        
        # Convert stored pHash string to ImageHash object
        existing_phash = imagehash.hex_to_hash(existing_phash_str)

        # Check if the current pHash is a duplicate
        if are_phashes_duplicates(phash, existing_phash, threshold=threshold):
            return media
    return None

# Database connection
db, cursor = config.gen_connection()

directory = 'check_if_exists'  # Directory containing user images

# Fetch existing media with pHashes (assuming media are images, adjust media_type if needed)
cursor.execute("SELECT id, phash, username FROM media WHERE media_type = %s AND phash IS NOT NULL", ['image'])
existing_medias = cursor.fetchall()

existing_phashes = [media[1] for media in existing_medias]

# Go through the directory folder where each subfolder is a username
users = os.listdir(directory)

for username in users:
    user_images_path = os.path.join(directory, username)
    if not os.path.isdir(user_images_path):
        continue  # Skip non-directory files

    # Get all images for the current user
    images = get_files(user_images_path)  # Assuming this gets all image files

    for filepath in images:
        image_filename = os.path.basename(filepath)
        print(f'Processing {image_filename}...')

        # Generate pHash for the image
        phash = generate_image_phash(filepath, hash_size=8)
        if phash is None:
            continue  # Skip this image if there's an issue

        phash_str = str(phash)

        if phash_str not in existing_phashes:
            print(f'No duplicate found for {image_filename}')
            continue

        # Check if the image is a duplicate of any in the database
        duplicate_media = get_media_by_phash(phash, username, existing_medias, threshold=5)
        if duplicate_media:
            found_username = duplicate_media[2]
            print(f'Duplicate found: https://altpins.com/pin/{duplicate_media[0]}')
            print(f'Duplicate image path: {filepath}')
            newpath = os.path.join('duplicates', found_username, image_filename)
            os.makedirs(os.path.dirname(newpath), exist_ok=True)
            os.rename(filepath, newpath)
            print(f'Moved {image_filename} to duplicates/')