# Generated by Django 4.2.7 on 2024-01-02 13:15
from django.db import migrations


def workspace_user_properties(apps, schema_editor):
    WorkspaceMember = apps.get_model("db", "WorkspaceMember")
    WorkspaceUserProperties = apps.get_model("db", "WorkspaceUserProperties")
    updated_workspace_user_properties = []
    for workspace_members in WorkspaceMember.objects.all():
        updated_workspace_user_properties.append(
            WorkspaceUserProperties(
                user_id=workspace_members.member_id,
                display_filters=workspace_members.view_props.get(
                    "display_filters"
                ),
                display_properties=workspace_members.view_props.get(
                    "display_properties"
                ),
                workspace_id=workspace_members.workspace_id,
            )
        )
    WorkspaceUserProperties.objects.bulk_create(
        updated_workspace_user_properties,
        batch_size=2000,
    )


def project_user_properties(apps, schema_editor):
    IssueProperty = apps.get_model("db", "IssueProperty")
    ProjectMember = apps.get_model("db", "ProjectMember")
    updated_issue_user_properties = []
    for issue_property in IssueProperty.objects.all():
        project_member = ProjectMember.objects.filter(
            project_id=issue_property.project_id,
            member_id=issue_property.user_id,
        ).first()
        if project_member:
            issue_property.filters = project_member.view_props.get("filters")
            issue_property.display_filters = project_member.view_props.get(
                "display_filters"
            )
            updated_issue_user_properties.append(issue_property)

    IssueProperty.objects.bulk_update(
        updated_issue_user_properties,
        ["filters", "display_filters"],
        batch_size=2000,
    )


def issue_view(apps, schema_editor):
    GlobalView = apps.get_model("db", "GlobalView")
    IssueView = apps.get_model("db", "IssueView")
    updated_issue_views = []

    for global_view in GlobalView.objects.all():
        updated_issue_views.append(
            IssueView(
                workspace_id=global_view.workspace_id,
                name=global_view.name,
                description=global_view.description,
                query=global_view.query,
                access=global_view.access,
                filters=global_view.query_data.get("filters", {}),
                sort_order=global_view.sort_order,
                created_by_id=global_view.created_by_id,
                updated_by_id=global_view.updated_by_id,
            )
        )
    IssueView.objects.bulk_create(updated_issue_views, batch_size=100)


class Migration(migrations.Migration):
    dependencies = [
        ("db", "0052_auto_20231220_1141"),
    ]

    operations = [
        migrations.RunPython(workspace_user_properties),
        migrations.RunPython(project_user_properties),
        migrations.RunPython(issue_view),
    ]
