# SPDX-FileCopyrightText: 2022-2025 Helmholtz-Zentrum hereon GmbH
#
# SPDX-License-Identifier: EUPL-1.2

# Generated by Django 3.2.15 on 2023-02-13 15:01

from django.conf import settings
from django.db import migrations, models


def migrate_delivered(apps, schema_editor):
    """Migrate the delivered field to delivered_to."""
    BrokerMessage = apps.get_model("dasf_broker", "BrokerMessage")
    User = apps.get_model(*settings.AUTH_USER_MODEL.split("."))

    queryset = BrokerMessage.objects.filter(delivered=True)
    if queryset:
        username = getattr(settings, "ANONYMOUS_USER_NAME", "AnonymousUser")
        user = User.objects.get(username=username)
        for message in queryset:
            message.delivered_to.add(user)


def undo_migrate_delivered(apps, schema_editor):
    """Undo the migration of the delivered field to delivered_to."""

    BrokerMessage = apps.get_model("dasf_broker", "BrokerMessage")

    queryset = BrokerMessage.objects.filter(delivered_to__pk__isnull=False)
    queryset.update(delivered=True)


class Migration(migrations.Migration):
    dependencies = [
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
        ("dasf_broker", "0001_initial"),
    ]

    operations = [
        migrations.AddField(
            model_name="brokermessage",
            name="delivered_to",
            field=models.ManyToManyField(
                help_text="To whom has this message been delivered already?",
                related_name="brokermessage_set_delivered",
                to=settings.AUTH_USER_MODEL,
            ),
        ),
        migrations.RunPython(migrate_delivered, undo_migrate_delivered),
        migrations.RemoveField(
            model_name="brokermessage",
            name="delivered",
        ),
        migrations.AddField(
            model_name="brokertopic",
            name="supports_dasf",
            field=models.BooleanField(
                default=True,
                help_text="Does this topic support the DASF messaging protocoll?",
            ),
        ),
        migrations.AddField(
            model_name="brokertopic",
            name="garbage_collect_on",
            field=models.DateTimeField(
                blank=True,
                help_text="Specify a time when this topic should be removed from the database.",
                null=True,
            ),
        ),
        migrations.AddField(
            model_name="brokertopic",
            name="store_messages",
            field=models.CharField(
                blank=True,
                choices=[
                    (
                        None,
                        "Use the server default",
                    ),
                    ("disabled", "Do not store messages at all"),
                    (
                        "cache",
                        "Cache until it has been delivered to at least on consumer",
                    ),
                    (
                        "cacheall",
                        "Cache until it has been delivered to all consumers",
                    ),
                    ("store", "Store messages forever"),
                ],
                help_text="Should messages be stored?",
                max_length=20,
                null=True,
            ),
        ),
        migrations.AddField(
            model_name="brokertopic",
            name="last_ping",
            field=models.DateTimeField(
                blank=True,
                help_text="When has the topic last been pinged?",
                null=True,
            ),
        ),
        migrations.AddField(
            model_name="brokertopic",
            name="last_pong",
            field=models.DateTimeField(
                blank=True,
                help_text="When has the topic last replyed on a ping?",
                null=True,
            ),
        ),
        migrations.AlterModelOptions(
            name="brokertopic",
            options={
                "permissions": (
                    (
                        "can_produce",
                        "Can publish messages to the topic (Producer).",
                    ),
                    (
                        "can_consume",
                        "Can consume messages to the topic (Consumer).",
                    ),
                    (
                        "can_view_status",
                        "Can view the status of the consumer.",
                    ),
                )
            },
        ),
    ]
