#!/usr/bin/env python3
"""Migration v3: broader patterns for auth_db.users -> syndicos.auth_users"""
import re, os, subprocess

BETA = os.path.expanduser('~/beta')

def find_files():
    r = subprocess.run(['grep','-rln','getAuthConnection',BETA,'--include=*.php'],
                       capture_output=True, text=True)
    return sorted(f for f in r.stdout.strip().split('\n')
                  if f and 'vendor/' not in f and 'Controllers/Controllers/' not in f)

# Auth variable names used as local vars or properties
AUTH_PATTERNS = [
    'dbAuth', 'pdoAuth', 'authPdo', 'auth',
]

def has_auth_context(line):
    """Check if this line uses an auth PDO variable or is a Database::q with auth variable."""
    # Direct method calls: $pdoAuth->prepare, $this->dbAuth->query, etc.
    for p in AUTH_PATTERNS:
        if '$' + p + '->' in line or '$this->' + p + '->' in line:
            return True
    # Static helper: Database::q($pdoAuth, ...) or \Database::q($this->dbAuth, ...)
    if 'Database::q(' in line:
        for p in AUTH_PATTERNS:
            if '$' + p in line or '$this->' + p in line:
                return True
    # $pdo-> where $pdo was assigned from getAuthConnection (broader)
    if '$pdo->' in line or '$pdo ' in line:
        return False  # $pdo is ambiguous, skip (handled by v1 script)
    return False

def replace_users(line):
    """Replace SQL table name 'users' with 'auth_users' in auth context."""
    nl = line
    nl = re.sub(r'FROM users\b', 'FROM auth_users', nl)
    nl = re.sub(r'INTO users\b', 'INTO auth_users', nl)
    nl = re.sub(r'INTO users\(', 'INTO auth_users(', nl)
    nl = re.sub(r'UPDATE users\b', 'UPDATE auth_users', nl)
    nl = re.sub(r'JOIN users\b', 'JOIN auth_users', nl)
    nl = re.sub(r'COUNT\(\*\) FROM users', 'COUNT(*) FROM auth_users', nl)
    return nl

def migrate_file(path):
    with open(path) as f:
        content = f.read()
    
    # Also detect locally-scoped auth variables
    local_auth_vars = set(re.findall(r'\$(\w+)\s*=\s*.*?getAuthConnection\(\)', content))
    
    lines = content.split('\n')
    changes = 0
    for i, line in enumerate(lines):
        if 'users' not in line:
            continue
        # Check named auth patterns
        do_replace = has_auth_context(line)
        # Check local vars found from getAuthConnection
        if not do_replace:
            for v in local_auth_vars:
                if '$' + v + '->' in line or '$' + v + ' ->' in line:
                    do_replace = True
                    break
            # Also check Database::q($localvar, ...) pattern
            if not do_replace and 'Database::q(' in line:
                for v in local_auth_vars:
                    if '$' + v in line:
                        do_replace = True
                        break
        
        if do_replace:
            nl = replace_users(line)
            if nl != line:
                lines[i] = nl
                changes += 1
    
    if changes:
        with open(path, 'w') as f:
            f.write('\n'.join(lines))
    return changes

def main():
    print("=== Migration v3: broader patterns ===")
    files = find_files()
    print(f"Processing {len(files)} files...")
    
    total = 0
    for path in files:
        if path.endswith('Database.php'):
            continue
        n = migrate_file(path)
        if n:
            rel = path.replace(BETA + '/', '')
            print(f"  OK {rel} ({n})")
            total += n
    
    print(f"\nTotal: {total} replacements")
    
    # Verify: any remaining auth + FROM users?
    print("\nVerification...")
    issues = 0
    for path in files:
        if path.endswith('Database.php'):
            continue
        with open(path) as f:
            c = f.read()
        for i, line in enumerate(c.split('\n')):
            if has_auth_context(line) and re.search(r'(?:FROM|INTO|UPDATE|JOIN)\s+users\b', line):
                rel = path.replace(BETA + '/', '')
                print(f"  REMAINING {rel}:{i+1}: {line.strip()[:120]}")
                issues += 1
            # Also check local vars
            local_vars = set(re.findall(r'\$(\w+)\s*=\s*.*?getAuthConnection\(\)', c))
            for v in local_vars:
                if ('$' + v + '->' in line or '$' + v + ' ->' in line) and re.search(r'(?:FROM|INTO|UPDATE|JOIN)\s+users\b', line):
                    rel = path.replace(BETA + '/', '')
                    print(f"  REMAINING {rel}:{i+1}: {line.strip()[:120]}")
                    issues += 1
    
    if not issues:
        print("  All clear!")
    else:
        print(f"  {issues} remaining")

if __name__ == '__main__':
    main()
