# Generated by Django 5.2.4 on 2025-09-19 01:48

import django.db.models.deletion
from django.db import migrations, models


def copy_live_subscriptions(apps, schema_editor):
    LiveSubscription = apps.get_model("core", "LiveSubscription")
    EnergyAccount = apps.get_model("core", "EnergyAccount")

    for subscription in LiveSubscription.objects.order_by("start_date", "pk"):
        if not subscription.account_id:
            continue
        update_kwargs = {
            "live_subscription_product_id": subscription.product_id,
            "live_subscription_start_date": subscription.start_date,
            "live_subscription_next_renewal": subscription.next_renewal,
        }
        EnergyAccount.objects.filter(pk=subscription.account_id).update(**update_kwargs)


class Migration(migrations.Migration):

    dependencies = [
        ("core", "0030_delete_aplead"),
    ]

    operations = [
        migrations.AddField(
            model_name="energyaccount",
            name="live_subscription_next_renewal",
            field=models.DateField(blank=True, null=True),
        ),
        migrations.AddField(
            model_name="energyaccount",
            name="live_subscription_product",
            field=models.ForeignKey(
                blank=True,
                null=True,
                on_delete=django.db.models.deletion.SET_NULL,
                related_name="live_subscription_accounts",
                to="core.product",
            ),
        ),
        migrations.AddField(
            model_name="energyaccount",
            name="live_subscription_start_date",
            field=models.DateField(blank=True, null=True),
        ),
        migrations.RunPython(copy_live_subscriptions, migrations.RunPython.noop),
        migrations.DeleteModel(
            name="LiveSubscription",
        ),
    ]
