import logging
from datetime import datetime, timedelta

# For direct usage without server-based tool calls:
from gmail_mcp.utils.GCP.gmail_auth import get_gmail_service
from gmail_mcp.utils.GCP.gmail_search import search_emails

# ---------------------------------------------------------------------------------------------
# Example direct usage of the Gmail search
# ---------------------------------------------------------------------------------------------
if __name__ == "__main__":
    logging.basicConfig(level=logging.INFO)
    logger = logging.getLogger("gmail_mcp-example")

    service = get_gmail_service()

    # Example: Search emails and get complete thread contents
    logger.info(
        "Performing first search for 'important meeting' within the last 7 days with attachments")
    thread_contents = search_emails(
        service,
        keywords="important meeting",
        after_date=datetime.now() - timedelta(days=7),
        has_attachment=True
    )

    print("\nFirst Search Results:")
    print(thread_contents)

    # Example: Save to file
    with open('email_threads.txt', 'w', encoding='utf-8') as f:
        f.write(thread_contents)

    # Example of full search capability with different parameters
    logger.info(
        "Performing second search for 'meeting' within the last 7 days with attachments")
    full_search_results = search_emails(
        service,
        sender=None,
        recipient=None,
        subject=None,
        has_attachment=True,
        filename=None,
        after_date=datetime.now() - timedelta(days=7),
        before_date=datetime.now(),
        newer_than_days=None,
        older_than_days=None,
        label=None,
        is_unread=None,
        max_results=None,
        keywords="meeting"
    )

    print("\nSecond Search Results:")
    print(full_search_results)


# ---------------------------------------------------------------------------------------------
# Example of how to call the tool if you were using the MCP approach:
# ---------------------------------------------------------------------------------------------
# from mcp.server import Server
# from gmail_mcp.server import server, inflight_requests
#
# async def call_search_tool():
#     # You might want to generate a unique request_id each time
#     request_id = "test_request_001"
#     arguments = {
#         "__request_id__": request_id,
#         "has_attachment": True,
#         "keywords": "meeting",
#         "days_after": 7
#     }
#     # This is how you'd call the tool via the server
#     result = await server.call_tool_async("perform-email-search", arguments)
#     for content in result:
#         if content.type == "text":
#             print(content.text)
#
# # You would then run something like:
# # asyncio.run(call_search_tool())
#
# # However, note that you typically run the server (server.main()), and have the external
# # client send a "call tool" request, rather than calling the server directly from within
# # the same process.
