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



# SCOPES = ['https://www.googleapis.com/auth/gmail.send']

# def create_message(sender, to, subject, message_text):
#     message = f"From: {sender}\nTo: {to}\nSubject: {subject}\n\n{message_text}"
#     b64_bytes = base64.urlsafe_b64encode(message.encode("utf-8"))
#     return {'raw': b64_bytes.decode("utf-8")}

# def send_message(service, user_id, message):
#     try:
#         message = service.users().messages().send(userId=user_id, body=message).execute()
#         print('Message Id: %s' % message['id'])
#         return message
#     except Exception as e:
#         print('An error occurred: %s' % e)
#         return None

# def main(sender, to, subject, message_text):
#     creds = None
#     if os.path.exists('token.json'):
#         creds = Credentials.from_authorized_user_file('token.json')
    
#     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('client_secret.json', SCOPES)
#             creds = flow.run_local_server(port=0)
#         with open('token.json', 'w') as token:
#             token.write(creds.to_json())

#     service = build('gmail', 'v1', credentials=creds)

#     message = create_message(sender, to, subject, message_text)
#     send_message(service, "me", message)

# if __name__ == '__main__':
#     sender = "bankswift05@gmail.com"
#     to = "goodall.andrea.ag.ag@gmail.com"
#     subject = "Test Email from BankSwift"
#     message_text = "Hello, this is a test email sent from bankswift."
#     main(sender, to, subject, message_text)

SCOPES = ['https://www.googleapis.com/auth/gmail.send']

def create_message(sender, to, subject, message_text):
    message = f"From: {sender}\nTo: {to}\nSubject: {subject}\n\n{message_text}"
    b64_bytes = base64.urlsafe_b64encode(message.encode("utf-8"))
    return {'raw': b64_bytes.decode("utf-8")}

def send_message(service, user_id, message):
    try:
        message = service.users().messages().send(userId=user_id, body=message).execute()
        print('Message Id: %s' % message['id'])
        return message
    except Exception as e:
        print('An error occurred: %s' % e)
        return None

def main(sender, to, subject, message_text):
    creds = None
    if os.path.exists('token.json'):
        creds = Credentials.from_authorized_user_file('token.json')
    
    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('client_secret.json', SCOPES)
            creds = flow.run_local_server(port=0)
        with open('token.json', 'w') as token:
            token.write(creds.to_json())

    service = build('gmail', 'v1', credentials=creds)

    message = create_message(sender, to, subject, message_text)
    send_message(service, "me", message)
