import base64, json, os, sys, urllib.parse, urllib.request, urllib.error
from datetime import datetime, timezone

TOKEN_URL = "https://oauth2.googleapis.com/token"
API = "https://gmail.googleapis.com/gmail/v1/users/me"


def _cfg():
    return json.loads(os.environ.get("INTEGRATION_SECRETS", "{}"))


def _token():
    cfg = _cfg()
    refresh = str(cfg.get("refresh_token") or "")
    if not refresh:
        raise Exception("refresh_token is not set — run gmail-single-user-exchange-code and paste it into the config")
    data = urllib.parse.urlencode({
        "client_id": str(cfg.get("client_id") or ""),
        "client_secret": str(cfg.get("client_secret") or ""),
        "refresh_token": refresh,
        "grant_type": "refresh_token",
    }).encode("utf-8")
    req = urllib.request.Request(TOKEN_URL, data=data,
                                 headers={"Content-Type": "application/x-www-form-urlencoded", "Accept": "application/json"},
                                 method="POST")
    with urllib.request.urlopen(req, timeout=60) as r:
        tok = json.loads(r.read())
    if not tok.get("access_token"):
        raise Exception("Token refresh failed: " + json.dumps(tok))
    return tok["access_token"]


def request(method, path, params=None, body=None, token=None):
    url = API + path
    q = {k: str(x) for k, x in (params or {}).items() if x not in (None, "")}
    if q:
        url += ("&" if "?" in url else "?") + urllib.parse.urlencode(q)
    data = json.dumps(body).encode("utf-8") if body is not None else None
    headers = {"Accept": "application/json", "Authorization": "Bearer " + (token or _token())}
    if data is not None:
        headers["Content-Type"] = "application/json"
    req = urllib.request.Request(url, data=data, headers=headers, method=method)
    with urllib.request.urlopen(req, timeout=90) as r:
        raw = r.read()
        return json.loads(raw) if raw else {}


def flatten_message(msg):
    payload = msg.get("payload", {})
    headers = {h.get("name", "").lower(): h.get("value", "") for h in payload.get("headers", [])}
    acc = {"body_text": "", "body_html": "", "attachments": []}

    def walk(part):
        for sub in part.get("parts", []) or []:
            walk(sub)
        mime = part.get("mimeType", "")
        data = part.get("body", {}).get("data")
        if part.get("filename"):
            acc["attachments"].append({
                "id": part.get("body", {}).get("attachmentId", ""),
                "name": part.get("filename", ""),
                "mime_type": mime,
            })
        elif data:
            pad = data + "=" * (-len(data) % 4)
            text = base64.urlsafe_b64decode(pad.encode("ascii")).decode("utf-8", "replace")
            if mime == "text/html":
                acc["body_html"] += text
            else:
                acc["body_text"] += text

    walk(payload)
    return {
        "id": msg.get("id"),
        "thread_id": msg.get("threadId"),
        "labels": msg.get("labelIds", []),
        "snippet": msg.get("snippet", ""),
        "internal_date": msg.get("internalDate"),
        "subject": headers.get("subject", ""),
        "from": headers.get("from", ""),
        "to": headers.get("to", ""),
        "cc": headers.get("cc", ""),
        "date": headers.get("date", ""),
        "message_id_header": headers.get("message-id", ""),
        "body_text": acc["body_text"],
        "body_html": acc["body_html"],
        "attachments": acc["attachments"],
    }


def to_epoch(v):
    s = str(v or "").strip()
    if not s:
        return None
    if s.isdigit():
        n = int(s)
        return n // 1000 if n > 10**12 else n
    try:
        dt = datetime.fromisoformat(s.replace("Z", "+00:00"))
        if dt.tzinfo is not None:
            dt = dt.astimezone(timezone.utc)
        return int(dt.timestamp())
    except Exception:
        return None


def build_query(inputs):
    parts = []
    for key, field in [("subject", "subject"), ("from", "from"), ("to", "to"), ("filename", "filename")]:
        if inputs.get(key):
            parts.append(field + ":" + str(inputs[key]))
    if inputs.get("has_attachments"):
        parts.append("has:attachment")
    if inputs.get("query"):
        parts.append(str(inputs["query"]))
    epoch = to_epoch(inputs.get("after"))
    if epoch is not None:
        parts.append("after:" + str(epoch))
    return " ".join(parts).strip()


def main():
    inputs = json.loads(os.environ.get("INTEGRATION_INPUTS", "{}"))
    token = _token()
    limit = int(inputs.get("max") or 50)
    params = {"q": build_query(inputs), "maxResults": limit}
    if inputs.get("label_id"):
        params["labelIds"] = str(inputs["label_id"])
    listing = request("GET", "/messages", params=params, token=token)
    result = []
    for m in listing.get("messages", []):
        full = request("GET", "/messages/" + urllib.parse.quote(m["id"]), params={"format": "full"}, token=token)
        result.append(flatten_message(full))
    print(json.dumps({"result": result}))


try:
    main()
except urllib.error.HTTPError as e:
    print(json.dumps({"error": "HTTP " + str(e.code), "detail": e.read().decode("utf-8", "replace")}))
    sys.exit(1)
except Exception as e:
    print(json.dumps({"error": str(e)}))
    sys.exit(1)
