# Generated by Django 4.2.11 on 2024-04-10 12:50

from enum import IntEnum

from django.db import migrations


def _recurringuserplan_has_automatic_renewal_backup_deprecated_to_renewal_triggered_by(
    apps, schema_editor
):
    RecurringUserPlan = apps.get_model("plans", "RecurringUserPlan")
    recurringuserplans_changed = (
        RecurringUserPlan.objects.select_for_update()
        .exclude(
            _has_automatic_renewal_backup_deprecated=True,
            renewal_triggered_by=_RenewalTriggeredByEnum.TASK,
        )
        .exclude(
            _has_automatic_renewal_backup_deprecated=False,
            renewal_triggered_by=_RenewalTriggeredByEnum.USER,
        )
    )
    for recurringuserplan_changed in recurringuserplans_changed:
        print(
            "RecurringUserPlan's renewal_triggered_by will be overwritten:",
            recurringuserplan_changed.pk,
        )
    RecurringUserPlan.objects.filter(
        _has_automatic_renewal_backup_deprecated=True
    ).update(renewal_triggered_by=_RenewalTriggeredByEnum.TASK)
    RecurringUserPlan.objects.filter(
        _has_automatic_renewal_backup_deprecated=False
    ).update(renewal_triggered_by=_RenewalTriggeredByEnum.USER)


def _recurringuserplan_renewal_triggered_by_to_has_automatic_renewal_backup_deprecated(
    apps, schema_editor
):
    RecurringUserPlan = apps.get_model("plans", "RecurringUserPlan")
    recurringuserplans_changed = (
        RecurringUserPlan.objects.select_for_update()
        .exclude(
            renewal_triggered_by__in={
                _RenewalTriggeredByEnum.TASK,
                _RenewalTriggeredByEnum.OTHER,
            },
            _has_automatic_renewal_backup_deprecated=True,
        )
        .exclude(
            renewal_triggered_by=_RenewalTriggeredByEnum.USER,
            _has_automatic_renewal_backup_deprecated=False,
        )
    )
    for recurringuserplan_changed in recurringuserplans_changed:
        print(
            "RecurringUserPlan's _has_automatic_renewal_backup_deprecated will be overwritten:",
            recurringuserplan_changed.pk,
        )
    RecurringUserPlan.objects.filter(
        renewal_triggered_by__in={
            _RenewalTriggeredByEnum.TASK,
            _RenewalTriggeredByEnum.OTHER,
        }
    ).update(_has_automatic_renewal_backup_deprecated=True)
    RecurringUserPlan.objects.filter(
        renewal_triggered_by=_RenewalTriggeredByEnum.USER
    ).update(_has_automatic_renewal_backup_deprecated=False)
    RecurringUserPlan.objects.update(renewal_triggered_by=_RenewalTriggeredByEnum.USER)


class _RenewalTriggeredByEnum(IntEnum):
    OTHER = 1
    USER = 2
    TASK = 3


class Migration(migrations.Migration):

    dependencies = [
        ("plans", "0013_alter_recurringuserplan_has_automatic_renewal_and_more"),
    ]

    operations = [
        migrations.RunPython(
            _recurringuserplan_has_automatic_renewal_backup_deprecated_to_renewal_triggered_by,
            reverse_code=_recurringuserplan_renewal_triggered_by_to_has_automatic_renewal_backup_deprecated,
        )
    ]
