"""CLI entry point — filter Gmail messages by sender."""

import argparse
import sys

from mail_connector import GmailConnector


def main(argv: list[str] | None = None) -> None:
    """List messages from a specific sender."""
    parser = argparse.ArgumentParser(description="List Gmail messages from a specific sender.")
    parser.add_argument("sender", help="Sender email address to filter by")
    parser.add_argument("-n", "--max-results", type=int, default=10, help="Max messages to return (default: 10)")
    args = parser.parse_args(argv)

    gmail = GmailConnector()
    messages = gmail.list_messages(query=f"from:{args.sender}", max_results=args.max_results)

    if not messages:
        print(f"No messages from {args.sender}")
        return

    for stub in messages:
        msg = gmail.get_message(stub["id"])
        headers = {h["name"]: h["value"] for h in msg["payload"].get("headers", [])}
        subject = headers.get("Subject", "(no subject)")
        date = headers.get("Date", "")
        print(f"[{date}] {subject}")


if __name__ == "__main__":
    main()
