#!/usr/bin/env python3
"""
Script to replace placeholder pattern images with actual images from scraped content.
"""

import re
from pathlib import Path

# Use a suitable image from scraped content for patterns
# For slider cards, use a construction/project related image
PATTERN_IMAGE = "assets/scraped_images/2-11-600x300.jpg"  # Good size for patterns
# For mission-values background pattern, use a different image
MISSION_PATTERN_IMAGE = "assets/scraped_images/2-11-600x300.jpg"

def fix_placeholder_images(file_path):
    """Replace placeholder images with actual images."""
    encodings = ['utf-8', 'latin-1', 'cp1252', 'iso-8859-1']
    content = None
    
    for encoding in encodings:
        try:
            with open(file_path, 'r', encoding=encoding) as f:
                content = f.read()
            break
        except UnicodeDecodeError:
            continue
    
    if content is None:
        print(f"Could not read {file_path}")
        return False
    
    # Replace mission-values-container background pattern (larger image)
    mission_pattern = r'(<img class="mission-values-container--bgPattern" src=")https://via\.placeholder\.com/800x600/e8e8e8/666666\?text=Pattern(")'
    content = re.sub(mission_pattern, rf'\1{MISSION_PATTERN_IMAGE}\2', content)
    
    # Replace join-us-card pattern images (smaller decorative images)
    # Match with or without quotes, and handle different quote styles
    card_patterns = [
        r'(<img src=")https://via\.placeholder\.com/200x200/e8e8e8/666666\?text=Pattern(")',
        r"(<img src=')https://via\.placeholder\.com/200x200/e8e8e8/666666\?text=Pattern(')",
        r'(<img src=)https://via\.placeholder\.com/200x200/e8e8e8/666666\?text=Pattern(>)',
        r'(src=")https://via\.placeholder\.com/200x200/e8e8e8/666666\?text=Pattern(")',
        r"(src=')https://via\.placeholder\.com/200x200/e8e8e8/666666\?text=Pattern(')",
    ]
    
    for pattern in card_patterns:
        content = re.sub(pattern, rf'\1{PATTERN_IMAGE}\2', content)
    
    # Also check for any other placeholder patterns (catch-all)
    general_pattern = r'https://via\.placeholder\.com/[^"\'>]*\?text=Pattern'
    content = re.sub(general_pattern, PATTERN_IMAGE, content)
    
    # Write back
    try:
        with open(file_path, 'w', encoding='utf-8') as f:
            f.write(content)
        print(f"✓ Updated {file_path}")
        return True
    except Exception as e:
        print(f"Error writing {file_path}: {e}")
        return False

def main():
    """Main function."""
    files_to_update = [
        'join.html',
        'join-ar.html',
        'about.html',
        'about-ar.html',
        'join-Job Opportunities.html',
        'join-Job Opportunities-ar.html'
    ]
    
    for file_path in files_to_update:
        if Path(file_path).exists():
            fix_placeholder_images(file_path)
        else:
            print(f"⚠ File not found: {file_path}")
    
    print("\nDone!")

if __name__ == '__main__':
    main()
