# Generated by Django 4.2.1 on 2023-11-06 12:37

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


def forwards_func(apps, schema_editor):
    MeshNode = apps.get_model("mesh", "MeshNode")
    MeshUplink = apps.get_model("mesh", "MeshUplink")
    for node in MeshNode.objects.annotate(first_relayed=Min('relayed_messages__datetime'),
                                          last_relayed=Max('relayed_messages__datetime')):
        if node.first_relayed is None:
            continue
        uplink = MeshUplink.objects.create(
            name="migration_generated",
            started=node.first_relayed,
            node=node,
            last_ping=node.last_relayed,
        )
        node.routed_nodes.update(uplink=uplink)
        node.relayed_messages.update(uplink=uplink)


def backwards_func(apps, schema_editor):
    MeshUplink = apps.get_model("mesh", "MeshUplink")
    for uplink in MeshUplink.objects.select_related('node'):
        uplink.routed_nodes.update(uplink_node=uplink.node)
        uplink.relayed_messages.update(uplink_node=uplink.node)



class Migration(migrations.Migration):
    dependencies = [
        ("mesh", "0008_firmwarebuild_firmwarebuildboard_firmwareversion_and_more"),
    ]

    operations = [
        migrations.AlterField(
            model_name="nodemessage",
            name="uplink_node",
            field=models.ForeignKey(
                null=True,
                on_delete=django.db.models.deletion.PROTECT,
                related_name="relayed_messages",
                to="mesh.meshnode",
                verbose_name="uplink node",
            ),
        ),
        migrations.CreateModel(
            name="MeshUplink",
            fields=[
                (
                    "id",
                    models.AutoField(
                        auto_created=True,
                        primary_key=True,
                        serialize=False,
                        verbose_name="ID",
                    ),
                ),
                ("name", models.CharField(max_length=128, verbose_name="channel name")),
                (
                    "started",
                    models.DateTimeField(auto_now_add=True, verbose_name="started"),
                ),
                (
                    "last_ping",
                    models.DateTimeField(verbose_name="last ping from consumer"),
                ),
                (
                    "end_reason",
                    models.CharField(
                        choices=[
                            ("closed", "closed"),
                            ("replaced", "replaced"),
                            ("new_timeout", "new (timeout)"),
                        ],
                        max_length=16,
                        null=True,
                        verbose_name="end reason",
                    ),
                ),
                (
                    "node",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.PROTECT,
                        related_name="uplink_sessions",
                        to="mesh.meshnode",
                        verbose_name="node",
                    ),
                ),
            ],
        ),
        migrations.AddField(
            model_name="nodemessage",
            name="uplink",
            field=models.ForeignKey(
                null=True,
                on_delete=django.db.models.deletion.PROTECT,
                related_name="relayed_messages",
                to="mesh.meshuplink",
                verbose_name="uplink",
            ),
        ),
        migrations.AddConstraint(
            model_name="meshuplink",
            constraint=models.UniqueConstraint(
                condition=models.Q(("end_reason__isnull", True)),
                fields=("node",),
                name="only_one_active_uplink",
            ),
        ),
        migrations.RenameField(
            model_name="meshnode",
            old_name="uplink",
            new_name="uplink_node",
        ),
        migrations.AddField(
            model_name="meshnode",
            name="uplink",
            field=models.ForeignKey(
                null=True,
                on_delete=django.db.models.deletion.PROTECT,
                related_name="routed_nodes",
                to="mesh.meshuplink",
                verbose_name="uplink",
            ),
        ),
        migrations.RunPython(forwards_func, backwards_func),
        migrations.RemoveField(
            model_name="meshnode",
            name="uplink_node",
        ),
        migrations.RemoveField(
            model_name="nodemessage",
            name="uplink_node",
        ),
        migrations.AlterField(
            model_name="nodemessage",
            name="uplink",
            field=models.ForeignKey(
                on_delete=django.db.models.deletion.PROTECT,
                related_name="relayed_messages",
                to="mesh.meshuplink",
                verbose_name="uplink",
            ),
        ),
    ]
