# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.

import structlog

from code_review_bot import taskcluster
from code_review_bot.config import settings
from code_review_bot.report.base import Reporter

logger = structlog.get_logger(__name__)


EMAIL_STATS_LINE = "* **{analyzer}**: {publishable} publishable ({total} total)"

EMAIL_HEADER = """
# Found {publishable} publishable issues ({total} total)

{stats}

Review Url: {review_url}

"""
EMAIL_HEADER_PATCH = "* Improvement patch from {}"


class MailReporter(Reporter):
    """
    Send an email to admins through Taskcluster service
    """

    def __init__(self, configuration):
        (self.emails,) = self.requires(configuration, "emails")
        assert len(self.emails) > 0, "Missing emails data"

        # Load TC services & secrets
        self.notify = taskcluster.get_service("notify")

        logger.info("Mail report enabled", emails=self.emails)

    def publish(self, issues, revision, task_failures, links, reviewers):
        """
        Send an email to administrators
        """

        # For no issues do not publish anything
        if len(issues) == 0:
            return

        # Build stats display for all issues
        # One line per issues class
        stats = "\n".join(
            [
                EMAIL_STATS_LINE.format(
                    analyzer=stat["analyzer"],
                    total=stat["total"],
                    publishable=stat["publishable"],
                )
                for stat in self.calc_stats(issues)
            ]
        )

        content = EMAIL_HEADER.format(
            total=len(issues),
            publishable=sum([i.is_publishable() for i in issues]),
            stats=stats,
            review_url=revision.url,
        )
        if revision.improvement_patches:
            content += "## Improvement patches:\n\n{}\n\n".format(
                "\n".join(
                    EMAIL_HEADER_PATCH.format(patch)
                    for patch in revision.improvement_patches
                )
            )
        content += "\n\n".join([i.as_markdown() for i in issues])
        if len(content) > 102400:
            # Content is 102400 chars max
            content = content[:102000] + "\n\n... Content max limit reached!"
        subject = f"[{settings.app_channel}] New Static Analysis {revision}"
        for email in self.emails:
            self.notify.email(
                {
                    "address": email,
                    "subject": subject,
                    "content": content,
                    "template": "fullscreen",
                }
            )
