# Generated by Django 2.2.6 on 2019-10-31 11:55

from django.db import migrations, models
import django.utils.timezone


def create_system_pay_subscription(apps, schema):
    SystemPaySubscription = apps.get_model("system_pay", "SystemPaySubscription")
    SystemPayTransaction = apps.get_model("system_pay", "SystemPayTransaction")

    for sp_transaction in SystemPayTransaction.objects.filter(
        subscription__isnull=False, status=1  # STATUS_COMPLETED
    ):
        subscription_id = sp_transaction.webhook_calls[-1]["vads_subscription"]

        SystemPaySubscription.objects.create(
            subscription=sp_transaction.subscription,
            alias=sp_transaction.alias,
            identifier=subscription_id,
        )


class Migration(migrations.Migration):
    dependencies = [
        ("system_pay", "0007_remove_sensitive_info"),
    ]

    operations = [
        migrations.CreateModel(
            name="SystemPaySubscription",
            fields=[
                (
                    "id",
                    models.AutoField(
                        auto_created=True,
                        primary_key=True,
                        serialize=False,
                        verbose_name="ID",
                    ),
                ),
                (
                    "created",
                    models.DateTimeField(
                        default=django.utils.timezone.now,
                        editable=False,
                        verbose_name="created",
                    ),
                ),
                (
                    "modified",
                    models.DateTimeField(auto_now=True, verbose_name="modified"),
                ),
                (
                    "identifier",
                    models.CharField(
                        max_length=30,
                        unique=True,
                        verbose_name="Identifiant de la souscription",
                    ),
                ),
                (
                    "active",
                    models.BooleanField(
                        default=True,
                        verbose_name="La souscription est active côté SystemPay",
                    ),
                ),
                (
                    "alias",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.PROTECT,
                        to="system_pay.SystemPayAlias",
                    ),
                ),
                (
                    "subscription",
                    models.OneToOneField(
                        on_delete=django.db.models.deletion.PROTECT,
                        related_name="system_pay_subscription",
                        to="payments.Subscription",
                    ),
                ),
            ],
            options={"abstract": False},
        ),
        migrations.RunPython(
            code=create_system_pay_subscription, reverse_code=migrations.RunPython.noop
        ),
    ]
