# Generated by Django 4.2.7 on 2024-01-08 06:48

from django.db import migrations


def create_widgets(apps, schema_editor):
    Widget = apps.get_model("db", "Widget")
    widgets_list = [
        {"key": "overview_stats", "filters": {}},
        {
            "key": "assigned_issues",
            "filters": {
                "duration": "this_week",
                "tab": "upcoming",
            },
        },
        {
            "key": "created_issues",
            "filters": {
                "duration": "this_week",
                "tab": "upcoming",
            },
        },
        {
            "key": "issues_by_state_groups",
            "filters": {
                "duration": "this_week",
            },
        },
        {
            "key": "issues_by_priority",
            "filters": {
                "duration": "this_week",
            },
        },
        {"key": "recent_activity", "filters": {}},
        {"key": "recent_projects", "filters": {}},
        {"key": "recent_collaborators", "filters": {}},
    ]
    Widget.objects.bulk_create(
        [
            Widget(
                key=widget["key"],
                filters=widget["filters"],
            )
            for widget in widgets_list
        ],
        batch_size=10,
    )


def create_dashboards(apps, schema_editor):
    Dashboard = apps.get_model("db", "Dashboard")
    User = apps.get_model("db", "User")
    Dashboard.objects.bulk_create(
        [
            Dashboard(
                name="Home dashboard",
                description_html="<p></p>",
                identifier=None,
                owned_by_id=user_id,
                type_identifier="home",
                is_default=True,
            )
            for user_id in User.objects.values_list("id", flat=True)
        ],
        batch_size=2000,
    )


def create_dashboard_widgets(apps, schema_editor):
    Widget = apps.get_model("db", "Widget")
    Dashboard = apps.get_model("db", "Dashboard")
    DashboardWidget = apps.get_model("db", "DashboardWidget")

    updated_dashboard_widget = [
        DashboardWidget(
            widget_id=widget_id,
            dashboard_id=dashboard_id,
        )
        for widget_id in Widget.objects.values_list("id", flat=True)
        for dashboard_id in Dashboard.objects.values_list("id", flat=True)
    ]

    DashboardWidget.objects.bulk_create(
        updated_dashboard_widget, batch_size=2000
    )


class Migration(migrations.Migration):
    dependencies = [
        ("db", "0054_dashboard_widget_dashboardwidget"),
    ]

    operations = [
        migrations.RunPython(create_widgets),
        migrations.RunPython(create_dashboards),
        migrations.RunPython(create_dashboard_widgets),
    ]
