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


class Message:
    def __init__(
        self,
        from_: tuple[str | None, str],
        to: tuple[str | None, str],
        subject: str,
        reply_to: tuple[str | None, str] | 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 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, filename: str | None = None) -> None:
        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()
