import os
import random
import re

# Curated Unsplash photo IDs by category 
# Use auto=format&fit=crop&w=800&q=80 for all to match sizing
# These are real, high-quality Unsplash image IDs for these domains
CATEGORIES = {
    'ai': [
        '1677442136019-21780ecad995', '1620712943543-bcc4688e7485', '1550751827-4bd374c3f58b',
        '1518770660439-4636190af475', '1485290334039-a3c69043e517', '1526374965328-7f61d4dc18c5'
    ],
    'management': [
        '1542744173-8e7e53415bb0', '1552664730-d307ca884978', '1517245386807-bb43f82c33c4',
        '1553877522-43ce6dc3bb46', '1454165804606-c3d57bc86b40', '1531482615713-2afd69097998'
    ],
    'data': [
        '1551288049-bebda4e38f71', '1504868584819-f8e8b4ecca53', '1460925895917-afdab827c52f',
        '1518186285589-2f7649de83e0', '1666887360680-692ab8dcc6de'
    ],
    'cloud': [
        '1451187580459-43490279c0fa', '1550751827-4bd374c3f58b', '1457305237443-44c3d5a30b89',
        '1544197150-b99a580bb7a8', '1614064000300-47b2c5dc44bd'
    ],
    'security': [
        '1550751827-4bd374c3f58b', '1526374965328-7f61d4dc18c5', '1510511459019-1d227f4220b3',
        '1563206767541-3eb2c5952d76', '1614064000300-47b2c5dc44bd'
    ],
    'programming': [
        '1498050108023-c5249f4df085', '1555066931-4365d14bab8c', '1517694712202-14dd9538aa97',
        '1525547719571-a2d4ac8945e2', '1461749280684-dccba630e2f6'
    ],
    'generic': [
        '1522071820081-009f0129c71c', '1497215968147-3f36c5df5ed3', '1516321318423-f06f85e504b3',
        '1486312338219-ce68d2c6f44d', '1523961131990-521072f1616c'
    ]
}

def get_category_by_path(path):
    p = path.lower()
    if 'ai' in p or 'artificial' in p or 'machine-learning' in p: return 'ai'
    if 'agile' in p or 'scrum' in p or 'management' in p or 'safe' in p: return 'management'
    if 'data' in p or 'analytics' in p or 'bi-' in p: return 'data'
    if 'cloud' in p or 'aws' in p or 'azure' in p or 'devops' in p: return 'cloud'
    if 'cyber' in p or 'security' in p or 'hacking' in p: return 'security'
    if 'programming' in p or 'web' in p or 'software' in p: return 'programming'
    return 'generic'

target_dir = '/Applications/XAMPP/xamppfiles/htdocs/aaa'
count = 0

for root, dirs, files in os.walk(target_dir):
    for f in files:
        if f.endswith('.php'):
            path = os.path.join(root, f)
            with open(path, 'r', encoding='utf-8', errors='ignore') as file:
                content = file.read()
            
            # Find images that look like course banners
            # e.g., src="https://images.unsplash.com/photo-1516321318423-f06f85e504b3..."
            if 'images.unsplash.com/photo-1516321318423-f06f85e504b3' in content:
                # determine category
                rel_path = os.path.relpath(path, target_dir)
                cat = get_category_by_path(rel_path)
                
                # Pick a random image ID using the file path as a seed so it's stable per file
                import hashlib
                seed = int(hashlib.md5(rel_path.encode()).hexdigest(), 16)
                local_rand = random.Random(seed)
                selected_id = local_rand.choice(CATEGORIES[cat])
                
                new_url = f"https://images.unsplash.com/photo-{selected_id}?ixlib=rb-4.0.3&auto=format&fit=crop&w=800&q=80"
                
                new_content = re.sub(
                    r'https://images\.unsplash\.com/photo-1516321318423-[a-zA-Z0-9]+[^\"\'\>\s]*',
                    new_url,
                    content
                )
                
                if new_content != content:
                    with open(path, 'w', encoding='utf-8') as write_file:
                        write_file.write(new_content)
                    count += 1

print(f"Replaced images in {count} course files.")
