# -*- coding: utf-8 -*-
from django.apps import AppConfig
from django.db.models.signals import post_migrate
from oscar.core.loading import get_model

from .constants import AVAILABLE_PAYMENT_METHODS, PRETTY_PAYMENT_METHOD_NAMES, APP_CODE


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

        for method in AVAILABLE_PAYMENT_METHODS:
            SourceType.objects.get_or_create(code="%s:%s" % (APP_CODE, method), defaults={"name": PRETTY_PAYMENT_METHOD_NAMES.get(method)})


class OcyanInvoicePaymentPluginAppConfig(AppConfig):
    name = "ocyan.plugin.payment_invoice"
    label = "payment_invoice"

    def ready(self):
        post_migrate.connect(create_source_type)
