import os
import re

# Order matters! Specific to General.
CONTENT_MAP = [
    # Git URLs - keep them pointing to the new SDK but use original package names if needed
    ('https://github.com/paritytech/polkadot-sdk', 'https://github.com/pezkuwichain/pezkuwi-sdk'),
    ('https://github.com/polkadotchain/polkadot-sdk', 'https://github.com/pezkuwichain/pezkuwi-sdk'),
    
    # Specific Package Renames (from REBRAND_MAP in prompt)
    ('asset-test-utils', 'asset-test-pezutils'),
    ('chain-spec-guide-runtime', 'pez-chain-spec-guide-runtime'),
    ('equivocation-detector', 'pez-equivocation-detector'),
    ('erasure-coding-fuzzer', 'pez-erasure-coding-fuzzer'),
    ('ethereum-standards', 'pez-ethereum-standards'),
    ('finality-relay', 'pez-finality-relay'),
    ('fork-tree', 'pez-fork-tree'),
    ('generate-bags', 'pez-generate-bags'),
    ('kitchensink-runtime', 'pez-kitchensink-runtime'),
    ('messages-relay', 'pez-messages-relay'),
    ('minimal-template-node', 'pez-minimal-template-node'),
    ('minimal-template-runtime', 'pez-minimal-template-runtime'),
    ('node-bench', 'pez-node-bench'),
    ('node-primitives', 'pez-node-primitives'),
    ('node-rpc', 'pez-node-rpc'),
    ('node-runtime-generate-bags', 'pez-node-runtime-generate-bags'),
    ('node-template-release', 'pez-node-template-release'),
    ('node-testing', 'pez-node-testing'),
    ('penpal-emulated-chain', 'pez-penpal-emulated-chain'),
    ('penpal-runtime', 'pez-penpal-runtime'),
    ('remote-ext-tests-bags-list', 'pez-remote-ext-tests-bags-list'),
    ('revive-dev-node', 'pez-revive-dev-node'),
    ('revive-dev-runtime', 'pez-revive-dev-runtime'),
    ('slot-range-helper', 'pez-slot-range-helper'),
    ('solochain-template-node', 'pez-solochain-template-node'),
    ('solochain-template-runtime', 'pez-solochain-template-runtime'),
    ('subkey', 'pez-subkey'),
    ('template-zombienet-tests', 'pez-template-zombienet-tests'),
    ('test-runtime-constants', 'peztest-runtime-constants'),
    ('tracing-gum', 'pez-tracing-gum'),
    ('tracing-gum-proc-macro', 'pez-tracing-gum-proc-macro'),
    
    # Core Terminology
    ('Polkadot SDK', 'Pezkuwi SDK'),
    ('polkadot-sdk', 'pezkuwi-sdk'),
    ('Polkadot', 'Pezkuwi'),
    ('polkadot', 'pezkuwi'),
    ('Substrate', 'Bizinikiwi'),
    ('substrate', 'bizinikiwi'),
    ('Cumulus', 'Pezcumulus'),
    ('cumulus', 'pezcumulus'),
    ('Parachain', 'Teyrchain'),
    ('parachain', 'teyrchain'),
    ('Rococo', 'Pezkuwichain'),
    ('rococo', 'pezkuwichain'),
    ('Westend', 'Zagros'),
    ('westend', 'zagros'),
    ('DOT', 'HEZ'),
    ('WND', 'ZGR'),
    ('ROC', 'TYR'),
    ('PARACHAIN', 'TEYRCHAIN'),
    ('POLKADOT', 'PEZKUWI'),
    
    # Prefix Rebranding (Lower)
    (r'\bframe-', 'pezframe-'),
    (r'\bpallet-', 'pezpallet-'),
    (r'\bsp-', 'pezsp-'),
    (r'\bsc-', 'pezsc-'),
    (r'\bcumulus-', 'pezcumulus-'),
    
    # Prefix Rebranding (Underscore for code)
    (r'\bframe_', 'pezframe_'),
    (r'\bpallet_', 'pezpallet_'),
    (r'\bsp_', 'pezsp_'),
    (r'\bsc_', 'pezsc_'),
    (r'\bcumulus_', 'pezcumulus_'),
    
    # Capitalized variants
    (r'\bFrame', 'Pezframe'),
    (r'\bPallet', 'Pezpallet'),
]

# File renaming map
FILE_RENAME_MAP = [
    ('polkadot', 'pezkuwi'),
    ('substrate', 'bizinikiwi'),
    ('cumulus', 'pezcumulus'),
    ('parachain', 'teyrchain'),
    ('rococo', 'pezkuwichain'),
    ('westend', 'zagros'),
    ('frame', 'pezframe'),
    ('pallet', 'pezpallet'),
    ('sp-', 'pezsp-'),
    ('sc-', 'pezsc-'),
]

EXTENSIONS = ('.rs', '.toml', '.adoc', '.md', '.sh', '.yml', '.yaml', '.json')

def rebrand_content(content):
    # First, protect existing "pez" prefixes to avoid "pezpez"
    # Actually, the regex \b already helps, but let's be safer.
    for pattern, replacement in CONTENT_MAP:
        if pattern.startswith(r'\b'):
            content = re.sub(pattern, replacement, content)
        else:
            content = content.replace(pattern, replacement)
    
    # Cleanup any "pezpez" accidental double branding
    content = content.replace('pezpez', 'pez')
    content = content.replace('PezPez', 'Pez')
    return content

def main():
    # 1. Content Rebrand
    for root, dirs, files in os.walk('.'):
        if '.git' in root or 'target' in root: continue
        for file in files:
            if file.endswith(EXTENSIONS):
                path = os.path.join(root, file)
                try:
                    with open(path, 'r', encoding='utf-8') as f:
                        content = f.read()
                    new_content = rebrand_content(content)
                    if content != new_content:
                        with open(path, 'w', encoding='utf-8') as f:
                            f.write(new_content)
                except: pass

    # 2. Filename Rebrand (bottom-up)
    for root, dirs, files in os.walk('.', topdown=False):
        if '.git' in root or 'target' in root: continue
        
        for name in files + dirs:
            if name in ('.git', 'target', '.', '..'): continue
            new_name = name
            for old, new in FILE_RENAME_MAP:
                if old in new_name:
                    new_name = new_name.replace(old, new)
            
            # Cleanup double pez in filenames
            new_name = new_name.replace('pezpez', 'pez')
            
            if new_name != name:
                os.rename(os.path.join(root, name), os.path.join(root, new_name))

if __name__ == "__main__":
    main()
