# Generated by Django 4.2.3 on 2023-07-23 16:33
import random
from django.db import migrations, models
import pi_dash.db.models.workspace


def rename_field(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_workspace_member_props(apps, schema_editor):
    Model = apps.get_model("db", "WorkspaceMember")

    updated_workspace_member = []

    for obj in Model.objects.all():
        if obj.view_props is None:
            obj.view_props = {
                "filters": {"type": None},
                "groupByProperty": None,
                "issueView": "list",
                "orderBy": "-created_at",
                "properties": {
                    "assignee": True,
                    "due_date": True,
                    "key": True,
                    "labels": True,
                    "priority": True,
                    "state": True,
                    "sub_issue_count": True,
                    "attachment_count": True,
                    "link": True,
                    "estimate": True,
                    "created_on": True,
                    "updated_on": True,
                },
                "showEmptyGroups": True,
            }
        else:
            current_view_props = obj.view_props
            obj.view_props = {
                "filters": {"type": None},
                "groupByProperty": None,
                "issueView": "list",
                "orderBy": "-created_at",
                "showEmptyGroups": True,
                "properties": current_view_props,
            }

        updated_workspace_member.append(obj)

    Model.objects.bulk_update(
        updated_workspace_member, ["view_props"], batch_size=100
    )


def update_project_member_sort_order(apps, schema_editor):
    Model = apps.get_model("db", "ProjectMember")

    updated_project_members = []

    for obj in Model.objects.all():
        obj.sort_order = random.randint(1, 65536)
        updated_project_members.append(obj)

    Model.objects.bulk_update(
        updated_project_members, ["sort_order"], batch_size=100
    )


class Migration(migrations.Migration):
    dependencies = [
        ("db", "0038_auto_20230720_1505"),
    ]

    operations = [
        migrations.RunPython(rename_field),
        migrations.RunPython(update_workspace_member_props),
        migrations.AlterField(
            model_name="workspacemember",
            name="view_props",
            field=models.JSONField(
                default=pi_dash.db.models.workspace.get_default_props
            ),
        ),
        migrations.AddField(
            model_name="workspacemember",
            name="default_props",
            field=models.JSONField(
                default=pi_dash.db.models.workspace.get_default_props
            ),
        ),
        migrations.AddField(
            model_name="projectmember",
            name="sort_order",
            field=models.FloatField(default=65535),
        ),
        migrations.RunPython(update_project_member_sort_order),
    ]
