# src/backend/fetcher.py

import os
import base64
from typing import List, Optional

from google.auth.transport.requests import Request
from google.oauth2.credentials   import Credentials
from google_auth_oauthlib.flow    import InstalledAppFlow
from googleapiclient.discovery    import build

# Gmail API scope (read-only)
SCOPES = ["https://www.googleapis.com/auth/gmail.readonly"]

def _get_service():
    """
    Authenticate (or refresh) and return a Gmail service client.
    """
    creds: Optional[Credentials] = None

    # 1) Load existing tokens, if present
    if os.path.exists("token.json"):
        creds = Credentials.from_authorized_user_file("token.json", SCOPES)

    # 2) If no valid creds, run the OAuth2 flow
    if not creds or not creds.valid:
        if creds and creds.expired and creds.refresh_token:
            creds.refresh(Request())
        else:
            flow = InstalledAppFlow.from_client_secrets_file(
                "credentials.json", SCOPES
            )
            creds = flow.run_local_server(port=0)
        # Save for next time
        with open("token.json", "w") as token:
            token.write(creds.to_json())

    return build("gmail", "v1", credentials=creds)

def fetch_thread_raw(thread_id: str) -> str:
    """
    Fetch every message in a Gmail thread as raw MIME, concatenate them,
    and return one big string.
    """
    service = _get_service()

    # First get the thread metadata (to list all message IDs)
    thread = (
        service.users()
               .threads()
               .get(userId="me", id=thread_id)
               .execute()
    )

    parts: List[str] = []
    for msg_meta in thread.get("messages", []):
        mid = msg_meta["id"]
        # Now fetch each message in raw format
        msg = (
            service.users()
                   .messages()
                   .get(userId="me", id=mid, format="raw")
                   .execute()
        )
        raw_bytes = base64.urlsafe_b64decode(msg["raw"].encode("ASCII"))
        parts.append(raw_bytes.decode("utf-8", errors="replace"))

    return "\n".join(parts)

def fetch_message_raw(message_id: str) -> str:
    """
    Fetch a single Gmail message as raw MIME.
    """
    service = _get_service()
    msg = (
        service.users()
               .messages()
               .get(userId="me", id=message_id, format="raw")
               .execute()
    )
    raw_bytes = base64.urlsafe_b64decode(msg["raw"].encode("ASCII"))
    return raw_bytes.decode("utf-8", errors="replace")

def list_recent_threads(n: int = 5) -> List[str]:
    """
    Return the thread IDs of your n most recent threads.
    """
    service = _get_service()
    resp = service.users().threads().list(userId="me", maxResults=n).execute()
    return [t["id"] for t in resp.get("threads", [])]
