"""Gmail MCP tools."""

from __future__ import annotations

from typing import Optional

from fastmcp import FastMCP

from ..services import gmail as gmail_svc

mcp = FastMCP("Gmail")


@mcp.tool(tags={"gmail", "read"})
def gmail_search(
    query: str,
    profile: Optional[str] = None,
    max_results: int = 20,
    page_token: Optional[str] = None,
) -> dict:
    """Search emails using Gmail query syntax. Supports from:, to:, subject:, has:attachment, after:, before:, label:, etc.

    Args:
        query: Gmail search query (same syntax as Gmail search bar)
        profile: Account profile name. Use list_google_profiles to see available profiles. Defaults to first profile.
        max_results: Maximum messages to return (1-100, default 20)
        page_token: Pagination token from previous result
    """
    result = gmail_svc.search(query, profile=profile, max_results=max_results, page_token=page_token)
    result["profile"] = profile or "default"
    return result


@mcp.tool(tags={"gmail", "read"})
def gmail_read(message_id: str, profile: Optional[str] = None) -> dict:
    """Read a full email message by ID. Returns headers, body text, and attachment list.

    Args:
        message_id: Message ID (from gmail_search results)
        profile: Account profile name
    """
    result = gmail_svc.read_message(message_id, profile=profile)
    result["profile"] = profile or "default"
    return result


@mcp.tool(tags={"gmail", "read"})
def gmail_read_thread(thread_id: str, profile: Optional[str] = None) -> dict:
    """Read an entire email thread/conversation by thread ID.

    Args:
        thread_id: Thread ID (from gmail_search results)
        profile: Account profile name
    """
    messages = gmail_svc.read_thread(thread_id, profile=profile)
    return {"profile": profile or "default", "threadId": thread_id, "messages": messages}


@mcp.tool(tags={"gmail", "write"})
def gmail_send(
    to: str,
    subject: str,
    body: str,
    profile: Optional[str] = None,
    cc: Optional[str] = None,
    bcc: Optional[str] = None,
    in_reply_to: Optional[str] = None,
    thread_id: Optional[str] = None,
) -> dict:
    """Send an email or reply to a thread.

    Args:
        to: Recipient email address
        subject: Email subject line
        body: Email body (plain text)
        profile: Account profile name
        cc: CC recipients (comma-separated)
        bcc: BCC recipients (comma-separated)
        in_reply_to: Message-ID header to reply to
        thread_id: Thread ID to add this message to (for replies)
    """
    result = gmail_svc.send_message(
        to=to, subject=subject, body=body, profile=profile,
        cc=cc, bcc=bcc, in_reply_to=in_reply_to, thread_id=thread_id,
    )
    result["profile"] = profile or "default"
    return result


@mcp.tool(tags={"gmail", "read"})
def gmail_get_attachment(
    message_id: str,
    attachment_id: str,
    profile: Optional[str] = None,
    save_path: Optional[str] = None,
) -> dict:
    """Download an email attachment. Can save to local file or return preview.

    Args:
        message_id: Message ID containing the attachment
        attachment_id: Attachment ID (from gmail_read results)
        profile: Account profile name
        save_path: Local file path to save. If omitted, returns a data preview.
    """
    result = gmail_svc.get_attachment(
        message_id=message_id, attachment_id=attachment_id,
        profile=profile, save_path=save_path,
    )
    result["profile"] = profile or "default"
    return result
