# -*- coding: utf-8 -*-
from django.apps import AppConfig
from django.core.management import call_command
from django.db.models.signals import post_migrate

from .constants import APP_LABEL

# if more communication templates are used, add them here
USER_COMMUNICATION_TYPES = [
    "PASSWORD_CHANGED",
    "EMAIL_CHANGED",
    "PASSWORD_RESET",
    "PRODUCT_ALERT",
    "PRODUCT_ALERT_CONFIRMATION",
    "REGISTRATION",
]

ORDER_COMMUNICATION_TYPES = ["ORDER_PLACED", "shipped"]


def populate_dashboard_emails(sender, app_config, **kwargs):  # pylint: disable=W0613
    if app_config.label == "customer":
        call_command("populate_dashboard_emails", *USER_COMMUNICATION_TYPES)
        call_command(
            "populate_dashboard_emails", "--is-order-email", *ORDER_COMMUNICATION_TYPES
        )


class OcyanPluginOscarCommunicationsAppConfig(AppConfig):
    name = "ocyan.plugin.oscar_communications"
    label = APP_LABEL

    def ready(self):
        post_migrate.connect(populate_dashboard_emails)
        # pylint: disable=unused-import
        from . import signal_handlers
