# Generated by Django 3.1.8 on 2021-05-05 09:23

from django.conf import settings
import django.contrib.postgres.fields
from django.db import migrations, models
import django.db.models.deletion


# Functions from the following migrations need manual copying.
# Move them and any dependencies into this file, then update the
# RunPython operations to refer to the local versions:
# api.barriers.migrations.0107_auto_20210427_1213


def add_light_touch_reviews_object(apps, schema_editor):
    PublicBarrier = apps.get_model("barriers", "PublicBarrier")
    PublicBarrierLightTouchReviews = apps.get_model(
        "barriers", "PublicBarrierLightTouchReviews"
    )
    for public_barrier in PublicBarrier.objects.filter(
        light_touch_reviews__isnull=True
    ):
        new_light_touch_reviews = PublicBarrierLightTouchReviews(
            public_barrier=public_barrier
        )
        new_light_touch_reviews.save()


class Migration(migrations.Migration):

    replaces = [
        ("barriers", "0107_auto_20210427_1213"),
        ("barriers", "0108_auto_20210504_1149"),
        ("barriers", "0109_auto_20210504_1226"),
        ("barriers", "0110_historicalpublicbarrier_light_touch_reviews_cache"),
        ("barriers", "0111_auto_20210504_1439"),
    ]

    dependencies = [
        ("metadata", "0020_add_more_organisations"),
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
        ("barriers", "0106_auto_20210416_1238"),
    ]

    operations = [
        migrations.CreateModel(
            name="PublicBarrierLightTouchReviews",
            fields=[
                (
                    "id",
                    models.AutoField(
                        auto_created=True,
                        primary_key=True,
                        serialize=False,
                        verbose_name="ID",
                    ),
                ),
                (
                    "created_on",
                    models.DateTimeField(auto_now_add=True, db_index=True, null=True),
                ),
                ("modified_on", models.DateTimeField(auto_now=True, null=True)),
                ("archived", models.BooleanField(default=False)),
                ("archived_on", models.DateTimeField(blank=True, null=True)),
                ("archived_reason", models.TextField(blank=True)),
                ("unarchived_reason", models.TextField(blank=True)),
                ("unarchived_on", models.DateTimeField(blank=True, null=True)),
                (
                    "approved_by_content_team",
                    models.BooleanField(blank=True, default=False),
                ),
                (
                    "has_content_changed_since_approval",
                    models.BooleanField(blank=True, default=False),
                ),
                (
                    "approval_by_hm_trade_commissioner",
                    models.BooleanField(blank=True, default=False),
                ),
                (
                    "archived_by",
                    models.ForeignKey(
                        blank=True,
                        null=True,
                        on_delete=django.db.models.deletion.SET_NULL,
                        related_name="+",
                        to=settings.AUTH_USER_MODEL,
                    ),
                ),
                (
                    "created_by",
                    models.ForeignKey(
                        blank=True,
                        null=True,
                        on_delete=django.db.models.deletion.SET_NULL,
                        related_name="+",
                        to=settings.AUTH_USER_MODEL,
                    ),
                ),
            ],
            options={
                "abstract": False,
            },
        ),
        migrations.CreateModel(
            name="PublicBarrierOrganisationReviews",
            fields=[
                (
                    "id",
                    models.AutoField(
                        auto_created=True,
                        primary_key=True,
                        serialize=False,
                        verbose_name="ID",
                    ),
                ),
                (
                    "created_on",
                    models.DateTimeField(auto_now_add=True, db_index=True, null=True),
                ),
                ("modified_on", models.DateTimeField(auto_now=True, null=True)),
                ("archived", models.BooleanField(default=False)),
                ("archived_on", models.DateTimeField(blank=True, null=True)),
                ("archived_reason", models.TextField(blank=True)),
                ("unarchived_reason", models.TextField(blank=True)),
                ("unarchived_on", models.DateTimeField(blank=True, null=True)),
                ("approved", models.BooleanField(blank=True, default=False)),
                (
                    "archived_by",
                    models.ForeignKey(
                        blank=True,
                        null=True,
                        on_delete=django.db.models.deletion.SET_NULL,
                        related_name="+",
                        to=settings.AUTH_USER_MODEL,
                    ),
                ),
                (
                    "created_by",
                    models.ForeignKey(
                        blank=True,
                        null=True,
                        on_delete=django.db.models.deletion.SET_NULL,
                        related_name="+",
                        to=settings.AUTH_USER_MODEL,
                    ),
                ),
                (
                    "modified_by",
                    models.ForeignKey(
                        blank=True,
                        null=True,
                        on_delete=django.db.models.deletion.SET_NULL,
                        related_name="+",
                        to=settings.AUTH_USER_MODEL,
                    ),
                ),
                (
                    "organisation",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        to="metadata.organisation",
                    ),
                ),
                (
                    "public_barrier_reviews",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        to="barriers.publicbarrierlighttouchreviews",
                    ),
                ),
                (
                    "unarchived_by",
                    models.ForeignKey(
                        blank=True,
                        null=True,
                        on_delete=django.db.models.deletion.SET_NULL,
                        related_name="+",
                        to=settings.AUTH_USER_MODEL,
                    ),
                ),
            ],
            options={
                "abstract": False,
            },
        ),
        migrations.AddField(
            model_name="publicbarrierlighttouchreviews",
            name="government_organisation_approvals",
            field=models.ManyToManyField(
                through="barriers.PublicBarrierOrganisationReviews",
                to="metadata.Organisation",
            ),
        ),
        migrations.AddField(
            model_name="publicbarrierlighttouchreviews",
            name="modified_by",
            field=models.ForeignKey(
                blank=True,
                null=True,
                on_delete=django.db.models.deletion.SET_NULL,
                related_name="+",
                to=settings.AUTH_USER_MODEL,
            ),
        ),
        migrations.AddField(
            model_name="publicbarrierlighttouchreviews",
            name="public_barrier",
            field=models.OneToOneField(
                on_delete=django.db.models.deletion.CASCADE,
                related_name="light_touch_reviews",
                to="barriers.publicbarrier",
            ),
        ),
        migrations.AddField(
            model_name="publicbarrierlighttouchreviews",
            name="unarchived_by",
            field=models.ForeignKey(
                blank=True,
                null=True,
                on_delete=django.db.models.deletion.SET_NULL,
                related_name="+",
                to=settings.AUTH_USER_MODEL,
            ),
        ),
        migrations.RunPython(
            code=add_light_touch_reviews_object,
        ),
        migrations.RemoveField(
            model_name="publicbarrierlighttouchreviews",
            name="government_organisation_approvals",
        ),
        migrations.AddField(
            model_name="publicbarrierlighttouchreviews",
            name="government_organisation_approvals",
            field=django.contrib.postgres.fields.ArrayField(
                base_field=models.IntegerField(blank=True),
                blank=True,
                default=list,
                size=None,
            ),
        ),
        migrations.DeleteModel(
            name="PublicBarrierOrganisationReviews",
        ),
        migrations.RenameField(
            model_name="publicbarrierlighttouchreviews",
            old_name="approval_by_hm_trade_commissioner",
            new_name="content_team_approval",
        ),
        migrations.RenameField(
            model_name="publicbarrierlighttouchreviews",
            old_name="approved_by_content_team",
            new_name="hm_trade_commissioner_approval",
        ),
        migrations.AddField(
            model_name="publicbarrierlighttouchreviews",
            name="hm_trade_commissioner_approval_enabled",
            field=models.BooleanField(blank=True, default=False),
        ),
        migrations.AddField(
            model_name="historicalpublicbarrier",
            name="light_touch_reviews_cache",
            field=models.JSONField(default=dict),
        ),
    ]
