from django.db import migrations, models
import django.db.models.deletion
import django_extensions.db.fields
import uuid


def migrate_journal_data(apps, schema_editor):
    Paper = apps.get_model("books", "Paper")
    Journal = apps.get_model("books", "Journal")
    for paper in Paper.objects.all():
        old_journal = getattr(paper, "journal", None)
        if old_journal:
            journal, _ = Journal.objects.get_or_create(title=str(old_journal))
            paper._journal_tmp = journal
            paper.save(update_fields=["_journal_tmp"])


def reverse_migrate_journal_data(apps, schema_editor):
    Paper = apps.get_model("books", "Paper")
    for paper in Paper.objects.all():
        if paper._journal_tmp:
            paper.journal = paper._journal_tmp.title
            paper.save(update_fields=["journal"])


class Migration(migrations.Migration):

    dependencies = [
        ("books", "0038_paper_pdf_file"),
    ]

    operations = [
        migrations.CreateModel(
            name="Journal",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True,
                        primary_key=True,
                        serialize=False,
                        verbose_name="ID",
                    ),
                ),
                (
                    "created",
                    django_extensions.db.fields.CreationDateTimeField(
                        auto_now_add=True, verbose_name="created"
                    ),
                ),
                (
                    "modified",
                    django_extensions.db.fields.ModificationDateTimeField(
                        auto_now=True, verbose_name="modified"
                    ),
                ),
                (
                    "uuid",
                    models.UUIDField(
                        blank=True, default=uuid.uuid4, editable=False, null=True
                    ),
                ),
                ("title", models.CharField(max_length=255)),
                ("description", models.TextField(blank=True, null=True)),
                ("website_url", models.URLField(blank=True, max_length=500, null=True)),
            ],
            options={
                "get_latest_by": "modified",
                "abstract": False,
            },
        ),
        migrations.AddField(
            model_name="paper",
            name="_journal_tmp",
            field=models.ForeignKey(
                blank=True,
                null=True,
                on_delete=django.db.models.deletion.DO_NOTHING,
                to="books.journal",
            ),
        ),
        migrations.RunPython(migrate_journal_data, reverse_migrate_journal_data),
        migrations.RemoveField(
            model_name="paper",
            name="journal",
        ),
        migrations.RenameField(
            model_name="paper",
            old_name="_journal_tmp",
            new_name="journal",
        ),
    ]
