# Generated by Django 4.2.3 on 2023-11-27 22:32

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


def forwards_func(apps, schema_editor):
    UserPermissions = apps.get_model("control", "UserPermissions")
    for permissions in UserPermissions.objects.select_related("user"):
        if permissions.api_secret:
            permissions.user.api_secrets.create(
                name="legacy secret (migrated)",
                api_secret=permissions.api_secret,
                readonly=False,
                scope_grant_permissions=True,
                scope_editor=True,
                scope_mesh=True,
                valid_until=None,
            )


def backwards_func(apps, schema_editor):
    UserPermissions = apps.get_model("control", "UserPermissions")
    for permissions in UserPermissions.objects.select_related("user"):
        secret = permissions.user.api_secrets.first()
        if secret:
            permissions.api_secret = secret.api_secret


class Migration(migrations.Migration):
    dependencies = [
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
        ("api", "0002_django_4_0"),
        ("control", "0010_userpermissions_mesh_control"),
    ]

    operations = [
        migrations.RenameModel(
            old_name="Token",
            new_name="LoginToken",
        ),
        migrations.CreateModel(
            name="Secret",
            fields=[
                (
                    "id",
                    models.AutoField(
                        auto_created=True,
                        primary_key=True,
                        serialize=False,
                        verbose_name="ID",
                    ),
                ),
                ("name", models.CharField(verbose_name="name", max_length=32)),
                (
                    "created",
                    models.DateTimeField(
                        auto_now_add=True, verbose_name="creation date"
                    ),
                ),
                (
                    "api_secret",
                    models.CharField(max_length=64, unique=True, verbose_name="API secret"),
                ),
                ("readonly", models.BooleanField(verbose_name="readonly")),
                (
                    "scope_grant_permissions",
                    models.BooleanField(
                        default=False, verbose_name="grant map access permissions"
                    ),
                ),
                (
                    "scope_editor",
                    models.BooleanField(default=False, verbose_name="editor access"),
                ),
                (
                    "scope_mesh",
                    models.BooleanField(default=False, verbose_name="mesh access"),
                ),
                (
                    "valid_until",
                    models.DateTimeField(null=True, verbose_name="valid_until"),
                ),
                (
                    "user",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="api_secrets",
                        to=settings.AUTH_USER_MODEL,
                    ),
                ),
            ],
        ),
        migrations.RunPython(forwards_func, backwards_func),
        migrations.AlterModelOptions(
            name="secret",
            options={
                "verbose_name": "API secret",
                "verbose_name_plural": "API secrets",
            },
        ),
        migrations.AlterUniqueTogether(
            name="secret",
            unique_together={("user", "name")},
        ),
    ]
