from datetime import datetime
from typing import Annotated

from mcp.server.fastmcp import FastMCP

from mcp_email_server.config import (
    AccountAttributes,
    EmailSettings,
    ProviderSettings,
    get_settings,
)
from mcp_email_server.emails.dispatcher import dispatch_handler
from mcp_email_server.emails.models import EmailPageResponse

mcp = FastMCP("email")


@mcp.resource("email://{account_name}")
async def get_account(account_name: str) -> EmailSettings | ProviderSettings | None:
    settings = get_settings()
    return settings.get_account(account_name, masked=True)


@mcp.tool()
async def list_available_accounts() -> list[AccountAttributes]:
    settings = get_settings()
    return [account.masked() for account in settings.get_accounts()]


@mcp.tool()
async def add_email_account(email: EmailSettings) -> None:
    settings = get_settings()
    settings.add_email(email)
    settings.store()


@mcp.tool(description="Paginate emails, page start at 1, before and since as UTC datetime.")
async def page_email(
    account_name: Annotated[str, "The name of the email account."],
    page: Annotated[int, "The page number to retrieve (starting from 1)."] = 1,
    page_size: Annotated[int, "The number of emails to retrieve per page."] = 10,
    before: Annotated[datetime | None, "Retrieve emails before this datetime (UTC)."] = None,
    since: Annotated[datetime | None, "Retrieve emails since this datetime (UTC)."] = None,
    subject: Annotated[str | None, "Filter emails by subject."] = None,
    body: Annotated[str | None, "Filter emails by body."] = None,
    text: Annotated[str | None, "Filter emails by text."] = None,
    from_address: Annotated[str | None, "Filter emails by sender address."] = None,
    to_address: Annotated[str | None, "Filter emails by recipient address."] = None,
) -> EmailPageResponse:
    handler = dispatch_handler(account_name)

    return await handler.get_emails(
        page=page,
        page_size=page_size,
        before=before,
        since=since,
        subject=subject,
        body=body,
        text=text,
        from_address=from_address,
        to_address=to_address,
    )


@mcp.tool(
    description="Send an email using the specified account. Recipient should be a list of email addresses.",
)
async def send_email(
    account_name: Annotated[str, "The name of the email account to send from."],
    recipients: Annotated[list[str], "A list of recipient email addresses."],
    subject: Annotated[str, "The subject of the email."],
    body: Annotated[str, "The body of the email."],
    cc: Annotated[list[str] | None, "A list of CC email addresses."] = None,
    bcc: Annotated[list[str] | None, "A list of BCC email addresses."] = None,
) -> None:
    handler = dispatch_handler(account_name)
    await handler.send_email(recipients, subject, body, cc, bcc)
    return
