#!/usr/bin/env python3
"""
Bulk text replacement script for SureCertificate website.
Safe replacement of brand names, emails, phone numbers.
"""
import os
import re

ROOT = "/Applications/XAMPP/xamppfiles/htdocs/aaa"

SKIP_DIRS = {
    ".git", ".cursor", "Archive.zip", "__website-next", "_next", "_https_",
    "assets", "infinity_images", "cdn-cgi", "moengage", "website_next_favicons"
}
SKIP_FILES = {"bulk_replace.py", "bulk_fix_all.py", "bulk_fast.py", "bulk_refactor.py"}

def should_process(path):
    if not path.endswith(".php"):
        return False
    name = os.path.basename(path)
    if name in SKIP_FILES:
        return False
    for skip in SKIP_DIRS:
        if skip in path:
            return False
    return True

def fix_text(text):
    # Brand name replacements (case variations)
    text = re.sub(r'KnowledgeHut', 'SureCertificate', text)
    text = re.sub(r'Knowledgehut', 'SureCertificate', text)
    text = re.sub(r'knowledgehut\.com', 'surecertificate.com', text)
    # Only replace standalone lowercase knowledgehut, not inside URLs that might break
    text = re.sub(r'\bknowledgehut\b', 'surecertificate', text)
    
    # Email replacements
    text = re.sub(r'info@surecertificate\.online', 'Admin@mytechcertificate', text)
    text = re.sub(r'support@surecertificate\.online', 'Admin@mytechcertificate', text)
    text = re.sub(r'contact@surecertificate\.online', 'Admin@mytechcertificate', text)
    text = re.sub(r'noreply@surecertificate\.online', 'Admin@mytechcertificate', text)
    
    # WhatsApp number fix
    text = re.sub(r'https://wa\.me/16816772366', 'https://wa.me/919354009315', text)
    text = re.sub(r'wa\.me/16816772366', 'wa.me/919354009315', text)
    text = re.sub(r'tel:\+918800000000', 'tel:+18887658856', text)
    
    # Fix some common broken scraped paths
    text = re.sub(r'https://www\.surecertificate\.com/', '', text)
    
    return text

def main():
    changed = 0
    scanned = 0
    for dirpath, dirnames, filenames in os.walk(ROOT):
        dirnames[:] = [d for d in dirnames if d not in SKIP_DIRS and not d.startswith('.')]
        for fname in filenames:
            fpath = os.path.join(dirpath, fname)
            if should_process(fpath):
                scanned += 1
                try:
                    with open(fpath, "r", encoding="utf-8", errors="ignore") as f:
                        content = f.read()
                except Exception:
                    continue
                new_content = fix_text(content)
                if new_content != content:
                    try:
                        with open(fpath, "w", encoding="utf-8") as f:
                            f.write(new_content)
                        changed += 1
                        if changed <= 20 or changed % 100 == 0:
                            print(f"CHANGED: {fpath}")
                    except Exception as e:
                        print(f"ERROR writing {fpath}: {e}")
    print(f"\nDone. Scanned {scanned} files, changed {changed} files.")

if __name__ == "__main__":
    main()
