import os.path
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
from googleapiclient.errors import HttpError
import logging

logger = logging.getLogger("gmail_mcp")

# If modifying these scopes, delete the file token.json.
SCOPES = [
    "https://www.googleapis.com/auth/gmail.modify",  # Read, compose, and send
    "https://www.googleapis.com/auth/gmail.send"     # Send-only permission
]


def get_gmail_service():
    """
    Handles Gmail API authentication and returns the service object.

    Returns:
        service: Authenticated Gmail API service instance

    Raises:
        Exception: If authentication fails
    """
    creds = None
    credentials_path = os.getenv('GMAIL_CREDENTIALS_PATH', 'credentials.json')
    token_path = os.path.join(os.path.dirname(credentials_path), 'token.json')

    logger.info(f"Using credentials from: {credentials_path}")

    # The file token.json stores the user's access and refresh tokens, and is
    # created automatically when the authorization flow completes for the first time.
    if os.path.exists(token_path):
        creds = Credentials.from_authorized_user_file(token_path, SCOPES)

    # If there are no (valid) credentials available, let the user log in.
    if not creds or not creds.valid:
        if creds and creds.expired and creds.refresh_token:
            creds.refresh(Request())
        else:
            if not os.path.exists(credentials_path):
                raise FileNotFoundError(
                    f"Credentials file not found at {credentials_path}. "
                    "Please set GMAIL_CREDENTIALS_PATH environment variable to point to your credentials.json file."
                )

            flow = InstalledAppFlow.from_client_secrets_file(
                credentials_path, SCOPES
            )
            creds = flow.run_local_server(port=0)

        # Save the credentials for the next run
        with open(token_path, "w") as token:
            token.write(creds.to_json())

    try:
        # Return Gmail API service
        return build("gmail", "v1", credentials=creds)
    except HttpError as error:
        logger.error(f"An error occurred: {error}")
        raise error


# Example usage (only if run directly):
if __name__ == "__main__":
    try:
        service = get_gmail_service()
        logger.info("Gmail API service created successfully!")
    except Exception as e:
        logger.error(f"Failed to create Gmail service: {e}")
