# Generated by Django 4.2.3 on 2023-08-14 07:12

from django.conf import settings
import django.contrib.postgres.fields
from django.db import migrations, models
import django.db.models.deletion
import pi_dash.db.models.exporter
import pi_dash.db.models.project
import uuid
import random
import string


def generate_display_name(apps, schema_editor):
    UserModel = apps.get_model("db", "User")
    updated_users = []
    for obj in UserModel.objects.all():
        obj.display_name = (
            obj.email.split("@")[0]
            if len(obj.email.split("@"))
            else "".join(random.choice(string.ascii_letters) for _ in range(6))
        )
        updated_users.append(obj)
    UserModel.objects.bulk_update(
        updated_users, ["display_name"], batch_size=100
    )


def rectify_field_issue_activity(apps, schema_editor):
    Model = apps.get_model("db", "IssueActivity")
    updated_activity = []
    for obj in Model.objects.filter(field="assignee"):
        obj.field = "assignees"
        updated_activity.append(obj)

    Model.objects.bulk_update(updated_activity, ["field"], batch_size=100)


def update_assignee_issue_activity(apps, schema_editor):
    Model = apps.get_model("db", "IssueActivity")
    updated_activity = []

    # Get all the users
    User = apps.get_model("db", "User")
    users = User.objects.values("id", "email", "display_name")

    for obj in Model.objects.filter(field="assignees"):
        if bool(obj.new_value) and not bool(obj.old_value):
            # Get user from list
            assigned_user = [
                user for user in users if user.get("email") == obj.new_value
            ]
            if assigned_user:
                obj.new_value = assigned_user[0].get("display_name")
                obj.new_identifier = assigned_user[0].get("id")
                # Update the comment
                words = obj.comment.split()
                words[-1] = assigned_user[0].get("display_name")
                obj.comment = " ".join(words)

        if bool(obj.old_value) and not bool(obj.new_value):
            # Get user from list
            assigned_user = [
                user for user in users if user.get("email") == obj.old_value
            ]
            if assigned_user:
                obj.old_value = assigned_user[0].get("display_name")
                obj.old_identifier = assigned_user[0].get("id")
                # Update the comment
                words = obj.comment.split()
                words[-1] = assigned_user[0].get("display_name")
                obj.comment = " ".join(words)

        updated_activity.append(obj)

    Model.objects.bulk_update(
        updated_activity,
        [
            "old_value",
            "new_value",
            "old_identifier",
            "new_identifier",
            "comment",
        ],
        batch_size=200,
    )


def update_name_activity(apps, schema_editor):
    Model = apps.get_model("db", "IssueActivity")
    update_activity = []
    for obj in Model.objects.filter(field="name"):
        obj.comment = obj.comment.replace("start date", "name")
        update_activity.append(obj)

    Model.objects.bulk_update(update_activity, ["comment"], batch_size=1000)


def random_cycle_order(apps, schema_editor):
    CycleModel = apps.get_model("db", "Cycle")
    updated_cycles = []
    for obj in CycleModel.objects.all():
        obj.sort_order = random.randint(1, 65536)
        updated_cycles.append(obj)
    CycleModel.objects.bulk_update(
        updated_cycles, ["sort_order"], batch_size=100
    )


def random_module_order(apps, schema_editor):
    ModuleModel = apps.get_model("db", "Module")
    updated_modules = []
    for obj in ModuleModel.objects.all():
        obj.sort_order = random.randint(1, 65536)
        updated_modules.append(obj)
    ModuleModel.objects.bulk_update(
        updated_modules, ["sort_order"], batch_size=100
    )


def update_user_issue_properties(apps, schema_editor):
    IssuePropertyModel = apps.get_model("db", "IssueProperty")
    updated_issue_properties = []
    for obj in IssuePropertyModel.objects.all():
        obj.properties["start_date"] = True
        updated_issue_properties.append(obj)
    IssuePropertyModel.objects.bulk_update(
        updated_issue_properties, ["properties"], batch_size=100
    )


def workspace_member_properties(apps, schema_editor):
    WorkspaceMemberModel = apps.get_model("db", "WorkspaceMember")
    updated_workspace_members = []
    for obj in WorkspaceMemberModel.objects.all():
        obj.view_props["properties"]["start_date"] = True
        obj.default_props["properties"]["start_date"] = True
        updated_workspace_members.append(obj)

    WorkspaceMemberModel.objects.bulk_update(
        updated_workspace_members,
        ["view_props", "default_props"],
        batch_size=100,
    )


class Migration(migrations.Migration):
    dependencies = [
        ("db", "0040_projectmember_preferences_user_cover_image_and_more"),
    ]

    operations = [
        migrations.AddField(
            model_name="cycle",
            name="sort_order",
            field=models.FloatField(default=65535),
        ),
        migrations.AddField(
            model_name="issuecomment",
            name="access",
            field=models.CharField(
                choices=[("INTERNAL", "INTERNAL"), ("EXTERNAL", "EXTERNAL")],
                default="INTERNAL",
                max_length=100,
            ),
        ),
        migrations.AddField(
            model_name="module",
            name="sort_order",
            field=models.FloatField(default=65535),
        ),
        migrations.AddField(
            model_name="user",
            name="display_name",
            field=models.CharField(default="", max_length=255),
        ),
        migrations.CreateModel(
            name="ExporterHistory",
            fields=[
                (
                    "created_at",
                    models.DateTimeField(
                        auto_now_add=True, verbose_name="Created At"
                    ),
                ),
                (
                    "updated_at",
                    models.DateTimeField(
                        auto_now=True, verbose_name="Last Modified At"
                    ),
                ),
                (
                    "id",
                    models.UUIDField(
                        db_index=True,
                        default=uuid.uuid4,
                        editable=False,
                        primary_key=True,
                        serialize=False,
                        unique=True,
                    ),
                ),
                (
                    "project",
                    django.contrib.postgres.fields.ArrayField(
                        base_field=models.UUIDField(default=uuid.uuid4),
                        blank=True,
                        null=True,
                        size=None,
                    ),
                ),
                (
                    "provider",
                    models.CharField(
                        choices=[
                            ("json", "json"),
                            ("csv", "csv"),
                            ("xlsx", "xlsx"),
                        ],
                        max_length=50,
                    ),
                ),
                (
                    "status",
                    models.CharField(
                        choices=[
                            ("queued", "Queued"),
                            ("processing", "Processing"),
                            ("completed", "Completed"),
                            ("failed", "Failed"),
                        ],
                        default="queued",
                        max_length=50,
                    ),
                ),
                ("reason", models.TextField(blank=True)),
                ("key", models.TextField(blank=True)),
                (
                    "url",
                    models.URLField(blank=True, max_length=800, null=True),
                ),
                (
                    "token",
                    models.CharField(
                        default=pi_dash.db.models.exporter.generate_token,
                        max_length=255,
                        unique=True,
                    ),
                ),
                (
                    "created_by",
                    models.ForeignKey(
                        null=True,
                        on_delete=django.db.models.deletion.SET_NULL,
                        related_name="%(class)s_created_by",
                        to=settings.AUTH_USER_MODEL,
                        verbose_name="Created By",
                    ),
                ),
                (
                    "initiated_by",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="workspace_exporters",
                        to=settings.AUTH_USER_MODEL,
                    ),
                ),
                (
                    "updated_by",
                    models.ForeignKey(
                        null=True,
                        on_delete=django.db.models.deletion.SET_NULL,
                        related_name="%(class)s_updated_by",
                        to=settings.AUTH_USER_MODEL,
                        verbose_name="Last Modified By",
                    ),
                ),
                (
                    "workspace",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="workspace_exporters",
                        to="db.workspace",
                    ),
                ),
            ],
            options={
                "verbose_name": "Exporter",
                "verbose_name_plural": "Exporters",
                "db_table": "exporters",
                "ordering": ("-created_at",),
            },
        ),
        migrations.CreateModel(
            name="ProjectDeployBoard",
            fields=[
                (
                    "created_at",
                    models.DateTimeField(
                        auto_now_add=True, verbose_name="Created At"
                    ),
                ),
                (
                    "updated_at",
                    models.DateTimeField(
                        auto_now=True, verbose_name="Last Modified At"
                    ),
                ),
                (
                    "id",
                    models.UUIDField(
                        db_index=True,
                        default=uuid.uuid4,
                        editable=False,
                        primary_key=True,
                        serialize=False,
                        unique=True,
                    ),
                ),
                (
                    "anchor",
                    models.CharField(
                        db_index=True,
                        default=pi_dash.db.models.project.get_anchor,
                        max_length=255,
                        unique=True,
                    ),
                ),
                ("comments", models.BooleanField(default=False)),
                ("reactions", models.BooleanField(default=False)),
                ("votes", models.BooleanField(default=False)),
                (
                    "views",
                    models.JSONField(
                        default=pi_dash.db.models.project.get_default_views
                    ),
                ),
                (
                    "created_by",
                    models.ForeignKey(
                        null=True,
                        on_delete=django.db.models.deletion.SET_NULL,
                        related_name="%(class)s_created_by",
                        to=settings.AUTH_USER_MODEL,
                        verbose_name="Created By",
                    ),
                ),
                (
                    "inbox",
                    models.ForeignKey(
                        null=True,
                        on_delete=django.db.models.deletion.SET_NULL,
                        related_name="bord_inbox",
                        to="db.inbox",
                    ),
                ),
                (
                    "project",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="project_%(class)s",
                        to="db.project",
                    ),
                ),
                (
                    "updated_by",
                    models.ForeignKey(
                        null=True,
                        on_delete=django.db.models.deletion.SET_NULL,
                        related_name="%(class)s_updated_by",
                        to=settings.AUTH_USER_MODEL,
                        verbose_name="Last Modified By",
                    ),
                ),
                (
                    "workspace",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="workspace_%(class)s",
                        to="db.workspace",
                    ),
                ),
            ],
            options={
                "verbose_name": "Project Deploy Board",
                "verbose_name_plural": "Project Deploy Boards",
                "db_table": "project_deploy_boards",
                "ordering": ("-created_at",),
                "unique_together": {("project", "anchor")},
            },
        ),
        migrations.CreateModel(
            name="IssueVote",
            fields=[
                (
                    "created_at",
                    models.DateTimeField(
                        auto_now_add=True, verbose_name="Created At"
                    ),
                ),
                (
                    "updated_at",
                    models.DateTimeField(
                        auto_now=True, verbose_name="Last Modified At"
                    ),
                ),
                (
                    "id",
                    models.UUIDField(
                        db_index=True,
                        default=uuid.uuid4,
                        editable=False,
                        primary_key=True,
                        serialize=False,
                        unique=True,
                    ),
                ),
                (
                    "vote",
                    models.IntegerField(
                        choices=[(-1, "DOWNVOTE"), (1, "UPVOTE")]
                    ),
                ),
                (
                    "actor",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="votes",
                        to=settings.AUTH_USER_MODEL,
                    ),
                ),
                (
                    "created_by",
                    models.ForeignKey(
                        null=True,
                        on_delete=django.db.models.deletion.SET_NULL,
                        related_name="%(class)s_created_by",
                        to=settings.AUTH_USER_MODEL,
                        verbose_name="Created By",
                    ),
                ),
                (
                    "issue",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="votes",
                        to="db.issue",
                    ),
                ),
                (
                    "project",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="project_%(class)s",
                        to="db.project",
                    ),
                ),
                (
                    "updated_by",
                    models.ForeignKey(
                        null=True,
                        on_delete=django.db.models.deletion.SET_NULL,
                        related_name="%(class)s_updated_by",
                        to=settings.AUTH_USER_MODEL,
                        verbose_name="Last Modified By",
                    ),
                ),
                (
                    "workspace",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="workspace_%(class)s",
                        to="db.workspace",
                    ),
                ),
            ],
            options={
                "verbose_name": "Issue Vote",
                "verbose_name_plural": "Issue Votes",
                "db_table": "issue_votes",
                "ordering": ("-created_at",),
                "unique_together": {("issue", "actor")},
            },
        ),
        migrations.AlterField(
            model_name="modulelink",
            name="title",
            field=models.CharField(blank=True, max_length=255, null=True),
        ),
        migrations.RunPython(generate_display_name),
        migrations.RunPython(rectify_field_issue_activity),
        migrations.RunPython(update_assignee_issue_activity),
        migrations.RunPython(update_name_activity),
        migrations.RunPython(random_cycle_order),
        migrations.RunPython(random_module_order),
        migrations.RunPython(update_user_issue_properties),
        migrations.RunPython(workspace_member_properties),
    ]
