#!/usr/bin/env python3
import re, os, subprocess

BETA = os.path.expanduser('~/beta')
f = os.path.join(BETA, 'app/Controllers/AuthController.php')

with open(f) as fh:
    content = fh.read()

# Show lines with getAuthConnection
lines = content.split('\n')
for i, line in enumerate(lines):
    if 'getAuthConnection' in line:
        print(f'AUTH_LINE {i}: [{line.strip()}]')

# Try different regex patterns
patterns = [
    r'\$(\w+)\s*=\s*\\?Database::getAuthConnection\(\)',
    r'\$(\w+)\s*=\s*\\\\?Database::getAuthConnection\(\)',
    r'\$(\w+)\s*=\s*[\\\\]?Database::getAuthConnection\(\)',
    r'\$(\w+)\s*=\s*.*?getAuthConnection\(\)',
]
for p in patterns:
    v = re.findall(p, content)
    print(f'PATTERN [{p}] => {v}')

# Check Database.php return
df = os.path.join(BETA, 'core/Database.php')
with open(df) as fh:
    dc = fh.read()
if 'return self::$authPdo;' in dc:
    print('DB_RETURN: found self::$authPdo')
elif 'return self::$authPdo' in dc:
    print('DB_RETURN: found without semicolon')
else:
    for line in dc.split('\n'):
        if 'authPdo' in line and 'return' in line:
            print(f'DB_RETURN_LINE: [{line.strip()}]')
