# -*- coding: utf-8 -*-
from django.apps import AppConfig
from django.db.models.signals import post_migrate
from django.utils.translation import gettext as _
from django.utils.translation import override

from oscar.core.loading import get_model

from .constants import PAYMENT_KEY


def create_source_type(sender, app_config, **kwargs):  # pylint: disable=W0613
    if app_config.label == "payment":
        SourceType = get_model("payment", "SourceType")

        # fetch name in english
        with override("en"):
            source_name = _("Dummy payment")

        with override(None):  # deactivate language
            SourceType.objects.get_or_create(
                code=PAYMENT_KEY, defaults={"name": source_name}
            )


class OcyanDummyPaymentPluginAppConfig(AppConfig):
    name = "ocyan.plugin.payment_dummy"
    label = "payment_dummy"

    def ready(self):
        # Registers the production-safety system check without changing runtime flow.
        from . import checks  # noqa: F401

        post_migrate.connect(create_source_type)
