from BunnyCDN.Storage import Storage
import os, uuid, config, funcs
from datetime import datetime
from PIL import Image

def dump_facebook(folder_path):
    for folder in os.listdir(folder_path):
        if os.path.isdir(os.path.join(folder_path, folder)):
            username = folder
            
            for filename in os.listdir(os.path.join(folder_path, folder)):
                filepath = os.path.join(folder_path, folder, filename)
                
                upload_file(username=username, filepath=filepath)
                
def upload_file(filepath, username):
    filename = os.path.basename(filepath)
    media_id = filename.split('.')[0]
    
    file_extension = os.path.splitext(filename)[1].lower()

    media_type = funcs.get_media_type(filename)

    file_hash = funcs.calculate_file_hash(filepath)

    duration = funcs.get_video_duration(filepath) if media_type == 'video' else 0
    
    width, height = funcs.get_video_dimensions(filepath) if media_type == 'video' else Image.open(filepath).size


    dirtype = funcs.determine_post_type(filepath, media_type)
    server_path = os.path.join('media', dirtype, username, f'{media_id}{file_extension}')
        
    obj_storage.PutFile(filepath, server_path)

    file_url = f"https://storysave.b-cdn.net/{server_path}"

    if file_hash in existing_files:
        print('Duplicate file detected. Removing...')
        os.remove(filepath)
        return False

    query = "INSERT IGNORE INTO media (username, media_type, media_url, width, height, platform, hash, filename, duration, media_id) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s)"
    values = (username, media_type, file_url, width, height, 'tiktok', file_hash, filename, duration, media_id)

    newCursor.execute(query, values)
    newDB.commit()
    print(f'[{newCursor.rowcount}] records updated. File {filename} uploaded to {file_url}')

    if newCursor.rowcount > 0:
        os.remove(filepath)

    return True

if __name__ == '__main__':
    print('Starting processing...')

    newDB, newCursor = config.gen_connection()

    obj_storage = Storage('345697f9-d9aa-4a6b-a5ec8bffc16d-ceaf-453e', 'storysave')

    newCursor.execute("SELECT hash FROM media WHERE platform='tiktok' AND hash IS NOT NULL")
    existing_files = [image[0] for image in newCursor.fetchall()]

    dump_facebook('tiktok/')

    print("Processing completed.")