#!/usr/bin/env python3
"""Body-extract tender-bearing emails. Clean version."""
import json, base64, re, urllib.request, html as htmllib

tok = json.load(open('/root/.hermes/google_token.json'))
HEADERS = {'Authorization': f"Bearer {tok['access_token']}"}

def fetch(mid):
    req = urllib.request.Request(
        f'https://gmail.googleapis.com/gmail/v1/users/me/messages/{mid}?format=full',
        headers=HEADERS,
    )
    with urllib.request.urlopen(req, timeout=30) as r:
        return json.loads(r.read())

def part_text(part):
    data = (part.get('body') or {}).get('data')
    if data:
        raw = data + '=' * (-len(data) % 4)
        decoded = base64.urlsafe_b64decode(raw).decode('utf-8', 'replace')
        return decoded
    return ''

def extract_plain(payload):
    s = part_text(payload)
    if s and 'must be viewed in HTML mode' not in s.lower():
        return s
    for p in payload.get('parts') or []:
        t = extract_plain(p)
        if t and 'must be viewed in HTML mode' not in t.lower():
            return t
    return ''

def extract_html(payload):
    if (payload.get('mimeType') or '') == 'text/html':
        s = part_text(payload)
        if s: return s
    for p in payload.get('parts') or []:
        t = extract_html(p)
        if t: return t
    return ''

def html_to_text(h):
    t = re.sub(r'<style[^>]*>.*?</style>', '', h, flags=re.S | re.I)
    t = re.sub(r'<[^>]+>', ' ', t)
    t = htmllib.unescape(t)
    return re.sub(r'\s+', ' ', t).strip()

data = json.load(open('/tmp/morning_fetch.json'))
id_subj = {e['id']: e['subject'] for e in data['emails']}

targets = []
for e in data['emails']:
    frm = (e.get('from') or '').lower()
    if 'liam.fitzgerald@lfcs' in frm or 'docs@estimateone' in frm:
        targets.append(e)

for e in targets[:7]:
    mid = e['id']
    try:
        full = fetch(mid)
        p = full.get('payload') or {}
        body = extract_plain(p)
        if not body:
            h = extract_html(p)
            if h: body = 'HTML: ' + html_to_text(h)[:800]
    except Exception as ex:
        body = f'(fetch failed: {type(ex).__name__}: {ex})'
    print(f"\n=== {e['subject']}")
    print(f"FROM: {e['from']}")
    print(f"BODY[{len(body)} chars]:")
    print(body[:1500])
