#
#   Debian Changes Bot
#   Copyright (C) 2008 Chris Lamb <chris@chris-lamb.co.uk>
#   Copyright (C) 2015-2026 Sebastian Ramacher <sramacher@debian.org>
#
#   This program is free software: you can redistribute it and/or modify
#   it under the terms of the GNU Affero General Public License as
#   published by the Free Software Foundation, either version 3 of the
#   License, or (at your option) any later version.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU Affero General Public License for more details.
#
#   You should have received a copy of the GNU Affero General Public License
#   along with this program.  If not, see <http://www.gnu.org/licenses/>.


from abc import abstractmethod
from typing import Protocol


class Message(Protocol):
    def __init__(self) -> None:
        if hasattr(self, "FIELDS"):
            for field in self.FIELDS:
                setattr(self, field, None)
        if hasattr(self, "OPTIONAL"):
            for field in self.OPTIONAL:
                setattr(self, field, None)

    def __bool__(self):
        if hasattr(self, "FIELDS"):
            for field in self.FIELDS:
                if getattr(self, field) is None:
                    return False
        return True

    @abstractmethod
    def format(self) -> str: ...

    def for_irc(self) -> str:
        return self.format()

    def package_name(self) -> str:
        from .. import pseudo_packages

        if pseudo_packages.is_pseudo_package(self.package):
            return "[pseudo-package]%s[reset]" % self.package
        else:
            return "[package]%s[reset]" % self.package


class MailParser(Protocol):
    @staticmethod
    @abstractmethod
    def parse(headers, body, **kwargs) -> Message | None: ...


from .accepted_upload import AcceptedUploadParser, AcceptedUploadMessage
from .bug_closed import BugClosedParser, BugClosedMessage
from .bug_submitted import BugSubmittedParser, BugSubmittedMessage


_PARSERS = (
    AcceptedUploadParser,
    BugClosedParser,
    BugSubmittedParser,
)


def get_message(email, **kwargs) -> Message | None:
    headers, body = email

    for parser in _PARSERS:
        msg = parser.parse(headers, body, **kwargs)
        if msg:
            return msg
