from email.mime.application import MIMEApplication
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.utils import formataddr as formataddr_
from pathlib import Path

type Email = str
type NamedEmail = tuple[str, Email]
type Address = Email | list[Email] | NamedEmail | list[NamedEmail]


class Message:
    def __init__(
        self,
        from_: NamedEmail,
        to: Address,
        subject: str,
        cc: Address | None = None,
        bcc: Address | None = None,
        reply_to: NamedEmail | None = None,
        content: str | None = None,
    ) -> None:
        self._references = set()
        self._body = MIMEMultipart('alternative')
        self._message = MIMEMultipart('mixed')
        self._message['From'] = formataddr_(from_)
        self._message['To'] = formataddr(to)
        self._message['Subject'] = subject
        self._message.attach(self._body)

        if reply_to:
            self._message['Reply-To'] = formataddr_(reply_to)

        if cc:
            self._message['Cc'] = formataddr(cc)

        if bcc:
            self._message['Bcc'] = formataddr(bcc)

        if content:
            self.add_alternative(content, subtype='plain')

    def add_header(self, name: str, value: str) -> None:
        self._message.add_header(name, value)

    def add_in_reply_to(self, message_id: str) -> None:
        self._message['In-Reply-To'] = message_id
        self._references.add(message_id)  # Add to set avoids duplicates

    def add_alternative(
        self,
        text: str,
        /,
        subtype: str = 'html',
        charset: str = 'utf-8',
    ) -> None:
        self._body.attach(MIMEText(text, subtype, charset))

    def attach(self, path: Path | MIMEApplication, filename: str | None = None) -> None:
        if isinstance(path, MIMEApplication):
            self._message.attach(path)
            return

        if not path.is_file():
            return None

        with path.open('rb') as fp:
            part = MIMEApplication(fp.read())
            part.add_header(
                'Content-Disposition',
                'attachment',
                filename=filename or path.name,
            )
            self._message.attach(part)

    def as_bytes(self) -> bytes:
        if self._references:
            self._message['References'] = ' '.join(self._references)

        return self._message.as_bytes()


def formataddr(address: Address) -> str:
    if isinstance(address, str):
        return formataddr_((None, address))

    if isinstance(address, tuple):
        return formataddr_(address)

    return ', '.join([formataddr(addr) for addr in address])
