import subprocess, json, re, html as htmlmod

TENANT = 'dd4a82e8-85a3-44ac-8800-07945ab4d95f'
CLIENT_ID = 'fabb3421-8b34-484b-bc17-e46de9703418'
CLIENT_SECRET = '~QJ8Q~NyQSs4OcGqHZyPrA2CVnq9KBfKiimntbMO'
USER = 'barbara@bardach.net'

r = subprocess.run(['curl', '-s', '-X', 'POST',
    f'https://login.microsoftonline.com/{TENANT}/oauth2/v2.0/token',
    '-d', f'client_id={CLIENT_ID}', '-d', f'client_secret={CLIENT_SECRET}',
    '-d', 'scope=https://graph.microsoft.com/.default', '-d', 'grant_type=client_credentials'],
    capture_output=True, text=True)
token = json.loads(r.stdout)['access_token']

# Test with a real estate agent who likely has phone in signature
email = 'brandonlopez@longrealty.com'
r2 = subprocess.run(['curl', '-s', '-G',
    f'https://graph.microsoft.com/v1.0/users/{USER}/messages',
    '--data-urlencode', f'$search="from:{email}"',
    '--data-urlencode', '$select=subject,from,body',
    '--data-urlencode', '$top=1',
    '-H', f'Authorization: Bearer {token}',
    '-H', 'Content-Type: application/json',
    '-H', 'ConsistencyLevel: eventual'],
    capture_output=True, text=True)

data = json.loads(r2.stdout)
if 'value' in data and data['value']:
    body = data['value'][0].get('body',{}).get('content','')
    # Strip HTML
    text = re.sub(r'<br\s*/?>', '\n', body, flags=re.IGNORECASE)
    text = re.sub(r'</?(?:p|div|tr|td|li|blockquote)[^>]*>', '\n', text, flags=re.IGNORECASE)
    text = re.sub(r'<[^>]+>', '', text)
    text = htmlmod.unescape(text)

    # Show last 1500 chars
    print(f"=== Stripped text tail (last 1500 chars) ===")
    print(text[-1500:])

    # Search for phone patterns
    phone_re = re.compile(r'[\(]?\d{3}[\)\s.\-]?\s?\d{3}[\s.\-]?\d{4}')
    phones = phone_re.findall(text)
    print(f"\n=== Phone numbers found: {phones} ===")

    labeled_re = re.compile(r'(?:Tel|Phone|Cell|Mobile|Office|Direct|Fax)[:\s]*\(?\d{3}\)?[\s.\-]?\d{3}[\s.\-]?\d{4}', re.IGNORECASE)
    labeled = labeled_re.findall(text)
    print(f"=== Labeled phones: {labeled} ===")
