# Generated by Django 5.1.4 on 2025-02-09 13:04

import django.db.models.deletion
import ram.utils
import tinymce.models
from django.db import migrations, models


def migrate_document(apps, schema_editor):
    document = apps.get_model("metadata", "GenericDocument")
    document_new = apps.get_model("repository", "GenericDocument")
    for d in document.objects.all():
        n = document_new.objects.create(
            notes=d.notes,
            description=d.description,
            file=d.file,
            private=d.private,
            creation_time=d.creation_time,
            updated_time=d.updated_time,
        )
        for t in d.tags.all():
            n.tags.add(t)


def migrate_decoder(apps, schema_editor):
    dcc_document = apps.get_model("metadata", "DecoderDocument")
    dcc_document_new = apps.get_model("repository", "DecoderDocument")
    for d in dcc_document.objects.all():
        dcc_document_new.objects.create(
            decoder=d.decoder,
            description=d.description,
            file=d.file,
            private=d.private,
            creation_time=d.creation_time,
            updated_time=d.updated_time,
        )


def migrate_rollingstock(apps, schema_editor):
    rs_document = apps.get_model("roster", "RollingStockDocument")
    rs_document_new = apps.get_model("repository", "RollingStockDocument")
    for d in rs_document.objects.all():
        rs_document_new.objects.create(
            rolling_stock=d.rolling_stock,
            description=d.description,
            file=d.file,
            private=d.private,
            creation_time=d.creation_time,
            updated_time=d.updated_time,
        )


def migrate_book(apps, schema_editor):
    book_document = apps.get_model("bookshelf", "BaseBookDocument")
    book_document_new = apps.get_model("repository", "BaseBookDocument")
    catalog_document_new = apps.get_model("repository", "CatalogDocument")
    for d in book_document.objects.all():
        if hasattr(d.book, "book"):
            book_document_new.objects.create(
                book=d.book.book,
                description=d.description,
                file=d.file,
                private=d.private,
                creation_time=d.creation_time,
                updated_time=d.updated_time,
            )
        else:
            catalog_document_new.objects.create(
                catalog=d.book.catalog,
                description=d.description,
                file=d.file,
                private=d.private,
                creation_time=d.creation_time,
                updated_time=d.updated_time,
            )


class Migration(migrations.Migration):

    dependencies = [
        ("bookshelf", "0022_basebook_shop"),
        ("metadata", "0023_shop"),
        ("roster", "0035_alter_rollingstock_shop"),
    ]

    operations = [
        migrations.CreateModel(
            name="BaseBookDocument",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True,
                        primary_key=True,
                        serialize=False,
                        verbose_name="ID",
                    ),
                ),
                ("description", models.CharField(blank=True, max_length=128)),
                (
                    "file",
                    models.FileField(
                        storage=ram.utils.DeduplicatedStorage(), upload_to="files/"
                    ),
                ),
                (
                    "private",
                    models.BooleanField(
                        default=False,
                        help_text="Document will be visible only to logged users",
                    ),
                ),
                ("creation_time", models.DateTimeField(auto_now_add=True)),
                ("updated_time", models.DateTimeField(auto_now=True)),
                (
                    "book",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="new_document",
                        to="bookshelf.basebook",
                    ),
                ),
            ],
            options={
                "verbose_name_plural": "Documents",
                "abstract": False,
            },
        ),
        migrations.CreateModel(
            name="BookDocument",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True,
                        primary_key=True,
                        serialize=False,
                        verbose_name="ID",
                    ),
                ),
                ("description", models.CharField(blank=True, max_length=128)),
                (
                    "file",
                    models.FileField(
                        storage=ram.utils.DeduplicatedStorage(), upload_to="files/"
                    ),
                ),
                ("creation_time", models.DateTimeField(auto_now_add=True)),
                ("updated_time", models.DateTimeField(auto_now=True)),
                (
                    "private",
                    models.BooleanField(
                        default=False,
                        help_text="Document will be visible only to logged users",
                    ),
                ),
                (
                    "book",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="document",
                        to="bookshelf.book",
                    ),
                ),
            ],
            options={
                "verbose_name_plural": "Book documents",
                "constraints": [
                    models.UniqueConstraint(
                        fields=("book", "file"), name="unique_book_file"
                    )
                ],
            },
        ),
        migrations.CreateModel(
            name="CatalogDocument",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True,
                        primary_key=True,
                        serialize=False,
                        verbose_name="ID",
                    ),
                ),
                ("description", models.CharField(blank=True, max_length=128)),
                (
                    "file",
                    models.FileField(
                        storage=ram.utils.DeduplicatedStorage(), upload_to="files/"
                    ),
                ),
                ("creation_time", models.DateTimeField(auto_now_add=True)),
                ("updated_time", models.DateTimeField(auto_now=True)),
                (
                    "private",
                    models.BooleanField(
                        default=False,
                        help_text="Document will be visible only to logged users",
                    ),
                ),
                (
                    "catalog",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="document",
                        to="bookshelf.catalog",
                    ),
                ),
            ],
            options={
                "verbose_name_plural": "Catalog documents",
                "constraints": [
                    models.UniqueConstraint(
                        fields=("catalog", "file"), name="unique_catalog_file"
                    )
                ],
            },
        ),
        migrations.CreateModel(
            name="GenericDocument",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True,
                        primary_key=True,
                        serialize=False,
                        verbose_name="ID",
                    ),
                ),
                ("description", models.CharField(blank=True, max_length=128)),
                (
                    "file",
                    models.FileField(
                        storage=ram.utils.DeduplicatedStorage(), upload_to="files/"
                    ),
                ),
                (
                    "private",
                    models.BooleanField(
                        default=False,
                        help_text="Document will be visible only to logged users",
                    ),
                ),
                ("creation_time", models.DateTimeField(auto_now_add=True)),
                ("updated_time", models.DateTimeField(auto_now=True)),
                ("notes", tinymce.models.HTMLField(blank=True)),
                (
                    "tags",
                    models.ManyToManyField(
                        blank=True, related_name="new_document", to="metadata.tag"
                    ),
                ),
            ],
            options={
                "verbose_name_plural": "Generic Documents",
            },
        ),
        migrations.CreateModel(
            name="RollingStockDocument",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True,
                        primary_key=True,
                        serialize=False,
                        verbose_name="ID",
                    ),
                ),
                ("description", models.CharField(blank=True, max_length=128)),
                (
                    "file",
                    models.FileField(
                        storage=ram.utils.DeduplicatedStorage(), upload_to="files/"
                    ),
                ),
                (
                    "private",
                    models.BooleanField(
                        default=False,
                        help_text="Document will be visible only to logged users",
                    ),
                ),
                ("creation_time", models.DateTimeField(auto_now_add=True)),
                ("updated_time", models.DateTimeField(auto_now=True)),
                (
                    "rolling_stock",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="new_document",
                        to="roster.rollingstock",
                    ),
                ),
            ],
            options={
                "verbose_name_plural": "Documents",
                "abstract": False,
            },
        ),
        migrations.CreateModel(
            name="DecoderDocument",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True,
                        primary_key=True,
                        serialize=False,
                        verbose_name="ID",
                    ),
                ),
                ("description", models.CharField(blank=True, max_length=128)),
                (
                    "file",
                    models.FileField(
                        storage=ram.utils.DeduplicatedStorage(), upload_to="files/"
                    ),
                ),
                (
                    "private",
                    models.BooleanField(
                        default=False,
                        help_text="Document will be visible only to logged users",
                    ),
                ),
                ("creation_time", models.DateTimeField(auto_now_add=True)),
                ("updated_time", models.DateTimeField(auto_now=True)),
                (
                    "decoder",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="new_document",
                        to="metadata.decoder",
                    ),
                ),
            ],
            options={
                "verbose_name_plural": "Documents",
                "abstract": False,
            },
        ),
        migrations.RunPython(
            migrate_document,
            reverse_code=migrations.RunPython.noop
        ),
        migrations.RunPython(
            migrate_decoder,
            reverse_code=migrations.RunPython.noop
        ),
        migrations.RunPython(
            migrate_rollingstock,
            reverse_code=migrations.RunPython.noop
        ),
        migrations.RunPython(
            migrate_book,
            reverse_code=migrations.RunPython.noop
        ),
    ]
