import os
import re
import hashlib
import random

target_dir = '/Applications/XAMPP/xamppfiles/htdocs/aaa'
file_count = 0
image_count = 0

# A curated list of 60 high-quality Unsplash image IDs related to education, course, business, learning, coding, tech
EDUCATION_IDS = [
    '1513258496099-48135e5d3c8f', '1522202176988-66273c2fd55f', '1503676260728-1c00da094a0b', '1454165804606-c3d57bc86b40', '1552664730-d307ca884978',
    '1517245386807-bb43f82c33c4', '1524178232363-1fb2b075b655', '1498050108023-c5249f4df085', '1555066931-4365d14bab8c', '1517694712202-14dd9538aa97', 
    '1525547719571-a2d4ac8945e2', '1461749280684-dccba630e2f6', '1551288049-bebda4e38f71', '1504868584819-f8e8b4ecca53', '1460925895917-afdab827c52f', 
    '1518186285589-2f7649de83e0', '1666887360680-692ab8dcc6de', '1451187580459-43490279c0fa', '1550751827-4bd374c3f58b', '1457305237443-44c3d5a30b89', 
    '1544197150-b99a580bb7a8', '1614064000300-47b2c5dc44bd', '1510511459019-1d227f4220b3', '1563206767541-3eb2c5952d76', '1523240795612-9a054b0db644', 
    '1521737604893-d14cc237f11d', '1516321318423-f06f85e504b3', '1486312338219-ce68d2c6f44d', '1523961131990-521072f1616c', '1497215968147-3f36c5df5ed3', 
    '1531482615713-2afd69097998', '1553877522-43ce6dc3bb46', '1478104724775-8bd31ff3e44b', '1581091226825-a6a2a5aee158', '1550133730-695473e544be', 
    '1515169061895-37fbb16acd37', '1540316687405-b04ebbbddfb1', '1501504905252-473c47e087f8', '1508228518973-455b550cd00b', '1532012197267-da84d127e765', 
    '1488190211105-8b0e84b7e8d1', '1517048676732-dce42f022dc7', '1515378791009-187515dbb2f3', '1531538398863-7fc8db5d8f63', '1488190211105-8b0e84b7e8d1',
    '1522071820081-009f0129c71c', '1513258496099-48135e5d3c8f', '1499540633125-bd2b2f65a7e6', '1522202176988-66273c2fd55f', '1544531586-fde5298cdd40',
    '1552664730-d307ca884978', '1542744173-8e7e53415bb0', '1535905557-58cb6c7a44aa', '1523240795612-9a054b0db644', '1581091226825-a6a2a5aee158'
]

def process_img_tag(match, path):
    global image_count
    img_tag_original = match.group(0)
    
    # We want to replace images that are either picsum.photos, image.pollinations.ai, source.unsplash.com, or already generic images.unsplash.com
    is_broken_or_generic = (
        'picsum.photos' in img_tag_original or 
        'image.pollinations.ai' in img_tag_original or 
        'source.unsplash.com' in img_tag_original or 
        'images.unsplash.com' in img_tag_original
    )
    
    if not is_broken_or_generic:
        return img_tag_original
        
    alt_match = re.search(r'alt="([^"]+)"', img_tag_original)
    if alt_match:
        prompt = alt_match.group(1).strip()
    else:
        prompt = os.path.splitext(os.path.basename(path))[0]
        
    # generate a stable seed based on file path and prompt
    seed_str = f"{path}_{prompt}"
    seed = int(hashlib.md5(seed_str.encode()).hexdigest()[:8], 16)
    
    # Use random with seed to pick one of the curated education/course IDs
    local_rand = random.Random(seed)
    selected_id = local_rand.choice(EDUCATION_IDS)
    
    new_src = f'https://images.unsplash.com/photo-{selected_id}?ixlib=rb-4.0.3&auto=format&fit=crop&w=1000&q=80'
    
    # replace the src
    new_tag = re.sub(
        r'src=[\"\']https?://(?:picsum\.photos/|image\.pollinations\.ai/|source\.unsplash\.com/|images\.unsplash\.com/)[^\"\']+[\"\']', 
        f'src="{new_src}"', 
        img_tag_original
    )
    
    if new_tag != img_tag_original:
        image_count += 1
        return new_tag
        
    return img_tag_original

for root, dirs, files in os.walk(target_dir):
    for f in files:
        if f.endswith('.php') or f.endswith('.html'):
            path = os.path.join(root, f)
            
            with open(path, 'r', encoding='utf-8', errors='ignore') as file:
                content = file.read()
            
            new_content = re.sub(
                r'<img[^>]+>', 
                lambda m: process_img_tag(m, path), 
                content
            )
            
            if new_content != content:
                with open(path, 'w', encoding='utf-8') as write_file:
                    write_file.write(new_content)
                file_count += 1

print(f"Updated {image_count} images in {file_count} files across the website to professional education/course Unsplash imagery.")
